]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemDeliveryData.php
f007537f99c02d687754ae50af8dfaf24996b779
[friendica.git] / src / Model / ItemDeliveryData.php
1 <?php
2
3 /**
4  * @file src/Model/ItemDeliveryData.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Database\DBA;
10 use \BadMethodCallException;
11
12 class ItemDeliveryData
13 {
14         const LEGACY_FIELD_LIST = [
15                 // Legacy fields moved from item table
16                 'postopts',
17                 'inform',
18         ];
19
20         const FIELD_LIST = [
21                 // New delivery fields with virtual field name in item fields
22                 'queue_count' => 'delivery_queue_count',
23                 'queue_done'  => 'delivery_queue_done',
24                 'queue_failed'  => 'delivery_queue_failed',
25         ];
26
27         const ACTIVITYPUB = 1;
28         const DFRN = 2;
29         const LEGACY_DFRN = 3;
30         const DIASPORA = 4;
31         const OSTATUS = 5;
32
33         /**
34          * Extract delivery data from the provided item fields
35          *
36          * @param array $fields
37          * @return array
38          */
39         public static function extractFields(array &$fields)
40         {
41                 $delivery_data = [];
42                 foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
43                         if (is_int($key) && isset($fields[$field])) {
44                                 // Legacy field moved from item table
45                                 $delivery_data[$field] = $fields[$field];
46                                 $fields[$field] = null;
47                         } elseif (isset($fields[$field])) {
48                                 // New delivery field with virtual field name in item fields
49                                 $delivery_data[$key] = $fields[$field];
50                                 unset($fields[$field]);
51                         }
52                 }
53
54                 return $delivery_data;
55         }
56
57         /**
58          * Increments the queue_done for the given item ID.
59          *
60          * Avoids racing condition between multiple delivery threads.
61          *
62          * @param integer $item_id
63          * @param integer $protocol
64          * @return bool
65          * @throws \Exception
66          */
67         public static function incrementQueueDone($item_id, $protocol = 0)
68         {
69                 $sql = '';
70
71                 switch ($protocol) {
72                         case self::ACTIVITYPUB:
73                                 $sql = ", `activitypub` = `activitypub` + 1";
74                                 break;
75                         case self::DFRN:
76                                 $sql = ", `dfrn` = `dfrn` + 1";
77                                 break;
78                         case self::LEGACY_DFRN:
79                                 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
80                                 break;
81                         case self::DIASPORA:
82                                 $sql = ", `diaspora` = `diaspora` + 1";
83                                 break;
84                         case self::OSTATUS:
85                                 $sql = ", `ostatus` = `ostatus` + 1";
86                                 break;
87                 }
88
89                 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
90         }
91
92         /**
93          * Increments the queue_failed for the given item ID.
94          *
95          * Avoids racing condition between multiple delivery threads.
96          *
97          * @param integer $item_id
98          * @return bool
99          * @throws \Exception
100          */
101         public static function incrementQueueFailed($item_id)
102         {
103                 return DBA::e('UPDATE `item-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `iid` = ?', $item_id);
104         }
105
106         /**
107          * Insert a new item delivery data entry
108          *
109          * @param integer $item_id
110          * @param array   $fields
111          * @return bool
112          * @throws \Exception
113          */
114         public static function insert($item_id, array $fields)
115         {
116                 if (empty($item_id)) {
117                         throw new BadMethodCallException('Empty item_id');
118                 }
119
120                 $fields['iid'] = $item_id;
121
122                 return DBA::insert('item-delivery-data', $fields);
123         }
124
125         /**
126          * Update/Insert item delivery data
127          *
128          * If you want to update queue_done, please use incrementQueueDone instead.
129          *
130          * @param integer $item_id
131          * @param array   $fields
132          * @return bool
133          * @throws \Exception
134          */
135         public static function update($item_id, array $fields)
136         {
137                 if (empty($item_id)) {
138                         throw new BadMethodCallException('Empty item_id');
139                 }
140
141                 if (empty($fields)) {
142                         // Nothing to do, update successful
143                         return true;
144                 }
145
146                 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
147         }
148
149         /**
150          * Delete item delivery data
151          *
152          * @param integer $item_id
153          * @return bool
154          * @throws \Exception
155          */
156         public static function delete($item_id)
157         {
158                 if (empty($item_id)) {
159                         throw new BadMethodCallException('Empty item_id');
160                 }
161
162                 return DBA::delete('item-delivery-data', ['iid' => $item_id]);
163         }
164 }