3 * @copyright Copyright (C) 2010-2022, 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;
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.
42 * Like PREPARED, but additionally the connector settings can differ.
43 * This is used when manually publishing scheduled posts.
45 const PREPARED_NO_HOOK = 2;
48 * Insert a new delayed post
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
59 public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
61 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
62 Logger::notice('No uid or already found');
66 if (empty($delayed)) {
67 $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
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);
75 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
77 $wid = Worker::add(['priority' => Worker::PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
84 'uid' => $item['uid'],
85 'delayed' => $delayed,
89 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
90 return DBA::lastInsertId();
97 * Delete a delayed post
102 * @return bool delete success
104 private static function delete(string $uri, int $uid)
106 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
110 * Delete scheduled posts and the associated workerqueue entry
115 public static function deleteById(int $id)
117 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
118 if (empty($post['wid'])) {
122 DBA::delete('delayed-post', ['id' => $id]);
123 DBA::delete('workerqueue', ['id' => $post['wid']]);
127 * Check if an entry exists
132 * @return bool "true" if an entry with that URI exists
134 public static function exists(string $uri, int $uid)
136 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
140 * Fetch parameters for delayed posts
145 public static function getParametersForid(int $id)
147 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
148 if (empty($delayed['wid'])) {
152 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
153 if (empty($worker)) {
157 $parameters = json_decode($worker['parameter'], true);
158 if (empty($parameters)) {
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']);
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],
180 * Publish a delayed post
184 * @param array $taglist
185 * @param array $attachments
186 * @param int $preparation_mode
190 public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
192 if (!empty($attachments)) {
193 $item['attachments'] = $attachments;
196 $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
198 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id'] ?? 'N/A']);
200 if (empty($uri) && !empty($item['uri'])) {
204 if (!empty($uri) && self::exists($uri, $item['uid'])) {
205 self::delete($uri, $item['uid']);
208 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
209 $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
211 foreach ($taglist as $tag) {
212 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);