friendica/src/Model/Post/Delayed.php

114 lines
3.2 KiB
PHP
Raw Normal View History

2020-12-01 17:11:29 -05:00
<?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 $delayed
* @param array $item
* @param integer $notify
* @param array $taglist
* @param array $attachments
* @return bool insert success
*/
2020-12-01 19:05:03 -05:00
public static function add(string $delayed, array $item, int $notify = 0, array $taglist = [], array $attachments = [])
2020-12-01 17:11:29 -05:00
{
2020-12-01 19:34:10 -05:00
if (empty($item['uri']) || empty($item['uid']) || self::exists($item['uri'], $item['uid'])) {
2020-12-01 17:11:29 -05:00
return false;
}
2020-12-01 19:05:03 -05:00
Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $item['uri']]);
2020-12-01 17:11:29 -05:00
Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments);
2020-12-01 19:05:03 -05:00
return DBA::insert('delayed-post', ['uri' => $item['uri'], 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE);
2020-12-01 17:11:29 -05:00
}
/**
* Delete a delayed post
*
* @param string $uri
*
* @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
*
* @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
* @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']]);
2020-12-02 00:07:44 -05:00
if (!empty($item['uri']) && self::exists($item['uri'], $item['uid'])) {
2020-12-02 03:36:23 -05:00
self::delete($item['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))) {
2020-12-01 18:04:04 -05:00
$feeditem = Item::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);
}
foreach ($attachments as $attachment) {
$attachment['uri-id'] = $feeditem['uri-id'];
Media::insert($attachment);
}
}
return $id;
}
}