2018-12-07 05:52:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-02-09 14:45:36 +00:00
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-12-07 05:52:14 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
namespace Friendica\Model\Post;
|
2018-12-07 05:52:14 +00:00
|
|
|
|
|
|
|
use Friendica\Database\DBA;
|
2019-02-19 00:56:41 +00:00
|
|
|
use \BadMethodCallException;
|
2018-12-07 05:52:14 +00:00
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
class DeliveryData
|
2018-12-07 05:52:14 +00:00
|
|
|
{
|
2018-12-08 20:22:54 +00:00
|
|
|
const LEGACY_FIELD_LIST = [
|
2018-12-07 05:52:14 +00:00
|
|
|
// Legacy fields moved from item table
|
|
|
|
'postopts',
|
|
|
|
'inform',
|
2018-12-08 20:22:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const FIELD_LIST = [
|
2018-12-07 05:52:14 +00:00
|
|
|
// New delivery fields with virtual field name in item fields
|
|
|
|
'queue_count' => 'delivery_queue_count',
|
|
|
|
'queue_done' => 'delivery_queue_done',
|
2019-08-20 07:39:13 +00:00
|
|
|
'queue_failed' => 'delivery_queue_failed',
|
2018-12-07 05:52:14 +00:00
|
|
|
];
|
|
|
|
|
2019-06-28 09:03:58 +00:00
|
|
|
const ACTIVITYPUB = 1;
|
|
|
|
const DFRN = 2;
|
|
|
|
const LEGACY_DFRN = 3;
|
|
|
|
const DIASPORA = 4;
|
|
|
|
const OSTATUS = 5;
|
2019-11-23 04:31:40 +00:00
|
|
|
const MAIL = 6;
|
2019-06-28 09:03:58 +00:00
|
|
|
|
2018-12-07 05:52:14 +00:00
|
|
|
/**
|
|
|
|
* Extract delivery data from the provided item fields
|
|
|
|
*
|
|
|
|
* @param array $fields
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function extractFields(array &$fields)
|
|
|
|
{
|
|
|
|
$delivery_data = [];
|
2020-05-02 19:34:02 +00:00
|
|
|
foreach (array_merge(self::FIELD_LIST, self::LEGACY_FIELD_LIST) as $key => $field) {
|
2018-12-07 05:52:14 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Increments the queue_done for the given URI ID.
|
2018-12-07 05:52:14 +00:00
|
|
|
*
|
|
|
|
* Avoids racing condition between multiple delivery threads.
|
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2019-06-28 09:03:58 +00:00
|
|
|
* @param integer $protocol
|
2018-12-07 05:52:14 +00:00
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-12-07 05:52:14 +00:00
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function incrementQueueDone(int $uri_id, int $protocol = 0)
|
2018-12-07 05:52:14 +00:00
|
|
|
{
|
2019-06-28 09:03:58 +00: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;
|
|
|
|
}
|
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
return DBA::e('UPDATE `post-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `uri-id` = ?', $uri_id);
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 07:39:13 +00:00
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Increments the queue_failed for the given URI ID.
|
2019-08-20 07:39:13 +00:00
|
|
|
*
|
|
|
|
* Avoids racing condition between multiple delivery threads.
|
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2019-08-20 07:39:13 +00:00
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function incrementQueueFailed(int $uri_id)
|
2019-08-20 07:39:13 +00:00
|
|
|
{
|
2020-05-02 19:34:02 +00:00
|
|
|
return DBA::e('UPDATE `post-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `uri-id` = ?', $uri_id);
|
2019-08-20 07:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 03:25:05 +00:00
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Increments the queue_count for the given URI ID.
|
2019-09-02 03:25:05 +00:00
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2019-09-02 03:25:05 +00:00
|
|
|
* @param integer $increment
|
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function incrementQueueCount(int $uri_id, int $increment = 1)
|
2019-09-02 03:25:05 +00:00
|
|
|
{
|
2020-05-02 19:34:02 +00:00
|
|
|
return DBA::e('UPDATE `post-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `uri-id` = ?', $increment, $uri_id);
|
2019-09-02 03:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 05:52:14 +00:00
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Insert a new URI delivery data entry
|
2018-12-07 05:52:14 +00:00
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2018-12-07 05:52:14 +00:00
|
|
|
* @param array $fields
|
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-12-07 05:52:14 +00:00
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function insert(int $uri_id, array $fields)
|
2018-12-07 05:52:14 +00:00
|
|
|
{
|
2020-05-02 19:34:02 +00:00
|
|
|
if (empty($uri_id)) {
|
|
|
|
throw new BadMethodCallException('Empty URI_id');
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
$fields['uri-id'] = $uri_id;
|
2018-12-07 05:52:14 +00:00
|
|
|
|
2020-08-28 05:47:41 +00:00
|
|
|
return DBA::replace('post-delivery-data', $fields);
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Update/Insert URI delivery data
|
2018-12-07 05:52:14 +00:00
|
|
|
*
|
|
|
|
* If you want to update queue_done, please use incrementQueueDone instead.
|
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2018-12-07 05:52:14 +00:00
|
|
|
* @param array $fields
|
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-12-07 05:52:14 +00:00
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function update(int $uri_id, array $fields)
|
2018-12-07 05:52:14 +00:00
|
|
|
{
|
2020-05-02 19:34:02 +00:00
|
|
|
if (empty($uri_id)) {
|
|
|
|
throw new BadMethodCallException('Empty URI_id');
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
// Nothing to do, update successful
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
return DBA::update('post-delivery-data', $fields, ['uri-id' => $uri_id], true);
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-02 19:34:02 +00:00
|
|
|
* Delete URI delivery data
|
2018-12-07 05:52:14 +00:00
|
|
|
*
|
2020-05-02 19:34:02 +00:00
|
|
|
* @param integer $uri_id
|
2018-12-07 05:52:14 +00:00
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-12-07 05:52:14 +00:00
|
|
|
*/
|
2020-05-02 19:34:02 +00:00
|
|
|
public static function delete(int $uri_id)
|
2018-12-07 05:52:14 +00:00
|
|
|
{
|
2020-05-02 19:34:02 +00:00
|
|
|
if (empty($uri_id)) {
|
|
|
|
throw new BadMethodCallException('Empty URI_id');
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 19:34:02 +00:00
|
|
|
return DBA::delete('post-delivery-data', ['uri-id' => $uri_id]);
|
2018-12-07 05:52:14 +00:00
|
|
|
}
|
|
|
|
}
|