Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function markProcessed(INotification $notification): void {
$this->push->deferPayloads();
}
foreach ($deleted as $user => $notifications) {
foreach ($notifications as $notificationId) {
$this->push->pushDeleteToDevice($user, $notificationId);
foreach ($notifications as $data) {
$this->push->pushDeleteToDevice($user, $data['id'], $data['app']);
}
}
if (!$isAlreadyDeferring) {
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ public function deleteNotification(int $id): DataResponse {
}

try {
$notification = $this->handler->getById($id, $this->getCurrentUser());
$deleted = $this->handler->deleteById($id, $this->getCurrentUser());

if ($deleted) {
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id);
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id, $notification->getApp());
}
} catch (NotificationNotFoundException $e) {
}
Expand Down
11 changes: 7 additions & 4 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ public function delete(INotification $notification): array {
$deleted[$row['user']] = [];
}

$deleted[$row['user']][] = (int) $row['notification_id'];
$deleted[$row['user']][] = [
'id' => (int) $row['notification_id'],
'app' => $row['app'],
];
$notifications[(int) $row['notification_id']] = $this->notificationFromRow($row);
}
$statement->closeCursor();

foreach ($deleted as $user => $notificationIds) {
foreach ($notificationIds as $notificationId) {
$this->deleteById($notificationId, $user, $notifications[$notificationId]);
foreach ($deleted as $user => $entries) {
foreach ($entries as $entry) {
$this->deleteById($entry['id'], $user, $notifications[$entry['id']]);
}
}

Expand Down
62 changes: 37 additions & 25 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ public function flushPayloads(): void {
$this->sendNotificationsToProxies();
}

public function filterDeviceList(array $devices, string $app): array {
$isTalkNotification = \in_array($app, ['spreed', 'talk', 'admin_notification_talk'], true);

$talkDevices = array_filter($devices, static function ($device) {
return $device['apptype'] === 'talk';
});
$otherDevices = array_filter($devices, static function ($device) {
return $device['apptype'] !== 'talk';
});

$this->printInfo('Identified ' . count($talkDevices) . ' Talk devices and ' . count($otherDevices) . ' others.');

if (!$isTalkNotification) {
if (empty($otherDevices)) {
// We only send file notifications to the files app.
// If you don't have such a device, bye!
return [];
}
return $otherDevices;
}

if (empty($talkDevices)) {
// If you don't have a talk device,
// we fall back to the files app.
return $otherDevices;
}
return $talkDevices;
}

public function pushToDevice(int $id, INotification $notification, ?OutputInterface $output = null): void {
if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
return;
Expand Down Expand Up @@ -173,30 +202,9 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
$this->printInfo('Public user key size: ' . strlen($userKey->getPublic()));

$isTalkNotification = \in_array($notification->getApp(), ['spreed', 'talk', 'admin_notification_talk'], true);
$talkDevices = array_filter($devices, function ($device) {
return $device['apptype'] === 'talk';
});
$otherDevices = array_filter($devices, function ($device) {
return $device['apptype'] !== 'talk';
});

$this->printInfo('Identified ' . count($talkDevices) . ' Talk devices and ' . count($otherDevices) . ' others.');

if (!$isTalkNotification) {
if (empty($otherDevices)) {
// We only send file notifications to the files app.
// If you don't have such a device, bye!
return;
}
$devices = $otherDevices;
} else {
if (empty($talkDevices)) {
// If you don't have a talk device,
// we fall back to the files app.
$devices = $otherDevices;
} else {
$devices = $talkDevices;
}
$devices = $this->filterDeviceList($devices, $notification->getApp());
if (empty($devices)) {
return;
}

// We don't push to devices that are older than 60 days
Expand Down Expand Up @@ -230,7 +238,7 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
}
}

public function pushDeleteToDevice(string $userId, int $notificationId): void {
public function pushDeleteToDevice(string $userId, int $notificationId, string $app = ''): void {
if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
return;
}
Expand All @@ -241,6 +249,10 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}

$devices = $this->getDevicesForUser($userId);
if ($notificationId !== 0 && $app !== '') {
// Only filter when it's not a single delete
$devices = $this->filterDeviceList($devices, $app);
}
if (empty($devices)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,11 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, $isTalkNoti
->willReturn('valid');

if ($isTalkNotification) {
$notification->expects($this->once())
$notification->expects($this->any())
->method('getApp')
->willReturn('spreed');
} else {
$notification->expects($this->once())
$notification->expects($this->any())
->method('getApp')
->willReturn('notifications');
}
Expand Down