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;
37 * Insert a new delayed post
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
48 public static function add(string $uri, array $item, int $notify = 0, bool $unprepared = false, string $delayed = '', array $taglist = [], array $attachments = [])
50 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
51 Logger::notice('No uid or already found');
55 if (empty($delayed)) {
56 $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
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);
62 $next_publish = strtotime($delayed);
65 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
67 $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri);
72 DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
76 'uid' => $item['uid'],
77 'delayed' => $delayed,
81 if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) {
82 return DBA::lastInsertId();
89 * Delete a delayed post
94 * @return bool delete success
96 private static function delete(string $uri, int $uid)
98 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
102 * Delete scheduled posts and the associated workerqueue entry
107 public static function deleteById(int $id)
109 $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]);
110 if (empty($post['wid'])) {
114 DBA::delete('delayed-post', ['id' => $id]);
115 DBA::delete('workerqueue', ['id' => $post['wid']]);
119 * Check if an entry exists
124 * @return bool "true" if an entry with that URI exists
126 public static function exists(string $uri, int $uid)
128 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
132 * Fetch parameters for delayed posts
137 public static function getParametersForid(int $id)
139 $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]);
140 if (empty($delayed['wid'])) {
144 $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
145 if (empty($worker)) {
149 $parameters = json_decode($worker['parameter'], true);
150 if (empty($parameters)) {
154 // Make sure to only publish the attachments in the dedicated array field
155 if (empty($parameters[3]) && !empty($parameters[0]['attachments'])) {
156 $parameters[3] = $parameters[0]['attachments'];
157 unset($parameters[0]['attachments']);
161 'parameters' => $delayed,
162 'item' => $parameters[0],
163 'notify' => $parameters[1],
164 'taglist' => $parameters[2],
165 'attachments' => $parameters[3],
166 'unprepared' => $parameters[4],
167 'uri' => $parameters[5],
172 * Publish a delayed post
175 * @param integer $notify
176 * @param array $taglist
177 * @param array $attachments
178 * @param bool $unprepared
182 public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '')
184 if (!empty($attachments)) {
185 $item['attachments'] = $attachments;
189 $_SESSION['authenticated'] = true;
190 $_SESSION['uid'] = $item['uid'];
193 $_REQUEST['api_source'] = true;
194 $_REQUEST['profile_uid'] = $item['uid'];
195 $_REQUEST['title'] = $item['title'] ?? '';
197 if (!empty($item['app'])) {
198 $_REQUEST['source'] = $item['app'];
201 require_once 'mod/item.php';
202 $id = item_post(DI::app());
204 if (empty($uri) && !empty($item['extid'])) {
205 $uri = $item['extid'];
208 Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'uri' => $uri]);
209 if (self::exists($uri, $item['uid'])) {
210 self::delete($uri, $item['uid']);
215 $id = Item::insert($item, $notify);
217 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
219 if (empty($uri) && !empty($item['uri'])) {
223 if (!empty($uri) && self::exists($uri, $item['uid'])) {
224 self::delete($uri, $item['uid']);
227 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
228 $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
230 foreach ($taglist as $tag) {
231 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);