3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Core\Worker;
27 use Friendica\Database\Database;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Tag;
32 use Friendica\Util\DateTimeFormat;
38 const PREPARED_NO_HOOK = 2;
41 * Insert a new delayed post
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
52 public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
54 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
55 Logger::notice('No uid or already found');
59 if (empty($delayed)) {
60 $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
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);
68 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
70 $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
77 'uid' => $item['uid'],
78 'delayed' => $delayed,
82 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
83 return DBA::lastInsertId();
90 * Delete a delayed post
95 * @return bool delete success
97 private static function delete(string $uri, int $uid)
99 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
103 * Delete scheduled posts and the associated workerqueue entry
108 public static function deleteById(int $id)
110 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
111 if (empty($post['wid'])) {
115 DBA::delete('delayed-post', ['id' => $id]);
116 DBA::delete('workerqueue', ['id' => $post['wid']]);
120 * Check if an entry exists
125 * @return bool "true" if an entry with that URI exists
127 public static function exists(string $uri, int $uid)
129 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
133 * Fetch parameters for delayed posts
138 public static function getParametersForid(int $id)
140 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
141 if (empty($delayed['wid'])) {
145 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
146 if (empty($worker)) {
150 $parameters = json_decode($worker['parameter'], true);
151 if (empty($parameters)) {
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']);
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],
173 * Publish a delayed post
177 * @param array $taglist
178 * @param array $attachments
179 * @param int $preparation_mode
183 public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
185 if (!empty($attachments)) {
186 $item['attachments'] = $attachments;
189 if ($preparation_mode == self::UNPREPARED) {
190 $_SESSION['authenticated'] = true;
191 $_SESSION['uid'] = $item['uid'];
194 $_REQUEST['api_source'] = true;
195 $_REQUEST['profile_uid'] = $item['uid'];
196 $_REQUEST['title'] = $item['title'] ?? '';
198 if (!empty($item['app'])) {
199 $_REQUEST['source'] = $item['app'];
202 require_once 'mod/item.php';
203 $id = item_post(DI::app());
205 if (empty($uri) && !empty($item['extid'])) {
206 $uri = $item['extid'];
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']);
217 $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
219 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
221 if (empty($uri) && !empty($item['uri'])) {
225 if (!empty($uri) && self::exists($uri, $item['uid'])) {
226 self::delete($uri, $item['uid']);
229 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
230 $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
232 foreach ($taglist as $tag) {
233 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);