summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-09-14 09:01:05 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-09-25 14:07:24 +0200
commit2ea24d7785441df45209be69750e026c2b9897c0 (patch)
tree1068a09015d74673a4be8a6fee850f2c45a04564 /lib
parentea34bc6e70de68fbf3af3a5b620af4619f6a26a1 (diff)
start migration to a better QueryBuilder.
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ActivityPubController.php32
-rw-r--r--lib/Db/CoreRequestBuilder.php39
-rw-r--r--lib/Db/SocialQueryBuilder.php285
3 files changed, 333 insertions, 23 deletions
diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php
index 5dd30fd7..8037193f 100644
--- a/lib/Controller/ActivityPubController.php
+++ b/lib/Controller/ActivityPubController.php
@@ -42,12 +42,14 @@ use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\StreamNotFoundException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Service\CacheActorService;
+use OCA\Social\Service\ConfigService;
use OCA\Social\Service\FediverseService;
use OCA\Social\Service\FollowService;
use OCA\Social\Service\ImportService;
use OCA\Social\Service\MiscService;
use OCA\Social\Service\SignatureService;
use OCA\Social\Service\StreamQueueService;
+use OCA\Social\Service\StreamService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
@@ -82,6 +84,12 @@ class ActivityPubController extends Controller {
/** @var FollowService */
private $followService;
+ /** @var StreamService */
+ private $streamService;
+
+ /** @var ConfigService */
+ private $configService;
+
/** @var MiscService */
private $miscService;
@@ -97,13 +105,16 @@ class ActivityPubController extends Controller {
* @param StreamQueueService $streamQueueService
* @param ImportService $importService
* @param FollowService $followService
+ * @param StreamService $streamService
+ * @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(
IRequest $request, SocialPubController $socialPubController,
FediverseService $fediverseService, CacheActorService $cacheActorService,
SignatureService $signatureService, StreamQueueService $streamQueueService,
- ImportService $importService, FollowService $followService, MiscService $miscService
+ ImportService $importService, FollowService $followService, StreamService $streamService,
+ ConfigService $configService, MiscService $miscService
) {
parent::__construct(Application::APP_NAME, $request);
@@ -114,6 +125,8 @@ class ActivityPubController extends Controller {
$this->streamQueueService = $streamQueueService;
$this->importService = $importService;
$this->followService = $followService;
+ $this->streamService = $streamService;
+ $this->configService = $configService;
$this->miscService = $miscService;
}
@@ -231,7 +244,7 @@ class ActivityPubController extends Controller {
$this->miscService->log('[<<] inbox: ' . $body, 1);
$requestTime = 0;
- $origin = $this->signatureService->checkRequest($this->request, $body,$requestTime);
+ $origin = $this->signatureService->checkRequest($this->request, $body, $requestTime);
$this->fediverseService->authorized($origin);
// TODO - check the recipient <-> username
@@ -288,7 +301,6 @@ class ActivityPubController extends Controller {
* @throws SocialAppConfigException
*/
public function followers(string $username): Response {
-
if (!$this->checkSourceActivityStreams()) {
return $this->socialPubController->followers($username);
}
@@ -334,18 +346,24 @@ class ActivityPubController extends Controller {
* @PublicPage
*
* @param string $username
- * @param string $postId
+ * @param string $token
*
* @return Response
* @throws SocialAppConfigException
* @throws StreamNotFoundException
*/
- public function displayPost(string $username, string $postId) {
+ public function displayPost(string $username, string $token): Response {
if (!$this->checkSourceActivityStreams()) {
- return $this->socialPubController->displayPost($username, $postId);
+ return $this->socialPubController->displayPost($username, $token);
}
- return $this->success([$username, $postId]);
+ // TODO - check viewer rights !
+ $postId = $this->configService->getSocialUrl() . '@' . $username . '/' . $token;
+ $stream = $this->streamService->getStreamById($postId, false);
+
+ $stream->setCompleteDetails(false);
+
+ return $this->directSuccess($stream);
}
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php
index 697df5fd..461b3931 100644
--- a/lib/Db/CoreRequestBuilder.php
+++ b/lib/Db/CoreRequestBuilder.php
@@ -36,8 +36,8 @@ use DateInterval;
use DateTime;
use Doctrine\DBAL\Query\QueryBuilder;
use Exception;
+use OC;
use OC\DB\SchemaWrapper;
-use OC\SystemConfig;
use OCA\Social\AP;
use OCA\Social\Exceptions\DateTimeException;
use OCA\Social\Exceptions\InvalidResourceException;
@@ -92,6 +92,8 @@ class CoreRequestBuilder {
self::TABLE_STREAM_ACTIONS
];
+ protected $logger;
+
/** @var IDBConnection */
protected $dbConnection;
@@ -102,41 +104,44 @@ class CoreRequestBuilder {
protected $miscService;
- /** @var string */
- protected $defaultSelectAlias;
/** @var Person */
protected $viewer = null;
+ /** @var string */
+ protected $defaultSelectAlias;
+
/**
* CoreRequestBuilder constructor.
*
* @param IDBConnection $connection
+ * @param ILogger $logger
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(
- IDBConnection $connection, ConfigService $configService, MiscService $miscService
+ IDBConnection $connection, ILogger $logger, ConfigService $configService, MiscService $miscService
) {
$this->dbConnection = $connection;
+ $this->logger = $logger;
$this->configService = $configService;
$this->miscService = $miscService;
}
-// /**
-// * @return SocialQueryBuilder
-// */
-// public function getQueryBuilder(): SocialQueryBuilder {
-// $qb = new SocialQueryBuilder(
-// $this->dbConnection,
-// $this->config,
-// $this->logger
-// );
-//
-// return $qb;
-// }
+ /**
+ * @return SocialQueryBuilder
+ */
+ public function getQueryBuilder(): SocialQueryBuilder {
+ $qb = new SocialQueryBuilder(
+ $this->dbConnection,
+ OC::$server->getSystemConfig(),
+ $this->logger
+ );
+
+ return $qb;
+ }
/**
@@ -540,6 +545,8 @@ class CoreRequestBuilder {
$qb->orderBy($pf . '.published_time', 'desc');
}
+//
+//
/**
* @param IQueryBuilder $qb
diff --git a/lib/Db/SocialQueryBuilder.php b/lib/Db/SocialQueryBuilder.php
index 9a9f82b5..b1e01a8a 100644
--- a/lib/Db/SocialQueryBuilder.php
+++ b/lib/Db/SocialQueryBuilder.php
@@ -32,6 +32,10 @@ namespace OCA\Social\Db;
use daita\MySmallPhpTools\Db\ExtendedQueryBuilder;
+use DateInterval;
+use DateTime;
+use Exception;
+use OCA\Social\Model\ActivityPub\Actor\Person;
/**
@@ -41,6 +45,41 @@ use daita\MySmallPhpTools\Db\ExtendedQueryBuilder;
*/
class SocialQueryBuilder extends ExtendedQueryBuilder {
+
+ /** @var Person */
+ private $viewer = null;
+
+
+ /**
+ * @return bool
+ */
+ public function hasViewer(): bool {
+ return ($this->viewer !== null);
+ }
+
+ /**
+ * @param Person $viewer
+ */
+ public function setViewer(Person $viewer): void {
+ $this->viewer = $viewer;
+ }
+
+ /**
+ * @return Person
+ */
+ public function getViewer(): Person {
+ return $this->viewer;
+ }
+
+
+ /**
+ * @param string $id
+ */
+ public function generatePrimaryKey(string $id) {
+ $this->setValue('id_prim', $this->createNamedParameter(hash('sha512', $id)));
+ }
+
+
/**
* Limit the request to the Type
*
@@ -54,5 +93,251 @@ class SocialQueryBuilder extends ExtendedQueryBuilder {
return $this;
}
+
+ /**
+ * Limit the request to the ActivityId
+ *
+ * @param string $activityId
+ */
+ protected function limitToActivityId(string $activityId) {
+ $this->limitToDBField('activity_id', $activityId, false);
+ }
+
+
+ /**
+ * Limit the request to the Id (string)
+ *
+ * @param string $id
+ */
+ protected function limitToInReplyTo(string $id) {
+ $this->limitToDBField('in_reply_to', $id, false);
+ }
+
+
+ /**
+ * Limit the request to the sub-type
+ *
+ * @param string $subType
+ */
+ protected function limitToSubType(string $subType) {
+ $this->limitToDBField('subtype', $subType);
+ }
+
+
+ /**
+ * @param string $type
+ */
+ protected function filterType(string $type) {
+ $this->filterDBField('type', $type);
+ }
+
+
+ /**
+ * Limit the request to the Preferred Username
+ *
+ * @param string $username
+ */
+ protected function limitToPreferredUsername(string $username) {
+ $this->limitToDBField('preferred_username', $username, false);
+ }
+
+ /**
+ * search using username
+ *
+ * @param string $username
+ */
+ protected function searchInPreferredUsername(string $username) {
+ $dbConn = $this->getConnection();
+ $this->searchInDBField('preferred_username', $dbConn->escapeLikeParameter($username) . '%');
+ }
+
+
+ /**
+ * Limit the request to the ActorId
+ */
+ protected function limitToPublic() {
+ $this->limitToDBFieldInt('public', 1);
+ }
+
+
+ /**
+ * Limit the request to the token
+ *
+ * @param string $token
+ */
+ protected function limitToToken(string $token) {
+ $this->limitToDBField('token', $token);
+ }
+
+ /**
+ * Limit the results to a given number
+ *
+ * @param int $limit
+ */
+ protected function limitResults(int $limit) {
+ $this->setMaxResults($limit);
+ }
+
+
+ /**
+ * Limit the request to the ActorId
+ *
+ * @param string $hashtag
+ */
+ protected function limitToHashtag(string $hashtag) {
+ $this->limitToDBField('hashtag', $hashtag, false);
+ }
+
+
+ /**
+ * Limit the request to the ActorId
+ *
+ * @param string $hashtag
+ * @param bool $all
+ */
+ protected function searchInHashtag(string $hashtag, bool $all = false) {
+ $dbConn = $this->getConnection();
+ $this->searchInDBField('hashtag', (($all) ? '%' : '') . $dbConn->escapeLikeParameter($hashtag) . '%');
+ }
+
+
+ /**
+ * Limit the request to the ActorId
+ *
+ * @param string $actorId
+ * @param string $alias
+ */
+ protected function limitToActorId(string $actorId, string $alias = '') {
+ $this->limitToDBField('actor_id', $actorId, false, $alias);
+ }
+
+
+ /**
+ * Limit the request to the FollowId
+ *
+ * @param string $followId
+ */
+ protected function limitToFollowId(string $followId) {
+ $this->limitToDBField('follow_id', $followId, false);
+ }
+
+
+ /**
+ * Limit the request to the FollowId
+ *
+ * @param bool $accepted
+ * @param string $alias
+ */
+ protected function limitToAccepted(bool $accepted, string $alias = '') {
+ $this->limitToDBField('accepted', ($accepted) ? '1' : '0', true, $alias);
+ }
+
+
+ /**
+ * Limit the request to the ServiceId
+ *
+ * @param string $objectId
+ */
+ protected function limitToObjectId(string $objectId) {
+ $this->limitToDBField('object_id', $objectId, false);
+ }
+
+
+ /**
+ * Limit the request to the account
+ *
+ * @param string $account
+ */
+ protected function limitToAccount(string $account) {
+ $this->limitToDBField('account', $account, false);
+ }
+
+
+ /**
+ * Limit the request to the account
+ *
+ * @param string $account
+ */
+ protected function searchInAccount(string $account) {
+ $dbConn = $this->getConnection();
+ $this->searchInDBField('account', $dbConn->escapeLikeParameter($account) . '%');
+ }
+
+
+ /**
+ * Limit the request to the creation
+ *
+ * @param int $delay
+ *
+ * @throws Exception
+ */
+ protected function limitToCaching(int $delay = 0) {
+ $date = new DateTime('now');
+ $date->sub(new DateInterval('PT' . $delay . 'M'));
+
+ $this->limitToDBFieldDateTime('caching', $date, true);
+ }
+
+
+ /**
+ * Limit the request to the url
+ *
+ * @param string $url
+ */
+ protected function limitToUrl(string $url) {
+ $this->limitToDBField('url', $url);
+ }
+
+
+ /**
+ * Limit the request to the url
+ *
+ * @param string $actorId
+ */
+ protected function limitToAttributedTo(string $actorId) {
+ $this->limitToDBField('attributed_to', $actorId, false);
+ }
+
+
+ /**
+ * Limit the request to the status
+ *
+ * @param int $status
+ */
+ protected function limitToStatus(int $status) {
+ $this->limitToDBFieldInt('status', $status);
+ }
+
+
+ /**
+ * Limit the request to the instance
+ *
+ * @param string $address
+ */
+ protected function limitToAddress(string $address) {
+ $this->limitToDBField('address', $address);
+ }
+
+
+ /**
+ * Limit the request to the instance
+ *
+ * @param bool $local
+ */
+ protected function limitToLocal(bool $local) {
+ $this->limitToDBField('local', ($local) ? '1' : '0');
+ }
+
+
+ /**
+ * Limit the request to the parent_id
+ *
+ * @param string $parentId
+ */
+ protected function limitToParentId(string $parentId) {
+ $this->limitToDBField('parent_id', $parentId);
+ }
+
+
}