]> git.mxchange.org Git - friendica.git/blob - src/Module/FollowConfirm.php
The follow confirmation process is now separated between DFRN and the rest
[friendica.git] / src / Module / FollowConfirm.php
1 <?php
2 namespace Friendica\Module;
3
4 use Friendica\App;
5 use Friendica\BaseModule;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Logger;
8 use Friendica\Core\Protocol;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Protocol\Diaspora;
12 use Friendica\Protocol\ActivityPub;
13 use Friendica\Util\DateTimeFormat;
14
15 class FollowConfirm extends BaseModule
16 {
17         public static function post()
18         {
19                 $a = self::getApp();
20
21                 $uid = local_user();
22                 if (!$uid) {
23                         notice(L10n::t('Permission denied.') . EOL);
24                         return;
25                 }
26
27                 $intro_id = intval(defaults($_POST, 'intro_id'  , 0));
28                 $duplex   = intval(defaults($_POST, 'duplex'    , 0));
29                 $cid      = intval(defaults($_POST, 'contact_id', 0));
30                 $hidden   = intval(defaults($_POST, 'hidden'    , 0));
31
32                 if (empty($cid)) {
33                         notice(L10n::t('No given contact.') . EOL);
34                         return;
35                 }
36
37                 Logger::log('Confirming follower with contact_id: ' . $cid);
38
39                 $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'uid' => $uid]);
40                 if (!DBA::isResult($contact)) {
41                         Logger::log('Contact not found in DB.');
42                         notice(L10n::t('Contact not found.') . EOL);
43                         return;
44                 }
45
46                 $relation = $contact['rel'];
47                 $new_relation = $contact['rel'];
48                 $writable = $contact['writable'];
49
50                 if (!empty($contact['protocol'])) {
51                         $protocol = $contact['protocol'];
52                 } else {
53                         $protocol = $contact['network'];
54                 }
55
56                 if ($protocol == Protocol::ACTIVITYPUB) {
57                         ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $uid);
58                 }
59
60                 if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
61                         if ($duplex) {
62                                 $new_relation = Contact::FRIEND;
63                         } else {
64                                 $new_relation = Contact::FOLLOWER;
65                         }
66
67                         if ($new_relation != Contact::FOLLOWER) {
68                                 $writable = 1;
69                         }
70                 }
71
72                 $fields = ['name-date' => DateTimeFormat::utcNow(),
73                         'uri-date' => DateTimeFormat::utcNow(),
74                         'blocked' => false, 'pending' => false, 'protocol' => $protocol,
75                         'writable' => $writable, 'hidden' => $hidden, 'rel' => $new_relation];
76                 DBA::update('contact', $fields, ['id' => $cid]);
77
78                 if ($new_relation == Contact::FRIEND) {
79                         if ($protocol == Protocol::DIASPORA) {
80                                 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
81                                 $contact = DBA::select('contact', [], ['id' => $cid]);
82                                 $ret = Diaspora::sendShare($user, $contact);
83                                 Logger::log('share returns: ' . $ret);
84                         } elseif ($protocol == Protocol::ACTIVITYPUB) {
85                                 ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid);
86                         }
87                 }
88
89                 DBA::delete('intro', ['id' => $intro_id]);
90
91                 $a->internalRedirect('contact/' . intval($cid));
92         }
93 }