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