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