Check for user defined channel matches before storing them

This commit is contained in:
Michael 2023-11-15 16:19:05 +00:00
parent e2891a660a
commit 43f9be367f
13 changed files with 236 additions and 42 deletions

View file

@ -240,7 +240,7 @@ class GServer
} elseif (!empty($contact['baseurl'])) {
$server = $contact['baseurl'];
} elseif ($contact['network'] == Protocol::DIASPORA) {
$parts = parse_url($contact['url']);
$parts = (array)parse_url($contact['url']);
unset($parts['path']);
$server = (string)Uri::fromParts($parts);
} else {
@ -589,7 +589,7 @@ class GServer
if ((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH)) &&
(parse_url($url, PHP_URL_PATH) == '')) {
Logger::debug('Found redirect. Mark old entry as failure and redirect to the basepath.', ['old' => $url, 'new' => $valid_url]);
$parts = parse_url($valid_url);
$parts = (array)parse_url($valid_url);
unset($parts['path']);
$valid_url = (string)Uri::fromParts($parts);

View file

@ -33,6 +33,7 @@ use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Model\Verb;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub\Receiver;
use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
@ -52,7 +53,7 @@ class Engagement
}
$parent = Post::selectFirst(['uri-id', 'created', 'author-id', 'owner-id', 'uid', 'private', 'contact-contact-type', 'language', 'network',
'title', 'content-warning', 'body', 'author-contact-type', 'author-nick', 'author-addr', 'owner-contact-type', 'owner-nick', 'owner-addr'],
'title', 'content-warning', 'body', 'author-contact-type', 'author-nick', 'author-addr', 'author-gsid', 'owner-contact-type', 'owner-nick', 'owner-addr'],
['uri-id' => $item['parent-uri-id']]);
if ($parent['created'] < self::getCreationDateLimit(false)) {
@ -79,7 +80,14 @@ class Engagement
$mediatype = self::getMediaType($item['parent-uri-id']);
if (!$store) {
$mediatype = !empty($mediatype);
$store = !empty($mediatype);
}
$searchtext = self::getSearchTextForItem($parent);
if (!$store) {
$content = trim(($parent['title'] ?? '') . ' ' . ($parent['content-warning'] ?? '') . ' ' . ($parent['body'] ?? ''));
$language = array_key_first(Item::getLanguageArray($content, 1, 0, $parent['author-id']));
$store = DI::userDefinedChannel()->match($searchtext, $language);
}
$engagement = [
@ -88,7 +96,7 @@ class Engagement
'contact-type' => $parent['contact-contact-type'],
'media-type' => $mediatype,
'language' => $parent['language'],
'searchtext' => self::getSearchText($parent),
'searchtext' => $searchtext,
'created' => $parent['created'],
'restricted' => !in_array($item['network'], Protocol::FEDERATED) || ($parent['private'] != Item::PUBLIC),
'comments' => DBA::count('post', ['parent-uri-id' => $item['parent-uri-id'], 'gravity' => Item::GRAVITY_COMMENT]),
@ -106,10 +114,56 @@ class Engagement
Logger::debug('Engagement stored', ['fields' => $engagement, 'ret' => $ret]);
}
private static function getSearchText(array $item): string
public static function getSearchTextForActivity(string $content, int $author_id, array $tags, array $receivers): string
{
$author = Contact::getById($author_id);
$item = [
'uri-id' => 0,
'network' => Protocol::ACTIVITYPUB,
'title' => '',
'content-warning' => '',
'body' => $content,
'private' => Item::PRIVATE,
'author-id' => $author_id,
'author-contact-type' => $author['contact-type'],
'author-nick' => $author['nick'],
'author-addr' => $author['addr'],
'author-gsid' => $author['gsid'],
'owner-id' => $author_id,
'owner-contact-type' => $author['contact-type'],
'owner-nick' => $author['nick'],
'owner-addr' => $author['addr'],
];
foreach ($receivers as $receiver) {
if ($receiver == Receiver::PUBLIC_COLLECTION) {
$item['private'] = Item::PUBLIC;
}
}
return self::getSearchText($item, $receivers, $tags);
}
private static function getSearchTextForItem(array $item): string
{
$receivers = array_column(Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]), 'url');
$tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
return self::getSearchText($item, $receivers, $tags);
}
private static function getSearchText(array $item, array $receivers, array $tags): string
{
$body = '[nosmile]network:' . $item['network'];
if (!empty($item['author-gsid'])) {
$gserver = DBA::selectFirst('gserver', ['platform'], ['id' => $item['author-gsid']]);
$platform = preg_replace( '/[\W]/', '', $gserver['platform'] ?? '');
if (!empty($platform)) {
$body .= ' platform:' . $platform;
}
}
switch ($item['private']) {
case Item::PUBLIC:
$body .= ' visibility:public';
@ -136,8 +190,8 @@ class Engagement
}
}
foreach (Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]) as $tag) {
$contact = Contact::getByURL($tag['name'], false, ['nick', 'addr', 'contact-type']);
foreach ($receivers as $receiver) {
$contact = Contact::getByURL($receiver, false, ['nick', 'addr', 'contact-type']);
if (empty($contact)) {
continue;
}
@ -149,8 +203,8 @@ class Engagement
}
}
foreach (Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]) as $tag) {
$body .= ' tag:' . $tag['name'];
foreach ($tags as $tag) {
$body .= ' tag:' . $tag;
}
$body .= ' ' . $item['title'] . ' ' . $item['content-warning'] . ' ' . $item['body'];

View file

@ -22,6 +22,8 @@
namespace Friendica\Model;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\DateTimeFormat;
/**
* Model for DB specific logic for the search entity
@ -36,14 +38,43 @@ class Search
*/
public static function getUserTags(): array
{
$termsStmt = DBA::p("SELECT DISTINCT(`term`) FROM `search`");
$user_condition = ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `user`.`uid` > ?", 0];
$abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
if (!empty($abandon_days)) {
$user_condition = DBA::mergeConditions($user_condition, ["`last-activity` > ?", DateTimeFormat::utc('now - ' . $abandon_days . ' days')]);
}
$condition = $user_condition;
$condition[0] = "SELECT DISTINCT(`term`) FROM `search` INNER JOIN `user` ON `search`.`uid` = `user`.`uid` WHERE " . $user_condition[0];
$sql = array_shift($condition);
$termsStmt = DBA::p($sql, $condition);
$tags = [];
while ($term = DBA::fetch($termsStmt)) {
$tags[] = trim(mb_strtolower($term['term']), '#');
}
DBA::close($termsStmt);
$condition = $user_condition;
$condition[0] = "SELECT `include-tags` FROM `channel` INNER JOIN `user` ON `channel`.`uid` = `user`.`uid` WHERE " . $user_condition[0];
$sql = array_shift($condition);
$channels = DBA::p($sql, $condition);
while ($channel = DBA::fetch($channels)) {
foreach (explode(',', $channel['include-tags']) as $tag) {
$tag = trim(mb_strtolower($tag));
if (empty($tag)) {
continue;
}
if (!in_array($tag, $tags)) {
$tags[] = $tag;
}
}
}
DBA::close($channels);
sort($tags);
return $tags;
}
}

View file

@ -582,6 +582,12 @@ class User
*/
public static function getLanguages(): array
{
$cachekey = 'user:getLanguages';
$languages = DI::cache()->get($cachekey);
if (!is_null($languages)) {
return $languages;
}
$supported = array_keys(DI::l10n()->getLanguageCodes());
$languages = [];
$uids = [];
@ -620,7 +626,10 @@ class User
DBA::close($channels);
ksort($languages);
return array_keys($languages);
$languages = array_keys($languages);
DI::cache()->set($cachekey, $languages);
return $languages;
}
/**