This commit is contained in:
Christian Pauly 2020-05-05 14:55:19 +02:00
commit f066c523a0
12 changed files with 123 additions and 119 deletions

View file

@ -12,30 +12,28 @@ extension LocalizedBody on Event {
MessageTypes.None,
};
String getLocalizedBody(BuildContext context,
String getLocalizedBody(I18n i18n,
{bool withSenderNamePrefix = false, bool hideReply = false}) {
if (this.redacted) {
return I18n.of(context)
.removedBy(redactedBecause.sender.calcDisplayname());
return i18n.removedBy(redactedBecause.sender.calcDisplayname());
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
localizedBody = I18n.of(context).sentASticker(senderName);
localizedBody = i18n.sentASticker(senderName);
break;
case EventTypes.Redaction:
localizedBody = I18n.of(context).redactedAnEvent(senderName);
localizedBody = i18n.redactedAnEvent(senderName);
break;
case EventTypes.RoomAliases:
localizedBody = I18n.of(context).changedTheRoomAliases(senderName);
localizedBody = i18n.changedTheRoomAliases(senderName);
break;
case EventTypes.RoomCanonicalAlias:
localizedBody =
I18n.of(context).changedTheRoomInvitationLink(senderName);
localizedBody = i18n.changedTheRoomInvitationLink(senderName);
break;
case EventTypes.RoomCreate:
localizedBody = I18n.of(context).createdTheChat(senderName);
localizedBody = i18n.createdTheChat(senderName);
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
@ -44,10 +42,10 @@ extension LocalizedBody on Event {
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
localizedBody = I18n.of(context).changedTheJoinRules(senderName);
localizedBody = i18n.changedTheJoinRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(context));
localizedBody = i18n.changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(i18n));
}
break;
case EventTypes.RoomMember:
@ -61,36 +59,35 @@ extension LocalizedBody on Event {
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
text = I18n.of(context).acceptedTheInvitation(targetName);
text = i18n.acceptedTheInvitation(targetName);
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
text = I18n.of(context).rejectedTheInvitation(targetName);
text = i18n.rejectedTheInvitation(targetName);
} else {
text = I18n.of(context)
.hasWithdrawnTheInvitationFor(senderName, targetName);
text = i18n.hasWithdrawnTheInvitationFor(senderName, targetName);
}
} else if (oldMembership == "leave" && newMembership == "join") {
text = I18n.of(context).joinedTheChat(targetName);
text = i18n.joinedTheChat(targetName);
} else if (oldMembership == "join" && newMembership == "ban") {
text = I18n.of(context).kickedAndBanned(senderName, targetName);
text = i18n.kickedAndBanned(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
text = I18n.of(context).kicked(senderName, targetName);
text = i18n.kicked(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
text = I18n.of(context).userLeftTheChat(targetName);
text = i18n.userLeftTheChat(targetName);
} else if (oldMembership == "invite" && newMembership == "ban") {
text = I18n.of(context).bannedUser(senderName, targetName);
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "leave" && newMembership == "ban") {
text = I18n.of(context).bannedUser(senderName, targetName);
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "ban" && newMembership == "leave") {
text = I18n.of(context).unbannedUser(senderName, targetName);
text = i18n.unbannedUser(senderName, targetName);
} else if (newMembership == "invite") {
text = I18n.of(context).invitedUser(senderName, targetName);
text = i18n.invitedUser(senderName, targetName);
} else if (newMembership == "join") {
text = I18n.of(context).joinedTheChat(targetName);
text = i18n.joinedTheChat(targetName);
}
} else if (newMembership == "join") {
final String newAvatar = this.content["avatar_url"] ?? "";
@ -107,29 +104,27 @@ extension LocalizedBody on Event {
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
text = I18n.of(context).changedTheProfileAvatar(targetName);
text = i18n.changedTheProfileAvatar(targetName);
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
text = I18n.of(context)
.changedTheDisplaynameTo(targetName, newDisplayname);
text = i18n.changedTheDisplaynameTo(targetName, newDisplayname);
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
localizedBody = I18n.of(context).changedTheChatPermissions(senderName);
localizedBody = i18n.changedTheChatPermissions(senderName);
break;
case EventTypes.RoomName:
localizedBody =
I18n.of(context).changedTheChatNameTo(senderName, content["name"]);
localizedBody = i18n.changedTheChatNameTo(senderName, content["name"]);
break;
case EventTypes.RoomTopic:
localizedBody = I18n.of(context)
.changedTheChatDescriptionTo(senderName, content["topic"]);
localizedBody =
i18n.changedTheChatDescriptionTo(senderName, content["topic"]);
break;
case EventTypes.RoomAvatar:
localizedBody = I18n.of(context).changedTheChatAvatar(senderName);
localizedBody = i18n.changedTheChatAvatar(senderName);
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
@ -138,11 +133,10 @@ extension LocalizedBody on Event {
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
localizedBody =
I18n.of(context).changedTheGuestAccessRules(senderName);
localizedBody = i18n.changedTheGuestAccessRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(context));
localizedBody = i18n.changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(i18n));
}
break;
case EventTypes.HistoryVisibility:
@ -153,40 +147,38 @@ extension LocalizedBody on Event {
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
localizedBody =
I18n.of(context).changedTheHistoryVisibility(senderName);
localizedBody = i18n.changedTheHistoryVisibility(senderName);
} else {
localizedBody = I18n.of(context).changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(context));
localizedBody = i18n.changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(i18n));
}
break;
case EventTypes.Encryption:
localizedBody =
I18n.of(context).activatedEndToEndEncryption(senderName);
localizedBody = i18n.activatedEndToEndEncryption(senderName);
if (!room.client.encryptionEnabled) {
localizedBody += ". " + I18n.of(context).needPantalaimonWarning;
localizedBody += ". " + i18n.needPantalaimonWarning;
}
break;
case EventTypes.Encrypted:
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
localizedBody = I18n.of(context).sentAPicture(senderName);
localizedBody = i18n.sentAPicture(senderName);
break;
case MessageTypes.File:
localizedBody = I18n.of(context).sentAFile(senderName);
localizedBody = i18n.sentAFile(senderName);
break;
case MessageTypes.Audio:
localizedBody = I18n.of(context).sentAnAudio(senderName);
localizedBody = i18n.sentAnAudio(senderName);
break;
case MessageTypes.Video:
localizedBody = I18n.of(context).sentAVideo(senderName);
localizedBody = i18n.sentAVideo(senderName);
break;
case MessageTypes.Location:
localizedBody = I18n.of(context).sharedTheLocation(senderName);
localizedBody = i18n.sharedTheLocation(senderName);
break;
case MessageTypes.Sticker:
localizedBody = I18n.of(context).sentASticker(senderName);
localizedBody = i18n.sentASticker(senderName);
break;
case MessageTypes.Emote:
localizedBody = "* $body";
@ -195,25 +187,23 @@ extension LocalizedBody on Event {
String errorText;
switch (body) {
case DecryptError.CHANNEL_CORRUPTED:
errorText = I18n.of(context).channelCorruptedDecryptError + ".";
errorText = i18n.channelCorruptedDecryptError + ".";
break;
case DecryptError.NOT_ENABLED:
errorText = I18n.of(context).encryptionNotEnabled + ".";
errorText = i18n.encryptionNotEnabled + ".";
break;
case DecryptError.UNKNOWN_ALGORITHM:
errorText = I18n.of(context).unknownEncryptionAlgorithm + ".";
errorText = i18n.unknownEncryptionAlgorithm + ".";
break;
case DecryptError.UNKNOWN_SESSION:
errorText = I18n.of(context).noPermission + ".";
errorText = i18n.noPermission + ".";
break;
default:
errorText = body;
break;
}
localizedBody = "🔒 " +
I18n.of(context).couldNotDecryptMessage +
": " +
errorText;
localizedBody =
"🔒 " + i18n.couldNotDecryptMessage + ": " + errorText;
break;
case MessageTypes.Text:
case MessageTypes.Notice:
@ -224,7 +214,7 @@ extension LocalizedBody on Event {
}
break;
default:
localizedBody = I18n.of(context).unknownEvent(this.typeKey);
localizedBody = i18n.unknownEvent(this.typeKey);
}
// Hide reply fallback
@ -237,9 +227,8 @@ extension LocalizedBody on Event {
if (withSenderNamePrefix &&
this.type == EventTypes.Message &&
textOnlyMessageTypes.contains(this.messageType)) {
final String senderNameOrYou = this.senderId == room.client.userID
? I18n.of(context).you
: senderName;
final String senderNameOrYou =
this.senderId == room.client.userID ? i18n.you : senderName;
localizedBody = "$senderNameOrYou: $localizedBody";
}

View file

@ -160,21 +160,25 @@ abstract class FirebaseController {
: i18n.unreadMessages(unreadEvents.toString());
// Calculate the body
final String body = event.getLocalizedBody(context,
withSenderNamePrefix: true, hideReply: true);
final String body = event.getLocalizedBody(
i18n,
withSenderNamePrefix: true,
hideReply: true,
);
// The person object for the android message style notification
final person = Person(
name: room.getLocalizedDisplayname(context),
name: room.getLocalizedDisplayname(i18n),
icon: room.avatar == null
? null
: await downloadAndSaveAvatar(
room.avatar,
client,
width: 126,
height: 126,
: BitmapFilePathAndroidIcon(
await downloadAndSaveAvatar(
room.avatar,
client,
width: 126,
height: 126,
),
),
iconSource: IconSource.FilePath,
);
// Show notification
@ -182,7 +186,6 @@ abstract class FirebaseController {
'fluffychat_push',
'FluffyChat push channel',
'Push notifications for FluffyChat',
style: AndroidNotificationStyle.Messaging,
styleInformation: MessagingStyleInformation(
person,
conversationTitle: title,
@ -200,8 +203,8 @@ abstract class FirebaseController {
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(0,
room.getLocalizedDisplayname(context), body, platformChannelSpecifics,
await _flutterLocalNotificationsPlugin.show(
0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics,
payload: roomId);
} catch (exception) {
debugPrint("[Push] Error while processing notification: " +

View file

@ -1,20 +1,19 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension LocalizedRoomDisplayname on Room {
String getLocalizedDisplayname(BuildContext context) {
String getLocalizedDisplayname(I18n i18n) {
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes != null && this.mHeroes.isNotEmpty)) {
return I18n.of(context).groupWith(this.displayname);
return i18n.groupWith(this.displayname);
}
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes?.isEmpty ?? true)) {
return I18n.of(context).emptyChat;
return i18n.emptyChat;
}
return this.displayname;
}

View file

@ -1,18 +1,17 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension HistoryVisibilityDisplayString on HistoryVisibility {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case HistoryVisibility.invited:
return I18n.of(context).fromTheInvitation;
return i18n.fromTheInvitation;
case HistoryVisibility.joined:
return I18n.of(context).fromJoining;
return i18n.fromJoining;
case HistoryVisibility.shared:
return I18n.of(context).visibleForAllParticipants;
return i18n.visibleForAllParticipants;
case HistoryVisibility.world_readable:
return I18n.of(context).visibleForEveryone;
return i18n.visibleForEveryone;
default:
return this.toString().replaceAll("HistoryVisibility.", "");
}
@ -20,12 +19,12 @@ extension HistoryVisibilityDisplayString on HistoryVisibility {
}
extension GuestAccessDisplayString on GuestAccess {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case GuestAccess.can_join:
return I18n.of(context).guestsCanJoin;
return i18n.guestsCanJoin;
case GuestAccess.forbidden:
return I18n.of(context).guestsAreForbidden;
return i18n.guestsAreForbidden;
default:
return this.toString().replaceAll("GuestAccess.", "");
}
@ -33,12 +32,12 @@ extension GuestAccessDisplayString on GuestAccess {
}
extension JoinRulesDisplayString on JoinRules {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case JoinRules.public:
return I18n.of(context).anyoneCanJoin;
return i18n.anyoneCanJoin;
case JoinRules.invite:
return I18n.of(context).invitedUsersOnly;
return i18n.invitedUsersOnly;
default:
return this.toString().replaceAll("JoinRules.", "");
}