]> git.mxchange.org Git - friendica.git/commitdiff
Replace almost every Introduction places
authorPhilipp <admin@philipp.info>
Mon, 18 Oct 2021 20:49:25 +0000 (22:49 +0200)
committerPhilipp <admin@philipp.info>
Thu, 21 Oct 2021 19:49:21 +0000 (21:49 +0200)
src/Contact/Introduction/Depository/Introduction.php
src/Contact/Introduction/Entity/Introduction.php
src/Contact/Introduction/Factory/Introduction.php
src/DI.php
src/Model/Contact.php
src/Module/Delegation.php
src/Protocol/DFRN.php
src/Worker/Contact/RemoveContent.php

index f697c99009a8ce37d8cdb8031251ae7b38fd6606..280ae26d6e23789b10dee34ae0571bd0e73a69e1 100644 (file)
@@ -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]);
index b9842a84292beee89e8ce7286bc3a821762ccefd..59db24ffcfd96137e9e250b74134b08cdabc4ed5 100644 (file)
@@ -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 $sid;
+       /** @var int|null */
        protected $fid;
-       /** @var int */
+       /** @var int|null */
        protected $cid;
-       /** @var int */
-       protected $sid;
        /** @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;
index 5f9d83734f39d46d238b7e96e2019c4b99e174c6..8fec61bb9f54113f0c966073b2ce4117149fcaa7 100644 (file)
@@ -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]);
+       }
 }
index c00fb82c1d7a8d8956dd3c56e6c75e9f3b97080f..28df28a3258a864f087fd9a28c86dd118c15410b 100644 (file)
@@ -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);
index 06e546d6145f3cfdeda927f3609789fc85f9cc15..0acb6d4d72d07099576406ca9e7e9524edf420f2 100644 (file)
@@ -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']);
index affe5e7b8fa8209f0745801bb5355ad66cdef3a1..12f8c5074cbe26db122cd2f27a17a22560071c0d 100644 (file)
@@ -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;
                }
index 8c511c2cd5fbe1b1225e3ab04e5501501b8f9a4c..9246d8583349591d78d838183e5d4e216e3aacec 100644 (file)
@@ -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,
index 567cab1a86c62165b937456d3dd845e9ec293836..f0e6e631d798b4be5f2afaf09e1669bf5e76c365 100644 (file)
@@ -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;
        }