]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Delayed.php
Fix uddate issues and improve speed when displaying contact posts
[friendica.git] / src / Model / Post / Delayed.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\Database\DBA;
26 use Friendica\Core\Worker;
27 use Friendica\Database\Database;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Tag;
32 use Friendica\Util\DateTimeFormat;
33
34 class Delayed
35 {
36         /**
37          * The content of the post is posted as is. Connector settings are using the default settings.
38          * This is used for automated scheduled posts via feeds or from the API.
39          */
40         const PREPARED = 0;
41         /**
42          * Like PREPARED, but additionally the connector settings can differ.
43          * This is used when manually publishing scheduled posts.
44          */
45         const PREPARED_NO_HOOK = 2;
46
47         /**
48          * Insert a new delayed post
49          *
50          * @param string $uri
51          * @param array  $item
52          * @param int    $notify
53          * @param int    $preparation_mode
54          * @param string $delayed
55          * @param array  $taglist
56          * @param array  $attachments
57          * @return int   ID of the created delayed post entry
58          */
59         public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
60         {
61                 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
62                         Logger::notice('No uid or already found');
63                         return 0;
64                 }
65
66                 if (empty($delayed)) {
67                         $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
68
69                         $last_publish = DI::pConfig()->get($item['uid'], 'system', 'last_publish', 0, true);
70                         $next_publish = max($last_publish + (60 * $min_posting), time());
71                         $delayed = date(DateTimeFormat::MYSQL, $next_publish);
72                         DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
73                 }
74
75                 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
76
77                 $wid = Worker::add(['priority' => Worker::PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
78                 if (!$wid) {
79                         return 0;
80                 }
81
82                 $delayed_post = [
83                         'uri'     => $uri,
84                         'uid'     => $item['uid'],
85                         'delayed' => $delayed,
86                         'wid'     => $wid,
87                 ];
88
89                 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
90                         return DBA::lastInsertId();
91                 } else {
92                         return 0;
93                 }
94         }
95
96         /**
97          * Delete a delayed post
98          *
99          * @param string $uri
100          * @param int    $uid
101          *
102          * @return bool delete success
103          */
104         private static function delete(string $uri, int $uid)
105         {
106                 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
107         }
108
109         /**
110          * Delete scheduled posts and the associated workerqueue entry
111          *
112          * @param integer $id
113          * @return void
114          */
115         public static function deleteById(int $id)
116         {
117                 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
118                 if (empty($post['wid'])) {
119                         return;
120                 }
121
122                 DBA::delete('delayed-post', ['id' => $id]);
123                 DBA::delete('workerqueue', ['id' => $post['wid']]);
124         }
125
126         /**
127          * Check if an entry exists
128          *
129          * @param string $uri
130          * @param int    $uid
131          *
132          * @return bool "true" if an entry with that URI exists
133          */
134         public static function exists(string $uri, int $uid)
135         {
136                 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
137         }
138
139         /**
140          * Fetch parameters for delayed posts
141          *
142          * @param integer $id
143          * @return array
144          */
145         public static function getParametersForid(int $id)
146         {
147                 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
148                 if (empty($delayed['wid'])) {
149                         return [];
150                 }
151
152                 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
153                 if (empty($worker)) {
154                         return [];
155                 }
156
157                 $parameters = json_decode($worker['parameter'], true);
158                 if (empty($parameters)) {
159                         return [];
160                 }
161
162                 // Make sure to only publish the attachments in the dedicated array field
163                 if (empty($parameters[3]) && !empty($parameters[0]['attachments'])) {
164                         $parameters[3] = $parameters[0]['attachments'];
165                         unset($parameters[0]['attachments']);
166                 }
167
168                 return [
169                         'parameters' => $delayed,
170                         'item' => $parameters[0],
171                         'notify' => $parameters[1],
172                         'taglist' => $parameters[2],
173                         'attachments' => $parameters[3],
174                         'unprepared' => $parameters[4],
175                         'uri' => $parameters[5],
176                 ];
177         }
178
179         /**
180          * Publish a delayed post
181          *
182          * @param array  $item
183          * @param int    $notify
184          * @param array  $taglist
185          * @param array  $attachments
186          * @param int    $preparation_mode
187          * @param string $uri
188          * @return bool
189          */
190         public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
191         {
192                 if (!empty($attachments)) {
193                         $item['attachments'] = $attachments;
194                 }
195
196                 $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
197
198                 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id'] ?? 'N/A']);
199
200                 if (empty($uri) && !empty($item['uri'])) {
201                         $uri = $item['uri'];
202                 }
203
204                 if (!empty($uri) && self::exists($uri, $item['uid'])) {
205                         self::delete($uri, $item['uid']);
206                 }
207
208                 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
209                         $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
210
211                         foreach ($taglist as $tag) {
212                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
213                         }
214                 }
215
216                 return $id;
217         }
218 }