]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Engagement.php
Replace split attachment code with PostMedia objects
[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\Tag;
33 use Friendica\Model\Verb;
34 use Friendica\Protocol\Activity;
35 use Friendica\Protocol\Relay;
36 use Friendica\Util\DateTimeFormat;
37
38 // Channel
39
40 class Engagement
41 {
42         /**
43          * Store engagement data from an item array
44          *
45          * @param array $item
46          * @return void
47          */
48         public static function storeFromItem(array $item)
49         {
50                 if (in_array($item['verb'], [Activity::FOLLOW, Activity::VIEW, Activity::READ])) {
51                         Logger::debug('Technical activities are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'verb' => $item['verb']]);
52                         return;
53                 }
54
55                 $parent = Post::selectFirst(['created', 'owner-id', 'uid', 'private', 'contact-contact-type', 'language'], ['uri-id' => $item['parent-uri-id']]);
56
57                 if ($parent['created'] < DateTimeFormat::utc('now - ' . DI::config()->get('channel', 'engagement_hours') . ' hour')) {
58                         Logger::debug('Post is too old', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'created' => $parent['created']]);
59                         return;
60                 }
61
62                 $store = ($item['gravity'] != Item::GRAVITY_PARENT);
63
64                 if (!$store) {
65                         $store = Contact::hasFollowers($parent['owner-id']);
66                 }
67
68                 if (!$store) {
69                         $tagList = Relay::getSubscribedTags();
70                         foreach (array_column(Tag::getByURIId($item['parent-uri-id'], [Tag::HASHTAG]), 'name') as $tag) {
71                                 if (in_array($tag, $tagList)) {
72                                         $store = true;
73                                         break;
74                                 }
75                         }
76                 }
77
78                 $mediatype = self::getMediaType($item['parent-uri-id']);
79
80                 if (!$store) {
81                         $mediatype = !empty($mediatype);
82                 }
83
84                 $engagement = [
85                         'uri-id'       => $item['parent-uri-id'],
86                         'owner-id'     => $parent['owner-id'],
87                         'contact-type' => $parent['contact-contact-type'],
88                         'media-type'   => $mediatype,
89                         'language'     => $parent['language'],
90                         'created'      => $parent['created'],
91                         'restricted'   => !in_array($item['network'], Protocol::FEDERATED) || ($parent['private'] != Item::PUBLIC),
92                         'comments'     => DBA::count('post', ['parent-uri-id' => $item['parent-uri-id'], 'gravity' => Item::GRAVITY_COMMENT]),
93                         'activities'   => DBA::count('post', [
94                                 "`parent-uri-id` = ? AND `gravity` = ? AND NOT `vid` IN (?, ?, ?)",
95                                 $item['parent-uri-id'], Item::GRAVITY_ACTIVITY,
96                                 Verb::getID(Activity::FOLLOW), Verb::getID(Activity::VIEW), Verb::getID(Activity::READ)
97                         ])
98                 ];
99                 if (!$store && ($engagement['comments'] == 0) && ($engagement['activities'] == 0)) {
100                         Logger::debug('No media, follower, subscribed tags, comments or activities. Engagement not stored', ['fields' => $engagement]);
101                         return;
102                 }
103                 $ret = DBA::insert('post-engagement', $engagement, Database::INSERT_UPDATE);
104                 Logger::debug('Engagement stored', ['fields' => $engagement, 'ret' => $ret]);
105         }
106
107         private static function getMediaType(int $uri_id): int
108         {
109                 $media = Post\Media::getByURIId($uri_id);
110                 $type  = 0;
111                 foreach ($media as $entry) {
112                         if ($entry['type'] == Post\Media::IMAGE) {
113                                 $type = $type | 1;
114                         } elseif ($entry['type'] == Post\Media::VIDEO) {
115                                 $type = $type | 2;
116                         } elseif ($entry['type'] == Post\Media::AUDIO) {
117                                 $type = $type | 4;
118                         }
119                 }
120                 return $type;
121         }
122
123         /**
124          * Expire old engagement data
125          *
126          * @return void
127          */
128         public static function expire()
129         {
130                 DBA::delete('post-engagement', ["`created` < ?", DateTimeFormat::utc('now - ' . DI::config()->get('channel', 'engagement_hours') . ' hour')]);
131                 Logger::notice('Cleared expired engagements', ['rows' => DBA::affectedRows()]);
132         }
133 }