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