--- /dev/null
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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;
+ }
+}
// 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']);
$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;
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();
+ }
+
+ $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);
- Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $publish_at],
- 'DelayedPublish', $posting['item'], $posting['notify'], $posting['taglist'], $posting['attachments']);
+ 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);
}
}
*/
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']]);
}
}
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
- define('DB_UPDATE_VERSION', 1381);
+ define('DB_UPDATE_VERSION', 1382);
}
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" => [