From 2aa302c570ca75969ad0ed4697d4f38cf98741ab Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Dec 2020 22:11:29 +0000 Subject: [PATCH] New class for handling delayed posts --- src/Model/Post/Delayed.php | 112 ++++++++++++++++++++++++++++++++++ src/Protocol/Feed.php | 30 ++++++--- src/Worker/DelayedPublish.php | 17 +----- static/dbstructure.config.php | 15 ++++- 4 files changed, 149 insertions(+), 25 deletions(-) create mode 100644 src/Model/Post/Delayed.php diff --git a/src/Model/Post/Delayed.php b/src/Model/Post/Delayed.php new file mode 100644 index 0000000000..ccb678d466 --- /dev/null +++ b/src/Model/Post/Delayed.php @@ -0,0 +1,112 @@ +. + * + */ + +namespace Friendica\Model\Post; + +use Friendica\Core\Logger; +use Friendica\Database\DBA; +use Friendica\Core\Worker; +use Friendica\Database\Database; +use Friendica\Model\Item; +use Friendica\Model\Tag; + +class Delayed +{ + /** + * Insert a new delayed post + * + * @param string $uri + * @param integer $uid + * @param string $delayed + * @param array $item + * @param integer $notify + * @param array $taglist + * @param array $attachments + * @return bool insert success + */ + public static function add(string $uri, int $uid, string $delayed, array $item, int $notify = 0, array $taglist = [], array $attachments = []) + { + if (self::exists($uri)) { + return false; + } + + Logger::notice('Adding post for delayed publishing', ['uid' => $uid, 'delayed' => $delayed, 'uri' => $uri]); + + Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments); + return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $uid, 'delayed' => $delayed], Database::INSERT_IGNORE); + } + + /** + * Delete a delayed post + * + * @param string $uri + * + * @return bool delete success + */ + private static function delete(string $uri) + { + return DBA::delete('delayed-post', ['uri' => $uri]); + } + + /** + * Undocumented function + * + * @param string $uri + * + * @return bool "true" if an entry with that URI exists + */ + public static function exists(string $uri) + { + return DBA::exists('delayed-post', ['uri' => $uri]); + } + + /** + * Publish a delayed post + * + * @param array $item + * @param integer $notify + * @param array $taglist + * @param array $attachments + * @return bool + */ + public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = []) + { + $id = Item::insert($item, $notify); + + Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]); + + if (!empty($id) && (!empty($taglist) || !empty($attachments))) { + $feeditem = Item::selectFirst(['uri-id', 'uri'], ['id' => $id]); + self::delete($feeditem['uri']); + + 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; + } +} diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index 6390fab658..f874b4b026 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -606,7 +606,7 @@ class Feed // Additionally we have to avoid conflicts with identical URI between imported feeds and these items. if ($notify) { $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname()); - unset($item['uri']); + $item['uri'] = Item::newURI($item['uid'], $item['guid']); unset($item['thr-parent']); unset($item['parent-uri']); @@ -614,16 +614,21 @@ class Feed $notify = PRIORITY_MEDIUM; } - $postings[] = ['item' => $item, 'notify' => $notify, - 'taglist' => $taglist, 'attachments' => $attachments]; + if (!Post\Delayed::exists($item["uri"])) { + $postings[] = ['item' => $item, 'notify' => $notify, + 'taglist' => $taglist, 'attachments' => $attachments]; + } else { + Logger::info('Post already exists in the delayed posts queue', ['uri' => $item["uri"]]); + } } if (!empty($postings)) { + $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0); $total = count($postings); if ($total > 1) { // Posts shouldn't be delayed more than a day $interval = min(1440, self::getPollInterval($contact)); - $delay = round(($interval * 60) / $total); + $delay = max(round(($interval * 60) / $total), 60 * $min_posting); Logger::notice('Got posting delay', ['delay' => $delay, 'interval' => $interval, 'items' => $total, 'cid' => $contact['id'], 'url' => $contact['url']]); } else { $delay = 0; @@ -633,15 +638,22 @@ class Feed foreach ($postings as $posting) { if ($delay > 0) { - $publish_at = DateTimeFormat::utc('now + ' . $post_delay . ' second'); - Logger::notice('Got publishing date', ['delay' => $delay, 'publish_at' => $publish_at, 'cid' => $contact['id'], 'url' => $contact['url']]); + $publish_time = time() + $post_delay; + Logger::notice('Got publishing date', ['delay' => $delay, 'cid' => $contact['id'], 'url' => $contact['url']]); $post_delay += $delay; } else { - $publish_at = DBA::NULL_DATETIME; + $publish_time = time(); } - Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $publish_at], - 'DelayedPublish', $posting['item'], $posting['notify'], $posting['taglist'], $posting['attachments']); + $last_publish = DI::pConfig()->get($posting['item']['uid'], 'system', 'last_publish', 0, true); + $next_publish = max($last_publish + (60 * $min_posting), time()); + if ($publish_time < $next_publish) { + $publish_time = $next_publish; + } + $publish_at = date(DateTimeFormat::MYSQL, $publish_time); + + Post\Delayed::add($item['uri'], $item['uid'], $publish_at, $posting['item'], $posting['notify'], $posting['taglist'], $posting['attachments']); + DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish); } } diff --git a/src/Worker/DelayedPublish.php b/src/Worker/DelayedPublish.php index 3dad540481..beffb22e30 100644 --- a/src/Worker/DelayedPublish.php +++ b/src/Worker/DelayedPublish.php @@ -39,20 +39,7 @@ class DelayedPublish */ public static function execute(array $item, int $notify = 0, array $taglist = [], array $attachments = []) { - $id = Item::insert($item, $notify); - - Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]); - - if (!empty($id) && (!empty($taglist) || !empty($attachments))) { - $feeditem = Item::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']; - Post\Media::insert($attachment); - } - } - + $id = Post\Delayed::publish($item, $notify, $taglist, $attachments); + Logger::notice('Post published', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]); } } diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 9a125cc15b..65b4fecf08 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1381); + define('DB_UPDATE_VERSION', 1382); } return [ @@ -527,6 +527,19 @@ return [ "received" => ["received"], ] ], + "delayed-post" => [ + "comment" => "Posts that are about to be posted at a later time", + "fields" => [ + "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"], + "uri" => ["type" => "varchar(255)", "comment" => "URI of the post that will be posted later"], + "uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Owner User id"], + "delayed" => ["type" => "datetime", "comment" => "delay time"], + ], + "indexes" => [ + "PRIMARY" => ["id"], + "url" => ["UNIQUE", "url"], + ] + ], "diaspora-interaction" => [ "comment" => "Signed Diaspora Interaction", "fields" => [