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
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 LINKEDIN = 'lnkd'; // LinkedIn
70 const NEWS = 'nntp'; // Network News Transfer Protocol
71 const PNUT = 'pnut'; // pnut.io
72 const XMPP = 'xmpp'; // XMPP
73 const ZOT = 'zot!'; // Zot!
75 const PHANTOM = 'unkn'; // Place holder
78 * Returns whether the provided protocol supports following
82 * @throws HTTPException\InternalServerErrorException
84 public static function supportsFollow($protocol): bool
86 if (in_array($protocol, self::NATIVE_SUPPORT)) {
91 'protocol' => $protocol,
94 Hook::callAll('support_follow', $hook_data);
96 return $hook_data['result'] === true;
100 * Returns whether the provided protocol supports revoking inbound follows
104 * @throws HTTPException\InternalServerErrorException
106 public static function supportsRevokeFollow($protocol): bool
108 if (in_array($protocol, self::NATIVE_SUPPORT)) {
113 'protocol' => $protocol,
116 Hook::callAll('support_revoke_follow', $hook_data);
118 return $hook_data['result'] === true;
122 * Send a follow message to a remote server.
124 * @param int $uid User Id
125 * @param array $contact Contact being followed
126 * @param ?string $protocol Expected protocol
127 * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
128 * @throws HTTPException\InternalServerErrorException
129 * @throws \ImagickException
131 public static function follow(int $uid, array $contact, ?string $protocol = null): bool
133 $owner = User::getOwnerDataById($uid);
134 if (!DBA::isResult($owner)) {
138 $protocol = $protocol ?? $contact['protocol'];
140 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
141 // create a follow slap
143 'verb' => Activity::FOLLOW,
144 'gravity' => Item::GRAVITY_ACTIVITY,
145 'follow' => $contact['url'],
152 $slap = OStatus::salmon($item, $owner);
154 if (!empty($contact['notify'])) {
155 Salmon::slapper($owner, $contact['notify'], $slap);
157 } elseif ($protocol == Protocol::DIASPORA) {
158 $contact = Diaspora::sendShare($owner, $contact);
159 Logger::notice('share returns: ' . $contact);
160 } elseif ($protocol == Protocol::ACTIVITYPUB) {
161 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
162 if (empty($activity_id)) {
163 // This really should never happen
167 $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
168 Logger::notice('Follow returns: ' . $success);
175 * Sends an unfollow message. Does not remove the contact
177 * @param array $contact Target public contact (uid = 0) array
178 * @param array $owner Source owner-view record
179 * @return bool|null true if successful, false if not, null if no remote action was performed
180 * @throws HTTPException\InternalServerErrorException
181 * @throws \ImagickException
183 public static function unfollow(array $contact, array $owner): ?bool
185 if (empty($contact['network'])) {
186 Logger::notice('Contact has got no network, we quit here', ['id' => $contact['id']]);
190 $protocol = $contact['network'];
191 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
192 $protocol = $contact['protocol'];
195 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
196 // create an unfollow slap
198 'verb' => Activity::O_UNFOLLOW,
199 'gravity' => Item::GRAVITY_ACTIVITY,
200 'follow' => $contact['url'],
207 $slap = OStatus::salmon($item, $owner);
209 if (empty($contact['notify'])) {
210 Logger::notice('OStatus/DFRN Contact is missing notify, we quit here', ['id' => $contact['id']]);
214 return Salmon::slapper($owner, $contact['notify'], $slap) === 0;
215 } elseif ($protocol == Protocol::DIASPORA) {
216 return Diaspora::sendUnshare($owner, $contact) > 0;
217 } elseif ($protocol == Protocol::ACTIVITYPUB) {
218 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $owner);
221 // Catch-all hook for connector addons
223 'contact' => $contact,
224 'uid' => $owner['uid'],
227 Hook::callAll('unfollow', $hook_data);
229 return $hook_data['result'];
233 * Revoke an incoming follow from the provided contact
235 * @param array $contact Target public contact (uid == 0) array
236 * @param array $owner Source owner-view record
237 * @return bool|null true if successful, false if not, null if no action was performed
238 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
239 * @throws \ImagickException
241 public static function revokeFollow(array $contact, array $owner): ?bool
243 if (empty($contact['network'])) {
244 throw new \InvalidArgumentException('Missing network key in contact array');
247 $protocol = $contact['network'];
248 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
249 $protocol = $contact['protocol'];
252 if ($protocol == Protocol::ACTIVITYPUB) {
253 return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $owner);
256 // Catch-all hook for connector addons
258 'contact' => $contact,
259 'uid' => $owner['uid'],
262 Hook::callAll('revoke_follow', $hook_data);
264 return $hook_data['result'];
268 * Send a block message to a remote server. Only useful for connector addons.
270 * @param array $contact Public contact record to block
271 * @param int $uid User issuing the block
272 * @return bool|null true if successful, false if not, null if no action was performed
273 * @throws HTTPException\InternalServerErrorException
275 public static function block(array $contact, int $uid): ?bool
277 // Catch-all hook for connector addons
279 'contact' => $contact,
283 Hook::callAll('block', $hook_data);
285 return $hook_data['result'];
289 * Send an unblock message to a remote server. Only useful for connector addons.
291 * @param array $contact Public contact record to unblock
292 * @param int $uid User revoking the block
293 * @return bool|null true if successful, false if not, null if no action was performed
294 * @throws HTTPException\InternalServerErrorException
296 public static function unblock(array $contact, int $uid): ?bool
298 // Catch-all hook for connector addons
300 'contact' => $contact,
304 Hook::callAll('unblock', $hook_data);
306 return $hook_data['result'];