3 * @copyright Copyright (C) 2020, Friendica
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;
24 use Friendica\Database\DBA;
25 use \BadMethodCallException;
27 class ItemDeliveryData
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(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::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 item ID.
76 * Avoids racing condition between multiple delivery threads.
78 * @param integer $item_id
79 * @param integer $protocol
83 public static function incrementQueueDone($item_id, $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 `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
109 * Increments the queue_failed for the given item ID.
111 * Avoids racing condition between multiple delivery threads.
113 * @param integer $item_id
117 public static function incrementQueueFailed($item_id)
119 return DBA::e('UPDATE `item-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `iid` = ?', $item_id);
123 * Increments the queue_count for the given item ID.
125 * @param integer $item_id
126 * @param integer $increment
130 public static function incrementQueueCount(int $item_id, int $increment = 1)
132 return DBA::e('UPDATE `item-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `iid` = ?', $increment, $item_id);
136 * Insert a new item delivery data entry
138 * @param integer $item_id
139 * @param array $fields
143 public static function insert($item_id, array $fields)
145 if (empty($item_id)) {
146 throw new BadMethodCallException('Empty item_id');
149 $fields['iid'] = $item_id;
151 return DBA::insert('item-delivery-data', $fields);
155 * Update/Insert item delivery data
157 * If you want to update queue_done, please use incrementQueueDone instead.
159 * @param integer $item_id
160 * @param array $fields
164 public static function update($item_id, array $fields)
166 if (empty($item_id)) {
167 throw new BadMethodCallException('Empty item_id');
170 if (empty($fields)) {
171 // Nothing to do, update successful
175 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
179 * Delete item delivery data
181 * @param integer $item_id
185 public static function delete($item_id)
187 if (empty($item_id)) {
188 throw new BadMethodCallException('Empty item_id');
191 return DBA::delete('item-delivery-data', ['iid' => $item_id]);