]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Introduction.php
Merge pull request #8071 from nupplaphil/task/Lock_to_lock
[friendica.git] / src / Model / Introduction.php
index 127765c0cb5f380ea7991e02dc983cf7d6c51f43..ee4f0e71dcd7047275a2748ef4542baa5c825cb6 100644 (file)
@@ -4,10 +4,13 @@ namespace Friendica\Model;
 
 use Friendica\BaseModel;
 use Friendica\Core\Protocol;
+use Friendica\Database\Database;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
+use Friendica\Repository;
 use Friendica\Util\DateTimeFormat;
+use Psr\Log\LoggerInterface;
 
 /**
  * @property int    uid
@@ -20,33 +23,40 @@ use Friendica\Util\DateTimeFormat;
  * @property string datetime
  * @property bool   blocked
  * @property bool   ignored
- *
- * @package Friendica\Model
  */
 final class Introduction extends BaseModel
 {
-       static $table_name = 'intro';
+       /** @var Repository\Introduction */
+       protected $intro;
+
+       public function __construct(Database $dba, LoggerInterface $logger, Repository\Introduction $intro, array $data = [])
+       {
+               parent::__construct($dba, $logger, $data);
+
+               $this->intro = $intro;
+       }
 
        /**
-        * Confirms a follow request and sends a notic to the remote contact.
+        * Confirms a follow request and sends a notice to the remote contact.
         *
-        * @param bool      $duplex Is it a follow back?
-        * @param bool|null $hidden Should this contact be hidden? null = no change
+        * @param bool               $duplex       Is it a follow back?
+        * @param bool|null          $hidden       Should this contact be hidden? null = no change
+        * @return bool
         * @throws HTTPException\InternalServerErrorException
-        * @throws \ImagickException
         * @throws HTTPException\NotFoundException
+        * @throws \ImagickException
         */
        public function confirm(bool $duplex = false, bool $hidden = null)
        {
                $this->logger->info('Confirming follower', ['cid' => $this->{'contact-id'}]);
 
-               $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
+               $contact = Model\Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
 
                if (!$contact) {
                        throw new HTTPException\NotFoundException('Contact record not found.');
                }
 
-               $new_relation = $contact['rel'];
+               $newRelation = $contact['rel'];
                $writable = $contact['writable'];
 
                if (!empty($contact['protocol'])) {
@@ -61,12 +71,12 @@ final class Introduction extends BaseModel
 
                if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
                        if ($duplex) {
-                               $new_relation = Contact::FRIEND;
+                               $newRelation = Model\Contact::FRIEND;
                        } else {
-                               $new_relation = Contact::FOLLOWER;
+                               $newRelation = Model\Contact::FOLLOWER;
                        }
 
-                       if ($new_relation != Contact::FOLLOWER) {
+                       if ($newRelation != Model\Contact::FOLLOWER) {
                                $writable = 1;
                        }
                }
@@ -79,43 +89,42 @@ final class Introduction extends BaseModel
                        'protocol'  => $protocol,
                        'writable'  => $writable,
                        'hidden'    => $hidden ?? $contact['hidden'],
-                       'rel'       => $new_relation,
+                       'rel'       => $newRelation,
                ];
                $this->dba->update('contact', $fields, ['id' => $contact['id']]);
 
                array_merge($contact, $fields);
 
-               if ($new_relation == Contact::FRIEND) {
+               if ($newRelation == Model\Contact::FRIEND) {
                        if ($protocol == Protocol::DIASPORA) {
-                               $ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
+                               $ret = Diaspora::sendShare(Model\Contact::getById($contact['uid']), $contact);
                                $this->logger->info('share returns', ['return' => $ret]);
                        } elseif ($protocol == Protocol::ACTIVITYPUB) {
                                ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
                        }
                }
 
-               $this->delete();
+               return $this->intro->delete($this);
        }
 
        /**
         * Silently ignores the introduction, hides it from notifications and prevents the remote contact from submitting
         * additional follow requests.
         *
-        * Chainable
-        *
-        * @return Introduction
+        * @return bool
         * @throws \Exception
         */
        public function ignore()
        {
-               $this->dba->update('intro', ['ignore' => true], ['id' => $this->id]);
+               $this->ignored = true;
 
-               return $this;
+               return $this->intro->update($this);
        }
 
        /**
         * Discards the introduction and sends a rejection message to AP contacts.
         *
+        * @return bool
         * @throws HTTPException\InternalServerErrorException
         * @throws HTTPException\NotFoundException
         * @throws \ImagickException
@@ -127,15 +136,15 @@ final class Introduction extends BaseModel
                if (!$this->fid) {
                        // When the contact entry had been created just for that intro, we want to get rid of it now
                        $condition = ['id' => $this->{'contact-id'}, 'uid' => $this->uid,
-                               'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
+                               'self' => false, 'pending' => true, 'rel' => [0, Model\Contact::FOLLOWER]];
                        if ($this->dba->exists('contact', $condition)) {
-                               Contact::remove($this->{'contact-id'});
+                               Model\Contact::remove($this->{'contact-id'});
                        } else {
                                $this->dba->update('contact', ['pending' => false], ['id' => $this->{'contact-id'}]);
                        }
                }
 
-               $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
+               $contact = Model\Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
 
                if (!$contact) {
                        throw new HTTPException\NotFoundException('Contact record not found.');
@@ -151,6 +160,6 @@ final class Introduction extends BaseModel
                        ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
                }
 
-               $this->delete();
+               return $this->intro->delete($this);
        }
 }