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