]> git.mxchange.org Git - friendica.git/blob - src/Model/Introduction.php
Fix: unknown variable
[friendica.git] / src / Model / Introduction.php
1 <?php
2
3 namespace Friendica\Model;
4
5 use Friendica\BaseModel;
6 use Friendica\Core\Protocol;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException;
9 use Friendica\Protocol\ActivityPub;
10 use Friendica\Protocol\Diaspora;
11 use Friendica\Repository;
12 use Friendica\Util\DateTimeFormat;
13 use Psr\Log\LoggerInterface;
14
15 /**
16  * @property int    uid
17  * @property int    fid
18  * @property int    contact-id
19  * @property bool   knowyou
20  * @property bool   duplex
21  * @property string note
22  * @property string hash
23  * @property string datetime
24  * @property bool   blocked
25  * @property bool   ignore
26  */
27 final class Introduction extends BaseModel
28 {
29         /** @var Repository\Introduction */
30         protected $intro;
31
32         public function __construct(Database $dba, LoggerInterface $logger, Repository\Introduction $intro, array $data = [])
33         {
34                 parent::__construct($dba, $logger, $data);
35
36                 $this->intro = $intro;
37         }
38
39         /**
40          * Confirms a follow request and sends a notice to the remote contact.
41          *
42          * @param bool               $duplex       Is it a follow back?
43          * @param bool|null          $hidden       Should this contact be hidden? null = no change
44          * @return bool
45          * @throws HTTPException\InternalServerErrorException
46          * @throws HTTPException\NotFoundException
47          * @throws \ImagickException
48          */
49         public function confirm(bool $duplex = false, bool $hidden = null)
50         {
51                 $this->logger->info('Confirming follower', ['cid' => $this->{'contact-id'}]);
52
53                 $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
54
55                 if (!$contact) {
56                         throw new HTTPException\NotFoundException('Contact record not found.');
57                 }
58
59                 $newRelation = $contact['rel'];
60                 $writable = $contact['writable'];
61
62                 if (!empty($contact['protocol'])) {
63                         $protocol = $contact['protocol'];
64                 } else {
65                         $protocol = $contact['network'];
66                 }
67
68                 if ($protocol == Protocol::ACTIVITYPUB) {
69                         ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $contact['uid']);
70                 }
71
72                 if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
73                         if ($duplex) {
74                                 $newRelation = Contact::FRIEND;
75                         } else {
76                                 $newRelation = Contact::FOLLOWER;
77                         }
78
79                         if ($newRelation != Contact::FOLLOWER) {
80                                 $writable = 1;
81                         }
82                 }
83
84                 $fields = [
85                         'name-date' => DateTimeFormat::utcNow(),
86                         'uri-date'  => DateTimeFormat::utcNow(),
87                         'blocked'   => false,
88                         'pending'   => false,
89                         'protocol'  => $protocol,
90                         'writable'  => $writable,
91                         'hidden'    => $hidden ?? $contact['hidden'],
92                         'rel'       => $newRelation,
93                 ];
94                 $this->dba->update('contact', $fields, ['id' => $contact['id']]);
95
96                 array_merge($contact, $fields);
97
98                 if ($newRelation == Contact::FRIEND) {
99                         if ($protocol == Protocol::DIASPORA) {
100                                 $ret = Diaspora::sendShare(Contact::getById($contact['uid']), $contact);
101                                 $this->logger->info('share returns', ['return' => $ret]);
102                         } elseif ($protocol == Protocol::ACTIVITYPUB) {
103                                 ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
104                         }
105                 }
106
107                 return $this->intro->delete($this);
108         }
109
110         /**
111          * Silently ignores the introduction, hides it from notifications and prevents the remote contact from submitting
112          * additional follow requests.
113          *
114          * @return bool
115          * @throws \Exception
116          */
117         public function ignore()
118         {
119                 $this->ignore = true;
120
121                 return $this->intro->update($this);
122         }
123
124         /**
125          * Discards the introduction and sends a rejection message to AP contacts.
126          *
127          * @return bool
128          * @throws HTTPException\InternalServerErrorException
129          * @throws HTTPException\NotFoundException
130          * @throws \ImagickException
131          */
132         public function discard()
133         {
134                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
135                 // that should not be deleted.
136                 if (!$this->fid) {
137                         // When the contact entry had been created just for that intro, we want to get rid of it now
138                         $condition = ['id' => $this->{'contact-id'}, 'uid' => $this->uid,
139                                 'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
140                         if ($this->dba->exists('contact', $condition)) {
141                                 Contact::remove($this->{'contact-id'});
142                         } else {
143                                 $this->dba->update('contact', ['pending' => false], ['id' => $this->{'contact-id'}]);
144                         }
145                 }
146
147                 $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
148
149                 if (!$contact) {
150                         throw new HTTPException\NotFoundException('Contact record not found.');
151                 }
152
153                 if (!empty($contact['protocol'])) {
154                         $protocol = $contact['protocol'];
155                 } else {
156                         $protocol = $contact['network'];
157                 }
158
159                 if ($protocol == Protocol::ACTIVITYPUB) {
160                         ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
161                 }
162
163                 return $this->intro->delete($this);
164         }
165 }