Skip to content

Commit

Permalink
Merge branch 'main' into karthi/fix-hangup-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-with-a-bushido authored Nov 6, 2023
2 parents 579b8aa + 6e6b2f3 commit 243275d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
8 changes: 5 additions & 3 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,8 @@ class Client extends MatrixApi {
await processToDeviceQueue();
} catch (_) {} // we want to dispose any errors this throws

await singleShotStaleCallChecker();

_retryDelay = Future.value();
onSyncStatus.add(SyncStatusUpdate(SyncStatus.finished));
} on MatrixException catch (e, s) {
Expand Down Expand Up @@ -2148,6 +2150,9 @@ class Client extends MatrixApi {
}
}

/// stores when we last checked for stale calls
DateTime lastStaleCallRun = DateTime(0);

Future<Room> _updateRoomsByRoomUpdate(
String roomId, SyncRoomUpdate chatUpdate) async {
// Update the chat list item.
Expand Down Expand Up @@ -2188,9 +2193,6 @@ class Client extends MatrixApi {
}
// If the membership is "leave" then remove the item and stop here
else if (found && membership == Membership.leave) {
// stop stale group call checker for left room.
room.stopStaleCallsChecker(room.id);

rooms.removeAt(roomIndex);

// in order to keep the archive in sync, add left room to archive
Expand Down
6 changes: 0 additions & 6 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ class Room {
/// Key-Value store for private account data only visible for this user.
Map<String, BasicRoomEvent> roomAccountData = {};

/// stores stale group call checking timers for rooms.
Map<String, Timer> staleGroupCallsTimer = {};

final _sendingQueue = <Completer>[];

Map<String, dynamic> toJson() => {
Expand Down Expand Up @@ -137,9 +134,6 @@ class Room {
setState(state);
}
}
if (!isArchived) {
startStaleCallsChecker(id);
}
partial = false;
}

Expand Down
43 changes: 19 additions & 24 deletions lib/src/voip/voip_room_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ extension GroupCallUtils on Room {
return [];
}

/// stops the stale call checker timer
void stopStaleCallsChecker(String roomId) {
if (staleGroupCallsTimer.tryGet(roomId) != null) {
staleGroupCallsTimer[roomId]!.cancel();
staleGroupCallsTimer.remove(roomId);
Logs().d('[VOIP] stopped stale group calls checker for room $id');
} else {
Logs().d('[VOIP] no stale call checker for room found');
}
}

static const staleCallCheckerDuration = Duration(seconds: 30);

bool callMemberStateIsExpired(
Expand Down Expand Up @@ -89,23 +78,16 @@ extension GroupCallUtils on Room {
}

/// checks for stale calls in a room and sends `m.terminated` if all the
/// expires_ts are expired. Call when opening a room
void startStaleCallsChecker(String roomId) async {
stopStaleCallsChecker(roomId);
await singleShotStaleCallCheckerOnRoom();
staleGroupCallsTimer[roomId] = Timer.periodic(
staleCallCheckerDuration,
(timer) async => await singleShotStaleCallCheckerOnRoom(),
);
}

/// expires_ts are expired. Called regularly on sync.
Future<void> singleShotStaleCallCheckerOnRoom() async {
Logs().d('[VOIP] checking for stale group calls in room $id');
// make sure we have all the to-device messages we are supposed to have
await client.oneShotSync();
if (partial) return;

final copyGroupCallIds =
states.tryGetMap<String, Event>(EventTypes.GroupCallPrefix);
if (copyGroupCallIds == null) return;

Logs().d('[VOIP] checking for stale group calls in room $id');

for (final groupCall in copyGroupCallIds.entries) {
final groupCallId = groupCall.key;
final groupCallEvent = groupCall.value;
Expand Down Expand Up @@ -165,3 +147,16 @@ extension GroupCallUtils on Room {
}
}
}

extension GroupCallClientUtils on Client {
// call after sync
Future<void> singleShotStaleCallChecker() async {
if (lastStaleCallRun
.add(GroupCallUtils.staleCallCheckerDuration)
.isBefore(DateTime.now())) {
await Future.wait(rooms
.where((r) => r.membership == Membership.join)
.map((r) => r.singleShotStaleCallCheckerOnRoom()));
}
}
}

0 comments on commit 243275d

Please sign in to comment.