Use the post-counts table to display content (#13781)

* Use the post-counts table to display content

* Use verb instead of vid

* Use verb

* Update counter on delete
This commit is contained in:
Michael Vogel 2023-12-31 12:50:07 +01:00 committed by GitHub
parent 0c68a53e1e
commit 8fc96477e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 250 additions and 126 deletions

View file

@ -338,7 +338,7 @@ class Item
// locate item to be deleted
$fields = [
'id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri-id', 'origin',
'deleted', 'resource-id', 'event-id',
'thr-parent-id', 'deleted', 'resource-id', 'event-id', 'vid', 'body',
'verb', 'object-type', 'object', 'target', 'contact-id', 'psid', 'gravity'
];
$item = Post::selectFirst($fields, ['id' => $item_id]);
@ -418,6 +418,10 @@ class Item
DI::notify()->deleteForItem($item['uri-id']);
DI::notification()->deleteForItem($item['uri-id']);
if (in_array($item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
Post\Counts::update($item['thr-parent-id'], $item['parent-uri-id'], $item['vid'], $item['verb'], $item['body']);
}
Logger::info('Item has been marked for deletion.', ['id' => $item_id]);
return true;
@ -1427,16 +1431,15 @@ class Item
Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
}
// Fill the cache with the rendered content.
if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) && ($posted_item['uid'] == 0)) {
self::updateDisplayCache($posted_item['uri-id']);
}
if (in_array($posted_item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT]) && ($posted_item['uid'] == 0)) {
Post\Counts::update($posted_item['thr-parent-id'], $posted_item['parent-uri-id'], $posted_item['vid'], $posted_item['verb'], $posted_item['body']);
}
if ($inserted) {
// Fill the cache with the rendered content.
if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT])) {
self::updateDisplayCache($posted_item['uri-id']);
}
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);
}

View file

@ -38,11 +38,20 @@ class Counts
*/
public static function update(int $uri_id, int $parent_uri_id, int $vid, string $verb, string $body = null)
{
$condition = ['thr-parent-id' => $uri_id, 'vid' => $vid];
if (!in_array($verb, [Activity::POST, Activity::LIKE, Activity::DISLIKE,
Activity::ATTEND, Activity::ATTENDMAYBE, Activity::ATTENDNO,
Activity::EMOJIREACT, Activity::ANNOUNCE, Activity::VIEW, Activity::READ])) {
return true;
}
$condition = ['thr-parent-id' => $uri_id, 'vid' => $vid, 'deleted' => false];
if ($body == $verb) {
$condition['body'] = null;
$body = '';
} elseif ($verb == Activity::POST) {
$condition['gravity'] = Item::GRAVITY_COMMENT;
$body = '';
} elseif (($verb != Activity::POST) && (mb_strlen($body) == 1) && Smilies::isEmojiPost($body)) {
$condition['body'] = $body;
} else {
@ -58,6 +67,7 @@ class Counts
];
if ($fields['count'] == 0) {
DBA::delete('post-counts', ['uri-id' => $uri_id, 'vid' => $vid, 'reaction' => $body]);
return true;
}
@ -76,14 +86,41 @@ class Counts
}
/**
* Retrieves counts of the given uri-id
* Retrieves counts of the given condition
*
* @param int $uriId
* @param array $condition
*
* @return array
*/
public static function getByURIId(int $uriId): array
public static function get(array $condition): array
{
return DBA::selectToArray('post-counts', [], ['uri-id' => $uriId]);
$counts = [];
$activity_emoji = [
Activity::LIKE => '👍',
Activity::DISLIKE => '👎',
Activity::ATTEND => '✔️',
Activity::ATTENDMAYBE => '❓',
Activity::ATTENDNO => '❌',
Activity::ANNOUNCE => '♻',
Activity::VIEW => '📺',
Activity::READ => '📖',
];
$verbs = array_merge(array_keys($activity_emoji), [Activity::EMOJIREACT, Activity::POST]);
$condition = DBA::mergeConditions($condition, ['verb' => $verbs]);
$countquery = DBA::select('post-counts-view', [], $condition);
while ($count = DBA::fetch($countquery)) {
if (!empty($count['reaction'])) {
$count['verb'] = Activity::EMOJIREACT;
$count['vid'] = Verb::getID($count['verb']);
} elseif (!empty($activity_emoji[$count['verb']])) {
$count['reaction'] = $activity_emoji[$count['verb']];
}
$counts[] = $count;
}
DBA::close($counts);
return $counts;
}
}

View file

@ -26,6 +26,8 @@ use Friendica\Database\DBA;
class Verb
{
static $verbs = [];
/**
* Insert a verb record and return its id
*
@ -40,14 +42,23 @@ class Verb
return 0;
}
$id = array_search($verb, self::$verbs);
if ($id !== false) {
return $id;
}
$verb_record = DBA::selectFirst('verb', ['id'], ['name' => $verb]);
if (DBA::isResult($verb_record)) {
self::$verbs[$verb_record['id']] = $verb;
return $verb_record['id'];
}
DBA::insert('verb', ['name' => $verb], Database::INSERT_IGNORE);
return DBA::lastInsertId();
$id = DBA::lastInsertId();
self::$verbs[$id] = $verb;
return $id;
}
/**
@ -62,11 +73,17 @@ class Verb
return '';
}
if (!empty(self::$verbs[$id])) {
return self::$verbs[$id];
}
$verb_record = DBA::selectFirst('verb', ['name'], ['id' => $id]);
if (!DBA::isResult($verb_record)) {
return '';
}
self::$verbs[$id] = $verb_record['name'];
return $verb_record['name'];
}
}