2020-12-01 17:11:29 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-12-01 17:11:29 -05:00
|
|
|
*
|
|
|
|
* @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;
|
2020-12-13 07:47:49 -05:00
|
|
|
use Friendica\DI;
|
2020-12-01 17:11:29 -05:00
|
|
|
use Friendica\Model\Item;
|
2021-01-15 23:13:22 -05:00
|
|
|
use Friendica\Model\Post;
|
2020-12-01 17:11:29 -05:00
|
|
|
use Friendica\Model\Tag;
|
2020-12-13 07:47:49 -05:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-12-01 17:11:29 -05:00
|
|
|
|
|
|
|
class Delayed
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Insert a new delayed post
|
|
|
|
*
|
2020-12-13 07:47:49 -05:00
|
|
|
* @param string $uri
|
|
|
|
* @param array $item
|
2020-12-01 17:11:29 -05:00
|
|
|
* @param integer $notify
|
2020-12-13 07:47:49 -05:00
|
|
|
* @param bool $unprepared
|
|
|
|
* @param string $delayed
|
|
|
|
* @param array $taglist
|
|
|
|
* @param array $attachments
|
2020-12-01 17:11:29 -05:00
|
|
|
* @return bool insert success
|
|
|
|
*/
|
2020-12-13 07:47:49 -05:00
|
|
|
public static function add(string $uri, array $item, int $notify = 0, bool $unprepared = false, string $delayed = '', array $taglist = [], array $attachments = [])
|
2020-12-01 17:11:29 -05:00
|
|
|
{
|
2020-12-13 07:47:49 -05:00
|
|
|
if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
|
2020-12-13 11:38:12 -05:00
|
|
|
Logger::notice('No uid or already found');
|
2020-12-13 07:47:49 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($delayed)) {
|
|
|
|
$min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
|
|
|
|
|
|
|
|
$last_publish = DI::pConfig()->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
|
|
|
|
|
2020-12-13 13:17:50 -05:00
|
|
|
if (!Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri)) {
|
2020-12-01 17:11:29 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-13 07:47:49 -05:00
|
|
|
DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
|
2020-12-01 17:11:29 -05:00
|
|
|
|
2020-12-13 07:47:49 -05:00
|
|
|
return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE);
|
2020-12-01 17:11:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a delayed post
|
|
|
|
*
|
|
|
|
* @param string $uri
|
2020-12-13 07:47:49 -05:00
|
|
|
* @param int $uid
|
2020-12-01 17:11:29 -05:00
|
|
|
*
|
|
|
|
* @return bool delete success
|
|
|
|
*/
|
2020-12-01 19:34:10 -05:00
|
|
|
private static function delete(string $uri, int $uid)
|
2020-12-01 17:11:29 -05:00
|
|
|
{
|
2020-12-01 19:34:10 -05:00
|
|
|
return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
|
2020-12-01 17:11:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-01 17:54:49 -05:00
|
|
|
* Check if an entry exists
|
2020-12-01 17:11:29 -05:00
|
|
|
*
|
|
|
|
* @param string $uri
|
2020-12-13 07:47:49 -05:00
|
|
|
* @param int $uid
|
2020-12-01 17:11:29 -05:00
|
|
|
*
|
|
|
|
* @return bool "true" if an entry with that URI exists
|
|
|
|
*/
|
2020-12-01 19:34:10 -05:00
|
|
|
public static function exists(string $uri, int $uid)
|
2020-12-01 17:11:29 -05:00
|
|
|
{
|
2020-12-01 19:34:10 -05:00
|
|
|
return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
|
2020-12-01 17:11:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish a delayed post
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @param integer $notify
|
|
|
|
* @param array $taglist
|
|
|
|
* @param array $attachments
|
2020-12-13 07:47:49 -05:00
|
|
|
* @param bool $unprepared
|
2020-12-13 13:17:50 -05:00
|
|
|
* @param string $uri
|
2020-12-01 17:11:29 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2020-12-13 13:17:50 -05:00
|
|
|
public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '')
|
2020-12-01 17:11:29 -05:00
|
|
|
{
|
2021-05-01 11:48:19 -04:00
|
|
|
if (!empty($attachments)) {
|
|
|
|
$item['attachments'] = $attachments;
|
|
|
|
}
|
|
|
|
|
2020-12-13 07:47:49 -05:00
|
|
|
if ($unprepared) {
|
|
|
|
$_SESSION['authenticated'] = true;
|
|
|
|
$_SESSION['uid'] = $item['uid'];
|
|
|
|
|
|
|
|
$_REQUEST = $item;
|
|
|
|
$_REQUEST['api_source'] = true;
|
|
|
|
$_REQUEST['profile_uid'] = $item['uid'];
|
|
|
|
$_REQUEST['title'] = $item['title'] ?? '';
|
|
|
|
|
|
|
|
if (!empty($item['app'])) {
|
|
|
|
$_REQUEST['source'] = $item['app'];
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once 'mod/item.php';
|
|
|
|
$id = item_post(DI::app());
|
|
|
|
|
2020-12-13 13:17:50 -05:00
|
|
|
if (empty($uri) && !empty($item['extid'])) {
|
|
|
|
$uri = $item['extid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'uri' => $uri]);
|
|
|
|
if (self::exists($uri, $item['uid'])) {
|
|
|
|
self::delete($uri, $item['uid']);
|
|
|
|
}
|
|
|
|
|
2020-12-13 12:22:50 -05:00
|
|
|
return $id;
|
2020-12-13 07:47:49 -05:00
|
|
|
}
|
2020-12-01 17:11:29 -05:00
|
|
|
$id = Item::insert($item, $notify);
|
|
|
|
|
|
|
|
Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
|
|
|
|
|
2020-12-13 13:17:50 -05:00
|
|
|
if (empty($uri) && !empty($item['uri'])) {
|
|
|
|
$uri = $item['uri'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($uri) && self::exists($uri, $item['uid'])) {
|
|
|
|
self::delete($uri, $item['uid']);
|
2020-12-01 18:04:04 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 17:11:29 -05:00
|
|
|
if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
|
2021-01-15 23:13:22 -05:00
|
|
|
$feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
|
2020-12-01 17:11:29 -05:00
|
|
|
|
|
|
|
foreach ($taglist as $tag) {
|
|
|
|
Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
}
|