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\Protocol;
24 use Friendica\Content\Smilies;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Cache\Enum\Duration;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Database\DBA;
31 use Friendica\Model\APContact;
32 use Friendica\Model\Contact;
33 use Friendica\Model\GServer;
34 use Friendica\Model\Item;
35 use Friendica\Model\Post;
36 use Friendica\Model\Search;
37 use Friendica\Model\Tag;
38 use Friendica\Model\User;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Strings;
43 * Base class for relay handling
44 * @see https://github.com/jaywink/social-relay
45 * @see https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
49 const SCOPE_NONE = '';
50 const SCOPE_ALL = 'all';
51 const SCOPE_TAGS = 'tags';
54 * Check if a post is wanted
58 * @param int $authorid
60 * @param string $network
61 * @param int $causerid
62 * @param array $languages
63 * @return boolean "true" is the post is wanted by the system
65 public static function isSolicitedPost(array $tags, string $body, int $authorid, string $url, string $network = '', int $causerid = 0, array $languages = []): bool
67 $config = DI::config();
69 $scope = $config->get('system', 'relay_scope');
71 if ($scope == self::SCOPE_NONE) {
72 Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
76 if (Contact::isBlocked($authorid)) {
77 Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
81 if (Contact::isHidden($authorid)) {
82 Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
86 if (!empty($causerid)) {
87 $contact = Contact::getById($causerid, ['url']);
88 $causer = $contact['url'] ?? '';
93 $body = ActivityPub\Processor::normalizeMentionLinks($body);
97 if ($scope == self::SCOPE_TAGS) {
98 $tagList = self::getSubscribedTags();
103 $deny_tags = $config->get('system', 'relay_deny_tags');
104 $tagitems = explode(',', mb_strtolower($deny_tags));
105 foreach ($tagitems as $tag) {
106 $tag = trim($tag, '# ');
110 if (!empty($tagList) || !empty($denyTags)) {
111 $content = mb_strtolower(BBCode::toPlaintext($body, false));
113 foreach ($tags as $tag) {
114 $tag = mb_strtolower($tag);
115 if (in_array($tag, $denyTags)) {
116 Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
120 if (in_array($tag, $tagList)) {
121 Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
125 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
126 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
127 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
128 Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
134 if (!self::isWantedLanguage($body, 0, $authorid, $languages)) {
135 Logger::info('Unwanted or Undetected language found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
139 if ($scope == self::SCOPE_ALL) {
140 Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
144 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
149 * Get a list of subscribed tags by both the users and the tags that are defined by the admin
153 public static function getSubscribedTags(): array
156 $server_tags = DI::config()->get('system', 'relay_server_tags');
158 foreach (explode(',', mb_strtolower($server_tags)) as $tag) {
159 $systemTags[] = trim($tag, '# ');
162 if (DI::config()->get('system', 'relay_user_tags')) {
163 $userTags = Search::getUserTags();
168 return array_unique(array_merge($systemTags, $userTags));
172 * Detect the language of a post and decide if the post should be accepted
174 * @param string $body
176 * @param int $author_id
177 * @param array $languages
180 public static function isWantedLanguage(string $body, int $uri_id = 0, int $author_id = 0, array $languages = [])
182 if (empty($languages) && (empty($body) || Smilies::isEmojiPost($body))) {
183 Logger::debug('Empty body or only emojis', ['body' => $body]);
188 $quality = DI::config()->get('system', 'relay_language_quality');
189 foreach (Item::getLanguageArray($body, DI::config()->get('system', 'relay_languages'), $uri_id, $author_id) as $language => $reliability) {
190 if (($reliability >= $quality) && ($quality > 0)) {
191 $detected[] = $language;
195 if (!empty($languages) || !empty($detected)) {
196 $cachekey = 'relay:isWantedLanguage';
197 $user_languages = DI::cache()->get($cachekey);
198 if (is_null($user_languages)) {
199 $user_languages = User::getLanguages();
200 DI::cache()->set($cachekey, $user_languages);
203 foreach ($detected as $language) {
204 if (in_array($language, $user_languages)) {
205 Logger::debug('Wanted language found in detected languages', ['language' => $language, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
209 foreach ($languages as $language) {
210 if (in_array($language, $user_languages)) {
211 Logger::debug('Wanted language found in defined languages', ['language' => $language, 'languages' => $languages, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
215 Logger::debug('No wanted language found', ['languages' => $languages, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
217 } elseif (DI::config()->get('system', 'relay_deny_undetected_language')) {
218 Logger::info('Undetected language found', ['body' => $body]);
226 * Update or insert a relay contact
228 * @param array $gserver Global server record
229 * @param array $fields Optional network specific fields
233 public static function updateContact(array $gserver, array $fields = [])
235 if (in_array($gserver['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
236 $system = APContact::getByURL($gserver['url'] . '/friendica');
237 if (!empty($system['sharedinbox'])) {
238 Logger::info('Successfully probed for relay contact', ['server' => $gserver['url']]);
239 $id = Contact::updateFromProbeByURL($system['url']);
240 Logger::info('Updated relay contact', ['server' => $gserver['url'], 'id' => $id]);
245 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
246 $old = DBA::selectFirst('contact', [], $condition);
247 if (!DBA::isResult($old)) {
248 $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($gserver['url'])];
249 $old = DBA::selectFirst('contact', [], $condition);
250 if (DBA::isResult($old)) {
251 $fields['gsid'] = $gserver['id'];
252 $fields['contact-type'] = Contact::TYPE_RELAY;
253 Logger::info('Assigning missing data for relay contact', ['server' => $gserver['url'], 'id' => $old['id']]);
255 } elseif (empty($fields)) {
256 Logger::info('No content to update, quitting', ['server' => $gserver['url']]);
260 if (DBA::isResult($old)) {
261 $fields['updated'] = DateTimeFormat::utcNow();
263 Logger::info('Update relay contact', ['server' => $gserver['url'], 'id' => $old['id'], 'fields' => $fields]);
264 Contact::update($fields, ['id' => $old['id']], $old);
266 $default = ['created' => DateTimeFormat::utcNow(),
267 'name' => 'relay', 'nick' => 'relay', 'url' => $gserver['url'],
268 'nurl' => Strings::normaliseLink($gserver['url']),
269 'network' => Protocol::DIASPORA, 'uid' => 0,
270 'batch' => $gserver['url'] . '/receive/public',
271 'rel' => Contact::FOLLOWER, 'blocked' => false,
272 'pending' => false, 'writable' => true,
273 'gsid' => $gserver['id'],
274 'baseurl' => $gserver['url'], 'contact-type' => Contact::TYPE_RELAY];
276 $fields = array_merge($default, $fields);
278 Logger::info('Create relay contact', ['server' => $gserver['url'], 'fields' => $fields]);
279 Contact::insert($fields);
284 * Mark the relay contact of the given contact for archival
285 * This is called whenever there is a communication issue with the server.
286 * It avoids sending stuff to servers who don't exist anymore.
287 * The relay contact is a technical contact entry that exists once per server.
289 * @param array $contact of the relay contact
292 public static function markForArchival(array $contact)
294 if (!empty($contact['contact-type']) && ($contact['contact-type'] == Contact::TYPE_RELAY)) {
295 // This is already the relay contact, we don't need to fetch it
296 $relay_contact = $contact;
297 } elseif (empty($contact['baseurl'])) {
298 if (!empty($contact['batch'])) {
299 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => Contact::TYPE_RELAY];
300 $relay_contact = DBA::selectFirst('contact', [], $condition);
305 $gserver = ['id' => $contact['gsid'] ?: GServer::getID($contact['baseurl'], true),
306 'url' => $contact['baseurl'], 'network' => $contact['network']];
307 $relay_contact = self::getContact($gserver, []);
310 if (!empty($relay_contact)) {
311 Logger::info('Relay contact will be marked for archival', ['id' => $relay_contact['id'], 'url' => $relay_contact['url']]);
312 Contact::markForArchival($relay_contact);
317 * Return a list of servers that we serve via the direct relay
319 * @param integer $item_id id of the item that is sent
320 * @param array $contacts Previously fetched contacts
321 * @param array $networks Networks of the relay servers
322 * @return array of relay servers
323 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
325 public static function getDirectRelayList(int $item_id): array
329 if (!DI::config()->get('system', 'relay_directly', false)) {
333 // We distribute our stuff based on the parent to ensure that the thread will be complete
334 $parent = Post::selectFirst(['uri-id'], ['id' => $item_id]);
335 if (!DBA::isResult($parent)) {
339 // Servers that want to get all content
340 $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'all']);
341 while ($server = DBA::fetch($servers)) {
342 $serverlist[$server['id']] = $server;
344 DBA::close($servers);
346 // All tags of the current post
347 $tags = DBA::select('tag-view', ['name'], ['uri-id' => $parent['uri-id'], 'type' => Tag::HASHTAG]);
349 while ($tag = DBA::fetch($tags)) {
350 $taglist[] = $tag['name'];
354 // All servers who wants content with this tag
356 if (!empty($taglist)) {
357 $tagserver = DBA::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
358 while ($server = DBA::fetch($tagserver)) {
359 $tagserverlist[] = $server['gserver-id'];
361 DBA::close($tagserver);
364 // All addresses with the given id
365 if (!empty($tagserverlist)) {
366 $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
367 while ($server = DBA::fetch($servers)) {
368 $serverlist[$server['id']] = $server;
370 DBA::close($servers);
375 // Now we are collecting all relay contacts
376 foreach ($serverlist as $gserver) {
377 // We don't send messages to ourselves
378 if (Strings::compareLink($gserver['url'], DI::baseUrl())) {
381 $contact = self::getContact($gserver);
382 if (empty($contact)) {
391 * Return a list of relay servers
393 * @param array $fields Field list
394 * @return array List of relay servers
397 public static function getList(array $fields = []): array
399 return DBA::selectToArray('apcontact', $fields,
400 ["`type` IN (?, ?) AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)", 'Application', 'Service', 0, Contact::FRIEND]);
404 * Return a contact for a given server address or creates a dummy entry
406 * @param array $gserver Global server record
407 * @param array $fields Fieldlist
408 * @return array|bool Array with the contact or false on error
411 private static function getContact(array $gserver, array $fields = ['batch', 'id', 'url', 'name', 'network', 'protocol', 'archive', 'blocked'])
413 // Fetch the relay contact
414 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
415 $contact = DBA::selectFirst('contact', $fields, $condition);
416 if (DBA::isResult($contact)) {
417 if ($contact['archive'] || $contact['blocked']) {
422 self::updateContact($gserver);
424 $contact = DBA::selectFirst('contact', $fields, $condition);
425 if (DBA::isResult($contact)) {
430 // It should never happen that we arrive here
435 * Resubscribe to all relay servers
439 public static function reSubscribe()
441 foreach (self::getList() as $server) {
442 $success = ActivityPub\Transmitter::sendRelayFollow($server['url']);
443 Logger::debug('Resubscribed', ['profile' => $server['url'], 'success' => $success]);