]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Delayed.php
Only spool mirrored posts
[friendica.git] / src / Model / Post / Delayed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model\Post;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Core\Worker;
27 use Friendica\Database\Database;
28 use Friendica\Model\Item;
29 use Friendica\Model\Tag;
30
31 class Delayed
32 {
33         /**
34          * Insert a new delayed post
35          *
36          * @param string $delayed
37          * @param array $item
38          * @param integer $notify
39          * @param array $taglist
40          * @param array $attachments
41          * @return bool insert success
42          */ 
43         public static function add(string $delayed, array $item, int $notify = 0, array $taglist = [], array $attachments = [])
44         {
45                 if (empty($item['uri']) || empty($item['uid']) || self::exists($item['uri'], $item['uid'])) {
46                         return false;
47                 }
48
49                 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $item['uri']]);
50
51                 Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments);
52                 return DBA::insert('delayed-post', ['uri' => $item['uri'], 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE);
53         }
54
55         /**
56          * Delete a delayed post
57          *
58          * @param string $uri
59          *
60          * @return bool delete success
61          */
62         private static function delete(string $uri, int $uid)
63         {
64                 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
65         }
66
67         /**
68          * Check if an entry exists
69          *
70          * @param string $uri
71          *
72          * @return bool "true" if an entry with that URI exists
73          */
74         public static function exists(string $uri, int $uid)
75         {
76                 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
77         }
78
79         /**
80          * Publish a delayed post
81          *
82          * @param array $item
83          * @param integer $notify
84          * @param array $taglist
85          * @param array $attachments
86          * @return bool
87          */
88         public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [])
89         {
90                 $id = Item::insert($item, $notify);
91
92                 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
93
94                 // It should always contain an URI since this is needed to create a delayed post entry
95                 if (!empty($item['uri']) && self::exists($item['uri'], $item['uid'])) {
96                         $result = self::delete($item['uri'], $item['uid']);
97                         Logger::notice('Delayed post entry deleted', ['result' => $result, 'uri' => $item['uri']]);
98                 }
99
100                 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
101                         $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
102
103                         foreach ($taglist as $tag) {
104                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
105                         }
106
107                         foreach ($attachments as $attachment) {
108                                 $attachment['uri-id'] = $feeditem['uri-id'];
109                                 Media::insert($attachment);
110                         }
111                 }
112
113                 return $id;
114         }
115 }