veilidchat/lib/proto/extensions.dart

51 lines
1.6 KiB
Dart
Raw Normal View History

2024-05-29 10:47:43 -04:00
import 'dart:typed_data';
import 'package:veilid_support/veilid_support.dart';
2024-05-25 22:46:43 -04:00
import 'proto.dart' as proto;
proto.Message messageFromJson(Map<String, dynamic> j) =>
proto.Message.create()..mergeFromJsonMap(j);
Map<String, dynamic> messageToJson(proto.Message m) => m.writeToJsonMap();
proto.ReconciledMessage reconciledMessageFromJson(Map<String, dynamic> j) =>
proto.ReconciledMessage.create()..mergeFromJsonMap(j);
Map<String, dynamic> reconciledMessageToJson(proto.ReconciledMessage m) =>
m.writeToJsonMap();
2024-05-29 10:47:43 -04:00
extension MessageExt on proto.Message {
2024-05-31 18:27:50 -04:00
Uint8List get idBytes => Uint8List.fromList(id);
Uint8List get authorUniqueIdBytes {
2024-05-29 10:47:43 -04:00
final author = this.author.toVeilid().decode();
final id = this.id;
return Uint8List.fromList([...author, ...id]);
}
2024-05-31 18:27:50 -04:00
String get authorUniqueIdString => base64UrlNoPadEncode(authorUniqueIdBytes);
2024-05-30 23:25:47 -04:00
static int compareTimestamp(proto.Message a, proto.Message b) =>
a.timestamp.compareTo(b.timestamp);
2024-05-29 10:47:43 -04:00
}
2024-06-15 23:29:15 -04:00
extension ContactExt on proto.Contact {
String get nameOrNickname => nickname.isNotEmpty ? nickname : profile.name;
2024-06-15 23:29:15 -04:00
String get displayName =>
nickname.isNotEmpty ? '$nickname (${profile.name})' : profile.name;
}
2024-06-20 19:04:39 -04:00
extension ChatExt on proto.Chat {
TypedKey get localConversationRecordKey {
switch (whichKind()) {
case proto.Chat_Kind.direct:
return direct.localConversationRecordKey.toVeilid();
case proto.Chat_Kind.group:
return group.localConversationRecordKey.toVeilid();
case proto.Chat_Kind.notSet:
throw StateError('unknown chat kind');
}
}
}