diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-12-05 17:37:49 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-12-05 17:37:49 -0100 |
commit | a850153582900141a7f5b6f83d8d07a05bee5cab (patch) | |
tree | f5e63cc05ea8e1f4d857ecdd54e18c7ca8641386 /lib | |
parent | 7afb4e5a7ed8d9b08b2b8782a66e2b978aade441 (diff) |
generate the entry in CacheDocuments and increment version
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Db/ActorsRequest.php | 11 | ||||
-rw-r--r-- | lib/Db/ActorsRequestBuilder.php | 5 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequest.php | 5 | ||||
-rw-r--r-- | lib/Db/CacheDocumentsRequest.php | 11 | ||||
-rw-r--r-- | lib/Db/CacheDocumentsRequestBuilder.php | 1 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Document.php | 2 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Item.php | 21 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Person.php | 22 | ||||
-rw-r--r-- | lib/Service/ActivityPub/DocumentService.php | 79 | ||||
-rw-r--r-- | lib/Service/ActivityPub/PersonService.php | 1 | ||||
-rw-r--r-- | lib/Service/ActorService.php | 29 | ||||
-rw-r--r-- | lib/Service/ConfigService.php | 23 | ||||
-rw-r--r-- | lib/Service/MiscService.php | 6 |
13 files changed, 199 insertions, 17 deletions
diff --git a/lib/Db/ActorsRequest.php b/lib/Db/ActorsRequest.php index 00572296..a8ad8a28 100644 --- a/lib/Db/ActorsRequest.php +++ b/lib/Db/ActorsRequest.php @@ -65,7 +65,6 @@ class ActorsRequest extends ActorsRequestBuilder { public function create(Person $actor): string { $id = $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername(); - $qb = $this->getActorsInsertSql(); $qb->setValue('id', $qb->createNamedParameter($id)) @@ -73,6 +72,7 @@ class ActorsRequest extends ActorsRequestBuilder { ->setValue('user_id', $qb->createNamedParameter($actor->getUserId())) ->setValue('name', $qb->createNamedParameter($actor->getName())) ->setValue('summary', $qb->createNamedParameter($actor->getSummary())) + ->setValue('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion())) ->setValue( 'preferred_username', $qb->createNamedParameter($actor->getPreferredUsername()) ) @@ -85,6 +85,15 @@ class ActorsRequest extends ActorsRequestBuilder { } + public function update(Person $actor) { + $qb = $this->getActorsUpdateSql(); + $qb->set('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion())); + $this->limitToIdString($qb, $actor->getId()); + + $qb->execute(); + } + + /** * return Actor from database based on the username * diff --git a/lib/Db/ActorsRequestBuilder.php b/lib/Db/ActorsRequestBuilder.php index 8d3e0dfc..0312dea3 100644 --- a/lib/Db/ActorsRequestBuilder.php +++ b/lib/Db/ActorsRequestBuilder.php @@ -78,8 +78,7 @@ class ActorsRequestBuilder extends CoreRequestBuilder { /** @noinspection PhpMethodParametersCountMismatchInspection */ $qb->select( 'sa.id', 'sa.user_id', 'sa.preferred_username', 'sa.name', 'sa.summary', - 'sa.public_key', - 'sa.private_key', 'sa.creation' + 'sa.public_key', 'sa.avatar_version', 'sa.private_key', 'sa.creation' ) ->from(self::TABLE_SERVER_ACTORS, 'sa'); @@ -116,10 +115,12 @@ class ActorsRequestBuilder extends CoreRequestBuilder { $actor->setType('Person'); $actor->setInbox($actor->getId() . '/inbox') ->setOutbox($actor->getId() . '/outbox') + ->setUserId($this->get('user_id', $data, '')) ->setFollowers($actor->getId() . '/followers') ->setFollowing($actor->getId() . '/following') ->setSharedInbox($root . 'inbox') ->setLocal(true) + ->setAvatarVersion($this->getInt('avatar_version', $data, -1)) ->setAccount( $actor->getPreferredUsername() . '@' . $this->configService->getCloudAddress(true) ); diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 8999bb4d..7a3f0ff2 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -94,9 +94,12 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { if ($actor->gotIcon()) { $iconId = $actor->getIcon() ->getId(); - $qb->setValue('icon_id', $qb->createNamedParameter($iconId)); + } else { + $iconId = $actor->getIconId(); } + $qb->setValue('icon_id', $qb->createNamedParameter($iconId)); + $qb->execute(); return $qb->getLastInsertId(); diff --git a/lib/Db/CacheDocumentsRequest.php b/lib/Db/CacheDocumentsRequest.php index 8241120c..3b5d917a 100644 --- a/lib/Db/CacheDocumentsRequest.php +++ b/lib/Db/CacheDocumentsRequest.php @@ -162,5 +162,16 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { return $documents; } + + /** + * @param string $url + */ + public function deleteByUrl(string $url) { + $qb = $this->getCacheDocumentsDeleteSql(); + $this->limitToUrl($qb, $url); + + $qb->execute(); + } + } diff --git a/lib/Db/CacheDocumentsRequestBuilder.php b/lib/Db/CacheDocumentsRequestBuilder.php index 56e0c223..4271fe6d 100644 --- a/lib/Db/CacheDocumentsRequestBuilder.php +++ b/lib/Db/CacheDocumentsRequestBuilder.php @@ -32,6 +32,7 @@ namespace OCA\Social\Db; use daita\MySmallPhpTools\Traits\TArrayTools; use OCA\Social\Model\ActivityPub\Document; +use OCA\Social\Model\ActivityPub\Image; use OCP\DB\QueryBuilder\IQueryBuilder; class CacheDocumentsRequestBuilder extends CoreRequestBuilder { diff --git a/lib/Model/ActivityPub/Document.php b/lib/Model/ActivityPub/Document.php index 4aa401cc..38624c74 100644 --- a/lib/Model/ActivityPub/Document.php +++ b/lib/Model/ActivityPub/Document.php @@ -33,6 +33,7 @@ namespace OCA\Social\Model\ActivityPub; use DateTime; use JsonSerializable; +use OCA\Social\Exceptions\InvalidResourceEntryException; use OCA\Social\Exceptions\UrlCloudException; @@ -196,6 +197,7 @@ class Document extends ACore implements JsonSerializable { * @param array $data * * @throws UrlCloudException + * @throws InvalidResourceEntryException */ public function import(array $data) { parent::import($data); diff --git a/lib/Model/ActivityPub/Item.php b/lib/Model/ActivityPub/Item.php index 448ad37c..7692cd0b 100644 --- a/lib/Model/ActivityPub/Item.php +++ b/lib/Model/ActivityPub/Item.php @@ -88,6 +88,9 @@ class Item { private $icon = null; /** @var string */ + private $iconId = ''; + + /** @var string */ private $objectId = ''; /** @var bool */ @@ -540,6 +543,24 @@ class Item { return $this; } + /** + * @return string + */ + public function getIconId(): string { + return $this->iconId; + } + + /** + * @param string $iconId + * + * @return Item + */ + public function setIconId(string $iconId): Item { + $this->iconId = $iconId; + + return $this; + } + /** * @return bool diff --git a/lib/Model/ActivityPub/Person.php b/lib/Model/ActivityPub/Person.php index 6b40e0b0..2ca62225 100644 --- a/lib/Model/ActivityPub/Person.php +++ b/lib/Model/ActivityPub/Person.php @@ -89,6 +89,9 @@ class Person extends ACore implements JsonSerializable { /** @var array */ private $details = []; + /** @var int */ + private $avatarVersion = -1; + /** * Person constructor. @@ -416,6 +419,25 @@ class Person extends ACore implements JsonSerializable { /** + * @return int + */ + public function getAvatarVersion(): int { + return $this->avatarVersion; + } + + /** + * @param int $avatarVersion + * + * @return Person + */ + public function setAvatarVersion(int $avatarVersion): Person { + $this->avatarVersion = $avatarVersion; + + return $this; + } + + + /** * @param array $data * * @throws UrlCloudException diff --git a/lib/Service/ActivityPub/DocumentService.php b/lib/Service/ActivityPub/DocumentService.php index 06e3bee1..42d2ce96 100644 --- a/lib/Service/ActivityPub/DocumentService.php +++ b/lib/Service/ActivityPub/DocumentService.php @@ -32,17 +32,24 @@ namespace OCA\Social\Service\ActivityPub; use Exception; +use OCA\Social\Db\ActorsRequest; use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheContentSizeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Document; +use OCA\Social\Model\ActivityPub\Image; +use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Service\CacheService; +use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IURLGenerator; class DocumentService implements ICoreService { @@ -52,12 +59,21 @@ class DocumentService implements ICoreService { const ERROR_MIMETYPE = 2; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var CacheDocumentsRequest */ private $cacheDocumentsRequest; + /** @var ActorsRequest */ + private $actorRequest; + /** @var CacheService */ private $cacheService; + /** @var ConfigService */ + private $configService; + /** @var MiscService */ private $miscService; @@ -65,15 +81,23 @@ class DocumentService implements ICoreService { /** * DocumentService constructor. * + * @param IUrlGenerator $urlGenerator * @param CacheDocumentsRequest $cacheDocumentsRequest + * @param ActorsRequest $actorRequest * @param CacheService $cacheService + * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - CacheDocumentsRequest $cacheDocumentsRequest, CacheService $cacheService, - MiscService $miscService + IUrlGenerator $urlGenerator, CacheDocumentsRequest $cacheDocumentsRequest, + ActorsRequest $actorRequest, + CacheService $cacheService, + ConfigService $configService, MiscService $miscService ) { + $this->urlGenerator = $urlGenerator; $this->cacheDocumentsRequest = $cacheDocumentsRequest; + $this->actorRequest = $actorRequest; + $this->configService = $configService; $this->cacheService = $cacheService; $this->miscService = $miscService; } @@ -151,6 +175,10 @@ class DocumentService implements ICoreService { $count = 0; foreach ($update as $item) { + if ($item->getLocalCopy() === 'avatar') { + continue; + } + try { $this->cacheRemoteDocument($item->getId()); } catch (Exception $e) { @@ -164,6 +192,49 @@ class DocumentService implements ICoreService { /** + * @param Person $actor + * + * @return string + * @throws SocialAppConfigException + * @throws UrlCloudException + */ + public function cacheLocalAvatarByUsername(Person $actor): string { + $url = $this->urlGenerator->linkToRouteAbsolute( + 'core.avatar.getAvatar', ['userId' => $actor->getUserId(), 'size' => 128] + ); + + $versionCurrent = + (int)$this->configService->getUserValue('version', $actor->getUserId(), 'avatar'); + $versionCached = $actor->getAvatarVersion(); + + if ($versionCurrent > $versionCached) { + echo $actor->getUserId() . ' ' . $versionCurrent . ' ' . $versionCached . "\n"; + + $icon = new Image(); + $icon->setUrl($url); + $icon->setUrlcloud($this->configService->getCloudAddress()); + $icon->generateUniqueId('/documents/avatar'); + $icon->setMediaType(''); + $icon->setLocalCopy('avatar'); + + $this->cacheDocumentsRequest->deleteByUrl($icon->getUrl()); + $this->cacheDocumentsRequest->save($icon); + + $actor->setAvatarVersion($versionCurrent); + $this->actorRequest->update($actor); + } else { + try { + $icon = $this->cacheDocumentsRequest->getBySource($url); + } catch (CacheDocumentDoesNotExistException $e) { + return ''; + } + } + + return $icon->getId(); + } + + + /** * @param ACore $item */ public function parse(ACore $item) { @@ -174,6 +245,8 @@ class DocumentService implements ICoreService { * @param ACore $item */ public function save(ACore $item) { + /** @var Document $item */ + $this->cacheDocumentsRequest->save($item); } @@ -181,7 +254,9 @@ class DocumentService implements ICoreService { * @param ACore $item */ public function delete(ACore $item) { +// $this->cacheDocumentsRequest->delete($item); } + } diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 9f0bd0f3..3674483c 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -127,6 +127,7 @@ class PersonService implements ICoreService { $actor->setLocal(true); $actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES)); + $this->save($actor); } diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index e92e59da..e54c3ddf 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -40,8 +40,13 @@ use OCA\Social\Exceptions\AccountAlreadyExistsException; use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Person; +use OCA\Social\Service\ActivityPub\DocumentService; use OCA\Social\Service\ActivityPub\PersonService; +use OCP\Accounts\IAccountManager; +use OCP\IURLGenerator; +use OCP\IUserManager; /** @@ -55,6 +60,9 @@ class ActorService { use TArrayTools; + /** @var IAccountManager */ + private $accountManager; + /** @var ActorsRequest */ private $actorsRequest; @@ -67,6 +75,9 @@ class ActorService { /** @var PersonService */ private $personService; + /** @var DocumentService */ + private $documentService; + /** @var ConfigService */ private $configService; @@ -77,21 +88,26 @@ class ActorService { /** * ActorService constructor. * + * @param IAccountManager $accountManager * @param ActorsRequest $actorsRequest * @param FollowsRequest $followsRequest * @param NotesRequest $notesRequest * @param PersonService $personService + * @param DocumentService $documentService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - ActorsRequest $actorsRequest, FollowsRequest $followsRequest, NotesRequest $notesRequest, - PersonService $personService, ConfigService $configService, MiscService $miscService + IAccountManager $accountManager, ActorsRequest $actorsRequest, + FollowsRequest $followsRequest, NotesRequest $notesRequest, PersonService $personService, + DocumentService $documentService, ConfigService $configService, MiscService $miscService ) { + $this->accountManager = $accountManager; $this->actorsRequest = $actorsRequest; $this->followsRequest = $followsRequest; $this->notesRequest = $notesRequest; $this->personService = $personService; + $this->documentService = $documentService; $this->configService = $configService; $this->miscService = $miscService; } @@ -134,6 +150,7 @@ class ActorService { * @throws ActorDoesNotExistException * @throws NoUserException * @throws SocialAppConfigException + * @throws UrlCloudException */ public function getActorFromUserId(string $userId, bool $create = false): Person { $this->miscService->confirmUserId($userId); @@ -167,6 +184,7 @@ class ActorService { * @throws AccountAlreadyExistsException * @throws NoUserException * @throws SocialAppConfigException + * @throws UrlCloudException */ public function createActor(string $userId, string $username) { @@ -235,10 +253,15 @@ class ActorService { * @param bool $refresh * * @throws SocialAppConfigException + * @throws UrlCloudException */ public function cacheLocalActorByUsername(string $username, bool $refresh = false) { try { - $actor = $this->getActor($username);; + $actor = $this->getActor($username); + + $iconId = $this->documentService->cacheLocalAvatarByUsername($actor); + $actor->setIconId($iconId); + $count = [ 'followers' => $this->followsRequest->countFollowers($actor->getId()), 'following' => $this->followsRequest->countFollowing($actor->getId()), diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 83741f16..fe7e9c66 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -31,6 +31,8 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; +use OC\IntegrityCheck\Helpers\AppLocator; +use OCA\Files\App; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\SocialAppConfigException; use OCP\IConfig; @@ -163,18 +165,25 @@ class ConfigService { * Get a user value by key * * @param string $key + * @param string $userId + * @param string $app * * @return string */ - public function getUserValue($key) { - $defaultValue = null; - if (array_key_exists($key, $this->defaults)) { - $defaultValue = $this->defaults[$key]; + public function getUserValue(string $key, string $userId = '', string $app = '') { + if ($userId === '') { + $userId = $this->userId; + } + + $defaultValue = ''; + if ($app === '') { + $app = Application::APP_NAME; + if (array_key_exists($key, $this->defaults)) { + $defaultValue = $this->defaults[$key]; + } } - return $this->config->getUserValue( - $this->userId, Application::APP_NAME, $key, $defaultValue - ); + return $this->config->getUserValue($userId, $app, $key, $defaultValue); } /** diff --git a/lib/Service/MiscService.php b/lib/Service/MiscService.php index 6be52d60..0d41633e 100644 --- a/lib/Service/MiscService.php +++ b/lib/Service/MiscService.php @@ -34,6 +34,7 @@ namespace OCA\Social\Service; use OC\User\NoUserException; use OCA\Social\AppInfo\Application; use OCP\ILogger; +use OCP\IUser; use OCP\IUserManager; @@ -81,9 +82,10 @@ class MiscService { /** * @param string $userId * + * @return IUser * @throws NoUserException */ - public function confirmUserId(string &$userId) { + public function confirmUserId(string &$userId): IUser { $user = $this->userManager->get($userId); if ($user === null) { @@ -91,6 +93,8 @@ class MiscService { } $userId = $user->getUID(); + + return $user; } } |