3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Verb;
32 use Friendica\Protocol\Activity;
33 use Friendica\Util\DateTimeFormat;
39 public static function storeFromItem(array $item)
41 if (!in_array($item['network'], Protocol::FEDERATED)) {
42 Logger::debug('No federated network', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'network' => $item['network']]);
46 if ($item['gravity'] == Item::GRAVITY_PARENT) {
47 Logger::debug('Parent posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id']]);
51 if (($item['uid'] != 0) && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
52 Logger::debug('Non public comments are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
56 if (in_array($item['verb'], [Activity::FOLLOW, Activity::VIEW, Activity::READ])) {
57 Logger::debug('Technical activities are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'verb' => $item['verb']]);
61 $parent = Post::selectFirst(['created', 'author-id', 'uid', 'private', 'contact-contact-type'], ['uri-id' => $item['parent-uri-id']]);
62 if ($parent['private'] != Item::PUBLIC) {
63 Logger::debug('Non public posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $parent['uid'], 'private' => $parent['private']]);
67 if ($parent['contact-contact-type'] == Contact::TYPE_COMMUNITY) {
68 Logger::debug('Group posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'author-id' => $parent['author-id']]);
72 if ($parent['created'] < DateTimeFormat::utc('now - 1 day')) {
73 Logger::debug('Post is too old', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'created' => $parent['created']]);
78 'uri-id' => $item['parent-uri-id'],
79 'author-id' => $parent['author-id'],
80 'contact-type' => $parent['contact-contact-type'],
81 'created' => $parent['created'],
82 'comments' => DBA::count('post', ['parent-uri-id' => $item['parent-uri-id'], 'gravity' => Item::GRAVITY_COMMENT]),
83 'activities' => DBA::count('post', [
84 "`parent-uri-id` = ? AND `gravity` = ? AND NOT `vid` IN (?, ?, ?)",
85 $item['parent-uri-id'], Item::GRAVITY_ACTIVITY,
86 Verb::getID(Activity::FOLLOW), Verb::getID(Activity::VIEW), Verb::getID(Activity::READ)
89 $ret = DBA::insert('post-engagement', $engagement, Database::INSERT_UPDATE);
90 Logger::debug('Engagement stored', ['fields' => $engagement, 'ret' => $ret]);
93 public static function expire()
95 DBA::delete('post-engagement', ["`created` < ?", DateTimeFormat::utc('now - 1 day')]);
96 Logger::notice('Cleared expired engagements', ['rows' => DBA::affectedRows()]);