]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Introduction.php
Merge pull request #13680 from annando/relation-queries
[friendica.git] / src / Model / Contact / Introduction.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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          * @param bool                $duplex       Is it a follow back?
41          * @param bool|null           $hidden       Should this contact be hidden? null = no change
42          *
43          * @throws HTTPException\InternalServerErrorException
44          * @throws HTTPException\NotFoundException
45          * @throws \ImagickException
46          */
47         public static function confirm(Entity\Introduction $introduction, bool $duplex = false, ?bool $hidden = null): void
48         {
49                 DI::logger()->info('Confirming follower', ['cid' => $introduction->cid]);
50
51                 $contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
52
53                 if (!$contact) {
54                         throw new HTTPException\NotFoundException('Contact record not found.');
55                 }
56
57                 $newRelation = $contact['rel'];
58                 $writable    = $contact['writable'];
59
60                 if (!empty($contact['protocol'])) {
61                         $protocol = $contact['protocol'];
62                 } else {
63                         $protocol = $contact['network'];
64                 }
65
66                 if ($protocol == Protocol::ACTIVITYPUB) {
67                         ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $contact['uid']);
68                 }
69
70                 if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
71                         if ($duplex) {
72                                 $newRelation = Contact::FRIEND;
73                         } else {
74                                 $newRelation = Contact::FOLLOWER;
75                         }
76
77                         if ($newRelation != Contact::FOLLOWER) {
78                                 $writable = 1;
79                         }
80                 }
81
82                 $fields = [
83                         'name-date' => DateTimeFormat::utcNow(),
84                         'uri-date'  => DateTimeFormat::utcNow(),
85                         'blocked'   => false,
86                         'pending'   => false,
87                         'protocol'  => $protocol,
88                         'writable'  => $writable,
89                         'hidden'    => $hidden ?? $contact['hidden'],
90                         'rel'       => $newRelation,
91                 ];
92                 Contact::update($fields, ['id' => $contact['id']]);
93
94                 array_merge($contact, $fields);
95
96                 if ($newRelation == Contact::FRIEND) {
97                         if ($protocol == Protocol::DIASPORA) {
98                                 $ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
99                                 DI::logger()->info('share returns', ['return' => $ret]);
100                         } elseif ($protocol == Protocol::ACTIVITYPUB) {
101                                 ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
102                         }
103                 }
104         }
105
106         /**
107          * Discards the introduction and sends a rejection message to AP contacts.
108          *
109          * @param Entity\Introduction $introduction
110          *
111          * @throws HTTPException\InternalServerErrorException
112          * @throws \ImagickException
113          */
114         public static function discard(Entity\Introduction $introduction): void
115         {
116                 $contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
117                 if (!empty($contact)) {
118                         if (!empty($contact['protocol'])) {
119                                 $protocol = $contact['protocol'];
120                         } else {
121                                 $protocol = $contact['network'];
122                         }
123
124                         if ($protocol == Protocol::ACTIVITYPUB) {
125                                 $owner = User::getOwnerDataById($contact['uid']);
126                                 if ($owner) {
127                                         ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $owner);
128                                 }
129                         }
130                 }
131         }
132 }