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