Skip to content

Commit

Permalink
style: code format
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-damocles committed Jan 8, 2025
1 parent c4dd9ad commit 9330702
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'conversation_message_actions_provider.c.g.dart';

@Riverpod(keepAlive: true)
Raw<Future<ConversationMessageActionsService>>
conversationMessageActionsService(
Raw<Future<ConversationMessageActionsService>> conversationMessageActionsService(
Ref ref,
) async {
final databaseService = ref.watch(conversationsDBServiceProvider);
final conversationMessageManagementService =
await ref.watch(conversationMessageManagementServiceProvider);

final eventSigner =
await ref.watch(currentUserNostrEventSignerProvider.future);
final eventSigner = await ref.watch(currentUserNostrEventSignerProvider.future);

return ConversationMessageActionsService(
eventSigner: eventSigner,
Expand Down Expand Up @@ -67,8 +65,7 @@ class ConversationMessageActionsService {
final IonConnectSealService sealService;
final IonConnectGiftWrapService wrapService;
final ConversationsDBService databaseService;
final ConversationMessageManagementService
conversationMessageManagementService;
final ConversationMessageManagementService conversationMessageManagementService;

Future<void> deleteMessage(String id) async {
await databaseService.markConversationMessageAsDeleted(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'conversation_message_management_provider.c.g.dart';

@Riverpod(keepAlive: true)
Raw<Future<ConversationMessageManagementService>>
conversationMessageManagementService(
Raw<Future<ConversationMessageManagementService>> conversationMessageManagementService(
Ref ref,
) async {
final eventSigner =
await ref.watch(currentUserNostrEventSignerProvider.future);
final eventSigner = await ref.watch(currentUserNostrEventSignerProvider.future);

return ConversationMessageManagementService(
eventSigner: eventSigner,
Expand Down Expand Up @@ -89,8 +87,7 @@ class ConversationMessageManagementService {

final results = await Future.wait(
participantsPubkeys.map((participantPubkey) async {
final encryptedMediaFiles =
await _encryptMediaFiles(compressedMediaFiles);
final encryptedMediaFiles = await _encryptMediaFiles(compressedMediaFiles);

final uploadedMediaFilesWithKeys = await Future.wait(
encryptedMediaFiles.map((encryptedMediaFile) async {
Expand Down Expand Up @@ -192,8 +189,7 @@ class ConversationMessageManagementService {
);

if (attachment.mediaType == MediaType.unknown) {
final decompressedFile =
await compressionService.decompressBrotli(file);
final decompressedFile = await compressionService.decompressBrotli(file);

decryptedDecompressedFiles.add(decompressedFile);
} else {
Expand Down Expand Up @@ -294,8 +290,7 @@ class ConversationMessageManagementService {
final mediaType = MediaType.fromMimeType(mediaFile.mimeType ?? '');

final compressedMediaFile = switch (mediaType) {
MediaType.video =>
await compressionService.compressVideo(mediaFile),
MediaType.video => await compressionService.compressVideo(mediaFile),
MediaType.image => await compressionService.compressImage(
mediaFile,
width: mediaFile.width,
Expand All @@ -308,9 +303,7 @@ class ConversationMessageManagementService {
path: await compressionService.compressAudio(mediaFile.path),
),
MediaType.unknown => MediaFile(
path: (await compressionService
.compressWithBrotli(File(mediaFile.path)))
.path,
path: (await compressionService.compressWithBrotli(File(mediaFile.path))).path,
)
};

Expand All @@ -332,14 +325,12 @@ class ConversationMessageManagementService {
) async {
final encryptedMediaFiles = await Future.wait(
compressedMediaFiles.map(
(compressedMediaFile) =>
Isolate.run<(MediaFile, String, String, String)>(() async {
(compressedMediaFile) => Isolate.run<(MediaFile, String, String, String)>(() async {
final secretKey = await AesGcm.with256bits().newSecretKey();
final secretKeyBytes = await secretKey.extractBytes();
final secretKeyString = base64Encode(secretKeyBytes);

final compressedMediaFileBytes =
await File(compressedMediaFile.path).readAsBytes();
final compressedMediaFileBytes = await File(compressedMediaFile.path).readAsBytes();

final secretBox = await AesGcm.with256bits().encrypt(
compressedMediaFileBytes,
Expand All @@ -350,8 +341,7 @@ class ConversationMessageManagementService {
final nonceString = base64Encode(nonceBytes);
final macString = base64Encode(secretBox.mac.bytes);

final compressedEncryptedFile =
File('${compressedMediaFile.path}.enc');
final compressedEncryptedFile = File('${compressedMediaFile.path}.enc');
// Rewrite compressed fieles with encrypted data
await compressedEncryptedFile.writeAsBytes(secretBox.cipherText);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'e2ee_group_conversation_management_provider.c.g.dart';

@Riverpod(keepAlive: true)
Raw<Future<E2EEGroupConversationManagementService>>
e2eeGroupConversationManagementService(
Raw<Future<E2EEGroupConversationManagementService>> e2eeGroupConversationManagementService(
Ref ref,
) async {
final databaseService = ref.watch(conversationsDBServiceProvider);
Expand All @@ -32,8 +31,7 @@ class E2EEGroupConversationManagementService {
});

final ConversationsDBService databaseService;
final ConversationMessageManagementService
conversationMessageManagementService;
final ConversationMessageManagementService conversationMessageManagementService;

Future<void> createGroup({
required String subject,
Expand All @@ -55,12 +53,10 @@ class E2EEGroupConversationManagementService {
assert(conversationSubject.isNotEmpty, 'Conversation subject is empty');
assert(participantPubkey.isNotEmpty, 'Participant pubkey is empty');

final conversationsEventMessages =
await databaseService.getAllConversations();
final conversationsEventMessages = await databaseService.getAllConversations();

final conversationsEntities = conversationsEventMessages
.map(PrivateDirectMessageEntity.fromEventMessage)
.toList();
final conversationsEntities =
conversationsEventMessages.map(PrivateDirectMessageEntity.fromEventMessage).toList();

final pubkeys = conversationsEntities
.singleWhere(
Expand All @@ -87,12 +83,10 @@ class E2EEGroupConversationManagementService {
assert(conversationSubject.isNotEmpty, 'Conversation subject is empty');
assert(participantPubkey.isNotEmpty, 'Participant pubkey is empty');

final conversationsEventMessages =
await databaseService.getAllConversations();
final conversationsEventMessages = await databaseService.getAllConversations();

final conversationsEntities = conversationsEventMessages
.map(PrivateDirectMessageEntity.fromEventMessage)
.toList();
final conversationsEntities =
conversationsEventMessages.map(PrivateDirectMessageEntity.fromEventMessage).toList();

final pubkeys = conversationsEntities
.singleWhere(
Expand Down Expand Up @@ -123,12 +117,10 @@ class E2EEGroupConversationManagementService {
assert(currentSubject.isNotEmpty, 'Current conversation subject is empty');
assert(newSubject.isNotEmpty, 'New conversation subject is empty');

final conversationsEventMessages =
await databaseService.getAllConversations();
final conversationsEventMessages = await databaseService.getAllConversations();

final conversationsEntities = conversationsEventMessages
.map(PrivateDirectMessageEntity.fromEventMessage)
.toList();
final conversationsEntities =
conversationsEventMessages.map(PrivateDirectMessageEntity.fromEventMessage).toList();

final pubkeys = conversationsEntities
.singleWhere(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class CreateGroupModal extends HookConsumerWidget {
final formKey = useMemoized(GlobalKey<FormState>.new);
final currentPubkey = ref.watch(currentPubkeySelectorProvider).valueOrNull;
final createGroupForm = ref.watch(createGroupFormControllerProvider);
final createGroupFormNotifier =
ref.watch(createGroupFormControllerProvider.notifier);
final createGroupFormNotifier = ref.watch(createGroupFormControllerProvider.notifier);
final nameController = useTextEditingController(text: createGroupForm.name);
final members = createGroupForm.members.toList();

Expand Down Expand Up @@ -115,8 +114,7 @@ class CreateGroupModal extends HookConsumerWidget {
Assets.svg.iconCategoriesFollowing.icon(size: 16.0.s),
SizedBox(width: 6.0.s),
Text(
context.i18n
.group_create_members_number(members.length),
context.i18n.group_create_members_number(members.length),
),
const Spacer(),
TextButton(
Expand Down Expand Up @@ -174,8 +172,7 @@ class CreateGroupModal extends HookConsumerWidget {
e2eeGroupConversationManagementServiceProvider,
);

final avatarProcessorState =
ref.read(avatarProcessorNotifierProvider);
final avatarProcessorState = ref.read(avatarProcessorNotifierProvider);

final groupPicture = avatarProcessorState.whenOrNull(
cropped: (file) => file,
Expand Down
67 changes: 22 additions & 45 deletions lib/app/features/nostr/providers/nostr_notifier.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class NostrNotifier extends _$NostrNotifier {

return null;
},
retryWhen: (error) =>
error is RelayRequestFailedException || _isAuthRequired(error),
retryWhen: (error) => error is RelayRequestFailedException || _isAuthRequired(error),
onRetry: () {
if (relay != null) dislikedRelaysUrls.add(relay!.url);
},
Expand All @@ -72,15 +71,13 @@ class NostrNotifier extends _$NostrNotifier {
ActionSource actionSource = const ActionSourceCurrentUser(),
bool cache = true,
}) async {
final result =
await sendEvents([event], actionSource: actionSource, cache: cache);
final result = await sendEvents([event], actionSource: actionSource, cache: cache);
return result?.elementAtOrNull(0);
}

Future<void> sendAuthEvent(NostrRelay relay) async {
final challenge = ref.read(authChallengeProvider(relay.url));
if (challenge == null && challenge.isEmpty)
throw AuthChallengeIsEmptyException();
if (challenge == null && challenge.isEmpty) throw AuthChallengeIsEmptyException();

final authEvent = AuthEvent(
challenge: challenge!,
Expand Down Expand Up @@ -120,8 +117,7 @@ class NostrNotifier extends _$NostrNotifier {
ActionSource actionSource = const ActionSourceCurrentUser(),
bool cache = true,
}) async {
final entities = await sendEntitiesData([entityData],
actionSource: actionSource, cache: cache);
final entities = await sendEntitiesData([entityData], actionSource: actionSource, cache: cache);
return entities?.elementAtOrNull(0);
}

Expand All @@ -141,8 +137,8 @@ class NostrNotifier extends _$NostrNotifier {
await sendAuthEvent(relay!);
}

await for (final event in nd.requestEvents(requestMessage, relay!,
keepSubscription: keepSubscription)) {
await for (final event
in nd.requestEvents(requestMessage, relay!, keepSubscription: keepSubscription)) {
if (event is NoticeMessage || event is ClosedMessage) {
throw RelayRequestFailedException(
relayUrl: relay!.url,
Expand All @@ -153,8 +149,7 @@ class NostrNotifier extends _$NostrNotifier {
}
}
},
retryWhen: (error) =>
error is RelayRequestFailedException || _isAuthRequired(error),
retryWhen: (error) => error is RelayRequestFailedException || _isAuthRequired(error),
onRetry: () {
if (relay != null) dislikedRelaysUrls.add(relay!.url);
},
Expand All @@ -165,8 +160,7 @@ class NostrNotifier extends _$NostrNotifier {
RequestMessage requestMessage, {
ActionSource actionSource = const ActionSourceCurrentUser(),
}) async {
final eventsStream =
requestEvents(requestMessage, actionSource: actionSource);
final eventsStream = requestEvents(requestMessage, actionSource: actionSource);
final events = await eventsStream.toList();
return events.isNotEmpty ? events.first : null;
}
Expand All @@ -175,13 +169,11 @@ class NostrNotifier extends _$NostrNotifier {
RequestMessage requestMessage, {
ActionSource actionSource = const ActionSourceCurrentUser(),
}) async* {
await for (final event
in requestEvents(requestMessage, actionSource: actionSource)) {
await for (final event in requestEvents(requestMessage, actionSource: actionSource)) {
try {
yield _parseAndCache(event);
} catch (error, stackTrace) {
Logger.log('Failed to process event ${event.id}',
error: error, stackTrace: stackTrace);
Logger.log('Failed to process event ${event.id}', error: error, stackTrace: stackTrace);
}
}
}
Expand All @@ -190,8 +182,7 @@ class NostrNotifier extends _$NostrNotifier {
RequestMessage requestMessage, {
ActionSource actionSource = const ActionSourceCurrentUser(),
}) async {
final entitiesStream =
requestEntities(requestMessage, actionSource: actionSource);
final entitiesStream = requestEntities(requestMessage, actionSource: actionSource);
final entities = await entitiesStream.toList();
return entities.isNotEmpty ? entities.first as T : null;
}
Expand All @@ -204,20 +195,16 @@ class NostrNotifier extends _$NostrNotifier {
final relay = await _getRelay(actionSource);
relay.sendMessage(requestEventMessage);
return relay.messages
.where((message) =>
message is EventMessage &&
message.kind == EventCountResultEntity.kind)
.where((message) => message is EventMessage && message.kind == EventCountResultEntity.kind)
.cast<EventMessage>()
.map(EventCountResultEntity.fromEventMessage)
.firstWhere(
(countResult) =>
countResult.data.requestEventId == requestEventMessage.id,
(countResult) => countResult.data.requestEventId == requestEventMessage.id,
);
}

Future<EventMessage> sign(EventSerializable entityData) async {
final eventSigner =
ref.read(currentUserNostrEventSignerProvider).valueOrNull;
final eventSigner = ref.read(currentUserNostrEventSignerProvider).valueOrNull;
final mainWallet = ref.read(mainWalletProvider).valueOrNull;

if (eventSigner == null) {
Expand All @@ -237,9 +224,7 @@ class NostrNotifier extends _$NostrNotifier {
}

bool _isAuthRequired(Object? error) =>
error != null &&
(error is SendEventException) &&
error.code.startsWith('auth-required');
error != null && (error is SendEventException) && error.code.startsWith('auth-required');

Future<NostrRelay> _getRelay(
ActionSource actionSource, {
Expand All @@ -253,15 +238,13 @@ class NostrNotifier extends _$NostrNotifier {
throw UserMasterPubkeyNotFoundException();
}
final userRelays = await _getUserRelays(pubkey);
final relays = _userRelaysAvoidingDislikedUrls(
userRelays.data.list, dislikedUrls);
final relays = _userRelaysAvoidingDislikedUrls(userRelays.data.list, dislikedUrls);
return await ref.read(relayProvider(relays.random.url).future);
}
case ActionSourceUser():
{
final userRelays = await _getUserRelays(actionSource.pubkey);
final relays = _userRelaysAvoidingDislikedUrls(
userRelays.data.list, dislikedUrls);
final relays = _userRelaysAvoidingDislikedUrls(userRelays.data.list, dislikedUrls);
return await ref.read(relayProvider(relays.random.url).future);
}
case ActionSourceIndexers():
Expand All @@ -285,15 +268,13 @@ class NostrNotifier extends _$NostrNotifier {
throw UserMasterPubkeyNotFoundException();
}
final userChatRelays = await _getUserChatRelays(pubkey);
final relays = _userRelaysAvoidingDislikedUrls(
userChatRelays.data.list, dislikedUrls);
final relays = _userRelaysAvoidingDislikedUrls(userChatRelays.data.list, dislikedUrls);
return await ref.read(relayProvider(relays.random.url).future);
}
case ActionSourceUserChat():
{
final userChatRelays = await _getUserChatRelays(actionSource.pubkey);
final relays = _userRelaysAvoidingDislikedUrls(
userChatRelays.data.list, dislikedUrls);
final relays = _userRelaysAvoidingDislikedUrls(userChatRelays.data.list, dislikedUrls);
return await ref.read(relayProvider(relays.random.url).future);
}
}
Expand Down Expand Up @@ -328,17 +309,13 @@ class NostrNotifier extends _$NostrNotifier {
List<UserRelay> relays,
Set<String> dislikedRelaysUrls,
) {
final urls = relays
.where((relay) => !dislikedRelaysUrls.contains(relay.url))
.toList();
final urls = relays.where((relay) => !dislikedRelaysUrls.contains(relay.url)).toList();
if (urls.isEmpty) return relays;
return urls;
}

List<String> _indexersAvoidingDislikedUrls(
List<String> indexers, Set<String> dislikedUrls) {
var urls =
indexers.where((indexer) => !dislikedUrls.contains(indexer)).toList();
List<String> _indexersAvoidingDislikedUrls(List<String> indexers, Set<String> dislikedUrls) {
var urls = indexers.where((indexer) => !dislikedUrls.contains(indexer)).toList();
if (urls.isEmpty) {
urls = indexers;
}
Expand Down

0 comments on commit 9330702

Please sign in to comment.