]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/DeliveryData.php
We now store the receivers as well
[friendica.git] / src / Model / Post / DeliveryData.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Database\DBA;
25 use \BadMethodCallException;
26
27 class DeliveryData
28 {
29         const LEGACY_FIELD_LIST = [
30                 // Legacy fields moved from item table
31                 'postopts',
32                 'inform',
33         ];
34
35         const FIELD_LIST = [
36                 // New delivery fields with virtual field name in item fields
37                 'queue_count' => 'delivery_queue_count',
38                 'queue_done'  => 'delivery_queue_done',
39                 'queue_failed'  => 'delivery_queue_failed',
40         ];
41
42         const ACTIVITYPUB = 1;
43         const DFRN = 2;
44         const LEGACY_DFRN = 3; // @deprecated since version 2021.09
45         const DIASPORA = 4;
46         const OSTATUS = 5;
47         const MAIL = 6;
48
49         /**
50          * Extract delivery data from the provided item fields
51          *
52          * @param array $fields
53          * @return array
54          */
55         public static function extractFields(array &$fields)
56         {
57                 $delivery_data = [];
58                 foreach (array_merge(self::FIELD_LIST, self::LEGACY_FIELD_LIST) as $key => $field) {
59                         if (is_int($key) && isset($fields[$field])) {
60                                 // Legacy field moved from item table
61                                 $delivery_data[$field] = $fields[$field];
62                                 $fields[$field] = null;
63                         } elseif (isset($fields[$field])) {
64                                 // New delivery field with virtual field name in item fields
65                                 $delivery_data[$key] = $fields[$field];
66                                 unset($fields[$field]);
67                         }
68                 }
69
70                 return $delivery_data;
71         }
72
73         /**
74          * Increments the queue_done for the given URI ID.
75          *
76          * Avoids racing condition between multiple delivery threads.
77          *
78          * @param integer $uri_id
79          * @param integer $protocol
80          * @return bool
81          * @throws \Exception
82          */
83         public static function incrementQueueDone(int $uri_id, int $protocol = 0)
84         {
85                 $sql = '';
86
87                 switch ($protocol) {
88                         case self::ACTIVITYPUB:
89                                 $sql = ", `activitypub` = `activitypub` + 1";
90                                 break;
91                         case self::DFRN:
92                                 $sql = ", `dfrn` = `dfrn` + 1";
93                                 break;
94                         case self::LEGACY_DFRN:
95                                 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
96                                 break;
97                         case self::DIASPORA:
98                                 $sql = ", `diaspora` = `diaspora` + 1";
99                                 break;
100                         case self::OSTATUS:
101                                 $sql = ", `ostatus` = `ostatus` + 1";
102                                 break;
103                 }
104
105                 return DBA::e('UPDATE `post-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `uri-id` = ?', $uri_id);
106         }
107
108         /**
109          * Increments the queue_failed for the given URI ID.
110          *
111          * Avoids racing condition between multiple delivery threads.
112          *
113          * @param integer $uri_id
114          * @return bool
115          * @throws \Exception
116          */
117         public static function incrementQueueFailed(int $uri_id)
118         {
119                 return DBA::e('UPDATE `post-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `uri-id` = ?', $uri_id);
120         }
121
122         /**
123          * Increments the queue_count for the given URI ID.
124          *
125          * @param integer $uri_id
126          * @param integer $increment
127          * @return bool
128          * @throws \Exception
129          */
130         public static function incrementQueueCount(int $uri_id, int $increment = 1)
131         {
132                 return DBA::e('UPDATE `post-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `uri-id` = ?', $increment, $uri_id);
133         }
134
135         /**
136          * Insert a new URI delivery data entry
137          *
138          * @param integer $uri_id
139          * @param array   $fields
140          * @return bool
141          * @throws \Exception
142          */
143         public static function insert(int $uri_id, array $fields)
144         {
145                 if (empty($uri_id)) {
146                         throw new BadMethodCallException('Empty URI_id');
147                 }
148
149                 $fields['uri-id'] = $uri_id;
150
151                 return DBA::replace('post-delivery-data', $fields);
152         }
153
154         /**
155          * Update/Insert URI delivery data
156          *
157          * If you want to update queue_done, please use incrementQueueDone instead.
158          *
159          * @param integer $uri_id
160          * @param array   $fields
161          * @return bool
162          * @throws \Exception
163          */
164         public static function update(int $uri_id, array $fields)
165         {
166                 if (empty($uri_id)) {
167                         throw new BadMethodCallException('Empty URI_id');
168                 }
169
170                 if (empty($fields)) {
171                         // Nothing to do, update successful
172                         return true;
173                 }
174
175                 return DBA::update('post-delivery-data', $fields, ['uri-id' => $uri_id], true);
176         }
177
178         /**
179          * Delete URI delivery data
180          *
181          * @param integer $uri_id
182          * @return bool
183          * @throws \Exception
184          */
185         public static function delete(int $uri_id)
186         {
187                 if (empty($uri_id)) {
188                         throw new BadMethodCallException('Empty URI_id');
189                 }
190
191                 return DBA::delete('post-delivery-data', ['uri-id' => $uri_id]);
192         }
193 }