]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Engagement.php
8c9ce1537a17f07815ccd6e8b7e313bcd871a1f0
[friendica.git] / src / Model / Post / Engagement.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Post;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Protocol\Activity;
34 use Friendica\Util\DateTimeFormat;
35
36 // Channel
37
38 class Engagement
39 {
40         public static function storeFromItem(array $item)
41         {
42                 if (!in_array($item['network'], Protocol::FEDERATED)) {
43                         Logger::debug('No federated network', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'network' => $item['network']]);
44                         return;
45                 }
46
47                 if ($item['gravity'] == Item::GRAVITY_PARENT) {
48                         Logger::debug('Parent posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id']]);
49                         return;
50                 }
51
52                 if (($item['uid'] != 0) && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
53                         Logger::debug('Non public comments are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
54                         return;
55                 }
56
57                 if (in_array($item['verb'], [Activity::FOLLOW, Activity::VIEW, Activity::READ])) {
58                         Logger::debug('Technical activities are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'verb' => $item['verb']]);
59                         return;
60                 }
61
62                 $parent = Post::selectFirst(['created', 'author-id', 'uid', 'private', 'contact-contact-type'], ['uri-id' => $item['parent-uri-id']]);
63                 if ($parent['private'] != Item::PUBLIC) {
64                         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']]);
65                         return;
66                 }
67
68                 if ($parent['contact-contact-type'] == Contact::TYPE_COMMUNITY) {
69                         Logger::debug('Group posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'author-id' => $parent['author-id']]);
70                         return;
71                 }
72
73                 if ($parent['created'] < DateTimeFormat::utc('now - ' . DI::config()->get('channel', 'engagement_hours') . ' hour')) {
74                         Logger::debug('Post is too old', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'created' => $parent['created']]);
75                         return;
76                 }
77
78                 $engagement = [
79                         'uri-id'       => $item['parent-uri-id'],
80                         'author-id'    => $parent['author-id'],
81                         'contact-type' => $parent['contact-contact-type'],
82                         'created'      => $parent['created'],
83                         'comments'     => DBA::count('post', ['parent-uri-id' => $item['parent-uri-id'], 'gravity' => Item::GRAVITY_COMMENT]),
84                         'activities'   => DBA::count('post', [
85                                 "`parent-uri-id` = ? AND `gravity` = ? AND NOT `vid` IN (?, ?, ?)",
86                                 $item['parent-uri-id'], Item::GRAVITY_ACTIVITY,
87                                 Verb::getID(Activity::FOLLOW), Verb::getID(Activity::VIEW), Verb::getID(Activity::READ)
88                         ])
89                 ];
90                 if (($engagement['comments'] == 0) && ($engagement['activities'] == 0)) {
91                         Logger::debug('No comments nor activities. Engagement not stored', ['fields' => $engagement]);
92                         return;
93                 }
94                 $ret = DBA::insert('post-engagement', $engagement, Database::INSERT_UPDATE);
95                 Logger::debug('Engagement stored', ['fields' => $engagement, 'ret' => $ret]);
96         }
97
98         public static function expire()
99         {
100                 DBA::delete('post-engagement', ["`created` < ?", DateTimeFormat::utc('now - ' . DI::config()->get('channel', 'engagement_hours') . ' hour')]);
101                 Logger::notice('Cleared expired engagements', ['rows' => DBA::affectedRows()]);
102         }
103 }