Language specific median calculations / collection

This commit is contained in:
Michael 2023-09-08 05:06:27 +00:00
parent 97c519990b
commit 9cbd0c34a7
4 changed files with 57 additions and 13 deletions

View File

@ -0,0 +1,28 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Content\Conversation\Collection;
use Friendica\BaseCollection;
class Channels extends BaseCollection
{
}

View File

@ -21,6 +21,7 @@
namespace Friendica\Content\Conversation\Factory;
use Friendica\Content\Conversation\Collection\Channels;
use Friendica\Model\User;
use Friendica\Content\Conversation\Entity\Channel as ChannelEntity;
use Friendica\Core\L10n;
@ -44,7 +45,7 @@ final class Channel extends \Friendica\BaseFactory
* @param integer $uid
* @return array
*/
public function getForUser(int $uid): array
public function getForUser(int $uid): Channels
{
$language = User::getLanguageCode($uid);
$languages = $this->l10n->getAvailableLanguages(true);
@ -59,6 +60,6 @@ final class Channel extends \Friendica\BaseFactory
new ChannelEntity(ChannelEntity::AUDIO, $this->l10n->t('Audio'), $this->l10n->t('Posts with audio'), 'd'),
new ChannelEntity(ChannelEntity::VIDEO, $this->l10n->t('Videos'), $this->l10n->t('Posts with videos'), 'v'),
];
return $tabs;
return new Channels($tabs);
}
}

View File

@ -284,11 +284,12 @@ class Channel extends BaseModule
} elseif (self::$content == ChannelEntity::SHARERSOFSHARERS) {
$cid = Contact::getPublicIdByUserId($this->session->getLocalUserId());
// @todo Suggest posts from contacts that are followed most by our followers
$condition = [
"`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `last-interaction` > ?
AND `relation-cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ? AND `relation-thread-score` >= ?)
AND NOT `cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?))",
DateTimeFormat::utc('now - 90 day'), $cid, $this->getMedianRelationThreadScore($cid, 4), $cid
DateTimeFormat::utc('now - ' . $this->config->get('channel', 'sharer_interaction_days') .' day'), $cid, $this->getMedianRelationThreadScore($cid, 4), $cid
];
} elseif (self::$content == ChannelEntity::IMAGE) {
$condition = ["`media-type` & ?", 1];
@ -371,41 +372,49 @@ class Channel extends BaseModule
private function getMedianComments(int $divider): int
{
$cache_key = 'Channel:getMedianComments:' . $divider;
$languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]);
$cache_key = 'Channel:getMedianComments:' . $divider . ':' . implode(':', $languages);
$comments = $this->cache->get($cache_key);
if (!empty($comments)) {
return $comments;
}
$limit = $this->database->count('post-engagement', ["`contact-type` != ? AND `comments` > ?", Contact::TYPE_COMMUNITY, 0]) / $divider;
$post = $this->database->selectToArray('post-engagement', ['comments'], ["`contact-type` != ?", Contact::TYPE_COMMUNITY], ['order' => ['comments' => true], 'limit' => [$limit, 1]]);
$condition = ["`contact-type` != ? AND `comments` > ?", Contact::TYPE_COMMUNITY, 0];
$condition = $this->addLanguageCondition($condition);
$limit = $this->database->count('post-engagement', $condition) / $divider;
$post = $this->database->selectToArray('post-engagement', ['comments'], $condition, ['order' => ['comments' => true], 'limit' => [$limit, 1]]);
$comments = $post[0]['comments'] ?? 0;
if (empty($comments)) {
return 0;
}
$this->cache->set($cache_key, $comments, Duration::HALF_HOUR);
$this->logger->debug('Calculated median comments', ['divider' => $divider, 'median' => $comments]);
$this->logger->debug('Calculated median comments', ['divider' => $divider, 'languages' => $languages, 'median' => $comments]);
return $comments;
}
private function getMedianActivities(int $divider): int
{
$cache_key = 'Channel:getMedianActivities:' . $divider;
$languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]);
$cache_key = 'Channel:getMedianActivities:' . $divider . ':' . implode(':', $languages);
$activities = $this->cache->get($cache_key);
if (!empty($activities)) {
return $activities;
}
$limit = $this->database->count('post-engagement', ["`contact-type` != ? AND `activities` > ?", Contact::TYPE_COMMUNITY, 0]) / $divider;
$post = $this->database->selectToArray('post-engagement', ['activities'], ["`contact-type` != ?", Contact::TYPE_COMMUNITY], ['order' => ['activities' => true], 'limit' => [$limit, 1]]);
$condition = ["`contact-type` != ? AND `activities` > ?", Contact::TYPE_COMMUNITY, 0];
$condition = $this->addLanguageCondition($condition);
$limit = $this->database->count('post-engagement', $condition) / $divider;
$post = $this->database->selectToArray('post-engagement', ['activities'], $condition, ['order' => ['activities' => true], 'limit' => [$limit, 1]]);
$activities = $post[0]['activities'] ?? 0;
if (empty($activities)) {
return 0;
}
$this->cache->set($cache_key, $activities, Duration::HALF_HOUR);
$this->logger->debug('Calculated median activities', ['divider' => $divider, 'median' => $activities]);
$this->logger->debug('Calculated median activities', ['divider' => $divider, 'languages' => $languages, 'median' => $activities]);
return $activities;
}
@ -417,8 +426,10 @@ class Channel extends BaseModule
return $score;
}
$limit = $this->database->count('contact-relation', ["`relation-cid` = ? AND `relation-thread-score` > ?", $cid, 0]) / $divider;
$relation = $this->database->selectToArray('contact-relation', ['relation-thread-score'], ['relation-cid' => $cid], ['order' => ['relation-thread-score' => true], 'limit' => [$limit, 1]]);
$condition = ["`relation-cid` = ? AND `relation-thread-score` > ?", $cid, 0];
$limit = $this->database->count('contact-relation', $condition) / $divider;
$relation = $this->database->selectToArray('contact-relation', ['relation-thread-score'], $condition, ['order' => ['relation-thread-score' => true], 'limit' => [$limit, 1]]);
$score = $relation[0]['relation-thread-score'] ?? 0;
if (empty($score)) {
return 0;

View File

@ -804,5 +804,9 @@ return [
// interaction_score_days (Integer)
// Number of days that are used to calculate the interaction score.
'interaction_score_days' => 30,
// sharer_interaction_days (Integer)
// Number of days of the last interaction that are used to define which sharers are used for the "sharers of sharers" channel.
'sharer_interaction_days' => 90,
],
];