Skip to content

Commit

Permalink
Merge pull request #2189 from bitfriend/activate-integration-test
Browse files Browse the repository at this point in the history
[smallfix] Miscellaneous fixes
  • Loading branch information
bitfriend authored Sep 19, 2024
2 parents 1a0bc72 + db3667d commit 6088a6b
Show file tree
Hide file tree
Showing 54 changed files with 392 additions and 554 deletions.
8 changes: 2 additions & 6 deletions app/lib/common/providers/common_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ final emailAddressesProvider = FutureProvider((ref) async {
ref.watch(genericUpdatesStream('global.acter.dev.three_pid'));
final confirmed = asDartStringList(await account.confirmedEmailAddresses());
final requested = asDartStringList(await account.requestedEmailAddresses());
final List<String> unconfirmed = [];
for (var i = 0; i < requested.length; i++) {
if (!confirmed.contains(requested[i])) {
unconfirmed.add(requested[i]);
}
}
final unconfirmed =
requested.where((email) => !confirmed.contains(email)).toList();
return EmailAddresses(confirmed, unconfirmed);
});

Expand Down
79 changes: 27 additions & 52 deletions app/lib/common/providers/room_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,19 @@ final maybeRoomProvider =
final roomVisibilityProvider = FutureProvider.family
.autoDispose<RoomVisibility?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
if (room == null) return null;
final joinRule = room.joinRuleStr();
switch (joinRule) {
case 'public':
return RoomVisibility.Public;
case 'restricted':
return RoomVisibility.SpaceVisible;
case 'invite':
return RoomVisibility.Private;
default:
_log.warning('Unsupported joinRule for $roomId: $joinRule');
throw 'Unsupported joinRule $joinRule';
final visibility = switch (joinRule) {
'public' => RoomVisibility.Public,
'restricted' => RoomVisibility.SpaceVisible,
'invite' => RoomVisibility.Private,
_ => null,
};
if (visibility == null) {
_log.warning('Unsupported joinRule for $roomId: $joinRule');
throw 'Unsupported joinRule $joinRule';
}
return visibility;
});

/// Get the members invited of a given roomId the user knows about. Errors
Expand Down Expand Up @@ -134,18 +132,12 @@ final parentIdsProvider =
try {
// FIXME: we should get only the parent Ids from the underlying SDK
final relations = await ref.watch(spaceRelationsProvider(roomId).future);
if (relations == null) {
return [];
}

if (relations == null) return [];
// Collect all parents: mainParent and otherParents
List<String> allParents = [];
final mainParent = relations.mainParent();
if (mainParent != null) {
allParents.add(mainParent.roomId().toString());
}
allParents
.addAll(relations.otherParents().map((p) => p.roomId().toString()));
List<String> allParents =
relations.mainParent().let((p0) => [p0.roomId().toString()]) ?? [];
final others = relations.otherParents().map((p) => p.roomId().toString());
allParents.addAll(others);
return allParents;
} catch (e) {
_log.warning('Failed to load parent ids for $roomId: $e');
Expand All @@ -157,9 +149,7 @@ final parentIdsProvider =
final roomDisplayNameProvider =
FutureProvider.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
if (room == null) return null;
return (await room.displayName()).text();
});

Expand All @@ -169,10 +159,7 @@ final roomAvatarProvider =
final sdk = await ref.watch(sdkProvider.future);
final thumbsize = sdk.api.newThumbSize(48, 48);
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null || !room.hasAvatar()) {
return null;
}

if (room == null || !room.hasAvatar()) return null;
final avatar = (await room.avatar(thumbsize)).data();
if (avatar != null) {
return MemoryImage(Uint8List.fromList(avatar.asTypedList()));
Expand All @@ -197,9 +184,7 @@ final parentAvatarInfosProvider =
final joinRulesAllowedRoomsProvider = FutureProvider.autoDispose
.family<List<String>, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return [];
}
if (room == null) return [];
return room.restrictedRoomIdsStr().map((e) => e.toDartString()).toList();
});

Expand All @@ -208,9 +193,7 @@ final joinRulesAllowedRoomsProvider = FutureProvider.autoDispose
final roomMembershipProvider = FutureProvider.family<Member?, String>(
(ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null || !room.isJoined()) {
return null;
}
if (room == null || !room.isJoined()) return null;
return await room.getMyMembership();
},
);
Expand All @@ -219,20 +202,16 @@ final roomMembershipProvider = FutureProvider.family<Member?, String>(
final roomNotificationStatusProvider =
FutureProvider.autoDispose.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
return room.notificationMode();
if (room == null) return null;
return await room.notificationMode();
});

/// Get the default RoomNotificationsStatus for this room type
final roomDefaultNotificationStatusProvider =
FutureProvider.autoDispose.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
return room.defaultNotificationMode();
if (room == null) return null;
return await room.defaultNotificationMode();
});

/// Get the default RoomNotificationsStatus for this room type
Expand Down Expand Up @@ -266,10 +245,8 @@ final membershipStatusStr =
final memberDisplayNameProvider =
FutureProvider.autoDispose.family<String?, MemberInfo>((ref, query) async {
try {
return ref
.watch(_memberProfileProvider(query))
.valueOrNull
?.displayName();
final profile = ref.watch(_memberProfileProvider(query)).valueOrNull;
return profile?.displayName();
} on RoomNotFound {
return null;
}
Expand Down Expand Up @@ -311,9 +288,7 @@ final memberAvatarInfoProvider =
final membersIdsProvider =
FutureProvider.family<List<String>, String>((ref, roomIdOrAlias) async {
final room = await ref.watch(maybeRoomProvider(roomIdOrAlias).future);
if (room == null) {
return [];
}
if (room == null) return [];
final members = await room.activeMembersIds();
return asDartStringList(members);
});
Expand Down
63 changes: 25 additions & 38 deletions app/lib/common/providers/space_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ final otherSpacesForInviteMembersProvider = FutureProvider.autoDispose
final spaceProvider =
FutureProvider.family<Space, String>((ref, spaceId) async {
final maybeSpace = await ref.watch(maybeSpaceProvider(spaceId).future);
if (maybeSpace != null) {
return maybeSpace;
}
throw 'Space not found';
if (maybeSpace == null) throw 'Space not found';
return maybeSpace;
});

final spaceIsBookmarkedProvider =
Expand All @@ -74,9 +72,7 @@ final maybeSpaceProvider =
final maybeSpaceInfoProvider =
FutureProvider.autoDispose.family<SpaceItem?, String>((ref, spaceId) async {
final space = await ref.watch(maybeSpaceProvider(spaceId).future);
if (space == null || !space.isJoined()) {
return null;
}
if (space == null || !space.isJoined()) return null;
final avatarInfo = ref.watch(roomAvatarInfoProvider(spaceId));
final membership = await space.getMyMembership();
return SpaceItem(
Expand All @@ -95,9 +91,7 @@ final selectedSpaceIdProvider =
/// gives current context space details based on id, will throw null if id is null
final selectedSpaceDetailsProvider = Provider.autoDispose<SpaceItem?>((ref) {
final selectedSpaceId = ref.watch(selectedSpaceIdProvider);
if (selectedSpaceId == null) {
return null;
}
if (selectedSpaceId == null) return null;
return ref.watch(briefSpaceItemProvider(selectedSpaceId));
});

Expand Down Expand Up @@ -145,9 +139,7 @@ final hasSpaceWithPermissionProvider =
final spaces = ref.watch(spacesProvider);
for (final element in spaces) {
final membership = await element.getMyMembership();
if (membership.canString(permission)) {
return true;
}
if (membership.canString(permission)) return true;
}
// none found
return false;
Expand All @@ -164,9 +156,8 @@ final _spaceIdAndNames =
List<_SpaceIdAndName> items = [];
for (final space in spaces) {
final roomId = space.getRoomIdStr();
items.add(
(roomId, await ref.watch(roomDisplayNameProvider(roomId).future)),
);
final dispName = await ref.watch(roomDisplayNameProvider(roomId).future);
items.add((roomId, dispName));
}
return items;
});
Expand Down Expand Up @@ -224,9 +215,7 @@ final briefSpaceItemProvider =
final spaceInvitedMembersProvider = FutureProvider.autoDispose
.family<List<Member>, String>((ref, roomIdOrAlias) async {
final space = await ref.watch(spaceProvider(roomIdOrAlias).future);
if (!space.isJoined()) {
return [];
}
if (!space.isJoined()) return [];
final members = await space.invitedMembers();
return members.toList();
});
Expand All @@ -237,27 +226,23 @@ final spaceInvitedMembersProvider = FutureProvider.autoDispose
final spaceRelationsOverviewProvider =
FutureProvider.family<SpaceRelationsOverview, String>((ref, spaceId) async {
final relatedSpaces = await ref.watch(spaceRelationsProvider(spaceId).future);
if (relatedSpaces == null) {
throw SpaceNotFound;
}
if (relatedSpaces == null) throw SpaceNotFound;
bool hasMore = false;
final List<String> knownSubspaces = [];
final List<String> knownChats = [];
final List<String> suggested = [];
for (final related in relatedSpaces.children()) {
String targetType = related.targetType();
final roomId = related.roomId().toString();
if (related.suggested()) {
suggested.add(roomId);
}
if (related.suggested()) suggested.add(roomId);

final room = ref.watch(maybeRoomProvider(roomId)).valueOrNull;
if (room == null || !room.isJoined()) {
// we don’t know this room or are not in it
hasMore = true;
continue;
}

if (targetType == 'ChatRoom') {
if (related.targetType() == 'ChatRoom') {
// we know this as a chat room
knownChats.add(roomId);
} else {
Expand Down Expand Up @@ -324,9 +309,7 @@ final spaceRemoteRelationsProvider =
FutureProvider.family<List<SpaceHierarchyRoomInfo>, String>(
(ref, spaceId) async {
final relatedSpaces = await ref.watch(spaceRelationsProvider(spaceId).future);
if (relatedSpaces == null) {
return [];
}
if (relatedSpaces == null) return [];
return (await relatedSpaces.queryHierarchy()).toList();
});

Expand Down Expand Up @@ -367,10 +350,12 @@ final suggestedChatsProvider =
await ref.watch(spaceRemoteRelationsProvider(spaceId).future);
// filter out the known rooms
final remoteRooms = roomHierarchy
.where((r) =>
!r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),)
.where(
(r) =>
!r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),
)
.toList();
return (localRooms, remoteRooms);
} on SpaceNotFound {
Expand All @@ -395,10 +380,12 @@ final suggestedSpacesProvider =
await ref.watch(spaceRemoteRelationsProvider(spaceId).future);
// filter out the known rooms
final remoteRooms = roomHierarchy
.where((r) =>
r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),)
.where(
(r) =>
r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),
)
.toList();
return (localRooms, remoteRooms);
} on SpaceNotFound {
Expand Down
22 changes: 9 additions & 13 deletions app/lib/common/themes/app_theme.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io';

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';

@pragma('vm:platform-const')
bool isDesktop = (Platform.isLinux || Platform.isWindows || Platform.isMacOS);
@pragma('vm:platform-const')
Expand All @@ -13,18 +14,13 @@ final usesNotoEmoji = !(Platform.isWindows ||
const defaultEmojiFont = 'NotoEmoji';

String? selectEmojiFont() {
switch (Platform.operatingSystem) {
case 'ios':
case 'macos':
return 'Apple Color Emoji';
case 'windows':
return 'Segoe UI Emoji';
case 'linux':
return defaultEmojiFont;
return switch (Platform.operatingSystem) {
'ios' || 'macos' => 'Apple Color Emoji',
'windows' => 'Segoe UI Emoji',
'linux' => defaultEmojiFont,
// we fallback to system supported emoji otherwise
default:
return null;
}
_ => null,
};
}

final emojiFont = selectEmojiFont();
Expand Down
31 changes: 11 additions & 20 deletions app/lib/common/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,14 @@ String getHumanReadableFileSize(int bytes) {
}

String documentTypeFromFileExtension(String fileExtension) {
switch (fileExtension) {
case '.png':
case '.jpg':
case '.jpeg':
return 'Image';
case '.mov':
case '.mp4':
return 'Video';
case '.mp3':
case '.wav':
return 'Audio';
case '.pdf':
return 'PDF';
case '.txt':
return 'Text File';
default:
return '';
}
return switch (fileExtension) {
'.png' || '.jpg' || '.jpeg' => 'Image',
'.mov' || '.mp4' => 'Video',
'.mp3' || '.wav' => 'Audio',
'.pdf' => 'PDF',
'.txt' => 'Text File',
_ => '',
};
}

Future<void> shareTextToWhatsApp(
Expand Down Expand Up @@ -269,8 +259,9 @@ Future<void> uploadAvatar(
try {
if (!context.mounted) return;
EasyLoading.show(status: L10n.of(context).avatarUploading);
final file = result.files.first;
if (file.path != null) await room.uploadAvatar(file.path!);
final filePath = result.files.first.path;
if (filePath == null) throw 'avatar path not available';
await room.uploadAvatar(filePath);
// close loading
EasyLoading.dismiss();
} catch (e, s) {
Expand Down
Loading

0 comments on commit 6088a6b

Please sign in to comment.