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 * 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.
48 * Like PREPARED, but additionally the connector settings can differ.
49 * This is used when manually publishing scheduled posts.
51 const PREPARED_NO_HOOK = 2;
54 * Insert a new delayed post
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
65 public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
67 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
68 Logger::notice('No uid or already found');
72 if (empty($delayed)) {
73 $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
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);
81 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
83 $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
90 'uid' => $item['uid'],
91 'delayed' => $delayed,
95 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
96 return DBA::lastInsertId();
103 * Delete a delayed post
108 * @return bool delete success
110 private static function delete(string $uri, int $uid)
112 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
116 * Delete scheduled posts and the associated workerqueue entry
121 public static function deleteById(int $id)
123 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
124 if (empty($post['wid'])) {
128 DBA::delete('delayed-post', ['id' => $id]);
129 DBA::delete('workerqueue', ['id' => $post['wid']]);
133 * Check if an entry exists
138 * @return bool "true" if an entry with that URI exists
140 public static function exists(string $uri, int $uid)
142 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
146 * Fetch parameters for delayed posts
151 public static function getParametersForid(int $id)
153 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
154 if (empty($delayed['wid'])) {
158 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
159 if (empty($worker)) {
163 $parameters = json_decode($worker['parameter'], true);
164 if (empty($parameters)) {
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']);
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],
186 * Publish a delayed post
190 * @param array $taglist
191 * @param array $attachments
192 * @param int $preparation_mode
196 public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
198 if (!empty($attachments)) {
199 $item['attachments'] = $attachments;
202 if ($preparation_mode == self::UNPREPARED) {
203 $_SESSION['authenticated'] = true;
204 $_SESSION['uid'] = $item['uid'];
207 $_REQUEST['api_source'] = true;
208 $_REQUEST['profile_uid'] = $item['uid'];
209 $_REQUEST['title'] = $item['title'] ?? '';
211 if (!empty($item['app'])) {
212 $_REQUEST['source'] = $item['app'];
215 require_once 'mod/item.php';
216 $id = item_post(DI::app());
218 if (empty($uri) && !empty($item['extid'])) {
219 $uri = $item['extid'];
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']);
230 $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
232 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
234 if (empty($uri) && !empty($item['uri'])) {
238 if (!empty($uri) && self::exists($uri, $item['uid'])) {
239 self::delete($uri, $item['uid']);
242 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
243 $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
245 foreach ($taglist as $tag) {
246 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);