FurryChat/lib/utils/event_extension.dart

227 lines
9 KiB
Dart
Raw Normal View History

2020-01-19 14:07:42 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-20 12:46:39 +00:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-19 14:07:42 +00: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,
};
getLocalizedBody(BuildContext context,
{bool withSenderNamePrefix = false, hideQuotes = false}) {
if (this.redacted) {
2020-01-20 12:46:39 +00:00
return I18n.of(context)
.removedBy(redactedBecause.sender.calcDisplayname());
2020-01-19 14:07:42 +00:00
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentASticker(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.Redaction:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).redactedAnEvent(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomAliases:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).changedTheRoomAliases(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomCanonicalAlias:
2020-01-20 12:46:39 +00:00
localizedBody =
I18n.of(context).changedTheRoomInvitationLink(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomCreate:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).createdTheChat(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
(r) =>
r.toString().replaceAll("JoinRules.", "") ==
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).changedTheJoinRules(senderName);
2020-01-19 14:07:42 +00:00
} else {
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(context));
2020-01-19 14:07:42 +00: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-01-20 12:46:39 +00:00
text = I18n.of(context).acceptedTheInvitation(targetName);
2020-01-19 16:12:03 +00:00
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).rejectedTheInvitation(targetName);
2020-01-19 16:12:03 +00:00
} else {
2020-01-20 12:46:39 +00:00
text = I18n.of(context)
.hasWithdrawnTheInvitationFor(senderName, targetName);
2020-01-19 16:12:03 +00:00
}
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "leave" && newMembership == "join") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).joinedTheChat(targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "join" && newMembership == "ban") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).kickedAndBanned(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).kicked(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).userLeftTheChat(targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "invite" && newMembership == "ban") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).bannedUser(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "leave" && newMembership == "ban") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).bannedUser(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (oldMembership == "ban" && newMembership == "leave") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).unbannedUser(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (newMembership == "invite") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).invitedUser(senderName, targetName);
2020-01-19 14:07:42 +00:00
} else if (newMembership == "join") {
2020-01-20 12:46:39 +00:00
text = I18n.of(context).joinedTheChat(targetName);
2020-01-19 14:07:42 +00: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-01-20 12:46:39 +00:00
text = I18n.of(context).changedTheProfileAvatar(targetName);
2020-01-19 14:07:42 +00:00
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
2020-01-20 12:46:39 +00:00
text = I18n.of(context)
.changedTheDisplaynameTo(targetName, newDisplayname);
2020-01-19 14:07:42 +00:00
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).changedTheChatPermissions(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomName:
localizedBody =
2020-01-20 12:46:39 +00:00
I18n.of(context).changedTheChatNameTo(senderName, content["name"]);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomTopic:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context)
.changedTheChatDescriptionTo(senderName, content["topic"]);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.RoomAvatar:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).changedTheChatAvatar(senderName);
2020-01-19 14:07:42 +00:00
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
(r) =>
r.toString().replaceAll("GuestAccess.", "") ==
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
localizedBody =
2020-01-20 12:46:39 +00:00
I18n.of(context).changedTheGuestAccessRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(context));
2020-01-19 14:07:42 +00:00
}
break;
case EventTypes.HistoryVisibility:
HistoryVisibility historyVisibility = HistoryVisibility.values
.firstWhere(
(r) =>
r.toString().replaceAll("HistoryVisibility.", "") ==
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
localizedBody =
2020-01-20 12:46:39 +00:00
I18n.of(context).changedTheHistoryVisibility(senderName);
} else {
localizedBody = I18n.of(context).changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(context));
2020-01-19 14:07:42 +00:00
}
break;
case EventTypes.Encryption:
2020-01-20 12:46:39 +00:00
localizedBody =
2020-02-04 13:42:35 +00:00
I18n.of(context).activatedEndToEndEncryption(senderName) +
". " +
I18n.of(context).needPantalaimonWarning;
2020-01-19 14:07:42 +00:00
break;
case EventTypes.Encrypted:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).couldNotDecryptMessage;
2020-01-19 14:07:42 +00:00
break;
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentAPicture(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.File:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentAFile(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.Audio:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentAnAudio(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.Video:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentAVideo(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.Location:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sharedTheLocation(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.Sticker:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).sentASticker(senderName);
2020-01-19 14:07:42 +00:00
break;
case MessageTypes.Emote:
localizedBody = "* $body";
break;
case MessageTypes.Text:
case MessageTypes.Notice:
case MessageTypes.None:
case MessageTypes.Reply:
localizedBody = body;
break;
}
break;
default:
2020-01-20 12:46:39 +00:00
localizedBody = I18n.of(context).unknownEvent(this.typeKey);
2020-01-19 14:07:42 +00:00
}
// Add the sender name prefix
if (withSenderNamePrefix &&
this.type == EventTypes.Message &&
textOnlyMessageTypes.contains(this.messageType)) {
2020-02-07 18:44:16 +00:00
final String senderNameOrYou = this.senderId == room.client.userID
? I18n.of(context).you
: senderName;
localizedBody = "$senderNameOrYou: $localizedBody";
2020-01-19 14:07:42 +00:00
}
// Hide quotes
if (hideQuotes) {
List<String> lines = localizedBody.split("\n");
lines.removeWhere((s) => s.startsWith("> "));
localizedBody = lines.join("\n");
}
return localizedBody;
}
}