blob: 78e63552f3fb2e3fa948fef0f044560695757e2d (
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
<?php
// Nextcloud - Social Support
// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Social\Service;
use Doctrine\Common\Collections\Collection;
use OCA\Social\Entity\Account;
use OCA\Social\Entity\Follow;
use OCP\DB\ORM\IEntityManager;
use OCP\DB\ORM\IEntityRepository;
use OCP\IRequest;
use OCP\IUser;
class AccountFinder {
private IEntityManager $entityManager;
private IEntityRepository $repository;
private IRequest $request;
private ?Account $representative = null;
public function __construct(IEntityManager $entityManager, IRequest $request) {
$this->entityManager = $entityManager;
$this->repository = $this->entityManager->getRepository(Account::class);
$this->request = $request;
}
public function findRemote(string $userName, ?string $domain): ?Account {
return $this->repository->findOneBy([
'domain' => $domain,
'userName' => $userName,
]);
}
public function findLocal(string $userName): ?Account {
return $this->findRemote($userName, null);
}
public function getAccountByNextcloudId(string $userId): ?Account {
return $this->repository->findOneBy([
'userId' => $userId,
]);
}
public function getCurrentAccount(IUser $user): Account {
$account = $this->getAccountByNextcloudId($user->getUID());
if ($account) {
return $account;
}
$account = Account::newLocal();
$account->setUserName($user->getUID());
$account->setUserId($user->getUID());
$account->setName($user->getDisplayName());
$account->generateKeys();
$this->entityManager->persist($account);
$this->entityManager->flush();
return $account;
}
public function getRepresentative(): Account {
if ($this->representative !== null) {
return $this->representative;
}
$account = $this->repository->findOneBy([
'userId' => '__self',
]);
if ($account) {
$this->representative = $account;
return $account;
}
$account = Account::newLocal();
$account->setRepresentative()
->setActorType(Account::TYPE_APPLICATION)
->setUserName($this->request->getServerHost())
->setUserId('__self')
->setLocked(true)
->generateKeys();
$this->entityManager->persist($account);
$this->entityManager->flush();
$this->representative = $account;
return $account;
}
/**
* @param Account $account
* @return array<Follow>
*/
public function getLocalFollowersOf(Account $account): array {
return $this->entityManager->createQuery('SELECT f,a FROM \OCA\Social\Entity\Follow f LEFT JOIN f.account a WHERE f.targetAccount = :target')
->setParameters(['target' => $account])->getResult();
}
}
|