]> git.mxchange.org Git - friendica.git/blob - src/Federation/Repository/DeliveryQueueItem.php
spelling: forums
[friendica.git] / src / Federation / Repository / DeliveryQueueItem.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Federation\Repository;
23
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;
31
32 final class DeliveryQueueItem extends \Friendica\BaseRepository
33 {
34         protected static $table_name = 'delivery-queue';
35
36         public function __construct(Database $database, LoggerInterface $logger, Factory\DeliveryQueueItem $factory)
37         {
38                 parent::__construct($database, $logger, $factory);
39         }
40
41         public function selectByServerId(int $gsid, int $maxFailedCount): Collection\DeliveryQueueItems
42         {
43                 $Entities = new Collection\DeliveryQueueItems();
44
45                 $deliveryQueueItems = $this->db->select(
46                         self::$table_name,
47                         [],
48                         ["`gsid` = ? AND `failed` < ?", $gsid, $maxFailedCount],
49                         ['order' => ['created']]
50                 );
51                 while ($deliveryQueueItem = $this->db->fetch($deliveryQueueItems)) {
52                         $Entities[] = $this->factory->createFromTableRow($deliveryQueueItem);
53                 }
54
55                 $this->db->close($deliveryQueueItems);
56
57                 return $Entities;
58         }
59
60         public function selectAggregateByServerId(): Collection\DeliveryQueueAggregates
61         {
62                 $Entities = new Collection\DeliveryQueueAggregates();
63
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']);
67                 }
68
69                 $this->db->close($deliveryQueueAggregates);
70
71                 return $Entities;
72         }
73
74         public function save(Entity\DeliveryQueueItem $deliveryQueueItem)
75         {
76                 $fields = [
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,
84                 ];
85
86                 $this->db->insert(self::$table_name, $fields, Database::INSERT_UPDATE);
87         }
88
89         public function remove(Entity\DeliveryQueueItem $deliveryQueueItem): bool
90         {
91                 return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, 'gsid' => $deliveryQueueItem->targetServerId]);
92         }
93
94         public function removeFailedByServerId(int $gsid, int $failedThreshold): bool
95         {
96                 return $this->db->delete(self::$table_name, ["`gsid` = ? AND `failed` >= ?", $gsid, $failedThreshold]);
97         }
98
99         public function incrementFailed(Entity\DeliveryQueueItem $deliveryQueueItem): bool
100         {
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
106                 );
107         }
108
109         public function optimizeStorage(): bool
110         {
111                 return $this->db->e("OPTIMIZE TABLE " . DBA::buildTableString([self::$table_name]));
112         }
113 }