2018-12-07 00:52:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file src/Model/ItemDeliveryData.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
|
|
|
use Friendica\Database\DBA;
|
2019-02-18 19:56:41 -05:00
|
|
|
use \BadMethodCallException;
|
2018-12-07 00:52:14 -05:00
|
|
|
|
|
|
|
class ItemDeliveryData
|
|
|
|
{
|
2018-12-08 15:22:54 -05:00
|
|
|
const LEGACY_FIELD_LIST = [
|
2018-12-07 00:52:14 -05:00
|
|
|
// Legacy fields moved from item table
|
|
|
|
'postopts',
|
|
|
|
'inform',
|
2018-12-08 15:22:54 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const FIELD_LIST = [
|
2018-12-07 00:52:14 -05:00
|
|
|
// New delivery fields with virtual field name in item fields
|
|
|
|
'queue_count' => 'delivery_queue_count',
|
|
|
|
'queue_done' => 'delivery_queue_done',
|
|
|
|
];
|
|
|
|
|
2019-06-28 05:03:58 -04:00
|
|
|
const ACTIVITYPUB = 1;
|
|
|
|
const DFRN = 2;
|
|
|
|
const LEGACY_DFRN = 3;
|
|
|
|
const DIASPORA = 4;
|
|
|
|
const OSTATUS = 5;
|
|
|
|
|
2018-12-07 00:52:14 -05:00
|
|
|
/**
|
|
|
|
* Extract delivery data from the provided item fields
|
|
|
|
*
|
|
|
|
* @param array $fields
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function extractFields(array &$fields)
|
|
|
|
{
|
|
|
|
$delivery_data = [];
|
2019-01-24 16:06:53 -05:00
|
|
|
foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
|
2018-12-07 00:52:14 -05:00
|
|
|
if (is_int($key) && isset($fields[$field])) {
|
|
|
|
// Legacy field moved from item table
|
|
|
|
$delivery_data[$field] = $fields[$field];
|
|
|
|
$fields[$field] = null;
|
|
|
|
} elseif (isset($fields[$field])) {
|
|
|
|
// New delivery field with virtual field name in item fields
|
|
|
|
$delivery_data[$key] = $fields[$field];
|
|
|
|
unset($fields[$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $delivery_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments the queue_done for the given item ID.
|
|
|
|
*
|
|
|
|
* Avoids racing condition between multiple delivery threads.
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
2019-06-28 05:03:58 -04:00
|
|
|
* @param integer $protocol
|
2018-12-07 00:52:14 -05:00
|
|
|
* @return bool
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-12-07 00:52:14 -05:00
|
|
|
*/
|
2019-06-28 05:03:58 -04:00
|
|
|
public static function incrementQueueDone($item_id, $protocol = 0)
|
2018-12-07 00:52:14 -05:00
|
|
|
{
|
2019-06-28 05:03:58 -04:00
|
|
|
$sql = '';
|
|
|
|
|
|
|
|
switch ($protocol) {
|
|
|
|
case self::ACTIVITYPUB:
|
|
|
|
$sql = ", `activitypub` = `activitypub` + 1";
|
|
|
|
break;
|
|
|
|
case self::DFRN:
|
|
|
|
$sql = ", `dfrn` = `dfrn` + 1";
|
|
|
|
break;
|
|
|
|
case self::LEGACY_DFRN:
|
|
|
|
$sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
|
|
|
|
break;
|
|
|
|
case self::DIASPORA:
|
|
|
|
$sql = ", `diaspora` = `diaspora` + 1";
|
|
|
|
break;
|
|
|
|
case self::OSTATUS:
|
|
|
|
$sql = ", `ostatus` = `ostatus` + 1";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
|
2018-12-07 00:52:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new item delivery data entry
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @param array $fields
|
|
|
|
* @return bool
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-12-07 00:52:14 -05:00
|
|
|
*/
|
|
|
|
public static function insert($item_id, array $fields)
|
|
|
|
{
|
|
|
|
if (empty($item_id)) {
|
2019-02-18 19:56:41 -05:00
|
|
|
throw new BadMethodCallException('Empty item_id');
|
2018-12-07 00:52:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$fields['iid'] = $item_id;
|
|
|
|
|
|
|
|
return DBA::insert('item-delivery-data', $fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update/Insert item delivery data
|
|
|
|
*
|
|
|
|
* If you want to update queue_done, please use incrementQueueDone instead.
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @param array $fields
|
|
|
|
* @return bool
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-12-07 00:52:14 -05:00
|
|
|
*/
|
|
|
|
public static function update($item_id, array $fields)
|
|
|
|
{
|
|
|
|
if (empty($item_id)) {
|
2019-02-18 19:56:41 -05:00
|
|
|
throw new BadMethodCallException('Empty item_id');
|
2018-12-07 00:52:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
// Nothing to do, update successful
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete item delivery data
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @return bool
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-12-07 00:52:14 -05:00
|
|
|
*/
|
|
|
|
public static function delete($item_id)
|
|
|
|
{
|
|
|
|
if (empty($item_id)) {
|
2019-02-18 19:56:41 -05:00
|
|
|
throw new BadMethodCallException('Empty item_id');
|
2018-12-07 00:52:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::delete('item-delivery-data', ['iid' => $item_id]);
|
|
|
|
}
|
|
|
|
}
|