feat(vote topic): Improved functionality & implemented caching (#2063)
Some checks are pending
Continuous Integration / Nightly builds 📦 (push) Waiting to run
Continuous Integration / 🎉 Deploy (push) Waiting to run
PHPMD / Run PHPMD scanning (push) Waiting to run

* feat(vote topic): Improved functionality & implemented caching

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php

* Update thanks.php
This commit is contained in:
Roman Kelesidis 2025-07-29 22:16:46 +03:00 committed by GitHub
commit e1337ef5bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 8 deletions

View file

@ -29,6 +29,40 @@ if (!$poster_id = (int)$this->request['poster_id']) {
$this->ajax_die($lang['NO_USER_ID_SPECIFIED']);
}
$cache_lifetime = 3600;
$thanks_cache_key = 'topic_thanks_' . $topic_id;
/**
* Get thanks by topic id
*
* @param $topic_id
* @param string $thanks_cache_key
* @param int $cache_lifetime
* @return array
*/
function get_thanks_list($topic_id, string $thanks_cache_key, int $cache_lifetime)
{
if (!$cached_thanks = CACHE('bb_cache')->get($thanks_cache_key)) {
$cached_thanks = [];
$sql = DB()->fetch_rowset('SELECT u.username, u.user_rank, u.user_id, thx.* FROM ' . BB_THX . ' thx, ' . BB_USERS . " u WHERE thx.topic_id = $topic_id AND thx.user_id = u.user_id");
foreach ($sql as $row) {
$cached_thanks[$row['user_id']] = [
'user_id' => $row['user_id'],
'username' => $row['username'],
'user_rank' => $row['user_rank'],
'time' => $row['time']
];
}
if (!empty($cached_thanks)) {
CACHE('bb_cache')->set($thanks_cache_key, $cached_thanks, $cache_lifetime);
}
}
return $cached_thanks;
}
switch ($mode) {
case 'add':
if (IS_GUEST) {
@ -39,7 +73,8 @@ switch ($mode) {
$this->ajax_die($lang['LIKE_OWN_POST']);
}
if (DB()->fetch_row('SELECT topic_id FROM ' . BB_THX . " WHERE topic_id = $topic_id AND user_id = " . $userdata['user_id'])) {
$cached_thanks = get_thanks_list($topic_id, $thanks_cache_key, $cache_lifetime);
if (isset($cached_thanks[$userdata['user_id']])) {
$this->ajax_die($lang['LIKE_ALREADY']);
}
@ -47,10 +82,34 @@ switch ($mode) {
$values = "$topic_id, {$userdata['user_id']}, " . TIMENOW;
DB()->query('INSERT IGNORE INTO ' . BB_THX . " ($columns) VALUES ($values)");
$cached_thanks[$userdata['user_id']] = [
'user_id' => $userdata['user_id'],
'username' => $userdata['username'],
'user_rank' => $userdata['user_rank'],
'time' => TIMENOW
];
// Limit voters per topic
$thanks_count = DB()->fetch_row('SELECT COUNT(*) as thx FROM ' . BB_THX . " WHERE topic_id = $topic_id")['thx'];
if ($thanks_count > (int)$bb_cfg['tor_thank_limit_per_topic']) {
DB()->query('DELETE FROM ' . BB_THX . " WHERE topic_id = $topic_id ORDER BY time ASC LIMIT 1");
$tor_thank_limit_per_topic = (int)$bb_cfg['tor_thank_limit_per_topic'];
if ($tor_thank_limit_per_topic > 0) {
$thanks_count = count($cached_thanks);
if ($thanks_count > $tor_thank_limit_per_topic) {
$oldest_user_id = null;
foreach ($cached_thanks as $user_id => $thanks_data) {
// First value
$oldest_user_id = $thanks_data['user_id'];
break;
}
if ($oldest_user_id) {
DB()->query('DELETE FROM ' . BB_THX . " WHERE topic_id = $topic_id AND user_id = $oldest_user_id LIMIT 1");
unset($cached_thanks[$oldest_user_id]);
}
}
}
if (!empty($cached_thanks)) {
CACHE('bb_cache')->set($thanks_cache_key, $cached_thanks, $cache_lifetime);
}
break;
case 'get':
@ -58,9 +117,9 @@ switch ($mode) {
$this->ajax_die($lang['NEED_TO_LOGIN_FIRST']);
}
$cached_thanks = get_thanks_list($topic_id, $thanks_cache_key, $cache_lifetime);
$user_list = [];
$sql = DB()->fetch_rowset('SELECT u.username, u.user_rank, u.user_id, t.* FROM ' . BB_THX . ' t, ' . BB_USERS . " u WHERE t.topic_id = $topic_id AND t.user_id = u.user_id");
foreach ($sql as $row) {
foreach ($cached_thanks as $row) {
$user_list[] = '<b>' . profile_url($row) . ' <i>(' . bb_date($row['time']) . ')</i></b>';
}

View file

@ -569,7 +569,7 @@ $bb_cfg['limit_max_search_results'] = false; // Limit for number of search resul
// Posting
$bb_cfg['prevent_multiposting'] = true; // TODO: replace "reply" with "edit last msg" if user (not admin or mod) is last topic poster
$bb_cfg['max_smilies'] = 25; //Max number of smilies in a post (0 - unlimited)
$bb_cfg['max_smilies'] = 25; // Max number of smilies in a post (0 - unlimited)
$bb_cfg['max_symbols_post'] = 5000; // TODO: Max number of symbols in a post (0 - unlimited)
// PM
@ -593,7 +593,7 @@ $bb_cfg['user_not_active_days_keep'] = 180; // After how many days should I dele
// Vote for torrents
$bb_cfg['tor_thank'] = true;
$bb_cfg['tor_thanks_list_guests'] = true; // Show voters to guests
$bb_cfg['tor_thank_limit_per_topic'] = 50;
$bb_cfg['tor_thank_limit_per_topic'] = 50; // Set "0" to disable limit
// Groups
$bb_cfg['group_members_per_page'] = 50; // How many groups will be displayed in a page