const DELIVER_FIELDLIST = [
'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
'parent-guid', 'conversation', 'received', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
- 'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
+ 'private', 'title', 'body', 'raw-body', 'language', 'location', 'coord', 'app',
'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-addr', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', 'post-reason',
'private', 'pubmail', 'visible', 'starred',
'unseen', 'deleted', 'origin', 'mention', 'global', 'network',
- 'title', 'content-warning', 'body', 'location', 'coord', 'app',
+ 'title', 'content-warning', 'body', 'language', 'location', 'coord', 'app',
'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'
*/
public static function markForDeletionById(int $item_id, int $priority = Worker::PRIORITY_HIGH): bool
{
- Logger::info('Mark item for deletion by id', ['id' => $item_id, 'callstack' => System::callstack()]);
+ Logger::info('Mark item for deletion by id', ['id' => $item_id]);
// locate item to be deleted
$fields = [
'id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri-id', 'origin',
}
if (!DBA::isResult($parent)) {
- Logger::notice('item parent was not found - ignoring item', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'callstack' => System::callstack(20)]);
+ Logger::notice('item parent was not found - ignoring item', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
return [];
}
if (is_int($notify) && in_array($notify, Worker::PRIORITIES)) {
$priority = $notify;
}
+
+ // Mastodon style API visibility
+ $copy_permissions = ($item['visibility'] ?? 'private') == 'private';
+ unset($item['visibility']);
} else {
$item['network'] = trim(($item['network'] ?? '') ?: Protocol::PHANTOM);
}
// Reshares have to keep their permissions to allow groups to work
if (!$defined_permissions && (!$item['origin'] || ($item['verb'] != Activity::ANNOUNCE))) {
- $item['allow_cid'] = $toplevel_parent['allow_cid'];
- $item['allow_gid'] = $toplevel_parent['allow_gid'];
- $item['deny_cid'] = $toplevel_parent['deny_cid'];
- $item['deny_gid'] = $toplevel_parent['deny_gid'];
+ // Don't store the permissions on pure AP posts
+ $store_permissions = ($item['network'] != Protocol::ACTIVITYPUB) || $item['origin'] || !empty($item['diaspora_signed_text']);
+ $item['allow_cid'] = $store_permissions ? $toplevel_parent['allow_cid'] : '';
+ $item['allow_gid'] = $store_permissions ? $toplevel_parent['allow_gid'] : '';
+ $item['deny_cid'] = $store_permissions ? $toplevel_parent['deny_cid'] : '';
+ $item['deny_gid'] = $store_permissions ? $toplevel_parent['deny_gid'] : '';
}
$parent_origin = $toplevel_parent['origin'];
if (!empty($quote_id)) {
// This is one of these "should not happen" situations.
// The protocol implementations should already have done this job.
- Logger::notice('Quote-uri-id detected in post', ['id' => $quote_id, 'guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'callstack' => System::callstack(20)]);
+ Logger::notice('Quote-uri-id detected in post', ['id' => $quote_id, 'guid' => $item['guid'], 'uri-id' => $item['uri-id']]);
$item['quote-uri-id'] = $quote_id;
}
}
// Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']);
- $item['language'] = self::getLanguage($item);
-
$notify_type = Delivery::POST;
// Filling item related side tables
}
}
+ $item['language'] = self::getLanguage($item);
+
$inserted = Post::insert($item['uri-id'], $item);
if ($item['gravity'] == self::GRAVITY_PARENT) {
if ($notify) {
DI::contentItem()->postProcessPost($posted_item);
+ if ($copy_permissions && ($posted_item['thr-parent-id'] != $posted_item['uri-id']) && ($posted_item['private'] == self::PRIVATE)) {
+ DI::contentItem()->copyPermissions($posted_item['thr-parent-id'], $posted_item['uri-id'], $posted_item['parent-uri-id']);
+ }
} else {
Hook::callAll('post_remote_end', $posted_item);
}
}
if (!empty($source) && ($transmit || DI::config()->get('debug', 'store_source'))) {
- Post\Activity::insert($item['uri-id'], $source);
+ Post\Activity::insert($posted_item['uri-id'], $source);
}
if ($transmit) {
+ ActivityPub\Transmitter::storeReceiversForItem($posted_item);
+
Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
}
*/
private static function setOwnerforResharedItem(array $item)
{
+ if ($item['uid'] == 0) {
+ return;
+ }
+
$parent = Post::selectFirst(
['id', 'causer-id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-reason'],
['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]
return;
}
+ $languages = $item['language'] ? array_keys(json_decode($item['language'], true)) : [];
+
foreach (Tag::getUIDListByURIId($item['uri-id']) as $uid => $tags) {
+ if (!empty($languages)) {
+ $keep = false;
+ $user_languages = User::getWantedLanguages($uid);
+ foreach ($user_languages as $language) {
+ if (in_array($language, $languages)) {
+ $keep = true;
+ }
+ }
+ if ($keep) {
+ Logger::debug('Wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+ } else {
+ Logger::debug('No wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+ continue;
+ }
+ }
+
$stored = self::storeForUserByUriId($item['uri-id'], $uid, ['post-reason' => self::PR_TAG]);
Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
foreach ($tags as $tag) {
* @return string detected language
* @throws \Text_LanguageDetect_Exception
*/
- private static function getLanguage(array $item): string
+ private static function getLanguage(array $item): ?string
{
if (!empty($item['language'])) {
return $item['language'];
}
- if (!in_array($item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) || empty($item['body'])) {
- return '';
+ $transmitted = [];
+ foreach ($item['transmitted-languages'] ?? [] as $language) {
+ $transmitted[$language] = 0;
}
- $languages = self::getLanguageArray(trim($item['title'] . "\n" . $item['body']), 3, $item['uri-id'], $item['author-id']);
+ $content = trim(($item['title'] ?? '') . ' ' . ($item['content-warning'] ?? '') . ' ' . ($item['body'] ?? ''));
+
+ if (!in_array($item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) || empty($content)) {
+ return !empty($transmitted) ? json_encode($transmitted) : null;
+ }
+
+ $languages = self::getLanguageArray($content, 3, $item['uri-id'], $item['author-id']);
if (empty($languages)) {
- return '';
+ return !empty($transmitted) ? json_encode($transmitted) : null;
+ }
+
+ if (!empty($transmitted)) {
+ $languages = array_merge($transmitted, $languages);
+ arsort($languages);
}
return json_encode($languages);
*/
public static function getLanguageArray(string $body, int $count, int $uri_id = 0, int $author_id = 0): array
{
- $naked_body = BBCode::toSearchText($body, $uri_id);
+ $searchtext = BBCode::toSearchText($body, $uri_id);
- if ((count(explode(' ', $naked_body)) < 10) && (mb_strlen($naked_body) < 30) && $author_id) {
+ if ((count(explode(' ', $searchtext)) < 10) && (mb_strlen($searchtext) < 30) && $author_id) {
$author = Contact::selectFirst(['about'], ['id' => $author_id]);
if (!empty($author['about'])) {
$about = BBCode::toSearchText($author['about'], 0);
- $about = self::getDominantLanguage($about);
- Logger::debug('About field added', ['author' => $author_id, 'body' => $naked_body, 'about' => $about]);
- $naked_body .= ' ' . $about;
+ Logger::debug('About field added', ['author' => $author_id, 'body' => $searchtext, 'about' => $about]);
+ $searchtext .= ' ' . $about;
}
}
- if (empty($naked_body)) {
+ if (empty($searchtext)) {
return [];
}
- $naked_body = self::getDominantLanguage($naked_body);
+ $ld = new Language(DI::l10n()->getDetectableLanguages());
- $availableLanguages = DI::l10n()->getAvailableLanguages(true);
- $availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
+ $result = [];
- $ld = new Language(array_keys($availableLanguages));
- $languages = $ld->detect($naked_body)->limit(0, $count)->close() ?: [];
+ foreach (self::splitByBlocks($searchtext) as $block) {
+ $languages = $ld->detect($block)->close() ?: [];
- $data = [
- 'text' => $naked_body,
- 'detected' => $languages,
- 'uri-id' => $uri_id,
- ];
+ $data = [
+ 'text' => $block,
+ 'detected' => $languages,
+ 'uri-id' => $uri_id,
+ 'author-id' => $author_id,
+ ];
+ Hook::callAll('detect_languages', $data);
- Hook::callAll('detect_languages', $data);
- $languages = $data['detected'];
+ foreach ($data['detected'] as $language => $quality) {
+ $result[$language] = max($result[$language] ?? 0, $quality * (strlen($block) / strlen($searchtext)));
+ }
+ }
+
+ $result = self::compactLanguages($result);
+ arsort($result);
+ return array_slice($result, 0, $count);
+ }
+
+ /**
+ * Concert the language code in the detection result to ISO 639-1.
+ * On duplicates the system uses the higher quality value.
+ *
+ * @param array $result
+ * @return array
+ */
+ private static function compactLanguages(array $result): array
+ {
+ $languages = [];
+ foreach ($result as $language => $quality) {
+ if ($quality == 0) {
+ continue;
+ }
+ $code = DI::l10n()->toISO6391($language);
+ if (empty($languages[$code]) || ($languages[$code] < $quality)) {
+ $languages[$code] = $quality;
+ }
+ }
return $languages;
}
/**
- * Check if latin or non latin are dominant in the body and only return the dominant one
+ * Split a string into different unicode blocks
+ * Currently the text is split into the latin and the non latin part.
*
* @param string $body
- * @return string
+ * @return array
*/
- private static function getDominantLanguage(string $body): string
+ private static function splitByBlocks(string $body): array
{
- $latin = '';
- $non_latin = '';
+ if (!class_exists('IntlChar')) {
+ return [$body];
+ }
+
+ $blocks = [];
+ $previous_block = 0;
+
for ($i = 0; $i < mb_strlen($body); $i++) {
$character = mb_substr($body, $i, 1);
- $ord = mb_ord($character);
-
- // We add the most common characters to both strings.
- if (($ord <= 64) || ($ord >= 91 && $ord <= 96) || ($ord >= 123 && $ord <= 191) || in_array($ord, [215, 247]) || ($ord >= 697 && $ord <= 735) || ($ord > 65535)) {
- $latin .= $character;
- $non_latin .= $character;
- } elseif ($ord < 768) {
- $latin .= $character;
+ $previous = ($i > 0) ? mb_substr($body, $i - 1, 1) : '';
+ $next = ($i < mb_strlen($body)) ? mb_substr($body, $i + 1, 1) : '';
+
+ if (!\IntlChar::isalpha($character)) {
+ if (($previous != '') && (\IntlChar::isalpha($previous))) {
+ $previous_block = self::getBlockCode($previous);
+ }
+
+ $block = (($next != '') && \IntlChar::isalpha($next)) ? self::getBlockCode($next) : $previous_block;
+ $blocks[$block] = ($blocks[$block] ?? '') . $character;
} else {
- $non_latin .= $character;
+ $block = self::getBlockCode($character);
+ $blocks[$block] = ($blocks[$block] ?? '') . $character;
+ }
+ }
+
+ foreach (array_keys($blocks) as $key) {
+ $blocks[$key] = trim($blocks[$key]);
+ if (empty($blocks[$key])) {
+ unset($blocks[$key]);
}
}
- return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+
+ return array_values($blocks);
+ }
+
+ /**
+ * returns the block code for the given character
+ *
+ * @param string $character
+ * @return integer 0 = no alpha character (blank, signs, emojis, ...), 1 = latin character, 2 = character in every other language
+ */
+ private static function getBlockCode(string $character): int
+ {
+ if (!\IntlChar::isalpha($character)) {
+ return 0;
+ }
+ return self::isLatin($character) ? 1 : 2;
+ }
+
+ /**
+ * Checks if the given character is in one of the latin code blocks
+ *
+ * @param string $character
+ * @return boolean
+ */
+ private static function isLatin(string $character): bool
+ {
+ return in_array(\IntlChar::getBlockCode($character), [
+ \IntlChar::BLOCK_CODE_BASIC_LATIN, \IntlChar::BLOCK_CODE_LATIN_1_SUPPLEMENT,
+ \IntlChar::BLOCK_CODE_LATIN_EXTENDED_A, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_B,
+ \IntlChar::BLOCK_CODE_LATIN_EXTENDED_C, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_D,
+ \IntlChar::BLOCK_CODE_LATIN_EXTENDED_E, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_ADDITIONAL
+ ]);
}
public static function getLanguageMessage(array $item): string
$used_languages = '';
foreach (json_decode($item['language'], true) as $language => $reliability) {
- $used_languages .= $iso639->languageByCode1($language) . ' (' . $language . "): " . number_format($reliability, 5) . '\n';
+ $code = DI::l10n()->toISO6391($language);
+
+ $native = $iso639->nativeByCode1($code);
+ $language = $iso639->languageByCode1($code);
+ if ($native != $language) {
+ $used_languages .= DI::l10n()->t('%s (%s - %s): %s', $native, $language, $code, number_format($reliability, 5)) . '\n';
+ } else {
+ $used_languages .= DI::l10n()->t('%s (%s): %s', $native, $code, number_format($reliability, 5)) . '\n';
+ }
}
$used_languages = DI::l10n()->t('Detected languages in this post:\n%s', $used_languages);
return $used_languages;
$hostPart = $host ?: $parsed['host'] ?? '';
if (!$hostPart) {
- Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed, 'callstack' => System::callstack(10)]);
+ Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed]);
}
// Glue it together to be able to make a hash from it
if (!empty($parsed)) {
- $host_id = implode('/', $parsed);
+ $host_id = implode('/', (array)$parsed);
} else {
$host_id = $uri;
}
$result = self::insert($datarray2);
Logger::info('remote-self post original item', ['contact' => $contact['url'], 'result' => $result, 'item' => $datarray2]);
} else {
- $datarray['private'] = self::PUBLIC;
$datarray['app'] = 'Feed';
$result = true;
}
+ if ($result) {
+ unset($datarray['private']);
+ }
+
return (bool)$result;
}
return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image/single_with_height_allocation.tpl'), [
'$image' => $PostMedia,
'$allocated_height' => $PostMedia->getAllocatedHeight(),
+ '$allocated_max_width' => ($PostMedia->previewWidth ?? $PostMedia->width) . 'px',
]);
}, $s);
} else {
unset($urlparts['fragment']);
try {
- $url = (string)Uri::fromParts($urlparts);
+ $url = (string)Uri::fromParts((array)$urlparts);
} catch (\InvalidArgumentException $e) {
DI::logger()->notice('Invalid URL', ['$url' => $url, '$urlparts' => $urlparts]);
/* See https://github.com/friendica/friendica/issues/12113
continue;
}
- if ($PostMedia->mimetype->type == 'image') {
- $preview_size = $PostMedia->width > $PostMedia->height ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE;
- $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size);
- } elseif ($PostMedia->preview) {
- $preview_size = Proxy::SIZE_LARGE;
+ if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) {
+ $preview_size = Proxy::SIZE_MEDIUM;
$preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size);
} else {
$preview_size = 0;
DI::profiler()->startRecording('rendering');
$trailing = '';
foreach ($PostMedias as $PostMedia) {
- if (strpos($item['body'], $PostMedia->url)) {
+ if (strpos($item['body'], (string)$PostMedia->url)) {
continue;
}
foreach ($options as $key => $option) {
if ($question['voters'] > 0) {
$percent = $option['replies'] / $question['voters'] * 100;
- $options[$key]['vote'] = DI::l10n()->tt('%2$s (%3$d%%, %1$d vote)', '%2$s (%3$d%%, %1$d votes)', $option['replies'], $option['name'], round($percent, 1));
+ $options[$key]['vote'] = DI::l10n()->tt('%2$s (%3$d%%, %1$d vote)', '%2$s (%3$d%%, %1$d votes)', $option['replies'] ?? 0, $option['name'], round($percent, 1));
} else {
- $options[$key]['vote'] = DI::l10n()->tt('%2$s (%1$d vote)', '%2$s (%1$d votes)', $option['replies'], $option['name']);
+ $options[$key]['vote'] = DI::l10n()->tt('%2$s (%1$d vote)', '%2$s (%1$d votes)', $option['replies'] ?? 0, $option['name']);
}
}
if (!empty($question['voters']) && !empty($question['endtime'])) {
- $summary = DI::l10n()->tt('%d voter. Poll end: %s', '%d voters. Poll end: %s', $question['voters'], Temporal::getRelativeDate($question['endtime']));
+ $summary = DI::l10n()->tt('%d voter. Poll end: %s', '%d voters. Poll end: %s', $question['voters'] ?? 0, Temporal::getRelativeDate($question['endtime']));
} elseif (!empty($question['voters'])) {
- $summary = DI::l10n()->tt('%d voter.', '%d voters.', $question['voters']);
+ $summary = DI::l10n()->tt('%d voter.', '%d voters.', $question['voters'] ?? 0);
} elseif (!empty($question['endtime'])) {
$summary = DI::l10n()->t('Poll end: %s', Temporal::getRelativeDate($question['endtime']));
} else {
* Fetches item for given URI or plink
*
* @param string $uri
- * @param integer $uid
+ * @param int $uid
+ * @param int $completion
*
* @return integer item id
*/
- public static function fetchByLink(string $uri, int $uid = 0): int
+ public static function fetchByLink(string $uri, int $uid = 0, int $completion = ActivityPub\Receiver::COMPLETION_MANUAL): int
{
Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]);
$item_id = self::searchByLink($uri, $uid);
return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
}
- $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', ActivityPub\Receiver::COMPLETION_MANUAL, $uid);
+ $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', $completion, $uid);
if ($fetched_uri) {
$item_id = self::searchByLink($fetched_uri, $uid);
}
$url = $shared['message_id'] ?: $shared['link'];
- $id = self::fetchByLink($url);
+ $id = self::fetchByLink($url, 0, ActivityPub\Receiver::COMPLETION_ASYNC);
if (!$id) {
Logger::notice('Post could not be fetched.', ['url' => $url, 'uid' => $uid]);
return 0;