From 7d7d310cc4f8661751fd2a62e33405c98f78aa62 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 22:49:25 +0200 Subject: [PATCH] Replace almost every Introduction places --- .../Introduction/Depository/Introduction.php | 42 +++++++++++++++-- .../Introduction/Entity/Introduction.php | 38 ++++++++------- .../Introduction/Factory/Introduction.php | 47 +++++++++++++++---- src/DI.php | 8 ++++ src/Model/Contact.php | 18 ++++--- src/Module/Delegation.php | 2 +- src/Protocol/DFRN.php | 13 +++-- src/Worker/Contact/RemoveContent.php | 3 +- 8 files changed, 128 insertions(+), 43 deletions(-) diff --git a/src/Contact/Introduction/Depository/Introduction.php b/src/Contact/Introduction/Depository/Introduction.php index f697c99009..280ae26d6e 100644 --- a/src/Contact/Introduction/Depository/Introduction.php +++ b/src/Contact/Introduction/Depository/Introduction.php @@ -76,7 +76,8 @@ class Introduction extends BaseDepository 'note' => $introduction->note, 'hash' => $introduction->hash, 'blocked' => $introduction->blocked ? 1 : 0, - 'ignore' => $introduction->ignore ? 1 : 0 + 'ignore' => $introduction->ignore ? 1 : 0, + 'datetime' => $introduction->datetime->format(DateTimeFormat::MYSQL), ]; } @@ -121,6 +122,42 @@ class Introduction extends BaseDepository return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount()); } + /** + * Selects the introduction for a given contact + * + * @param int $cid + * + * @return Entity\Introduction + * + * @throws IntroductionNotFoundException in case there is not Introduction for this contact + */ + public function selectForContact(int $cid): Entity\Introduction + { + try { + return $this->selectOne(['contact-id' => $cid]); + } catch (NotFoundException $exception) { + throw new IntroductionNotFoundException(sprintf('There is no Introduction for the contact %d', $cid), $exception); + } + } + + public function countActiveForUser($uid, array $params = []): int + { + try { + return $this->count(['blocked' => false, 'ignore' => false, 'uid' => $uid], $params); + } catch (\Exception $e) { + throw new IntroductionPersistenceException(sprintf('Cannot count Introductions for used %d', $uid), $e); + } + } + + public function existsForContact(int $cid, int $uid): bool + { + try { + return $this->exists(['uid' => $uid, 'suggest-cid' => $cid]); + } catch (\Exception $e) { + throw new IntroductionPersistenceException(sprintf('Cannot check Introductions for contact %d and user %d', $cid, $uid), $e); + } + } + /** * @param Entity\Introduction $introduction * @@ -151,8 +188,7 @@ class Introduction extends BaseDepository public function save(Entity\Introduction $introduction): Entity\Introduction { try { - $fields = $this->convertToTableRow($introduction); - $fields['datetime'] = DateTimeFormat::utcNow(); + $fields = $this->convertToTableRow($introduction); if ($introduction->id) { $this->db->update(self::$table_name, $fields, ['id' => $introduction->id]); diff --git a/src/Contact/Introduction/Entity/Introduction.php b/src/Contact/Introduction/Entity/Introduction.php index b9842a8429..59db24ffcf 100644 --- a/src/Contact/Introduction/Entity/Introduction.php +++ b/src/Contact/Introduction/Entity/Introduction.php @@ -25,9 +25,9 @@ use Friendica\BaseEntity; /** * @property-read int $uid - * @property-read int $fid - * @property-read int $cid * @property-read int $sid + * @property-read int|null $fid + * @property-read int|null $cid * @property-read bool $knowyou * @property-read bool $duplex * @property-read string $note @@ -42,11 +42,11 @@ class Introduction extends BaseEntity /** @var int */ protected $uid; /** @var int */ - protected $fid; - /** @var int */ - protected $cid; - /** @var int */ protected $sid; + /** @var int|null */ + protected $fid; + /** @var int|null */ + protected $cid; /** @var bool */ protected $knowyou; /** @var bool */ @@ -65,23 +65,25 @@ class Introduction extends BaseEntity protected $id; /** - * @param int $uid - * @param int $fid - * @param int $cid - * @param bool $knowyou - * @param bool $duplex - * @param string $note - * @param string $hash - * @param bool $blocked - * @param bool $ignore - * @param int|null $id + * @param int $uid + * @param int $sid + * @param int|null $fid + * @param int|null $cid + * @param bool $knowyou + * @param bool $duplex + * @param string $note + * @param string $hash + * @param \DateTime $datetime + * @param bool $blocked + * @param bool $ignore + * @param int|null $id */ - public function __construct(int $uid, int $fid, int $cid, int $sid, bool $knowyou, bool $duplex, string $note, string $hash, \DateTime $datetime, bool $blocked, bool $ignore, ?int $id) + public function __construct(int $uid, int $sid, ?int $fid, ?int $cid, bool $knowyou, bool $duplex, string $note, string $hash, \DateTime $datetime, bool $blocked, bool $ignore, ?int $id) { $this->uid = $uid; + $this->sid = $sid; $this->fid = $fid; $this->cid = $cid; - $this->sid = $sid; $this->knowyou = $knowyou; $this->duplex = $duplex; $this->note = $note; diff --git a/src/Contact/Introduction/Factory/Introduction.php b/src/Contact/Introduction/Factory/Introduction.php index 5f9d83734f..8fec61bb9f 100644 --- a/src/Contact/Introduction/Factory/Introduction.php +++ b/src/Contact/Introduction/Factory/Introduction.php @@ -24,6 +24,8 @@ namespace Friendica\Contact\Introduction\Factory; use Friendica\BaseFactory; use Friendica\Contact\Introduction\Entity; use Friendica\Capabilities\ICanCreateFromTableRow; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Strings; class Introduction extends BaseFactory implements ICanCreateFromTableRow { @@ -33,18 +35,47 @@ class Introduction extends BaseFactory implements ICanCreateFromTableRow public function createFromTableRow(array $row): Entity\Introduction { return new Entity\Introduction( - $row['uid'], - $row['fid'], - $row['contact-id'], - $row['suggested-cid'], + $row['uid'] ?? 0, + $row['suggest-cid'] ?? 0, + $row['fid'] ?? null, + $row['contact-id'] ?? null, !empty($row['knowyou']), - !empty($row['dupley']), - $row['note'], - $row['hash'], - new \DateTime($row['datetime'], new \DateTimeZone('UTC')), + !empty($row['duplex']), + $row['note'] ?? '', + $row['hash'] ?? '', + new \DateTime($row['datetime'] ?? 'now', new \DateTimeZone('UTC')), !empty($row['blocked']), !empty($row['ignore']), $row['id'] ?? null ); } + + public function createNew( + int $uid, + int $cid, + string $note, + int $fid = null, + int $sid = null, + bool $knowyou = false, + bool $duplex = false + ): Entity\Introduction { + return $this->createFromTableRow([ + 'uid' => $uid, + 'fid' => $fid, + 'contact-id' => $cid, + 'suggest-cid' => $sid, + 'knowyou' => $knowyou, + 'duplex' => $duplex, + 'note' => $note, + 'hash' => Strings::getRandomHex(), + 'datetime' => DateTimeFormat::utcNow(), + 'blocked' => false, + 'ignore' => false, + ]); + } + + public function createDummy(int $id): Entity\Introduction + { + return $this->createFromTableRow(['id' => $id]); + } } diff --git a/src/DI.php b/src/DI.php index c00fb82c1d..28df28a325 100644 --- a/src/DI.php +++ b/src/DI.php @@ -442,6 +442,14 @@ abstract class DI return self::$dice->create(Contact\Introduction\Depository\Introduction::class); } + /** + * @return Contact\Introduction\Factory\Introduction + */ + public static function introFactory() + { + return self::$dice->create(Contact\Introduction\Factory\Introduction::class); + } + public static function permissionSet(): Security\PermissionSet\Depository\PermissionSet { return self::$dice->create(Security\PermissionSet\Depository\PermissionSet::class); diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 06e546d614..0acb6d4d72 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -22,6 +22,7 @@ namespace Friendica\Model; use Friendica\App\BaseURL; +use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException; use Friendica\Content\Pager; use Friendica\Content\Text\HTML; use Friendica\Core\Hook; @@ -1085,9 +1086,11 @@ class Contact ]; if (!empty($contact['pending'])) { - $intro = DBA::selectFirst('intro', ['id'], ['contact-id' => $contact['id']]); - if (DBA::isResult($intro)) { + try { + $intro = DI::intro()->selectForContact($contact['id']); $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro['id'], true]; + } catch (IntroductionNotFoundException $exception) { + DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]); } } } @@ -2706,12 +2709,13 @@ class Contact $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]); if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) { // create notification - $hash = Strings::getRandomHex(); - if (is_array($contact_record)) { - DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'], - 'blocked' => false, 'knowyou' => false, 'note' => $note, - 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]); + $intro = DI::introFactory()->createNew( + $importer['uid'], + $contact_record['id'], + $note + ); + DI::intro()->save($intro); } Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']); diff --git a/src/Module/Delegation.php b/src/Module/Delegation.php index affe5e7b8f..12f8c5074c 100644 --- a/src/Module/Delegation.php +++ b/src/Module/Delegation.php @@ -133,7 +133,7 @@ class Delegation extends BaseModule $params = ['distinct' => true, 'expression' => 'convid']; $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params); - $notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]); + $notifications += DI::intro()->countActiveForUser($identity['uid']); $identities[$key]['notifications'] = $notifications; } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 8c511c2cd5..9246d85833 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1352,7 +1352,7 @@ class DFRN } // Quit if we already have an introduction for this person - if (DBA::exists('intro', ['uid' => $uid, 'suggest-cid' => $cid])) { + if (DI::intro()->existsForContact($cid, $uid)) { return false; } @@ -1366,10 +1366,13 @@ class DFRN $suggest['title'] = ''; $suggest['body'] = $note; - $hash = Strings::getRandomHex(); - $fields = ['uid' => $suggest['uid'], 'suggest-cid' => $cid, 'contact-id' => $suggest['cid'], - 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false]; - DBA::insert('intro', $fields); + DI::intro()->save(DI::introFactory()->createNew( + $suggest['uid'], + $suggest['cid'], + $suggest['body'], + null, + $cid + )); DI::notify()->createFromArray([ 'type' => Notification\Type::SUGGEST, diff --git a/src/Worker/Contact/RemoveContent.php b/src/Worker/Contact/RemoveContent.php index 567cab1a86..f0e6e631d7 100644 --- a/src/Worker/Contact/RemoveContent.php +++ b/src/Worker/Contact/RemoveContent.php @@ -24,6 +24,7 @@ namespace Friendica\Worker\Contact; use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\Database\DBStructure; +use Friendica\DI; use Friendica\Model\Photo; use Friendica\Model\Post; @@ -84,7 +85,7 @@ class RemoveContent DBA::delete('user-contact', ['cid' => $id]); DBA::delete('group_member', ['contact-id' => $id]); - DBA::delete('intro', ['contact-id' => $id]); + DI::intro()->delete(DI::introFactory()->createDummy($id)); return $contact; }