summaryrefslogtreecommitdiffstats
path: root/lib/Service/Feed/PostDeliveryService.php
blob: a1403bf6dc27919a41b9706030b6eadcb667fa65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);

// Nextcloud - Social Support
// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\Social\Service;

use OCA\Social\Entity\Account;
use OCA\Social\Entity\Mention;
use OCA\Social\Entity\Status;

class PostDeliveryService {
	private FeedManager $feedManager;
	private AccountFinder $accountFinder;

	public function __construct(FeedManager $feedManager, AccountFinder $accountFinder) {
		$this->feedManager = $feedManager;
		$this->accountFinder = $accountFinder;
	}

	public function run(Account $author, Status $status): void {
		// deliver to self
		if ($status->isLocal()) {
			$this->feedManager->addToHome($author->getId(), $status);
		}

		// deliver to mentioned accounts
		$localFollowers = $this->accountFinder->getLocalFollowersOf($author);
		$status->getActiveMentions()->forAll(function (Mention $mention) use ($status): void{
			if ($mention->getAccount()->isLocal()) {
				$this->deliverLocalAccount($status, $mention->getAccount());
			}
		});

		// deliver to local followers
		$localFollowers->forAll(function (Account $account) use ($status): void {
			$this->deliverLocalAccount($status, $account);
		});

	}

	public function deliverLocalAccount(Status $status, Account $account) {
		assert($account->isLocal());

		// TODO create notification

		$this->feedManager->addToHome($account->getId(), $status);
	}
}