Merge branch 'td/cacheserverprofile' into 'main'

cache option to fetchOwnProfileFromServer and missing awaits in group call enter/leave

See merge request famedly/company/frontend/famedlysdk!1243
This commit is contained in:
td 2023-02-20 18:31:41 +00:00
commit c5aa899038
5 changed files with 60 additions and 44 deletions

View File

@ -1,3 +1,7 @@
## [0.17.1] - 20th Feb 2023
- chore: add missing awaits in group call enter and leave funcs (td)
- chore: add useServerCache option to fetchOwnProfileFromServer and fix missing awaits (td)
## [0.17.0] - 17th Feb 2023
- fix: ability to upgrade audio calls to video calls (td)
- chore: add a fetchOwnProfileFromServer method which tries to get ownProfile from server first, disk then (td)

View File

@ -757,21 +757,23 @@ class Client extends MatrixApi {
/// Returns the user's own displayname and avatar url. In Matrix it is possible that
/// one user can have different displaynames and avatar urls in different rooms.
/// Tries to get the profile from homeserver first, if failed, falls back to a profile
/// from a room where the user exists.
Future<Profile> fetchOwnProfileFromServer() async {
/// from a room where the user exists. Set `useServerCache` to true to get any
/// prior value from this function
Future<Profile> fetchOwnProfileFromServer(
{bool useServerCache = false}) async {
try {
return getProfileFromUserId(
return await getProfileFromUserId(
userID!,
getFromRooms: false,
cache: false,
cache: useServerCache,
);
} catch (e) {
Logs().w(
'[Matrix] getting profile from homeserver failed, falling back to first room with required profile');
return getProfileFromUserId(
return await getProfileFromUserId(
userID!,
getFromRooms: true,
cache: false,
cache: true,
);
}
}
@ -790,7 +792,8 @@ class Client extends MatrixApi {
cache: cache,
);
final Map<String, ProfileInformation> _profileCache = {};
final Map<String, ProfileInformation> _profileRoomsCache = {};
final Map<String, ProfileInformation> _profileServerCache = {};
/// Get the combined profile information for this user.
/// If [getFromRooms] is true then the profile will first be searched from the
@ -801,12 +804,14 @@ class Client extends MatrixApi {
/// become outdated if the user changes the displayname or avatar in this session.
Future<Profile> getProfileFromUserId(String userId,
{bool cache = true, bool getFromRooms = true}) async {
var profile = _profileCache[userId];
var profile =
getFromRooms ? _profileRoomsCache[userId] : _profileServerCache[userId];
if (cache && profile != null) {
return Profile(
userId: userId,
displayName: profile.displayname,
avatarUrl: profile.avatarUrl);
userId: userId,
displayName: profile.displayname,
avatarUrl: profile.avatarUrl,
);
}
if (getFromRooms) {
@ -816,20 +821,27 @@ class Client extends MatrixApi {
if (room != null) {
final user =
room.getParticipants().firstWhere((User user) => user.id == userId);
return Profile(
userId: userId,
displayName: user.displayName,
avatarUrl: user.avatarUrl);
final profileFromRooms = Profile(
userId: userId,
displayName: user.displayName,
avatarUrl: user.avatarUrl,
);
_profileRoomsCache[userId] = ProfileInformation(
avatarUrl: profileFromRooms.avatarUrl,
displayname: profileFromRooms.displayName,
);
return profileFromRooms;
}
}
profile = await getUserProfile(userId);
if (cache || _profileCache.containsKey(userId)) {
_profileCache[userId] = profile;
if (cache || _profileServerCache.containsKey(userId)) {
_profileServerCache[userId] = profile;
}
return Profile(
userId: userId,
displayName: profile.displayname,
avatarUrl: profile.avatarUrl);
userId: userId,
displayName: profile.displayname,
avatarUrl: profile.avatarUrl,
);
}
final List<ArchivedRoom> _archivedRooms = [];

View File

@ -406,7 +406,7 @@ class GroupCall {
}
/// enter the group call.
void enter() async {
Future<void> enter() async {
if (!(state == GroupCallState.LocalCallFeedUninitialized ||
state == GroupCallState.LocalCallFeedInitialized)) {
throw Exception('Cannot enter call in the $state state');
@ -449,14 +449,14 @@ class GroupCall {
voip.delegate.handleNewGroupCall(this);
}
void dispose() {
Future<void> dispose() async {
if (localUserMediaStream != null) {
removeUserMediaStream(localUserMediaStream!);
localUserMediaStream = null;
}
if (localScreenshareStream != null) {
stopMediaStream(localScreenshareStream!.stream);
await stopMediaStream(localScreenshareStream!.stream);
removeScreenshareStream(localScreenshareStream!);
localScreenshareStream = null;
localDesktopCapturerSourceId = null;
@ -464,7 +464,7 @@ class GroupCall {
_removeParticipant(client.userID!);
removeMemberStateEvent();
await removeMemberStateEvent();
final callsCopy = calls.toList();
callsCopy.forEach((element) {
@ -473,11 +473,11 @@ class GroupCall {
activeSpeaker = null;
activeSpeakerLoopTimeout?.cancel();
_callSubscription?.cancel();
await _callSubscription?.cancel();
}
void leave() {
dispose();
Future<void> leave() async {
await dispose();
setState(GroupCallState.LocalCallFeedUninitialized);
voip.currentGroupCID = null;
voip.delegate.handleGroupCallEnded(this);
@ -487,7 +487,7 @@ class GroupCall {
justLeftGroupCall.intent != 'm.room' &&
justLeftGroupCall.participants.isEmpty &&
room.canCreateGroupCall) {
terminate();
await terminate();
} else {
Logs().d(
'[VOIP] left group call but cannot terminate. participants: ${participants.length}, pl: ${room.canCreateGroupCall}');
@ -495,10 +495,10 @@ class GroupCall {
}
/// terminate group call.
void terminate({bool emitStateEvent = true}) async {
Future<void> terminate({bool emitStateEvent = true}) async {
final existingStateEvent =
room.getState(EventTypes.GroupCallPrefix, groupCallId);
dispose();
await dispose();
participants = [];
voip.groupCalls.remove(room.id);
voip.groupCalls.remove(groupCallId);
@ -1116,8 +1116,8 @@ class GroupCall {
onGroupCallEvent.add(GroupCallEvent.UserMediaStreamsChanged);
}
void replaceUserMediaStream(
WrappedMediaStream existingStream, WrappedMediaStream replacementStream) {
Future<void> replaceUserMediaStream(WrappedMediaStream existingStream,
WrappedMediaStream replacementStream) async {
final streamIndex = userMediaStreams
.indexWhere((stream) => stream.userId == existingStream.userId);
@ -1127,7 +1127,7 @@ class GroupCall {
userMediaStreams.replaceRange(streamIndex, 1, [replacementStream]);
existingStream.dispose();
await existingStream.dispose();
//replacementStream.measureVolumeActivity(true);
onGroupCallEvent.add(GroupCallEvent.UserMediaStreamsChanged);
}
@ -1225,8 +1225,8 @@ class GroupCall {
onGroupCallEvent.add(GroupCallEvent.ScreenshareStreamsChanged);
}
void replaceScreenshareStream(
WrappedMediaStream existingStream, WrappedMediaStream replacementStream) {
Future<void> replaceScreenshareStream(WrappedMediaStream existingStream,
WrappedMediaStream replacementStream) async {
final streamIndex = screenshareStreams
.indexWhere((stream) => stream.userId == existingStream.userId);
@ -1236,7 +1236,7 @@ class GroupCall {
screenshareStreams.replaceRange(streamIndex, 1, [replacementStream]);
existingStream.dispose();
await existingStream.dispose();
onGroupCallEvent.add(GroupCallEvent.ScreenshareStreamsChanged);
}

View File

@ -660,15 +660,15 @@ class VoIP {
Future<void> startGroupCalls() async {
final rooms = client.rooms;
rooms.forEach((element) {
createGroupCallForRoom(element);
});
for (final room in rooms) {
await createGroupCallForRoom(room);
}
}
void stopGroupCalls() {
groupCalls.forEach((_, groupCall) {
groupCall.terminate();
});
Future<void> stopGroupCalls() async {
for (final groupCall in groupCalls.values) {
await groupCall.terminate();
}
groupCalls.clear();
}

View File

@ -1,6 +1,6 @@
name: matrix
description: Matrix Dart SDK
version: 0.17.0
version: 0.17.1
homepage: https://famedly.com
repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git