Skip to content

Commit 1d4cd07

Browse files
nickvergessenbackportbot[bot]
authored andcommitted
feat(federation): Expose a header with the pending invitations on room list
Signed-off-by: Joas Schilling <[email protected]>
1 parent 55a3746 commit 1d4cd07

File tree

18 files changed

+129
-16
lines changed

18 files changed

+129
-16
lines changed

docs/conversation.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040

4141
- Header:
4242

43-
| field | type | Description |
44-
|------------------------------------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
45-
| `X-Nextcloud-Talk-Hash` | string | Sha1 value over some config. When you receive a different value on subsequent requests, the capabilities and the signaling settings should be refreshed. |
46-
| `X-Nextcloud-Talk-Modified-Before` | string | Timestamp from before the database request that can be used as `modifiedSince` parameter in the next request |
43+
| field | type | Description |
44+
|---------------------------------------|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
45+
| `X-Nextcloud-Talk-Hash` | string | Sha1 value over some config. When you receive a different value on subsequent requests, the capabilities and the signaling settings should be refreshed. |
46+
| `X-Nextcloud-Talk-Modified-Before` | string | Timestamp from before the database request that can be used as `modifiedSince` parameter in the next request |
47+
| `X-Nextcloud-Talk-Federation-Invites` | string | *Optional:* Number of pending invites to federated conversations the user has. (Only available when the user can do federation and has at least one invite pending) |
4748

4849
- Data:
4950
Array of conversations, each conversation has at least:

lib/BackgroundJob/ResetAssignedSignalingServer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace OCA\Talk\BackgroundJob;
2525

26+
use OCA\Talk\CachePrefix;
2627
use OCA\Talk\Manager;
2728
use OCP\AppFramework\Utility\ITimeFactory;
2829
use OCP\BackgroundJob\IJob;
@@ -49,7 +50,7 @@ public function __construct(
4950
$this->setInterval(60 * 5);
5051
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
5152

52-
$this->cache = $cacheFactory->createDistributed('hpb_servers');
53+
$this->cache = $cacheFactory->createDistributed(CachePrefix::SIGNALING_ASSIGNED_SERVER);
5354
}
5455

5556
protected function run($argument): void {

lib/CachePrefix.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* @copyright Copyright (c) 2024 Joas Schilling <[email protected]>
4+
*/
5+
6+
declare(strict_types=1);
7+
/*
8+
* @copyright Copyright (c) 2024 Joas Schilling <[email protected]>
9+
*
10+
* @author Joas Schilling <[email protected]>
11+
*
12+
* @license GNU AGPL version 3 or any later version
13+
*
14+
* This program is free software: you can redistribute it and/or modify
15+
* it under the terms of the GNU Affero General Public License as
16+
* published by the Free Software Foundation, either version 3 of the
17+
* License, or (at your option) any later version.
18+
*
19+
* This program is distributed in the hope that it will be useful,
20+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
* GNU Affero General Public License for more details.
23+
*
24+
* You should have received a copy of the GNU Affero General Public License
25+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*
27+
*/
28+
29+
namespace OCA\Talk;
30+
31+
class CachePrefix {
32+
public const FEDERATED_PCM = 'talk/pcm/';
33+
public const CHAT_LAST_MESSAGE_ID = 'talk/lastmsgid';
34+
public const CHAT_UNREAD_COUNT = 'talk/unreadcount';
35+
public const SIGNALING_ASSIGNED_SERVER = 'hpb_servers';
36+
}

lib/Chat/ChatManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use DateInterval;
2828
use OC\Memcache\ArrayCache;
2929
use OC\Memcache\NullCache;
30+
use OCA\Talk\CachePrefix;
3031
use OCA\Talk\Events\BeforeChatMessageSentEvent;
3132
use OCA\Talk\Events\BeforeSystemMessageSentEvent;
3233
use OCA\Talk\Events\ChatMessageSentEvent;
@@ -113,8 +114,8 @@ public function __construct(
113114
protected IRequest $request,
114115
protected LoggerInterface $logger,
115116
) {
116-
$this->cache = $cacheFactory->createDistributed('talk/lastmsgid');
117-
$this->unreadCountCache = $cacheFactory->createDistributed('talk/unreadcount');
117+
$this->cache = $cacheFactory->createDistributed(CachePrefix::CHAT_LAST_MESSAGE_ID);
118+
$this->unreadCountCache = $cacheFactory->createDistributed(CachePrefix::CHAT_UNREAD_COUNT);
118119
}
119120

120121
/**

lib/Controller/FederationController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
* @psalm-import-type TalkRoom from ResponseDefinitions
5252
*/
5353
class FederationController extends OCSController {
54-
5554
public function __construct(
5655
IRequest $request,
5756
private FederationManager $federationManager,

lib/Controller/RoomController.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected function getTalkHashHeader(): array {
181181
* @param bool $includeStatus Include the user status
182182
* @param int $modifiedSince Filter rooms modified after a timestamp
183183
* @psalm-param non-negative-int $modifiedSince
184-
* @return DataResponse<Http::STATUS_OK, TalkRoom[], array{X-Nextcloud-Talk-Hash: string, X-Nextcloud-Talk-Modified-Before: numeric-string}>
184+
* @return DataResponse<Http::STATUS_OK, TalkRoom[], array{X-Nextcloud-Talk-Hash: string, X-Nextcloud-Talk-Modified-Before: numeric-string, X-Nextcloud-Talk-Federation-Invites?: numeric-string}>
185185
*
186186
* 200: Return list of rooms
187187
*/
@@ -191,6 +191,7 @@ public function getRooms(int $noStatusUpdate = 0, bool $includeStatus = false, i
191191

192192
$event = new BeforeRoomsFetchEvent($this->userId);
193193
$this->dispatcher->dispatchTyped($event);
194+
$user = $this->userManager->get($this->userId);
194195

195196
if ($noStatusUpdate === 0) {
196197
$isMobileApp = $this->request->isUserAgent([
@@ -201,7 +202,7 @@ public function getRooms(int $noStatusUpdate = 0, bool $includeStatus = false, i
201202
if ($isMobileApp) {
202203
// Bump the user status again
203204
$event = new UserLiveStatusEvent(
204-
$this->userManager->get($this->userId),
205+
$user,
205206
IUserStatus::ONLINE,
206207
$this->timeFactory->getTime()
207208
);
@@ -255,7 +256,19 @@ public function getRooms(int $noStatusUpdate = 0, bool $includeStatus = false, i
255256
}
256257
}
257258

258-
return new DataResponse($return, Http::STATUS_OK, array_merge($this->getTalkHashHeader(), ['X-Nextcloud-Talk-Modified-Before' => (string) $nextModifiedSince]));
259+
/** @var array{X-Nextcloud-Talk-Modified-Before: numeric-string, X-Nextcloud-Talk-Federation-Invites?: numeric-string} $headers */
260+
$headers = ['X-Nextcloud-Talk-Modified-Before' => (string) $nextModifiedSince];
261+
if ($this->talkConfig->isFederationEnabledForUserId($user)) {
262+
$numInvites = $this->federationManager->getNumberOfPendingInvitationsForUser($user);
263+
if ($numInvites !== 0) {
264+
$headers['X-Nextcloud-Talk-Federation-Invites'] = (string) $numInvites;
265+
}
266+
}
267+
268+
/** @var array{X-Nextcloud-Talk-Hash: string, X-Nextcloud-Talk-Modified-Before: numeric-string, X-Nextcloud-Talk-Federation-Invites?: numeric-string} $headers */
269+
$headers = array_merge($this->getTalkHashHeader(), $headers);
270+
271+
return new DataResponse($return, Http::STATUS_OK, $headers);
259272
}
260273

261274
/**

lib/Federation/CloudFederationProviderTalk.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Exception;
2929
use OCA\FederatedFileSharing\AddressHandler;
3030
use OCA\Talk\AppInfo\Application;
31+
use OCA\Talk\CachePrefix;
3132
use OCA\Talk\Config;
3233
use OCA\Talk\Events\AAttendeeRemovedEvent;
3334
use OCA\Talk\Events\ARoomModifiedEvent;
@@ -97,7 +98,7 @@ public function __construct(
9798
private UserConverter $userConverter,
9899
ICacheFactory $cacheFactory,
99100
) {
100-
$this->proxyCacheMessages = $cacheFactory->isAvailable() ? $cacheFactory->createDistributed('talk/pcm/') : null;
101+
$this->proxyCacheMessages = $cacheFactory->isAvailable() ? $cacheFactory->createDistributed(CachePrefix::FEDERATED_PCM) : null;
101102
}
102103

103104
/**

lib/Federation/FederationManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ public function getRemoteRoomShares(IUser $user): array {
257257
return $this->invitationMapper->getInvitationsForUser($user);
258258
}
259259

260+
public function getNumberOfPendingInvitationsForUser(IUser $user): int {
261+
return $this->invitationMapper->countInvitationsForUser($user, Invitation::STATE_PENDING);
262+
}
263+
260264
public function getNumberOfInvitations(Room $room): int {
261265
return $this->invitationMapper->countInvitationsForLocalRoom($room);
262266
}

lib/Federation/Proxy/TalkV1/Controller/ChatController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace OCA\Talk\Federation\Proxy\TalkV1\Controller;
2828

29+
use OCA\Talk\CachePrefix;
2930
use OCA\Talk\Chat\Notifier;
3031
use OCA\Talk\Exceptions\CannotReachRemoteException;
3132
use OCA\Talk\Federation\Proxy\TalkV1\ProxyRequest;
@@ -57,7 +58,7 @@ public function __construct(
5758
protected Notifier $notifier,
5859
ICacheFactory $cacheFactory,
5960
) {
60-
$this->proxyCacheMessages = $cacheFactory->isAvailable() ? $cacheFactory->createDistributed('talk/pcm/') : null;
61+
$this->proxyCacheMessages = $cacheFactory->isAvailable() ? $cacheFactory->createDistributed(CachePrefix::FEDERATED_PCM) : null;
6162
}
6263

6364
/**

lib/Model/InvitationMapper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ public function getInvitationsForUser(IUser $user): array {
9595
return $this->findEntities($qb);
9696
}
9797

98+
/**
99+
* @psalm-param Invitation::STATE_*|null $state
100+
*/
101+
public function countInvitationsForUser(IUser $user, ?int $state = null): int {
102+
$qb = $this->db->getQueryBuilder();
103+
104+
$qb->select($qb->func()->count('*'))
105+
->from($this->getTableName())
106+
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
107+
108+
if ($state !== null) {
109+
$qb->andWhere($qb->expr()->eq('state', $qb->createNamedParameter($state)));
110+
}
111+
112+
$result = $qb->executeQuery();
113+
$count = (int) $result->fetchOne();
114+
$result->closeCursor();
115+
116+
return $count;
117+
}
118+
98119
/**
99120
* @throws DoesNotExistException
100121
*/

0 commit comments

Comments
 (0)