generate the entry in CacheDocuments and increment version

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/144/head
Maxence Lange 2018-12-05 17:37:49 -01:00
rodzic 7afb4e5a7e
commit a850153582
15 zmienionych plików z 207 dodań i 18 usunięć

Wyświetl plik

@ -57,6 +57,13 @@
<length>2000</length>
</field>
<field>
<name>avatar_version</name>
<type>integer</type>
<length>2</length>
<notnull>false</notnull>
</field>
<field>
<name>creation</name>
<type>timestamp</type>

Wyświetl plik

@ -15,7 +15,7 @@
**🕸 Open standards:** We use the established ActivityPub standard!
]]></description>
<version>0.0.62</version>
<version>0.0.64</version>
<licence>agpl</licence>
<author mail="maxence@artificial-owl.com">Maxence Lange</author>
<author mail="jus@bitgrid.net">Julius Härtl</author>

Wyświetl plik

@ -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
*

Wyświetl plik

@ -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)
);

Wyświetl plik

@ -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();

Wyświetl plik

@ -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();
}
}

Wyświetl plik

@ -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 {

Wyświetl plik

@ -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);

Wyświetl plik

@ -87,6 +87,9 @@ class Item {
/** @var Document */
private $icon = null;
/** @var string */
private $iconId = '';
/** @var string */
private $objectId = '';
@ -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

Wyświetl plik

@ -89,6 +89,9 @@ class Person extends ACore implements JsonSerializable {
/** @var array */
private $details = [];
/** @var int */
private $avatarVersion = -1;
/**
* Person constructor.
@ -415,6 +418,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
*

Wyświetl plik

@ -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) {
@ -163,6 +191,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
*/
@ -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);
}
}

Wyświetl plik

@ -127,6 +127,7 @@ class PersonService implements ICoreService {
$actor->setLocal(true);
$actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES));
$this->save($actor);
}

Wyświetl plik

@ -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()),

Wyświetl plik

@ -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;
}
return $this->config->getUserValue(
$this->userId, Application::APP_NAME, $key, $defaultValue
);
$defaultValue = '';
if ($app === '') {
$app = Application::APP_NAME;
if (array_key_exists($key, $this->defaults)) {
$defaultValue = $this->defaults[$key];
}
}
return $this->config->getUserValue($userId, $app, $key, $defaultValue);
}
/**

Wyświetl plik

@ -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;
}
}