fluffychat/lib/utils/event_extension.dart

272 lines
10 KiB
Dart
Raw Normal View History

2020-01-19 15:07:42 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-20 13:46:39 +01:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-19 15:07:42 +01:00
import 'package:flutter/material.dart';
import 'room_state_enums_extensions.dart';
extension LocalizedBody on Event {
static Set<MessageTypes> textOnlyMessageTypes = {
MessageTypes.Text,
MessageTypes.Reply,
MessageTypes.Notice,
MessageTypes.Emote,
MessageTypes.None,
};
2020-05-05 14:55:19 +02:00
String getLocalizedBody(I18n i18n,
2020-02-20 20:45:38 +01:00
{bool withSenderNamePrefix = false, bool hideReply = false}) {
2020-01-19 15:07:42 +01:00
if (this.redacted) {
2020-05-05 14:55:19 +02:00
return i18n.removedBy(redactedBecause.sender.calcDisplayname());
2020-01-19 15:07:42 +01:00
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentASticker(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.Redaction:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.redactedAnEvent(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomAliases:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheRoomAliases(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomCanonicalAlias:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheRoomInvitationLink(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomCreate:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.createdTheChat(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
(r) =>
r.toString().replaceAll("JoinRules.", "") ==
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheJoinRules(senderName);
2020-01-19 15:07:42 +01:00
} else {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(i18n));
2020-01-19 15:07:42 +01:00
}
break;
case EventTypes.RoomMember:
String text = "Failed to parse member event";
final String targetName = this.stateKeyUser.calcDisplayname();
// Has the membership changed?
final String newMembership = this.content["membership"] ?? "";
final String oldMembership =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["membership"] ?? ""
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
2020-05-05 14:55:19 +02:00
text = i18n.acceptedTheInvitation(targetName);
2020-01-19 17:12:03 +01:00
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
2020-05-05 14:55:19 +02:00
text = i18n.rejectedTheInvitation(targetName);
2020-01-19 17:12:03 +01:00
} else {
2020-05-05 14:55:19 +02:00
text = i18n.hasWithdrawnTheInvitationFor(senderName, targetName);
2020-01-19 17:12:03 +01:00
}
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "leave" && newMembership == "join") {
2020-05-05 14:55:19 +02:00
text = i18n.joinedTheChat(targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "join" && newMembership == "ban") {
2020-05-05 14:55:19 +02:00
text = i18n.kickedAndBanned(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
2020-05-05 14:55:19 +02:00
text = i18n.kicked(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
2020-05-05 14:55:19 +02:00
text = i18n.userLeftTheChat(targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "invite" && newMembership == "ban") {
2020-05-05 14:55:19 +02:00
text = i18n.bannedUser(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "leave" && newMembership == "ban") {
2020-05-05 14:55:19 +02:00
text = i18n.bannedUser(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (oldMembership == "ban" && newMembership == "leave") {
2020-05-05 14:55:19 +02:00
text = i18n.unbannedUser(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (newMembership == "invite") {
2020-05-05 14:55:19 +02:00
text = i18n.invitedUser(senderName, targetName);
2020-01-19 15:07:42 +01:00
} else if (newMembership == "join") {
2020-05-05 14:55:19 +02:00
text = i18n.joinedTheChat(targetName);
2020-01-19 15:07:42 +01:00
}
} else if (newMembership == "join") {
final String newAvatar = this.content["avatar_url"] ?? "";
final String oldAvatar =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["avatar_url"] ?? ""
: "";
final String newDisplayname = this.content["displayname"] ?? "";
final String oldDisplayname =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["displayname"] ?? ""
: "";
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
2020-05-05 14:55:19 +02:00
text = i18n.changedTheProfileAvatar(targetName);
2020-01-19 15:07:42 +01:00
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
2020-05-05 14:55:19 +02:00
text = i18n.changedTheDisplaynameTo(targetName, newDisplayname);
2020-01-19 15:07:42 +01:00
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheChatPermissions(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomName:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheChatNameTo(senderName, content["name"]);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomTopic:
2020-05-05 14:55:19 +02:00
localizedBody =
i18n.changedTheChatDescriptionTo(senderName, content["topic"]);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.RoomAvatar:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheChatAvatar(senderName);
2020-01-19 15:07:42 +01:00
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
(r) =>
r.toString().replaceAll("GuestAccess.", "") ==
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheGuestAccessRules(senderName);
2020-01-20 13:46:39 +01:00
} else {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(i18n));
2020-01-19 15:07:42 +01:00
}
break;
case EventTypes.HistoryVisibility:
HistoryVisibility historyVisibility = HistoryVisibility.values
.firstWhere(
(r) =>
r.toString().replaceAll("HistoryVisibility.", "") ==
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheHistoryVisibility(senderName);
2020-01-20 13:46:39 +01:00
} else {
2020-05-05 14:55:19 +02:00
localizedBody = i18n.changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(i18n));
2020-01-19 15:07:42 +01:00
}
break;
case EventTypes.Encryption:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.activatedEndToEndEncryption(senderName);
2020-02-15 09:20:08 +01:00
if (!room.client.encryptionEnabled) {
2020-05-05 14:55:19 +02:00
localizedBody += ". " + i18n.needPantalaimonWarning;
2020-02-15 09:20:08 +01:00
}
2020-01-19 15:07:42 +01:00
break;
case EventTypes.Encrypted:
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentAPicture(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.File:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentAFile(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.Audio:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentAnAudio(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.Video:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentAVideo(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.Location:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sharedTheLocation(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.Sticker:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.sentASticker(senderName);
2020-01-19 15:07:42 +01:00
break;
case MessageTypes.Emote:
localizedBody = "* $body";
break;
2020-02-18 08:13:20 +01:00
case MessageTypes.BadEncrypted:
2020-02-22 08:27:08 +01:00
String errorText;
switch (body) {
case DecryptError.CHANNEL_CORRUPTED:
2020-05-05 14:55:19 +02:00
errorText = i18n.channelCorruptedDecryptError + ".";
2020-02-22 08:27:08 +01:00
break;
case DecryptError.NOT_ENABLED:
2020-05-05 14:55:19 +02:00
errorText = i18n.encryptionNotEnabled + ".";
2020-02-22 08:27:08 +01:00
break;
case DecryptError.UNKNOWN_ALGORITHM:
2020-05-05 14:55:19 +02:00
errorText = i18n.unknownEncryptionAlgorithm + ".";
2020-02-22 08:27:08 +01:00
break;
case DecryptError.UNKNOWN_SESSION:
2020-05-05 14:55:19 +02:00
errorText = i18n.noPermission + ".";
2020-02-22 08:27:08 +01:00
break;
default:
errorText = body;
break;
}
2020-05-05 14:55:19 +02:00
localizedBody =
"🔒 " + i18n.couldNotDecryptMessage + ": " + errorText;
2020-02-18 08:13:20 +01:00
break;
2020-01-19 15:07:42 +01:00
case MessageTypes.Text:
case MessageTypes.Notice:
case MessageTypes.None:
case MessageTypes.Reply:
localizedBody = body;
break;
}
break;
default:
2020-05-05 14:55:19 +02:00
localizedBody = i18n.unknownEvent(this.typeKey);
2020-01-19 15:07:42 +01:00
}
2020-02-20 20:45:38 +01:00
// Hide reply fallback
if (hideReply) {
2020-02-21 09:45:37 +01:00
localizedBody = localizedBody.replaceFirst(
2020-02-23 08:49:58 +01:00
RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), "");
2020-02-09 16:26:24 +01:00
}
2020-01-19 15:07:42 +01:00
// Add the sender name prefix
if (withSenderNamePrefix &&
this.type == EventTypes.Message &&
textOnlyMessageTypes.contains(this.messageType)) {
2020-05-05 14:55:19 +02:00
final String senderNameOrYou =
this.senderId == room.client.userID ? i18n.you : senderName;
2020-02-07 19:44:16 +01:00
localizedBody = "$senderNameOrYou: $localizedBody";
2020-01-19 15:07:42 +01:00
}
return localizedBody;
}
2020-03-13 20:09:32 +01:00
IconData get statusIcon {
switch (this.status) {
case -1:
return Icons.error_outline;
case 0:
return Icons.timer;
case 1:
return Icons.done;
case 2:
return Icons.done_all;
default:
return Icons.done;
}
}
2020-03-13 21:58:48 +01:00
String get sizeString {
if (content["info"] is Map<String, dynamic> &&
content["info"].containsKey("size")) {
num size = content["info"]["size"];
if (size < 1000000) {
size = size / 1000;
return "${size.toString()}kb";
} else if (size < 1000000000) {
size = size / 1000000;
return "${size.toString()}mb";
} else {
size = size / 1000000000;
return "${size.toString()}gb";
}
2020-03-14 11:05:28 +01:00
} else {
2020-03-13 21:58:48 +01:00
return null;
2020-03-14 11:05:28 +01:00
}
2020-03-13 21:58:48 +01:00
}
2020-01-19 15:07:42 +01:00
}