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

@ -25,24 +25,27 @@ use Friendica\BaseCollection;
use Friendica\Content\Conversation\Collection\UserDefinedChannels;
use Friendica\Content\Conversation\Entity;
use Friendica\Content\Conversation\Factory;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Post\Engagement;
use Friendica\Model\User;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface;
class UserDefinedChannel extends \Friendica\BaseRepository
{
protected static $table_name = 'channel';
/** @var IManagePersonalConfigValues */
private $pConfig;
/** @var IManageConfigValues */
private $config;
public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManagePersonalConfigValues $pConfig)
public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManageConfigValues $config)
{
parent::__construct($database, $logger, $factory);
$this->pConfig = $pConfig;
$this->config = $config;
}
/**
@ -63,6 +66,11 @@ class UserDefinedChannel extends \Friendica\BaseRepository
return $Entities;
}
public function select(array $condition, array $params = []): BaseCollection
{
return $this->_select($condition, $params);
}
/**
* Fetch a single user channel
*
@ -146,34 +154,127 @@ class UserDefinedChannel extends \Friendica\BaseRepository
*
* @param string $searchtext
* @param string $language
* @param array $tags
* @param int $media_type
* @return boolean
*/
public function match(string $searchtext, string $language): bool
public function match(string $searchtext, string $language, array $tags, int $media_type): bool
{
$condition = ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `user`.`uid` > ?", 0];
$abandon_days = intval($this->config->get('system', 'account_abandon_days'));
if (!empty($abandon_days)) {
$condition = DBA::mergeConditions($condition, ["`last-activity` > ?", DateTimeFormat::utc('now - ' . $abandon_days . ' days')]);
}
$users = $this->db->selectToArray('user', ['uid'], $condition);
if (empty($users)) {
return [];
}
return !empty($this->getMatches($searchtext, $language, $tags, $media_type, 0, array_column($users, 'uid'), false));
}
/**
* Fetch the channel users that have got matching channels
*
* @param string $searchtext
* @param string $language
* @param array $tags
* @param integer $media_type
* @return array
*/
public function getMatchingChannelUsers(string $searchtext, string $language, array $tags, int $media_type, int $author_id): array
{
$users = $this->db->selectToArray('user', ['uid'], ["`account-type` = ? AND `uid` != ?", User::ACCOUNT_TYPE_RELAY, 0]);
if (empty($users)) {
return [];
}
return $this->getMatches($searchtext, $language, $tags, $media_type, $author_id, array_column($users, 'uid'), true);
}
private function getMatches(string $searchtext, string $language, array $tags, int $media_type, int $author_id, array $channelUids, bool $relayMode): array
{
if (!in_array($language, User::getLanguages())) {
$this->logger->debug('Unwanted language found. No matched channel found.', ['language' => $language, 'searchtext' => $searchtext]);
return false;
return [];
}
$store = false;
$this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE);
$channels = $this->db->select(self::$table_name, ['full-text-search', 'uid', 'label'], ["`full-text-search` != ? AND `circle` = ?", '', 0]);
while ($channel = $this->db->fetch($channels)) {
$channelsearchtext = $channel['full-text-search'];
foreach (Engagement::KEYWORDS as $keyword) {
$channelsearchtext = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $channelsearchtext);
$uids = [];
$condition = ['uid' => $channelUids];
if (!$relayMode) {
$condition = DBA::mergeConditions($condition, ["`full-text-search` != ?", '']);
}
foreach ($this->select($condition) as $channel) {
if (in_array($channel->uid, $uids)) {
continue;
}
if ($this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $channelsearchtext])) {
if (in_array($language, $this->pConfig->get($channel['uid'], 'channel', 'languages', [User::getLanguageCode($channel['uid'])]))) {
$store = true;
$this->logger->debug('Matching channel found.', ['uid' => $channel['uid'], 'label' => $channel['label'], 'language' => $language, 'channelsearchtext' => $channelsearchtext, 'searchtext' => $searchtext]);
break;
if (!empty($channel->circle) && ($channel->circle > 0) && !in_array($channel->uid, $uids)) {
$account = Contact::selectFirstAccountUser(['id'], ['pid' => $author_id, 'uid' => $channel->uid]);
if (empty($account['id']) || !$this->db->exists('group_member', ['gid' => $channel->circle, 'contact-id' => $account['id']])) {
continue;
}
}
if (!empty($channel->languages) && !in_array($channel->uid, $uids)) {
if (!in_array($language, $channel->languages)) {
continue;
}
} elseif (!in_array($language, User::getWantedLanguages($channel->uid))) {
continue;
}
if (!empty($channel->includeTags) && !in_array($channel->uid, $uids)) {
if (empty($tags)) {
continue;
}
$match = false;
foreach (explode(',', $channel->includeTags) as $tag) {
if (in_array($tag, $tags)) {
$match = true;
break;
}
}
if (!$match) {
continue;
}
}
if (!empty($tags) && !empty($channel->excludeTags) && !in_array($channel->uid, $uids)) {
$match = false;
foreach (explode(',', $channel->excludeTags) as $tag) {
if (in_array($tag, $tags)) {
$match = true;
break;
}
}
if ($match) {
continue;
}
}
if (!empty($channel->mediaType) && !in_array($channel->uid, $uids)) {
if (!($channel->mediaType & $media_type)) {
continue;
}
}
if (!empty($channel->fullTextSearch) && !in_array($channel->uid, $uids)) {
$channelsearchtext = $channel->fullTextSearch;
foreach (Engagement::KEYWORDS as $keyword) {
$channelsearchtext = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $channelsearchtext);
}
if (!$this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $channelsearchtext])) {
continue;
}
}
$uids[] = $channel->uid;
$this->logger->debug('Matching channel found.', ['uid' => $channel->uid, 'label' => $channel->label, 'language' => $language, 'tags' => $tags, 'media_type' => $media_type, 'searchtext' => $searchtext]);
if (!$relayMode) {
return $uids;
}
}
$this->db->close($channels);
$this->db->delete('check-full-text-search', ['pid' => getmypid()]);
return $store;
return $uids;
}
}

View File

@ -436,7 +436,7 @@ class L10n
{
$iso639 = new \Matriphe\ISO639\ISO639;
$languages = [];
$languages = ['un' => $this->t('Undeteced Language')];
foreach ($this->getDetectableLanguages() as $code) {
$code = $this->toISO6391($code);

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);

View File

@ -432,11 +432,15 @@ class Timeline extends BaseModule
$conditions = [];
$languages = $languages ?: User::getWantedLanguages($uid);
foreach ($languages as $language) {
$conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
$condition[] = $language;
if ($language == 'un') {
$conditions[] = "`language` IS NULL";
} else {
$conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
$condition[] = $language;
}
}
if (!empty($conditions)) {
$condition[0] .= " AND (`language` IS NULL OR " . implode(' OR ', $conditions) . ")";
$condition[0] .= " AND (" . implode(' OR ', $conditions) . ")";
}
return $condition;
}

View File

@ -316,6 +316,8 @@ class Account extends BaseSettings
$page_flags = User::PAGE_FLAGS_SOAPBOX;
} elseif ($account_type == User::ACCOUNT_TYPE_COMMUNITY && !in_array($page_flags, [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
$page_flags = User::PAGE_FLAGS_COMMUNITY;
} elseif ($account_type == User::ACCOUNT_TYPE_RELAY && $page_flags != User::PAGE_FLAGS_SOAPBOX) {
$page_flags = User::PAGE_FLAGS_SOAPBOX;
}
$fields = [
@ -433,6 +435,7 @@ class Account extends BaseSettings
'$type_organisation' => User::ACCOUNT_TYPE_ORGANISATION,
'$type_news' => User::ACCOUNT_TYPE_NEWS,
'$type_community' => User::ACCOUNT_TYPE_COMMUNITY,
'$type_relay' => User::ACCOUNT_TYPE_RELAY,
'$account_person' => [
'account-type',
DI::l10n()->t('Personal Page'),
@ -461,6 +464,13 @@ class Account extends BaseSettings
DI::l10n()->t('Account for community discussions.'),
$user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY
],
'$account_relay' => [
'account-type',
DI::l10n()->t('Channel Relay'),
User::ACCOUNT_TYPE_RELAY,
DI::l10n()->t('Account for a service that automatically shares content based on user defined channels.'),
$user['account-type'] == User::ACCOUNT_TYPE_RELAY
],
'$page_normal' => [
'page-flags',
DI::l10n()->t('Normal Account Page'),

View File

@ -1787,7 +1787,7 @@ class Processor
$searchtext = Engagement::getSearchTextForActivity($content, $authorid, $messageTags, $receivers);
$languages = Item::getLanguageArray($content, 1, 0, $authorid);
$language = !empty($languages) ? array_key_first($languages) : '';
return DI::userDefinedChannel()->match($searchtext, $language);
return DI::userDefinedChannel()->match($searchtext, $language, $messageTags, 0);
}
/**

View File

@ -157,20 +157,16 @@ class Relay
*/
public static function getSubscribedTags(): array
{
$systemTags = [];
$server_tags = DI::config()->get('system', 'relay_server_tags');
foreach (explode(',', mb_strtolower($server_tags)) as $tag) {
$systemTags[] = trim($tag, '# ');
$tags = [];
foreach (explode(',', mb_strtolower(DI::config()->get('system', 'relay_server_tags'))) as $tag) {
$tags[] = trim($tag, '# ');
}
if (DI::config()->get('system', 'relay_user_tags')) {
$userTags = Search::getUserTags();
} else {
$userTags = [];
$tags = array_merge($tags, Search::getUserTags());
}
return array_unique(array_merge($systemTags, $userTags));
return array_unique($tags);
}
/**

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,8 @@
{{include file="field_radio.tpl" field=$page_prvgroup}}
</div>
{{include file="field_radio.tpl" field=$account_relay}}
<script language="javascript" type="text/javascript">
// This js part changes the state of page-flags radio buttons according
// to the selected account type.