X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FPost%2FDelayed.php;h=0f47264c538f65caf8cbe3b270ebd13ddf92407e;hb=2108be7e07792a069ef0650126ac22d470a4b7fc;hp=0e6d8b921ad4b4b65ae334e5122a7ce306394442;hpb=e4228c6218db70d600641430f4895f76cc049c5b;p=friendica.git diff --git a/src/Model/Post/Delayed.php b/src/Model/Post/Delayed.php index 0e6d8b921a..0f47264c53 100644 --- a/src/Model/Post/Delayed.php +++ b/src/Model/Post/Delayed.php @@ -1,6 +1,6 @@ get($item['uid'], 'system', 'last_publish', 0, true); $next_publish = max($last_publish + (60 * $min_posting), time()); $delayed = date(DateTimeFormat::MYSQL, $next_publish); - } else { - $next_publish = strtotime($delayed); + DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish); } Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]); - if (!Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri)) { - return false; + $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri); + if (!$wid) { + return 0; } - DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish); + $delayed_post = [ + 'uri' => $uri, + 'uid' => $item['uid'], + 'delayed' => $delayed, + 'wid' => $wid, + ]; - return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE); + if (DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE)) { + return DBA::lastInsertId(); + } else { + return 0; + } } /** @@ -85,6 +112,23 @@ class Delayed return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]); } + /** + * Delete scheduled posts and the associated workerqueue entry + * + * @param integer $id + * @return void + */ + public static function deleteById(int $id) + { + $post = DBA::selectFirst('delayed-post', ['wid'], ['id' => $id]); + if (empty($post['wid'])) { + return; + } + + DBA::delete('delayed-post', ['id' => $id]); + DBA::delete('workerqueue', ['id' => $post['wid']]); + } + /** * Check if an entry exists * @@ -98,20 +142,64 @@ class Delayed return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]); } + /** + * Fetch parameters for delayed posts + * + * @param integer $id + * @return array + */ + public static function getParametersForid(int $id) + { + $delayed = DBA::selectFirst('delayed-post', ['id', 'uid', 'wid', 'delayed'], ['id' => $id]); + if (empty($delayed['wid'])) { + return []; + } + + $worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']); + if (empty($worker)) { + return []; + } + + $parameters = json_decode($worker['parameter'], true); + if (empty($parameters)) { + return []; + } + + // Make sure to only publish the attachments in the dedicated array field + if (empty($parameters[3]) && !empty($parameters[0]['attachments'])) { + $parameters[3] = $parameters[0]['attachments']; + unset($parameters[0]['attachments']); + } + + return [ + 'parameters' => $delayed, + 'item' => $parameters[0], + 'notify' => $parameters[1], + 'taglist' => $parameters[2], + 'attachments' => $parameters[3], + 'unprepared' => $parameters[4], + 'uri' => $parameters[5], + ]; + } + /** * Publish a delayed post * - * @param array $item - * @param integer $notify - * @param array $taglist - * @param array $attachments - * @param bool $unprepared + * @param array $item + * @param int $notify + * @param array $taglist + * @param array $attachments + * @param int $preparation_mode * @param string $uri * @return bool */ - public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '') + public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '') { - if ($unprepared) { + if (!empty($attachments)) { + $item['attachments'] = $attachments; + } + + if ($preparation_mode == self::UNPREPARED) { $_SESSION['authenticated'] = true; $_SESSION['uid'] = $item['uid']; @@ -135,10 +223,11 @@ class Delayed if (self::exists($uri, $item['uid'])) { self::delete($uri, $item['uid']); } - + return $id; } - $id = Item::insert($item, $notify); + + $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED); Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]); @@ -151,16 +240,11 @@ class Delayed } if (!empty($id) && (!empty($taglist) || !empty($attachments))) { - $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]); + $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]); foreach ($taglist as $tag) { Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag); } - - foreach ($attachments as $attachment) { - $attachment['uri-id'] = $feeditem['uri-id']; - Media::insert($attachment); - } } return $id;