3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Database\DBA;
25 use \BadMethodCallException;
29 const LEGACY_FIELD_LIST = [
30 // Legacy fields moved from item table
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',
42 const ACTIVITYPUB = 1;
44 const LEGACY_DFRN = 3;
50 * Extract delivery data from the provided item fields
52 * @param array $fields
55 public static function extractFields(array &$fields)
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]);
70 return $delivery_data;
74 * Increments the queue_done for the given URI ID.
76 * Avoids racing condition between multiple delivery threads.
78 * @param integer $uri_id
79 * @param integer $protocol
83 public static function incrementQueueDone(int $uri_id, int $protocol = 0)
88 case self::ACTIVITYPUB:
89 $sql = ", `activitypub` = `activitypub` + 1";
92 $sql = ", `dfrn` = `dfrn` + 1";
94 case self::LEGACY_DFRN:
95 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
98 $sql = ", `diaspora` = `diaspora` + 1";
101 $sql = ", `ostatus` = `ostatus` + 1";
105 return DBA::e('UPDATE `post-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `uri-id` = ?', $uri_id);
109 * Increments the queue_failed for the given URI ID.
111 * Avoids racing condition between multiple delivery threads.
113 * @param integer $uri_id
117 public static function incrementQueueFailed(int $uri_id)
119 return DBA::e('UPDATE `post-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `uri-id` = ?', $uri_id);
123 * Increments the queue_count for the given URI ID.
125 * @param integer $uri_id
126 * @param integer $increment
130 public static function incrementQueueCount(int $uri_id, int $increment = 1)
132 return DBA::e('UPDATE `post-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `uri-id` = ?', $increment, $uri_id);
136 * Insert a new URI delivery data entry
138 * @param integer $uri_id
139 * @param array $fields
143 public static function insert(int $uri_id, array $fields)
145 if (empty($uri_id)) {
146 throw new BadMethodCallException('Empty URI_id');
149 $fields['uri-id'] = $uri_id;
151 return DBA::replace('post-delivery-data', $fields);
155 * Update/Insert URI delivery data
157 * If you want to update queue_done, please use incrementQueueDone instead.
159 * @param integer $uri_id
160 * @param array $fields
164 public static function update(int $uri_id, array $fields)
166 if (empty($uri_id)) {
167 throw new BadMethodCallException('Empty URI_id');
170 if (empty($fields)) {
171 // Nothing to do, update successful
175 return DBA::update('post-delivery-data', $fields, ['uri-id' => $uri_id], true);
179 * Delete URI delivery data
181 * @param integer $uri_id
185 public static function delete(int $uri_id)
187 if (empty($uri_id)) {
188 throw new BadMethodCallException('Empty URI_id');
191 return DBA::delete('post-delivery-data', ['uri-id' => $uri_id]);