-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathSync.php
More file actions
138 lines (114 loc) · 4.81 KB
/
Sync.php
File metadata and controls
138 lines (114 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace Selfoss\controllers\Items;
use Selfoss\daos;
use Selfoss\helpers\Authentication;
use Selfoss\helpers\Configuration;
use function Selfoss\helpers\json_response;
use Selfoss\helpers\View;
use Selfoss\helpers\ViewHelper;
/**
* Controller for synchronizing item statuses
*/
final readonly class Sync {
public function __construct(
private Authentication $authentication,
private Configuration $configuration,
private daos\Items $itemsDao,
private daos\Sources $sourcesDao,
private \Selfoss\controllers\Tags $tagsController,
private daos\Tags $tagsDao,
private View $view,
private ViewHelper $viewHelper
) {
}
/**
* returns updated database info (stats, item statuses)
* json
*/
public function sync(): void {
$this->authentication->ensureCanRead();
if (isset($_GET['since'])) {
$params = $_GET;
} elseif (isset($_POST['since'])) {
$params = $_POST;
} else {
$this->view->jsonError(['sync' => 'missing since argument']);
}
$since = new \DateTime($params['since']);
$since->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
$lastUpdate = $this->itemsDao->lastUpdate();
$sync = [
'lastUpdate' => $lastUpdate !== null ? $lastUpdate->format(\DateTime::ATOM) : null,
];
if (array_key_exists('itemsSinceId', $params)) {
$sinceId = (int) $params['itemsSinceId'];
if ($sinceId >= 0) {
$notBefore = isset($params['itemsNotBefore']) ? new \DateTime($params['itemsNotBefore']) : null;
if ($sinceId === 0 || !$notBefore) {
$sinceId = $this->itemsDao->lowestIdOfInterest() - 1;
// only send 1 day worth of items
$notBefore = new \DateTime();
$notBefore->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
$notBefore->sub(new \DateInterval('P1D'));
$notBefore->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
}
$itemsHowMany = $this->configuration->itemsPerpage;
if (array_key_exists('itemsHowMany', $params)
&& is_int($params['itemsHowMany'])) {
$itemsHowMany = min(
$params['itemsHowMany'],
2 * $itemsHowMany
);
}
$sync['newItems'] = function() use ($sinceId, $notBefore, $since, $itemsHowMany) {
foreach ($this->itemsDao->sync($sinceId, $notBefore, $since, $itemsHowMany) as $newItem) {
yield $this->viewHelper->preprocessEntry($newItem, $this->tagsController);
}
};
$sync['lastId'] = $this->itemsDao->lastId();
}
}
if ($lastUpdate === null || $lastUpdate > $since) {
$sync['stats'] = $this->itemsDao->stats();
if (array_key_exists('tags', $params) && $params['tags'] == 'true') {
$sync['tags'] = $this->tagsDao->getWithUnread();
}
if (array_key_exists('sources', $params) && $params['sources'] == 'true') {
$sync['sources'] = $this->sourcesDao->getWithUnread();
}
$wantItemsStatuses = array_key_exists('itemsStatuses', $params) && $params['itemsStatuses'] == 'true';
if ($wantItemsStatuses) {
$sync['itemUpdates'] = $this->itemsDao->statuses($since);
}
}
$this->view->sendResponse(json_response($sync));
}
/**
* Items statuses bulk update.
*/
public function updateStatuses(): void {
$this->authentication->ensureIsPrivileged();
if (isset($_POST['updatedStatuses'])) {
$updatedStatuses = $_POST['updatedStatuses'];
if (!is_array($updatedStatuses)) {
$this->view->jsonError(['updatedStatuses' => 'not an array']);
}
foreach ($updatedStatuses as $index => $status) {
if (!is_array($status)) {
$this->view->jsonError(["updatedStatuses[$index]" => 'not an array']);
}
if (!array_key_exists('id', $status)) {
$this->view->jsonError(["updatedStatuses[$index]" => 'missing id argument']);
}
$id = (int) $status['id'];
if ((string) $id !== $status['id']) {
$this->view->jsonError(["updatedStatuses[$index]['id']" => 'not a number']);
}
$status['id'] = $id;
}
$this->itemsDao->bulkStatusUpdate($updatedStatuses);
}
$this->sync();
}
}