]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Delayed.php
24b9989a4b2fca4db4a515e77ec51b048dc3dbd0
[friendica.git] / src / Model / Post / Delayed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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         const PREPARED = 0;
37         const UNPREPARED = 1;
38         const PREPARED_NO_HOOK = 2;
39
40         /**
41          * Insert a new delayed post
42          *
43          * @param string $uri
44          * @param array  $item
45          * @param int    $notify
46          * @param int    $preparation_mode
47          * @param string $delayed
48          * @param array  $taglist
49          * @param array  $attachments
50          * @return int   ID of the created delayed post entry
51          */
52         public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
53         {
54                 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
55                         Logger::notice('No uid or already found');
56                         return 0;
57                 }
58
59                 if (empty($delayed)) {
60                         $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
61
62                         $last_publish = DI::pConfig()->get($item['uid'], 'system', 'last_publish', 0, true);
63                         $next_publish = max($last_publish + (60 * $min_posting), time());
64                         $delayed = date(DateTimeFormat::MYSQL, $next_publish);
65                         DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
66                 }
67
68                 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
69
70                 $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
71                 if (!$wid) {
72                         return 0;
73                 }
74
75                 $delayed_post = [
76                         'uri'     => $uri,
77                         'uid'     => $item['uid'],
78                         'delayed' => $delayed,
79                         'wid'     => $wid,
80                 ];
81
82                 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
83                         return DBA::lastInsertId();
84                 } else {
85                         return 0;
86                 }
87         }
88
89         /**
90          * Delete a delayed post
91          *
92          * @param string $uri
93          * @param int    $uid
94          *
95          * @return bool delete success
96          */
97         private static function delete(string $uri, int $uid)
98         {
99                 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
100         }
101
102         /**
103          * Delete scheduled posts and the associated workerqueue entry
104          *
105          * @param integer $id
106          * @return void
107          */
108         public static function deleteById(int $id)
109         {
110                 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
111                 if (empty($post['wid'])) {
112                         return;
113                 }
114
115                 DBA::delete('delayed-post', ['id' => $id]);
116                 DBA::delete('workerqueue', ['id' => $post['wid']]);
117         }
118
119         /**
120          * Check if an entry exists
121          *
122          * @param string $uri
123          * @param int    $uid
124          *
125          * @return bool "true" if an entry with that URI exists
126          */
127         public static function exists(string $uri, int $uid)
128         {
129                 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
130         }
131
132         /**
133          * Fetch parameters for delayed posts
134          *
135          * @param integer $id
136          * @return array
137          */
138         public static function getParametersForid(int $id)
139         {
140                 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
141                 if (empty($delayed['wid'])) {
142                         return [];
143                 }
144
145                 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
146                 if (empty($worker)) {
147                         return [];
148                 }
149
150                 $parameters = json_decode($worker['parameter'], true);
151                 if (empty($parameters)) {
152                         return [];
153                 }
154
155                 // Make sure to only publish the attachments in the dedicated array field
156                 if (empty($parameters[3]) && !empty($parameters[0]['attachments'])) {
157                         $parameters[3] = $parameters[0]['attachments'];
158                         unset($parameters[0]['attachments']);
159                 }
160
161                 return [
162                         'parameters' => $delayed,
163                         'item' => $parameters[0],
164                         'notify' => $parameters[1],
165                         'taglist' => $parameters[2],
166                         'attachments' => $parameters[3],
167                         'unprepared' => $parameters[4],
168                         'uri' => $parameters[5],
169                 ];
170         }
171
172         /**
173          * Publish a delayed post
174          *
175          * @param array  $item
176          * @param int    $notify
177          * @param array  $taglist
178          * @param array  $attachments
179          * @param int    $preparation_mode
180          * @param string $uri
181          * @return bool
182          */
183         public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
184         {
185                 if (!empty($attachments)) {
186                         $item['attachments'] = $attachments;
187                 }
188
189                 if ($preparation_mode == self::UNPREPARED) {
190                         $_SESSION['authenticated'] = true;
191                         $_SESSION['uid'] = $item['uid'];
192
193                         $_REQUEST = $item;
194                         $_REQUEST['api_source'] = true;
195                         $_REQUEST['profile_uid'] = $item['uid'];
196                         $_REQUEST['title'] = $item['title'] ?? '';
197
198                         if (!empty($item['app'])) {
199                                 $_REQUEST['source'] = $item['app'];
200                         }
201
202                         require_once 'mod/item.php';
203                         $id = item_post(DI::app());
204
205                         if (empty($uri) && !empty($item['extid'])) {
206                                 $uri = $item['extid'];
207                         }
208
209                         Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'uri' => $uri]);
210                         if (self::exists($uri, $item['uid'])) {
211                                 self::delete($uri, $item['uid']);
212                         }
213
214                         return $id;
215                 }
216
217                 $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
218
219                 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
220
221                 if (empty($uri) && !empty($item['uri'])) {
222                         $uri = $item['uri'];
223                 }
224
225                 if (!empty($uri) && self::exists($uri, $item['uid'])) {
226                         self::delete($uri, $item['uid']);
227                 }
228
229                 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
230                         $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
231
232                         foreach ($taglist as $tag) {
233                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
234                         }
235                 }
236
237                 return $id;
238         }
239 }