]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Delayed.php
357a3e8aa960126483e37ed413d602c5de245e95
[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 $uri
37          * @param integer $uid
38          * @param string $delayed
39          * @param array $item
40          * @param integer $notify
41          * @param array $taglist
42          * @param array $attachments
43          * @return bool insert success
44          */ 
45         public static function add(string $uri, int $uid, string $delayed, array $item, int $notify = 0, array $taglist = [], array $attachments = [])
46         {
47                 if (self::exists($uri)) {
48                         return false;
49                 }
50
51                 Logger::notice('Adding post for delayed publishing', ['uid' => $uid, 'delayed' => $delayed, 'uri' => $uri]);
52
53                 Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments);
54                 return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $uid, 'delayed' => $delayed], Database::INSERT_IGNORE);
55         }
56
57         /**
58          * Delete a delayed post
59          *
60          * @param string $uri
61          *
62          * @return bool delete success
63          */
64         private static function delete(string $uri)
65         {
66                 return DBA::delete('delayed-post', ['uri' => $uri]);
67         }
68
69         /**
70          * Check if an entry exists
71          *
72          * @param string $uri
73          *
74          * @return bool "true" if an entry with that URI exists
75          */
76         public static function exists(string $uri)
77         {
78                 return DBA::exists('delayed-post', ['uri' => $uri]);
79         }
80
81         /**
82          * Publish a delayed post
83          *
84          * @param array $item
85          * @param integer $notify
86          * @param array $taglist
87          * @param array $attachments
88          * @return bool
89          */
90         public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [])
91         {
92                 $id = Item::insert($item, $notify);
93
94                 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
95
96                 // It should always contain an URI since this is needed to create a delayed post entry
97                 if (!empty($item['uri'])) {
98                         $result = self::delete($item['uri']);
99                         Logger::notice('Delayed post entry deleted', ['result' => $result, 'uri' => $item['uri']]);
100                 }
101
102                 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
103                         $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
104
105                         foreach ($taglist as $tag) {
106                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
107                         }
108
109                         foreach ($attachments as $attachment) {
110                                 $attachment['uri-id'] = $feeditem['uri-id'];
111                                 Media::insert($attachment);
112                         }
113                 }
114
115                 return $id;
116         }
117 }