From a04dc06aab74891d44a2af2a626cd4ec3b377a1b Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Jan 2024 22:55:47 +0000 Subject: [PATCH] Handle reshares / improved code --- .../Repository/UserDefinedChannel.php | 82 +++++++++++-------- src/Model/Item.php | 23 ++++-- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/Content/Conversation/Repository/UserDefinedChannel.php b/src/Content/Conversation/Repository/UserDefinedChannel.php index f0a0f3efe8..e03b28ee89 100644 --- a/src/Content/Conversation/Repository/UserDefinedChannel.php +++ b/src/Content/Conversation/Repository/UserDefinedChannel.php @@ -166,7 +166,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository return []; } - return !empty($this->getMatches($searchtext, $language, $tags, $media_type, 0, array_column($users, 'uid'), false)); + return !empty($this->getMatches($searchtext, $language, $tags, $media_type, 0, 0, array_column($users, 'uid'), false)); } /** @@ -177,9 +177,10 @@ class UserDefinedChannel extends \Friendica\BaseRepository * @param array $tags * @param int $media_type * @param int $owner_id + * @param int $reshare_id * @return array */ - public function getMatchingChannelUsers(string $searchtext, string $language, array $tags, int $media_type, int $owner_id): array + public function getMatchingChannelUsers(string $searchtext, string $language, array $tags, int $media_type, int $owner_id, int $reshare_id): array { $condition = $this->getUserCondition(); $condition = DBA::mergeConditions($condition, ["`account-type` IN (?, ?) AND `uid` != ?", User::ACCOUNT_TYPE_RELAY, User::ACCOUNT_TYPE_COMMUNITY, 0]); @@ -187,20 +188,16 @@ class UserDefinedChannel extends \Friendica\BaseRepository if (empty($users)) { return []; } - return $this->getMatches($searchtext, $language, $tags, $media_type, $owner_id, array_column($users, 'uid'), true); + return $this->getMatches($searchtext, $language, $tags, $media_type, $owner_id, $reshare_id, array_column($users, 'uid'), true); } - private function getMatches(string $searchtext, string $language, array $tags, int $media_type, int $owner_id, array $channelUids, bool $relayMode): array + private function getMatches(string $searchtext, string $language, array $tags, int $media_type, int $owner_id, int $reshare_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 []; } - array_walk($tags, function (&$value) { - $value = mb_strtolower($value); - }); - $this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE); $uids = []; @@ -217,8 +214,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository continue; } if (!empty($channel->circle) && ($channel->circle > 0) && !in_array($channel->uid, $uids)) { - $account = Contact::selectFirstAccountUser(['id'], ['pid' => $owner_id, 'uid' => $channel->uid]); - if (empty($account['id']) || !$this->db->exists('group_member', ['gid' => $channel->circle, 'contact-id' => $account['id']])) { + if (!$this->inCircle($channel->circle, $channel->uid, $owner_id) && !$this->inCircle($channel->circle, $channel->uid, $reshare_id)) { continue; } } @@ -230,29 +226,12 @@ class UserDefinedChannel extends \Friendica\BaseRepository 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) { + if (!$this->inTaglist($channel->includeTags, $tags)) { 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) { + if (!empty($channel->excludeTags) && !in_array($channel->uid, $uids)) { + if ($this->inTaglist($channel->excludeTags, $tags)) { continue; } } @@ -262,11 +241,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository } } 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])) { + if (!$this->inFulltext($channel->fullTextSearch)) { continue; } } @@ -281,6 +256,43 @@ class UserDefinedChannel extends \Friendica\BaseRepository return $uids; } + private function inCircle(int $circleId, int $uid, int $cid): bool + { + if ($cid == 0) { + return false; + } + + $account = Contact::selectFirstAccountUser(['id'], ['pid' => $cid, 'uid' => $uid]); + if (empty($account['id'])) { + return false; + } + return $this->db->exists('group_member', ['gid' => $circleId, 'contact-id' => $account['id']]); + } + + private function inTaglist(string $tagList, array $tags): bool + { + if (empty($tags)) { + return false; + } + array_walk($tags, function (&$value) { + $value = mb_strtolower($value); + }); + foreach (explode(',', $tagList) as $tag) { + if (in_array($tag, $tags)) { + return true; + } + } + return false; + } + + private function inFulltext(string $fullTextSearch): bool + { + foreach (Engagement::KEYWORDS as $keyword) { + $fullTextSearch = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $fullTextSearch); + } + return $this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $fullTextSearch]); + } + private function getUserCondition() { $condition = ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `user`.`uid` > ?", 0]; diff --git a/src/Model/Item.php b/src/Model/Item.php index a9361f0fbb..f84fcdad84 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1442,7 +1442,10 @@ class Item } $engagement_uri_id = Post\Engagement::storeFromItem($posted_item); - if ($engagement_uri_id) { + + if (($posted_item['gravity'] == self::GRAVITY_ACTIVITY) && ($posted_item['verb'] == Activity::ANNOUNCE) && ($posted_item['parent-uri-id'] == $posted_item['thr-parent-id'])) { + self::reshareChannelPost($posted_item['thr-parent-id'], $posted_item['author-id']); + } elseif ($engagement_uri_id) { self::reshareChannelPost($engagement_uri_id); } } @@ -1450,7 +1453,7 @@ class Item return $post_user_id; } - private static function reshareChannelPost(int $uri_id) + private static function reshareChannelPost(int $uri_id, int $reshare_id = 0) { if (!DI::config()->get('system', 'allow_relay_channels')) { return; @@ -1476,12 +1479,20 @@ class Item $language = !empty($item['language']) ? array_key_first(json_decode($item['language'], true)) : ''; $tags = array_column(Tag::getByURIId($uri_id, [Tag::HASHTAG]), 'name'); - Logger::debug('Prepare check', ['uri-id' => $uri_id, 'language' => $language, 'tags' => $tags, 'searchtext' => $engagement['searchtext'], 'media_type' => $engagement['media-type'], 'owner' => $item['owner-id']]); + Logger::debug('Prepare check', ['uri-id' => $uri_id, 'language' => $language, 'tags' => $tags, 'searchtext' => $engagement['searchtext'], 'media_type' => $engagement['media-type'], 'owner' => $item['owner-id'], 'reshare' => $reshare_id]); $count = 0; - foreach (DI::userDefinedChannel()->getMatchingChannelUsers($engagement['searchtext'], $language, $tags, $engagement['media-type'], $item['owner-id']) as $uid) { - Logger::debug('Reshare post', ['uid' => $uid, 'uri-id' => $uri_id]); - self::performActivity($item['id'], 'announce', $uid); + foreach (DI::userDefinedChannel()->getMatchingChannelUsers($engagement['searchtext'], $language, $tags, $engagement['media-type'], $item['owner-id'], $reshare_id) as $uid) { + $condition = [ + 'verb' => Activity::ANNOUNCE, 'deleted' => false, 'gravity' => self::GRAVITY_ACTIVITY, + 'author-id' => Contact::getPublicIdByUserId($uid), 'uid' => $uid, 'thr-parent-id' => $uri_id + ]; + if (!Post::exists($condition)) { + Logger::debug('Reshare post', ['uid' => $uid, 'uri-id' => $uri_id]); + self::performActivity($item['id'], 'announce', $uid); + } else { + Logger::debug('Reshare already exists', ['uid' => $uid, 'uri-id' => $uri_id]); + } $count++; }