summaryrefslogtreecommitdiffstats
path: root/lib/Service
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-08-12 10:00:40 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-08-12 10:00:40 -0100
commitb1c864e17a7458fcb9094d2b16d721f787f9b60f (patch)
treefe22671c283c48cd855ee2354105214c82577391 /lib/Service
parent708fef7c669bec999c1a016e8dae3c06f4bd3d94 (diff)
streamdetails & pushservice
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Service')
-rw-r--r--lib/Service/BoostService.php18
-rw-r--r--lib/Service/ConfigService.php1
-rw-r--r--lib/Service/DetailsService.php131
-rw-r--r--lib/Service/FollowService.php19
-rw-r--r--lib/Service/MiscService.php11
-rw-r--r--lib/Service/PushService.php127
6 files changed, 293 insertions, 14 deletions
diff --git a/lib/Service/BoostService.php b/lib/Service/BoostService.php
index bda714c4..b865266e 100644
--- a/lib/Service/BoostService.php
+++ b/lib/Service/BoostService.php
@@ -60,8 +60,8 @@ class BoostService {
/** @var StreamRequest */
private $streamRequest;
- /** @var NoteService */
- private $noteService;
+ /** @var StreamService */
+ private $streamService;
/** @var SignatureService */
private $signatureService;
@@ -83,7 +83,7 @@ class BoostService {
* BoostService constructor.
*
* @param StreamRequest $streamRequest
- * @param NoteService $noteService
+ * @param StreamService $streamService
* @param SignatureService $signatureService
* @param ActivityService $activityService
* @param StreamActionService $streamActionService
@@ -91,12 +91,12 @@ class BoostService {
* @param MiscService $miscService
*/
public function __construct(
- StreamRequest $streamRequest, NoteService $noteService, SignatureService $signatureService,
+ StreamRequest $streamRequest, StreamService $streamService, SignatureService $signatureService,
ActivityService $activityService, StreamActionService $streamActionService,
StreamQueueService $streamQueueService, MiscService $miscService
) {
$this->streamRequest = $streamRequest;
- $this->noteService = $noteService;
+ $this->streamService = $streamService;
$this->signatureService = $signatureService;
$this->activityService = $activityService;
$this->streamActionService = $streamActionService;
@@ -118,10 +118,10 @@ class BoostService {
public function create(Person $actor, string $postId, &$token = ''): ACore {
/** @var Announce $announce */
$announce = AP::$activityPub->getItemFromType(Announce::TYPE);
- $this->noteService->assignItem($announce, $actor, Stream::TYPE_ANNOUNCE);
+ $this->streamService->assignItem($announce, $actor, Stream::TYPE_ANNOUNCE);
$announce->setActor($actor);
- $note = $this->noteService->getNoteById($postId, true);
+ $note = $this->streamService->getStreamById($postId, true);
if ($note->getType() !== Note::TYPE) {
throw new StreamNotFoundException('Stream is not a Note');
}
@@ -180,10 +180,10 @@ class BoostService {
*/
public function delete(Person $actor, string $postId, &$token = ''): ACore {
$undo = new Undo();
- $this->noteService->assignItem($undo, $actor, Stream::TYPE_PUBLIC);
+ $this->streamService->assignItem($undo, $actor, Stream::TYPE_PUBLIC);
$undo->setActor($actor);
- $note = $this->noteService->getNoteById($postId, true);
+ $note = $this->streamService->getStreamById($postId, true);
if ($note->getType() !== Note::TYPE) {
throw new StreamNotFoundException('Stream is not a Note');
}
diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php
index 515521f4..fb5b1fe0 100644
--- a/lib/Service/ConfigService.php
+++ b/lib/Service/ConfigService.php
@@ -51,6 +51,7 @@ class ConfigService {
use TPathTools;
use TArrayTools;
+
const CLOUD_URL = 'cloud_url';
const SOCIAL_URL = 'social_url';
const SOCIAL_ADDRESS = 'social_address';
diff --git a/lib/Service/DetailsService.php b/lib/Service/DetailsService.php
new file mode 100644
index 00000000..5602446f
--- /dev/null
+++ b/lib/Service/DetailsService.php
@@ -0,0 +1,131 @@
+<?php
+declare(strict_types=1);
+
+
+/**
+ * Nextcloud - Social Support
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Social\Service;
+
+
+use OCA\Social\Exceptions\ActorDoesNotExistException;
+use OCA\Social\Exceptions\SocialAppConfigException;
+use OCA\Social\Model\ActivityPub\Stream;
+use OCA\Social\Model\StreamDetails;
+
+
+/**
+ * Class DetailsService
+ *
+ * @package OCA\Social\Service
+ */
+class DetailsService {
+
+ /** @var StreamService */
+ private $streamService;
+
+ /** @var AccountService */
+ private $accountService;
+
+ /** @var FollowService */
+ private $followService;
+
+ /** @var CacheActorService */
+ private $cacheActorService;
+
+
+ /**
+ * DetailsService constructor.
+ *
+ * @param StreamService $streamService
+ * @param AccountService $accountService
+ * @param FollowService $followService
+ * @param CacheActorService $cacheActorService
+ */
+ public function __construct(
+ StreamService $streamService, AccountService $accountService,
+ FollowService $followService, CacheActorService $cacheActorService
+ ) {
+ $this->streamService = $streamService;
+ $this->accountService = $accountService;
+ $this->followService = $followService;
+ $this->cacheActorService = $cacheActorService;
+ }
+
+
+ /**
+ * @param Stream $stream
+ *
+ * @return StreamDetails
+ * @throws SocialAppConfigException
+ */
+ public function generateDetailsFromStream(Stream $stream): StreamDetails {
+ $this->streamService->detectType($stream);
+
+ $details = new StreamDetails($stream);
+ $this->setStreamViewers($details);
+
+ if ($stream->getType() === Stream::TYPE_PUBLIC) {
+ if ($stream->isLocal()) {
+ $details->setPublic(true);
+ } else {
+ $details->setFederated(true);
+ }
+ }
+
+ return $details;
+ }
+
+
+ /**
+ * @param StreamDetails $details
+ *
+ * @throws SocialAppConfigException
+ */
+ private function setStreamViewers(StreamDetails $details) {
+ $stream = $details->getStream();
+
+ foreach ($stream->getRecipients(true) as $recipient) {
+ try {
+ $actor = $this->accountService->getFromId($recipient);
+ $details->addDirectViewer($actor);
+ } catch (ActorDoesNotExistException $e) {
+ }
+
+ $followers = $this->followService->getFollowersFromFollowId($recipient);
+ foreach ($followers as $follower) {
+ if (!$follower->hasActor()) {
+ continue;
+ }
+
+ $actor = $follower->getActor();
+ $details->addHomeViewer($actor);
+ }
+ }
+ }
+
+}
+
diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php
index 73a87294..0c677d35 100644
--- a/lib/Service/FollowService.php
+++ b/lib/Service/FollowService.php
@@ -39,21 +39,20 @@ use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Exceptions\FollowSameAccountException;
use OCA\Social\Exceptions\InvalidOriginException;
use OCA\Social\Exceptions\InvalidResourceException;
+use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\RedundancyLimitException;
use OCA\Social\Exceptions\RequestContentException;
-use OCA\Social\Exceptions\RequestResultNotJsonException;
-use OCA\Social\Exceptions\RetrieveAccountFormatException;
use OCA\Social\Exceptions\RequestNetworkException;
+use OCA\Social\Exceptions\RequestResultNotJsonException;
use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException;
+use OCA\Social\Exceptions\RetrieveAccountFormatException;
use OCA\Social\Exceptions\SocialAppConfigException;
-use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Exceptions\UrlCloudException;
-use OCA\Social\Model\ActivityPub\ACore;
-use OCA\Social\Model\ActivityPub\Object\Follow;
use OCA\Social\Model\ActivityPub\Activity\Undo;
use OCA\Social\Model\ActivityPub\Actor\Person;
+use OCA\Social\Model\ActivityPub\Object\Follow;
use OCA\Social\Model\ActivityPub\OrderedCollection;
use OCA\Social\Model\InstancePath;
@@ -285,5 +284,15 @@ class FollowService {
return $collection;
}
+
+ /**
+ * @param string $recipient
+ *
+ * @return Follow[]
+ */
+ public function getFollowersFromFollowId(string $recipient): array {
+ return $this->followsRequest->getFollowersByFollowId($recipient);
+ }
+
}
diff --git a/lib/Service/MiscService.php b/lib/Service/MiscService.php
index 0d41633e..1e6f9cb9 100644
--- a/lib/Service/MiscService.php
+++ b/lib/Service/MiscService.php
@@ -36,6 +36,7 @@ use OCA\Social\AppInfo\Application;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
+use OCP\Util;
/**
@@ -80,6 +81,16 @@ class MiscService {
/**
+ * @return int
+ */
+ public function getNcVersion(): int {
+ $ver = Util::getVersion();
+
+ return $ver[0];
+ }
+
+
+ /**
* @param string $userId
*
* @return IUser
diff --git a/lib/Service/PushService.php b/lib/Service/PushService.php
new file mode 100644
index 00000000..23124197
--- /dev/null
+++ b/lib/Service/PushService.php
@@ -0,0 +1,127 @@
+<?php
+declare(strict_types=1);
+
+
+/**
+ * Nextcloud - Social Support
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Social\Service;
+
+
+use daita\MySmallPhpTools\Traits\TAsync;
+use OCA\Social\Exceptions\SocialAppConfigException;
+use OCA\Social\Model\ActivityPub\Actor\Person;
+use OCA\Social\Model\ActivityPub\Stream;
+use OCP\AppFramework\QueryException;
+use OCP\Stratos\Exceptions\StratosInstallException;
+use OCP\Stratos\IStratosManager;
+use OCP\Stratos\Model\IStratosWrapper;
+
+/**
+ * Class PushService
+ *
+ * @package OCA\Social\Service
+ */
+class PushService {
+
+
+ use TAsync;
+
+
+ /** @var IStratosManager */
+ private $stratosManager;
+
+ /** @var DetailsService */
+ private $detailsService;
+
+ /** @var MiscService */
+ private $miscService;
+
+
+ /**
+ * DetailsService constructor.
+ *
+ * @param DetailsService $detailsService
+ * @param MiscService $miscService
+ */
+ public function __construct(DetailsService $detailsService, MiscService $miscService) {
+ $this->detailsService = $detailsService;
+ $this->miscService = $miscService;
+
+ // FIX ME: nc18/stratos
+ if ($this->miscService->getNcVersion() >= 17) {
+ try {
+ $this->stratosManager = \OC::$server->query(IStratosManager::class);
+ } catch (QueryException $e) {
+ $miscService->log('QueryException while loading StratosManager');
+ }
+ }
+ }
+
+
+ /**
+ * @param Stream $stream
+ *
+ * @throws SocialAppConfigException
+ */
+ public function onNewStream(Stream $stream) {
+ // FIXME: remove in nc18
+ if ($this->miscService->getNcVersion() < 17) {
+ return;
+ }
+
+ if (!$this->stratosManager->isAvailable()) {
+ return;
+ }
+
+ $details = $this->detailsService->generateDetailsFromStream($stream);
+ $home = array_map(
+ function(Person $item): string {
+ return $item->getUserId();
+ }, $details->getHomeViewers()
+ );
+ $direct = array_map(
+ function(Person $item): string {
+ return $item->getUserId();
+ }, $details->getDirectViewers()
+ );
+ }
+
+
+ /**
+ * @param $userId
+ *
+ * @return IStratosWrapper
+ * @throws StratosInstallException
+ */
+ public function testOnAccount(string $userId): IStratosWrapper {
+ $stratosHelper = $this->stratosManager->getStratosHelper();
+
+ return $stratosHelper->test($userId);
+ }
+
+}
+