3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core;
24 use Friendica\Database\DBA;
25 use Friendica\Model\Item;
26 use Friendica\Model\User;
27 use Friendica\Network\HTTPException;
28 use Friendica\Protocol\Activity;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Protocol\Diaspora;
31 use Friendica\Protocol\OStatus;
32 use Friendica\Protocol\Salmon;
35 * Manage compatibility with federated networks
40 const ACTIVITYPUB = 'apub'; // ActivityPub (Pleroma, Mastodon, Osada, ...)
41 const DFRN = 'dfrn'; // Friendica, Mistpark, other DFRN implementations
42 const DIASPORA = 'dspr'; // Diaspora, Hubzilla, Socialhome, Ganggo
43 const FEED = 'feed'; // RSS/Atom feeds with no known "post/notify" protocol
44 const MAIL = 'mail'; // IMAP/POP
45 const OSTATUS = 'stat'; // GNU Social and other OStatus implementations
47 const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
49 const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
51 const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
53 // Supported through a connector
54 const DIASPORA2 = 'dspc'; // Diaspora connector
55 const LINKEDIN = 'lnkd'; // LinkedIn
56 const PUMPIO = 'pump'; // pump.io
57 const STATUSNET = 'stac'; // Statusnet connector
58 const TWITTER = 'twit'; // Twitter
59 const DISCOURSE = 'dscs'; // Discourse
62 const APPNET = 'apdn'; // app.net - Dead protocol
63 const FACEBOOK = 'face'; // Facebook API - Not working anymore, API is closed
64 const GPLUS = 'goog'; // Google+ - Dead in 2019
66 // Currently unsupported
67 const ICALENDAR = 'ical'; // iCalendar
68 const MYSPACE = 'mysp'; // MySpace
69 const NEWS = 'nntp'; // Network News Transfer Protocol
70 const PNUT = 'pnut'; // pnut.io
71 const XMPP = 'xmpp'; // XMPP
72 const ZOT = 'zot!'; // Zot!
74 const PHANTOM = 'unkn'; // Place holder
77 * Returns whether the provided protocol supports following
81 * @throws HTTPException\InternalServerErrorException
83 public static function supportsFollow($protocol): bool
85 if (in_array($protocol, self::NATIVE_SUPPORT)) {
90 'protocol' => $protocol,
93 Hook::callAll('support_follow', $hook_data);
95 return $hook_data['result'] === true;
99 * Returns whether the provided protocol supports revoking inbound follows
103 * @throws HTTPException\InternalServerErrorException
105 public static function supportsRevokeFollow($protocol): bool
107 if (in_array($protocol, self::NATIVE_SUPPORT)) {
112 'protocol' => $protocol,
115 Hook::callAll('support_revoke_follow', $hook_data);
117 return $hook_data['result'] === true;
121 * Send a follow message to a remote server.
123 * @param int $uid User Id
124 * @param array $contact Contact being followed
125 * @param ?string $protocol Expected protocol
126 * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
127 * @throws HTTPException\InternalServerErrorException
128 * @throws \ImagickException
130 public static function follow(int $uid, array $contact, ?string $protocol = null): bool
132 $owner = User::getOwnerDataById($uid);
133 if (!DBA::isResult($owner)) {
137 $protocol = $protocol ?? $contact['protocol'];
139 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
140 // create a follow slap
142 'verb' => Activity::FOLLOW,
143 'gravity' => Item::GRAVITY_ACTIVITY,
144 'follow' => $contact['url'],
151 $slap = OStatus::salmon($item, $owner);
153 if (!empty($contact['notify'])) {
154 Salmon::slapper($owner, $contact['notify'], $slap);
156 } elseif ($protocol == Protocol::DIASPORA) {
157 $contact = Diaspora::sendShare($owner, $contact);
158 Logger::notice('share returns: ' . $contact);
159 } elseif ($protocol == Protocol::ACTIVITYPUB) {
160 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
161 if (empty($activity_id)) {
162 // This really should never happen
166 $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
167 Logger::notice('Follow returns: ' . $success);
174 * Sends an unfollow message. Does not remove the contact
176 * @param array $contact Target public contact (uid = 0) array
177 * @param array $user Source local user array
178 * @return bool|null true if successful, false if not, null if no remote action was performed
179 * @throws HTTPException\InternalServerErrorException
180 * @throws \ImagickException
182 public static function unfollow(array $contact, array $user): ?bool
184 if (empty($contact['network'])) {
185 Logger::notice('Contact has got no network, we quit here', ['id' => $contact['id']]);
189 $protocol = $contact['network'];
190 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
191 $protocol = $contact['protocol'];
194 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
195 // create an unfollow slap
197 'verb' => Activity::O_UNFOLLOW,
198 'gravity' => Item::GRAVITY_ACTIVITY,
199 'follow' => $contact['url'],
206 $slap = OStatus::salmon($item, $user);
208 if (empty($contact['notify'])) {
209 Logger::notice('OStatus/DFRN Contact is missing notify, we quit here', ['id' => $contact['id']]);
213 return Salmon::slapper($user, $contact['notify'], $slap) === 0;
214 } elseif ($protocol == Protocol::DIASPORA) {
215 return Diaspora::sendUnshare($user, $contact) > 0;
216 } elseif ($protocol == Protocol::ACTIVITYPUB) {
217 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
220 // Catch-all hook for connector addons
222 'contact' => $contact,
223 'uid' => $user['uid'],
226 Hook::callAll('unfollow', $hook_data);
228 return $hook_data['result'];
232 * Revoke an incoming follow from the provided contact
234 * @param array $contact Target public contact (uid == 0) array
235 * @param int $uid Source local user id
236 * @return bool|null true if successful, false if not, null if no action was performed
237 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
238 * @throws \ImagickException
240 public static function revokeFollow(array $contact, int $uid): ?bool
242 if (empty($contact['network'])) {
243 throw new \InvalidArgumentException('Missing network key in contact array');
246 $protocol = $contact['network'];
247 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
248 $protocol = $contact['protocol'];
251 if ($protocol == Protocol::ACTIVITYPUB) {
252 return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $uid);
255 // Catch-all hook for connector addons
257 'contact' => $contact,
261 Hook::callAll('revoke_follow', $hook_data);
263 return $hook_data['result'];
267 * Send a block message to a remote server. Only useful for connector addons.
269 * @param array $contact Public contact record to block
270 * @param int $uid User issuing the block
271 * @return bool|null true if successful, false if not, null if no action was performed
272 * @throws HTTPException\InternalServerErrorException
274 public static function block(array $contact, int $uid): ?bool
276 // Catch-all hook for connector addons
278 'contact' => $contact,
282 Hook::callAll('block', $hook_data);
284 return $hook_data['result'];
288 * Send an unblock message to a remote server. Only useful for connector addons.
290 * @param array $contact Public contact record to unblock
291 * @param int $uid User revoking the block
292 * @return bool|null true if successful, false if not, null if no action was performed
293 * @throws HTTPException\InternalServerErrorException
295 public static function unblock(array $contact, int $uid): ?bool
297 // Catch-all hook for connector addons
299 'contact' => $contact,
303 Hook::callAll('unblock', $hook_data);
305 return $hook_data['result'];