summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-07-20 17:42:42 -0100
committerGitHub <noreply@github.com>2019-07-20 17:42:42 -0100
commit0fdd2a40792f0f7ef1fb820eebf850f4977eda95 (patch)
tree107bc716f77421cd6f0026d4643afffe40ece816
parent81ce4030861cb8f4d5c45cde51873b3ff7e27d0d (diff)
parentd985dc87e2870430a84b7ed4573d1dde7bfc17bb (diff)
Merge pull request #642 from nextcloud/feature/640/stream-liked
Add 'liked' stream
-rw-r--r--appinfo/routes.php1
-rw-r--r--lib/Controller/LocalController.php22
-rw-r--r--lib/Db/CoreRequestBuilder.php55
-rw-r--r--lib/Db/StreamRequest.php39
-rw-r--r--lib/Service/NoteService.php15
5 files changed, 127 insertions, 5 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 93f73171..8d293d13 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -66,6 +66,7 @@ return [
['name' => 'Local#streamTag', 'url' => '/api/v1/stream/tag/{hashtag}/', 'verb' => 'GET'],
['name' => 'Local#streamFederated', 'url' => '/api/v1/stream/federated', 'verb' => 'GET'],
['name' => 'Local#streamDirect', 'url' => '/api/v1/stream/direct', 'verb' => 'GET'],
+ ['name' => 'Local#streamLiked', 'url' => '/api/v1/stream/liked', 'verb' => 'GET'],
['name' => 'Local#streamAccount', 'url' => '/api/v1/account/{username}/stream', 'verb' => 'GET'],
['name' => 'Local#postCreate', 'url' => '/api/v1/post', 'verb' => 'POST'],
diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php
index 69294bf8..b731be2e 100644
--- a/lib/Controller/LocalController.php
+++ b/lib/Controller/LocalController.php
@@ -468,6 +468,28 @@ class LocalController extends Controller {
/**
+ * Get liked post
+ *
+ * @NoAdminRequired
+ *
+ * @param int $since
+ * @param int $limit
+ *
+ * @return DataResponse
+ */
+ public function streamLiked(int $since = 0, int $limit = 5): DataResponse {
+ try {
+ $this->initViewer(true);
+ $posts = $this->noteService->getStreamLiked($since, $limit);
+
+ return $this->success($posts);
+ } catch (Exception $e) {
+ return $this->fail($e);
+ }
+ }
+
+
+ /**
* @NoAdminRequired
*
* @param string $account
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php
index 214a3b4c..cab86988 100644
--- a/lib/Db/CoreRequestBuilder.php
+++ b/lib/Db/CoreRequestBuilder.php
@@ -860,6 +860,59 @@ class CoreRequestBuilder {
/**
* @param IQueryBuilder $qb
+ * @param string $type
+ */
+ protected function leftJoinActions(IQueryBuilder &$qb, string $type) {
+ if ($qb->getType() !== QueryBuilder::SELECT || $this->viewer === null) {
+ return;
+ }
+
+ $expr = $qb->expr();
+ $func = $qb->func();
+
+ $pf = $this->defaultSelectAlias;
+
+ $qb->selectAlias('a.id', 'action_id')
+ ->selectAlias('a.actor_id', 'action_actor_id')
+ ->selectAlias('a.object_id', 'action_object_id')
+ ->selectAlias('a.type', 'action_type');
+
+ $andX = $expr->andX();
+ $andX->add($expr->eq($func->lower($pf . '.id'), $func->lower('a.object_id')));
+ $andX->add($expr->eq('a.type', $qb->createNamedParameter($type)));
+ $andX->add(
+ $expr->eq(
+ $func->lower('a.actor_id'),
+ $qb->createNamedParameter(strtolower($this->viewer->getId()))
+ )
+ );
+
+ $qb->leftJoin(
+ $this->defaultSelectAlias, CoreRequestBuilder::TABLE_ACTIONS, 'a', $andX
+ );
+ }
+
+
+ /**
+ * @param array $data
+ */
+ protected function parseActionsLeftJoin(array $data) {
+ $new = [];
+ foreach ($data as $k => $v) {
+ if (substr($k, 0, 7) === 'action_') {
+ $new[substr($k, 7)] = $v;
+ }
+ }
+
+// $action = new Action();
+// $action->importFromDatabase($new);
+
+// return $action;
+ }
+
+
+ /**
+ * @param IQueryBuilder $qb
* @param string $fieldDocumentId
*/
protected function leftJoinCacheDocuments(IQueryBuilder &$qb, string $fieldDocumentId) {
@@ -1100,6 +1153,6 @@ class CoreRequestBuilder {
$qb->where($this->exprLimitToDBField($qb, 'class', 'OCA\Social\Cron\Queue', true, true));
$qb->execute();
}
-
+
}
diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php
index 737dfd3d..365d83f5 100644
--- a/lib/Db/StreamRequest.php
+++ b/lib/Db/StreamRequest.php
@@ -44,6 +44,7 @@ use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification;
use OCA\Social\Model\ActivityPub\Object\Document;
+use OCA\Social\Model\ActivityPub\Object\Like;
use OCA\Social\Model\ActivityPub\Object\Note;
use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Service\ConfigService;
@@ -483,10 +484,7 @@ class StreamRequest extends StreamRequestBuilder {
$qb = $this->getStreamSelectSql();
$this->limitPaginate($qb, $since, $limit);
-// if ($localOnly) {
$this->limitToLocal($qb, $localOnly);
-// }
-
$this->limitToType($qb, Note::TYPE);
$this->leftJoinCacheActors($qb, 'attributed_to');
@@ -511,6 +509,41 @@ class StreamRequest extends StreamRequestBuilder {
/**
* Should returns:
+ * * All liked posts
+ *
+ * @param int $since
+ * @param int $limit
+ * @param bool $localOnly
+ *
+ * @return array
+ * @throws Exception
+ */
+ public function getLiked(int $since = 0, int $limit = 5, bool $localOnly = true): array {
+ $qb = $this->getStreamSelectSql();
+ $this->limitPaginate($qb, $since, $limit);
+
+ $this->limitToType($qb, Note::TYPE);
+
+ $this->leftJoinCacheActors($qb, 'attributed_to');
+ $this->leftJoinActions($qb, Like::TYPE);
+ $this->filterDBField($qb, 'id', '', false, 'a');
+
+ $streams = [];
+ $cursor = $qb->execute();
+ while ($data = $cursor->fetch()) {
+ try {
+ $streams[] = $this->parseStreamSelectSql($data);
+ } catch (Exception $e) {
+ }
+ }
+ $cursor->closeCursor();
+
+ return $streams;
+ }
+
+
+ /**
+ * 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)
diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php
index 27327709..b9043175 100644
--- a/lib/Service/NoteService.php
+++ b/lib/Service/NoteService.php
@@ -442,7 +442,7 @@ class NoteService {
}
- /**m
+ /**
*
* @param int $since
* @param int $limit
@@ -456,6 +456,19 @@ class NoteService {
/**
+ *
+ * @param int $since
+ * @param int $limit
+ *
+ * @return Note[]
+ * @throws Exception
+ */
+ public function getStreamLiked(int $since = 0, int $limit = 5): array {
+ return $this->streamRequest->getLiked($since, $limit);
+ }
+
+
+ /**
* @param $noteId
*
* @return Person