3 * @copyright Copyright (C) 2010-2023, the Friendica project
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\Federation\Repository;
24 use Friendica\Database\Database;
25 use Friendica\Database\DBA;
26 use Friendica\Federation\Collection;
27 use Friendica\Federation\Entity;
28 use Friendica\Federation\Factory;
29 use Friendica\Util\DateTimeFormat;
30 use Psr\Log\LoggerInterface;
32 final class DeliveryQueueItem extends \Friendica\BaseRepository
34 protected static $table_name = 'delivery-queue';
36 public function __construct(Database $database, LoggerInterface $logger, Factory\DeliveryQueueItem $factory)
38 parent::__construct($database, $logger, $factory);
41 public function selectByServerId(int $gsid, int $maxFailedCount): Collection\DeliveryQueueItems
43 $Entities = new Collection\DeliveryQueueItems();
45 $deliveryQueueItems = $this->db->select(
48 ["`gsid` = ? AND `failed` < ?", $gsid, $maxFailedCount],
49 ['order' => ['created']]
51 while ($deliveryQueueItem = $this->db->fetch($deliveryQueueItems)) {
52 $Entities[] = $this->factory->createFromTableRow($deliveryQueueItem);
55 $this->db->close($deliveryQueueItems);
60 public function selectAggregateByServerId(): Collection\DeliveryQueueAggregates
62 $Entities = new Collection\DeliveryQueueAggregates();
64 $deliveryQueueAggregates = $this->db->p("SELECT `gsid`, MAX(`failed`) AS `failed` FROM " . DBA::buildTableString([self::$table_name]) . " GROUP BY `gsid` ORDER BY RAND()");
65 while ($deliveryQueueAggregate = $this->db->fetch($deliveryQueueAggregates)) {
66 $Entities[] = new Entity\DeliveryQueueAggregate($deliveryQueueAggregate['gsid'], $deliveryQueueAggregate['failed']);
69 $this->db->close($deliveryQueueAggregates);
74 public function save(Entity\DeliveryQueueItem $deliveryQueueItem)
77 'gsid' => $deliveryQueueItem->targetServerId,
78 'uri-id' => $deliveryQueueItem->postUriId,
79 'created' => $deliveryQueueItem->created->format(DateTimeFormat::MYSQL),
80 'command' => $deliveryQueueItem->command,
81 'cid' => $deliveryQueueItem->targetContactId,
82 'uid' => $deliveryQueueItem->senderUserId,
83 'failed' => $deliveryQueueItem->failed,
86 $this->db->insert(self::$table_name, $fields, Database::INSERT_UPDATE);
89 public function remove(Entity\DeliveryQueueItem $deliveryQueueItem): bool
91 return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, 'gsid' => $deliveryQueueItem->targetServerId]);
94 public function removeFailedByServerId(int $gsid, int $failedThreshold): bool
96 return $this->db->delete(self::$table_name, ["`gsid` = ? AND `failed` >= ?", $gsid, $failedThreshold]);
99 public function incrementFailed(Entity\DeliveryQueueItem $deliveryQueueItem): bool
101 return $this->db->e("
102 UPDATE " . DBA::buildTableString([self::$table_name]) . "
103 SET `failed` = `failed` + 1
104 WHERE `uri-id` = ? AND `gsid` = ?",
105 $deliveryQueueItem->postUriId, $deliveryQueueItem->targetServerId
109 public function optimizeStorage(): bool
111 return $this->db->e("OPTIMIZE TABLE " . DBA::buildTableString([self::$table_name]));