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