]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Delayed.php
Delay unprepared 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\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Tag;
31 use Friendica\Util\DateTimeFormat;
32
33 class Delayed
34 {
35         /**
36          * Insert a new delayed post
37          *
38          * @param string  $uri
39          * @param array   $item
40          * @param integer $notify
41          * @param bool    $unprepared
42          * @param string  $delayed
43          * @param array   $taglist
44          * @param array   $attachments
45          * @return bool insert success
46          */ 
47         public static function add(string $uri, array $item, int $notify = 0, bool $unprepared = false, string $delayed = '', array $taglist = [], array $attachments = [])
48         {
49                 if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
50                         return false;
51                 }
52
53                 if (empty($delayed)) {
54                         $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
55
56                         $last_publish = DI::pConfig()->get($item['uid'], 'system', 'last_publish', 0, true);
57                         $next_publish = max($last_publish + (60 * $min_posting), time());
58                         $delayed = date(DateTimeFormat::MYSQL, $next_publish);
59                 } else {
60                         $next_publish = strtotime($delayed);
61                 }
62
63                 Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
64
65                 if (!Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared)) {
66                         return false;
67                 }
68
69                 DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
70
71                 return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE);
72         }
73
74         /**
75          * Delete a delayed post
76          *
77          * @param string $uri
78          * @param int    $uid
79          *
80          * @return bool delete success
81          */
82         private static function delete(string $uri, int $uid)
83         {
84                 return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
85         }
86
87         /**
88          * Check if an entry exists
89          *
90          * @param string $uri
91          * @param int    $uid
92          *
93          * @return bool "true" if an entry with that URI exists
94          */
95         public static function exists(string $uri, int $uid)
96         {
97                 return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
98         }
99
100         /**
101          * Publish a delayed post
102          *
103          * @param array $item
104          * @param integer $notify
105          * @param array $taglist
106          * @param array $attachments
107          * @param bool  $unprepared
108          * @return bool
109          */
110         public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false)
111         {
112                 if ($unprepared) {
113                         $_SESSION['authenticated'] = true;
114                         $_SESSION['uid'] = $item['uid'];
115
116                         $_REQUEST = $item;
117                         $_REQUEST['api_source'] = true;
118                         $_REQUEST['profile_uid'] = $item['uid'];
119                         $_REQUEST['title'] = $item['title'] ?? '';
120
121                         if (!empty($item['app'])) {
122                                 $_REQUEST['source'] = $item['app'];
123                         }
124
125                         require_once 'mod/item.php';
126                         $id = item_post(DI::app());
127
128                         Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'extid' => $item['extid']]);
129                         return;
130                 }
131                 $id = Item::insert($item, $notify);
132
133                 Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
134
135                 if (!empty($item['uri']) && self::exists($item['uri'], $item['uid'])) {
136                         self::delete($item['uri'], $item['uid']);
137                 }
138
139                 if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
140                         $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
141
142                         foreach ($taglist as $tag) {
143                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
144                         }
145
146                         foreach ($attachments as $attachment) {
147                                 $attachment['uri-id'] = $feeditem['uri-id'];
148                                 Media::insert($attachment);
149                         }
150                 }
151
152                 return $id;
153         }
154 }