3 * @copyright Copyright (C) 2010-2023, 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 PUMPIO = 'pump'; // pump.io
56 const STATUSNET = 'stac'; // Statusnet connector
57 const TWITTER = 'twit'; // Twitter
58 const DISCOURSE = 'dscs'; // Discourse
59 const TUMBLR = 'tmbl'; // Tumblr
60 const BLUESKY = 'bsky'; // Bluesky
63 const APPNET = 'apdn'; // app.net - Dead protocol
64 const FACEBOOK = 'face'; // Facebook API - Not working anymore, API is closed
65 const GPLUS = 'goog'; // Google+ - Dead in 2019
67 // Currently unsupported
68 const ICALENDAR = 'ical'; // iCalendar
69 const MYSPACE = 'mysp'; // MySpace
70 const LINKEDIN = 'lnkd'; // LinkedIn
71 const NEWS = 'nntp'; // Network News Transfer Protocol
72 const PNUT = 'pnut'; // pnut.io
73 const XMPP = 'xmpp'; // XMPP
74 const ZOT = 'zot!'; // Zot!
76 const PHANTOM = 'unkn'; // Place holder
79 * Returns whether the provided protocol supports following
83 * @throws HTTPException\InternalServerErrorException
85 public static function supportsFollow($protocol): bool
87 if (in_array($protocol, self::NATIVE_SUPPORT)) {
92 'protocol' => $protocol,
95 Hook::callAll('support_follow', $hook_data);
97 return $hook_data['result'] === true;
101 * Returns whether the provided protocol supports revoking inbound follows
105 * @throws HTTPException\InternalServerErrorException
107 public static function supportsRevokeFollow($protocol): bool
109 if (in_array($protocol, self::NATIVE_SUPPORT)) {
114 'protocol' => $protocol,
117 Hook::callAll('support_revoke_follow', $hook_data);
119 return $hook_data['result'] === true;
123 * Send a follow message to a remote server.
125 * @param int $uid User Id
126 * @param array $contact Contact being followed
127 * @param ?string $protocol Expected protocol
128 * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
129 * @throws HTTPException\InternalServerErrorException
130 * @throws \ImagickException
132 public static function follow(int $uid, array $contact, ?string $protocol = null): bool
134 $owner = User::getOwnerDataById($uid);
135 if (!DBA::isResult($owner)) {
139 $protocol = $protocol ?? $contact['protocol'];
141 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
142 // create a follow slap
144 'verb' => Activity::FOLLOW,
145 'gravity' => Item::GRAVITY_ACTIVITY,
146 'follow' => $contact['url'],
153 $slap = OStatus::salmon($item, $owner);
155 if (!empty($contact['notify'])) {
156 Salmon::slapper($owner, $contact['notify'], $slap);
158 } elseif ($protocol == Protocol::DIASPORA) {
159 $contact = Diaspora::sendShare($owner, $contact);
160 Logger::notice('share returns: ' . $contact);
161 } elseif ($protocol == Protocol::ACTIVITYPUB) {
162 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
163 if (empty($activity_id)) {
164 // This really should never happen
168 $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
169 Logger::notice('Follow returns: ' . $success);
176 * Sends an unfollow message. Does not remove the contact
178 * @param array $contact Target public contact (uid = 0) array
179 * @param array $owner Source owner-view record
180 * @return bool|null true if successful, false if not, null if no remote action was performed
181 * @throws HTTPException\InternalServerErrorException
182 * @throws \ImagickException
184 public static function unfollow(array $contact, array $owner): ?bool
186 if (empty($contact['network'])) {
187 Logger::notice('Contact has got no network, we quit here', ['id' => $contact['id']]);
191 $protocol = $contact['network'];
192 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
193 $protocol = $contact['protocol'];
196 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
197 // create an unfollow slap
199 'verb' => Activity::O_UNFOLLOW,
200 'gravity' => Item::GRAVITY_ACTIVITY,
201 'follow' => $contact['url'],
208 $slap = OStatus::salmon($item, $owner);
210 if (empty($contact['notify'])) {
211 Logger::notice('OStatus/DFRN Contact is missing notify, we quit here', ['id' => $contact['id']]);
215 return Salmon::slapper($owner, $contact['notify'], $slap) === 0;
216 } elseif ($protocol == Protocol::DIASPORA) {
217 return Diaspora::sendUnshare($owner, $contact) > 0;
218 } elseif ($protocol == Protocol::ACTIVITYPUB) {
219 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $owner);
222 // Catch-all hook for connector addons
224 'contact' => $contact,
225 'uid' => $owner['uid'],
228 Hook::callAll('unfollow', $hook_data);
230 return $hook_data['result'];
234 * Revoke an incoming follow from the provided contact
236 * @param array $contact Target public contact (uid == 0) array
237 * @param array $owner Source owner-view record
238 * @return bool|null true if successful, false if not, null if no action was performed
239 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
240 * @throws \ImagickException
242 public static function revokeFollow(array $contact, array $owner): ?bool
244 if (empty($contact['network'])) {
245 throw new \InvalidArgumentException('Missing network key in contact array');
248 $protocol = $contact['network'];
249 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
250 $protocol = $contact['protocol'];
253 if ($protocol == Protocol::ACTIVITYPUB) {
254 return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $owner);
257 // Catch-all hook for connector addons
259 'contact' => $contact,
260 'uid' => $owner['uid'],
263 Hook::callAll('revoke_follow', $hook_data);
265 return $hook_data['result'];
269 * Send a block message to a remote server. Only useful for connector addons.
271 * @param array $contact Public contact record to block
272 * @param int $uid User issuing the block
273 * @return bool|null true if successful, false if not, null if no action was performed
274 * @throws HTTPException\InternalServerErrorException
276 public static function block(array $contact, int $uid): ?bool
278 // Catch-all hook for connector addons
280 'contact' => $contact,
284 Hook::callAll('block', $hook_data);
286 return $hook_data['result'];
290 * Send an unblock message to a remote server. Only useful for connector addons.
292 * @param array $contact Public contact record to unblock
293 * @param int $uid User revoking the block
294 * @return bool|null true if successful, false if not, null if no action was performed
295 * @throws HTTPException\InternalServerErrorException
297 public static function unblock(array $contact, int $uid): ?bool
299 // Catch-all hook for connector addons
301 'contact' => $contact,
305 Hook::callAll('unblock', $hook_data);
307 return $hook_data['result'];
311 * Returns whether the provided protocol supports probing for contacts
315 * @throws HTTPException\InternalServerErrorException
317 public static function supportsProbe($protocol): bool
319 // "Mail" can only be probed for a specific user in a specific condition, so we are ignoring it here.
320 if ($protocol == self::MAIL) {
324 if (in_array($protocol, array_merge(self::NATIVE_SUPPORT, [self::ZOT, self::PHANTOM]))) {
329 'protocol' => $protocol,
332 Hook::callAll('support_probe', $hook_data);
334 return $hook_data['result'] === true;