From accf7e424d23e8f343f4d0ecf053f65ab2800e77 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 24 Jan 2019 10:26:44 -0100 Subject: implementing caching for incoming request Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 306c85ff..3d42679e 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -30,11 +30,13 @@ declare(strict_types=1); namespace OCA\Social\Db; +use daita\MySmallPhpTools\Model\Cache; use DateTime; use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -60,12 +62,17 @@ class NotesRequest extends NotesRequestBuilder { /** * Insert a new Note in the database. * - * @param Note $note + * @param Stream $note */ - public function save(Note $note) { + public function save(Stream $note) { $dTime = new DateTime(); $dTime->setTimestamp($note->getPublishedTime()); + $cache = '[]'; + if ($note->gotCache()) { + $cache = json_encode($note->getCache(), JSON_UNESCAPED_SLASHES); + } + $qb = $this->getNotesInsertSql(); $qb->setValue('id', $qb->createNamedParameter($note->getId())) ->setValue('type', $qb->createNamedParameter($note->getType())) @@ -94,6 +101,7 @@ class NotesRequest extends NotesRequestBuilder { ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) ->setValue('source', $qb->createNamedParameter($note->getSource())) + ->setValue('cache', $qb->createNamedParameter($cache)) ->setValue( 'instances', $qb->createNamedParameter( json_encode($note->getInstancePaths(), JSON_UNESCAPED_SLASHES) @@ -109,6 +117,20 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * @param Stream $stream + * @param Cache $cache + */ + public function updateCache(Stream $stream, Cache $cache) { + $qb = $this->getNotesUpdateSql(); + $qb->set('cache', $qb->createNamedParameter(json_encode($cache, JSON_UNESCAPED_SLASHES))); + + $this->limitToIdString($qb, $stream->getId()); + + $qb->execute(); + } + + /** * @param string $id * -- cgit v1.2.3 From db55361148c570b16a95f8a71294dc46e0cc8833 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 24 Jan 2019 10:27:36 -0100 Subject: activityId is saved in db for streamable objects Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 3d42679e..343fc1c2 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -157,6 +157,32 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * @param string $id + * + * @return Note + * @throws NoteNotFoundException + */ + public function getNoteByActivityId(string $id): Note { + if ($id === '') { + throw new NoteNotFoundException(); + }; + + $qb = $this->getNotesSelectSql(); + $this->limitToActivityId($qb, $id); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new NoteNotFoundException('Post not found'); + } + + return $this->parseNotesSelectSql($data); + } + + /** * @param string $actorId * -- cgit v1.2.3 From 823773fbbeca8740a510bf132b56838e32ecde35 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 26 Jan 2019 10:57:07 -0100 Subject: object returned in the stream Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 343fc1c2..e5757336 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -101,6 +101,7 @@ class NotesRequest extends NotesRequestBuilder { ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) ->setValue('source', $qb->createNamedParameter($note->getSource())) + ->setValue('object_id', $qb->createNamedParameter($note->getObjectId())) ->setValue('cache', $qb->createNamedParameter($cache)) ->setValue( 'instances', $qb->createNamedParameter( -- cgit v1.2.3 From 47fa5d08f30836c5ddeb4a5952effb1f16df6fca Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 4 Jan 2019 18:34:19 -0100 Subject: save hashtags and get from database Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 306c85ff..af5bd63f 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -87,6 +87,7 @@ class NotesRequest extends NotesRequestBuilder { ) ->setValue('content', $qb->createNamedParameter($note->getContent())) ->setValue('summary', $qb->createNamedParameter($note->getSummary())) + ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) ->setValue('published', $qb->createNamedParameter($note->getPublished())) ->setValue( 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) -- cgit v1.2.3 From c7d1da1f53a4fbde722a28ed0b98acf511409f22 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 4 Jan 2019 18:42:58 -0100 Subject: get stream for hashtag Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index af5bd63f..68634565 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -87,7 +87,7 @@ class NotesRequest extends NotesRequestBuilder { ) ->setValue('content', $qb->createNamedParameter($note->getContent())) ->setValue('summary', $qb->createNamedParameter($note->getSummary())) - ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) + ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) ->setValue('published', $qb->createNamedParameter($note->getPublished())) ->setValue( 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) @@ -307,6 +307,39 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * Should returns: + * - All public post related to a tag (not yet) + * - direct message related to a tag (not yet) + * - message to followers related to a tag (not yet) + * + * @param string $hashtag + * @param int $since + * @param int $limit + * + * @return array + */ + public function getStreamTag(string $hashtag, int $since = 0, int $limit = 5): array { + $qb = $this->getNotesSelectSql(); + +// // TODO: LIMIT TO NOTE RELATED TO THE VIEWER+PUBLIC + + $this->limitPaginate($qb, $since, $limit); + $qb->andWhere($this->exprValueWithinJsonFormat($qb, 'hashtags', '#' . $hashtag)); + + $this->leftJoinCacheActors($qb, 'attributed_to'); + + $notes = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $notes[] = $this->parseNotesSelectSql($data); + } + $cursor->closeCursor(); + + return $notes; + } + + /** * @param string $id */ -- cgit v1.2.3 From 3ef7737d14dad684825a040c2e42a1295bb33058 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sun, 6 Jan 2019 11:50:45 -0100 Subject: fill the server_hashtags with hashtags and trends Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 68634565..5701e319 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -340,6 +340,26 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * @param int $since + * + * @return Note[] + */ + public function getNotesSince(int $since): array { + $qb = $this->getNotesSelectSql(); + $this->limitToSince($qb, $since, 'published_time'); + + $notes = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $notes[] = $this->parseNotesSelectSql($data); + } + $cursor->closeCursor(); + + return $notes; + } + + /** * @param string $id */ -- cgit v1.2.3 From 8cb56b0710dc3ddc71b0961d2ab649fba3619456 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 7 Jan 2019 09:09:53 -0100 Subject: limit to viewable notes Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 5701e319..ccfbb58d 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -313,20 +313,25 @@ class NotesRequest extends NotesRequestBuilder { * - direct message related to a tag (not yet) * - message to followers related to a tag (not yet) * + * @param Person $actor * @param string $hashtag * @param int $since * @param int $limit * * @return array */ - public function getStreamTag(string $hashtag, int $since = 0, int $limit = 5): array { + public function getStreamTag(Person $actor, string $hashtag, int $since = 0, int $limit = 5 + ): array { $qb = $this->getNotesSelectSql(); -// // TODO: LIMIT TO NOTE RELATED TO THE VIEWER+PUBLIC + $on = $this->exprJoinFollowing($qb, $actor); + $on->add($this->exprLimitToRecipient($qb, ACore::CONTEXT_PUBLIC, false)); + $on->add($this->exprLimitToRecipient($qb, $actor->getId(), true)); + $qb->join($this->defaultSelectAlias, CoreRequestBuilder::TABLE_SERVER_FOLLOWS, 'f', $on); - $this->limitPaginate($qb, $since, $limit); $qb->andWhere($this->exprValueWithinJsonFormat($qb, 'hashtags', '#' . $hashtag)); + $this->limitPaginate($qb, $since, $limit); $this->leftJoinCacheActors($qb, 'attributed_to'); $notes = []; -- cgit v1.2.3 From 8c0c0bc5a3f20f86002be8cab6030f5cfab8af12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 11 Jan 2019 09:33:01 +0100 Subject: Fix setting hashtags when creating a post MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/NotesRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index ccfbb58d..b3ce7d74 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -329,7 +329,7 @@ class NotesRequest extends NotesRequestBuilder { $on->add($this->exprLimitToRecipient($qb, $actor->getId(), true)); $qb->join($this->defaultSelectAlias, CoreRequestBuilder::TABLE_SERVER_FOLLOWS, 'f', $on); - $qb->andWhere($this->exprValueWithinJsonFormat($qb, 'hashtags', '#' . $hashtag)); + $qb->andWhere($this->exprValueWithinJsonFormat($qb, 'hashtags', '' . $hashtag)); $this->limitPaginate($qb, $since, $limit); $this->leftJoinCacheActors($qb, 'attributed_to'); -- cgit v1.2.3 From 147e2800fa223874bcbd51353b2ae2afbf15472b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 11 Feb 2019 10:01:06 -0100 Subject: Delete on Actor delete also Note from this Actor Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index b3ce7d74..e364dc76 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -375,5 +375,16 @@ class NotesRequest extends NotesRequestBuilder { $qb->execute(); } + + /** + * @param string $actorId + */ + public function deleteByAuthor(string $actorId) { + $qb = $this->getNotesDeleteSql(); + $this->limitToAttributedTo($qb, $actorId); + + $qb->execute(); + } + } -- cgit v1.2.3 From 9ac8eb965b930110d0ec42d56988a10adf1e3f96 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 22 Feb 2019 23:04:00 -0100 Subject: boost creation Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 129 ++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 53 deletions(-) (limited to 'lib/Db/NotesRequest.php') diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 005d5202..2eef3de0 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -60,61 +60,18 @@ class NotesRequest extends NotesRequestBuilder { /** - * Insert a new Note in the database. - * - * @param Stream $note + * @param Stream $stream */ - public function save(Stream $note) { - $dTime = new DateTime(); - $dTime->setTimestamp($note->getPublishedTime()); - - $cache = '[]'; - if ($note->gotCache()) { - $cache = json_encode($note->getCache(), JSON_UNESCAPED_SLASHES); + public function save(Stream $stream) { + $qb = $this->saveStream($stream); + + if ($stream->getType() === Note::TYPE) { + /** @var Note $stream */ + $qb->setValue( + 'hashtags', $qb->createNamedParameter(json_encode($stream->getHashtags())) + ); } - $qb = $this->getNotesInsertSql(); - $qb->setValue('id', $qb->createNamedParameter($note->getId())) - ->setValue('type', $qb->createNamedParameter($note->getType())) - ->setValue('to', $qb->createNamedParameter($note->getTo())) - ->setValue( - 'to_array', $qb->createNamedParameter( - json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES) - ) - ) - ->setValue( - 'cc', $qb->createNamedParameter( - json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES) - ) - ) - ->setValue( - 'bcc', $qb->createNamedParameter( - json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES - ) - ) - ->setValue('content', $qb->createNamedParameter($note->getContent())) - ->setValue('summary', $qb->createNamedParameter($note->getSummary())) - ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) - ->setValue('published', $qb->createNamedParameter($note->getPublished())) - ->setValue( - 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) - ) - ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) - ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) - ->setValue('source', $qb->createNamedParameter($note->getSource())) - ->setValue('object_id', $qb->createNamedParameter($note->getObjectId())) - ->setValue('cache', $qb->createNamedParameter($cache)) - ->setValue( - 'instances', $qb->createNamedParameter( - json_encode($note->getInstancePaths(), JSON_UNESCAPED_SLASHES) - ) - ) - ->setValue('local', $qb->createNamedParameter(($note->isLocal()) ? '1' : '0')) - ->setValue( - 'creation', - $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) - ); - $qb->execute(); } @@ -135,11 +92,12 @@ class NotesRequest extends NotesRequestBuilder { /** * @param string $id + * @param bool $asViewer * * @return Note * @throws NoteNotFoundException */ - public function getNoteById(string $id): Note { + public function getNoteById(string $id, bool $asViewer = false): Note { if ($id === '') { throw new NoteNotFoundException(); }; @@ -147,6 +105,10 @@ class NotesRequest extends NotesRequestBuilder { $qb = $this->getNotesSelectSql(); $this->limitToIdString($qb, $id); + if ($asViewer) { + $this->limitToViewer($qb); + } + $cursor = $qb->execute(); $data = $cursor->fetch(); $cursor->closeCursor(); @@ -435,5 +397,66 @@ class NotesRequest extends NotesRequestBuilder { $qb->execute(); } + + /** + * Insert a new Note in the database. + * + * @param Stream $note + * + * @return IQueryBuilder + */ + public function saveStream(Stream $note): IQueryBuilder { + $dTime = new DateTime(); + $dTime->setTimestamp($note->getPublishedTime()); + + $cache = '[]'; + if ($note->gotCache()) { + $cache = json_encode($note->getCache(), JSON_UNESCAPED_SLASHES); + } + + $qb = $this->getNotesInsertSql(); + $qb->setValue('id', $qb->createNamedParameter($note->getId())) + ->setValue('type', $qb->createNamedParameter($note->getType())) + ->setValue('to', $qb->createNamedParameter($note->getTo())) + ->setValue( + 'to_array', $qb->createNamedParameter( + json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue( + 'cc', $qb->createNamedParameter( + json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue( + 'bcc', $qb->createNamedParameter( + json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES + ) + ) + ->setValue('content', $qb->createNamedParameter($note->getContent())) + ->setValue('summary', $qb->createNamedParameter($note->getSummary())) + ->setValue('published', $qb->createNamedParameter($note->getPublished())) + ->setValue( + 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) + ) + ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) + ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) + ->setValue('source', $qb->createNamedParameter($note->getSource())) + ->setValue('object_id', $qb->createNamedParameter($note->getObjectId())) + ->setValue('cache', $qb->createNamedParameter($cache)) + ->setValue( + 'instances', $qb->createNamedParameter( + json_encode($note->getInstancePaths(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue('local', $qb->createNamedParameter(($note->isLocal()) ? '1' : '0')) + ->setValue( + 'creation', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + + return $qb; + } + } -- cgit v1.2.3