]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Introduction.php
692ba7befecfdef31e0f75c91121c8bbc0722677
[friendica.git] / src / Model / Contact / Introduction.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model\Contact;
23
24 use Friendica\Contact\Introduction\Entity;
25 use Friendica\Core\Protocol;
26 use Friendica\DI;
27 use Friendica\Network\HTTPException;
28 use Friendica\Model\Contact;
29 use Friendica\Model\User;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Protocol\Diaspora;
32 use Friendica\Util\DateTimeFormat;
33
34 class Introduction
35 {
36         /**
37          * Confirms a follow request and sends a notice to the remote contact.
38          *
39          * @param Entity\Introduction $introduction
40          *
41          * @throws HTTPException\InternalServerErrorException
42          * @throws HTTPException\NotFoundException
43          * @throws \ImagickException
44          */
45         public static function confirm(Entity\Introduction $introduction): void
46         {
47                 DI::logger()->info('Confirming follower', ['cid' => $introduction->cid]);
48
49                 $contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
50
51                 if (!$contact) {
52                         throw new HTTPException\NotFoundException('Contact record not found.');
53                 }
54
55                 $newRelation = $contact['rel'];
56                 $writable    = $contact['writable'];
57
58                 if (!empty($contact['protocol'])) {
59                         $protocol = $contact['protocol'];
60                 } else {
61                         $protocol = $contact['network'];
62                 }
63
64                 if ($protocol == Protocol::ACTIVITYPUB) {
65                         ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $contact['uid']);
66                 }
67
68                 if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
69                         if ($introduction->duplex) {
70                                 $newRelation = Contact::FRIEND;
71                         } else {
72                                 $newRelation = Contact::FOLLOWER;
73                         }
74
75                         if ($newRelation != Contact::FOLLOWER) {
76                                 $writable = 1;
77                         }
78                 }
79
80                 $fields = [
81                         'name-date' => DateTimeFormat::utcNow(),
82                         'uri-date'  => DateTimeFormat::utcNow(),
83                         'blocked'   => false,
84                         'pending'   => false,
85                         'protocol'  => $protocol,
86                         'writable'  => $writable,
87                         'hidden'    => $hidden ?? $contact['hidden'],
88                         'rel'       => $newRelation,
89                 ];
90                 Contact::update($fields, ['id' => $contact['id']]);
91
92                 array_merge($contact, $fields);
93
94                 if ($newRelation == Contact::FRIEND) {
95                         if ($protocol == Protocol::DIASPORA) {
96                                 $ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
97                                 DI::logger()->info('share returns', ['return' => $ret]);
98                         } elseif ($protocol == Protocol::ACTIVITYPUB) {
99                                 ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
100                         }
101                 }
102         }
103
104         /**
105          * Discards the introduction and sends a rejection message to AP contacts.
106          *
107          * @param Entity\Introduction $introduction
108          *
109          * @throws HTTPException\InternalServerErrorException
110          * @throws \ImagickException
111          */
112         public static function discard(Entity\Introduction $introduction): void
113         {
114                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
115                 // that should not be deleted.
116                 if (!$introduction->fid) {
117                         // When the contact entry had been created just for that intro, we want to get rid of it now
118                         $condition = [
119                                 'id'      => $introduction->cid,
120                                 'uid'     => $introduction->uid,
121                                 'self'    => false,
122                                 'pending' => true,
123                                 'rel'     => [0, Contact::FOLLOWER]];
124                         if (DI::dba()->exists('contact', $condition)) {
125                                 Contact::remove($introduction->cid);
126                         } else {
127                                 Contact::update(['pending' => false], ['id' => $introduction->cid]);
128                         }
129                 }
130
131                 $contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
132                 if (!empty($contact)) {
133                         if (!empty($contact['protocol'])) {
134                                 $protocol = $contact['protocol'];
135                         } else {
136                                 $protocol = $contact['network'];
137                         }
138
139                         if ($protocol == Protocol::ACTIVITYPUB) {
140                                 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
141                         }
142                 }
143         }
144 }