New user account type "Channel Relay"

This commit is contained in:
Michael
2024-01-06 17:27:42 +00:00
parent 16b12e1545
commit 811a9f01bc
12 changed files with 434 additions and 261 deletions

View File

@@ -172,6 +172,11 @@ class Contact
return DBA::selectFirst('account-view', $fields, $condition, $params);
}
public static function selectFirstAccountUser(array $fields = [], array $condition = [], array $params = [])
{
return DBA::selectFirst('account-user-view', $fields, $condition, $params);
}
/**
* Insert a row into the contact table
* Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.

View File

@@ -1440,12 +1440,41 @@ class Item
if (in_array($posted_item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
Post\Counts::update($posted_item['thr-parent-id'], $posted_item['parent-uri-id'], $posted_item['vid'], $posted_item['verb'], $posted_item['body']);
}
Post\Engagement::storeFromItem($posted_item);
$engagement_uri_id = Post\Engagement::storeFromItem($posted_item);
if ($engagement_uri_id) {
self::reshareChannelPost($engagement_uri_id);
}
}
return $post_user_id;
}
private static function reshareChannelPost(int $uri_id)
{
$item = Post::selectFirst(['id', 'private', 'network', 'language', 'author-id'], ['uri-id' => $uri_id, 'uid' => 0]);
if (empty($item['id'])) {
return;
}
if (($item['private'] != self::PUBLIC) || !in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
return;
}
$engagement = DBA::selectFirst('post-engagement', ['searchtext', 'media-type'], ['uri-id' => $uri_id]);
if (empty($engagement['searchtext'])) {
return;
}
$language = !empty($item['language']) ? array_key_first(json_decode($item['language'], true)) : '';
$tags = array_column(Tag::getByURIId($uri_id, [Tag::HASHTAG]), 'name');
foreach (DI::userDefinedChannel()->getMatchingChannelUsers($engagement['searchtext'], $language, $tags, $engagement['media-type'], $item['author-id']) as $uid) {
Logger::debug('Reshare post', ['uid' => $uid, 'uri-id' => $uri_id, 'language' => $language, 'tags' => $tags, 'searchtext' => $engagement['searchtext'], 'media_type' => $engagement['media-type']]);
self::performActivity($item['id'], 'announce', $uid);
}
}
/**
* Fetch the post reason for a given item array
*

View File

@@ -45,13 +45,13 @@ class Engagement
* Store engagement data from an item array
*
* @param array $item
* @return void
* @return int uri-id of the engagement post if newly inserted, 0 on update
*/
public static function storeFromItem(array $item)
public static function storeFromItem(array $item): int
{
if (in_array($item['verb'], [Activity::FOLLOW, Activity::VIEW, Activity::READ])) {
Logger::debug('Technical activities are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'verb' => $item['verb']]);
return;
return 0;
}
$parent = Post::selectFirst(['uri-id', 'created', 'author-id', 'owner-id', 'uid', 'private', 'contact-contact-type', 'language', 'network',
@@ -60,7 +60,7 @@ class Engagement
if ($parent['created'] < self::getCreationDateLimit(false)) {
Logger::debug('Post is too old', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'created' => $parent['created']]);
return;
return 0;
}
$store = ($item['gravity'] != Item::GRAVITY_PARENT);
@@ -87,10 +87,9 @@ class Engagement
$searchtext = self::getSearchTextForItem($parent);
if (!$store) {
$content = trim(($parent['title'] ?? '') . ' ' . ($parent['content-warning'] ?? '') . ' ' . ($parent['body'] ?? ''));
$languages = Item::getLanguageArray($content, 1, 0, $parent['author-id']);
$language = !empty($languages) ? array_key_first($languages) : '';
$store = DI::userDefinedChannel()->match($searchtext, $language);
$tags = array_column(Tag::getByURIId($item['parent-uri-id'], [Tag::HASHTAG]), 'name');
$language = !empty($parent['language']) ? array_key_first(json_decode($parent['language'], true)) : '';
$store = DI::userDefinedChannel()->match($searchtext, $language, $tags, $mediatype);
}
$engagement = [
@@ -111,10 +110,17 @@ class Engagement
];
if (!$store && ($engagement['comments'] == 0) && ($engagement['activities'] == 0)) {
Logger::debug('No media, follower, subscribed tags, comments or activities. Engagement not stored', ['fields' => $engagement]);
return;
return 0;
}
$ret = DBA::insert('post-engagement', $engagement, Database::INSERT_UPDATE);
Logger::debug('Engagement stored', ['fields' => $engagement, 'ret' => $ret]);
$exists = DBA::exists('post-engagement', ['uri-id' => $engagement['uri-id']]);
if ($exists) {
$ret = DBA::update('post-engagement', $engagement, ['uri-id' => $engagement['uri-id']]);
Logger::debug('Engagement updated', ['uri-id' => $engagement['uri-id'], 'ret' => $ret]);
} else {
$ret = DBA::insert('post-engagement', $engagement);
Logger::debug('Engagement inserted', ['uri-id' => $engagement['uri-id'], 'ret' => $ret]);
}
return ($ret || !$exists) ? $engagement['uri-id'] : 0;
}
public static function getSearchTextForActivity(string $content, int $author_id, array $tags, array $receivers): string

View File

@@ -638,6 +638,12 @@ class User
}
DBA::close($channels);
foreach (DI::userDefinedChannel()->select(["NOT `languages` IS NULL"]) as $channel) {
foreach ($channel->languages ?? [] as $language) {
$languages[$language] = $language;
}
}
ksort($languages);
$languages = array_keys($languages);
DI::cache()->set($cachekey, $languages);