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