fluffychat/lib/components/message_content.dart

325 lines
12 KiB
Dart
Raw Normal View History

2020-01-03 09:09:14 +01:00
import 'package:bubble/bubble.dart';
import 'package:cached_network_image/cached_network_image.dart';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-03 09:09:14 +01:00
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2020-01-06 20:36:11 +01:00
import 'package:link_text/link_text.dart';
2020-01-01 19:10:13 +01:00
import 'package:url_launcher/url_launcher.dart';
import 'matrix.dart';
class MessageContent extends StatelessWidget {
final Event event;
final Color textColor;
final bool textOnly;
const MessageContent(this.event, {this.textColor, this.textOnly = false});
@override
Widget build(BuildContext context) {
final int maxLines = textOnly ? 1 : null;
2020-01-03 09:20:30 +01:00
final Widget unknown = Text(
"${event.sender.calcDisplayname()} sent a ${event.typeKey} event",
maxLines: maxLines,
overflow: textOnly ? TextOverflow.ellipsis : null,
style: TextStyle(
color: textColor,
decoration: event.redacted ? TextDecoration.lineThrough : null,
),
);
2020-01-01 19:10:13 +01:00
switch (event.type) {
case EventTypes.Message:
case EventTypes.Sticker:
switch (event.messageType) {
case MessageTypes.Image:
case MessageTypes.Sticker:
if (textOnly) {
return Text(
2020-01-05 12:27:03 +01:00
"${event.sender.calcDisplayname()} sent a picture",
maxLines: maxLines,
style: TextStyle(
color: textColor,
decoration:
event.redacted ? TextDecoration.lineThrough : null,
),
);
}
final int size = 400;
final String src = MxContent(event.content["url"]).getThumbnail(
Matrix.of(context).client,
width: size * MediaQuery.of(context).devicePixelRatio,
height: size * MediaQuery.of(context).devicePixelRatio,
method: ThumbnailMethod.scale,
);
return Bubble(
padding: BubbleEdges.all(0),
radius: Radius.circular(10),
elevation: 0,
child: InkWell(
onTap: () => launch(
MxContent(event.content["url"])
.getDownloadLink(Matrix.of(context).client),
),
child: kIsWeb
? Image.network(
src,
width: size.toDouble(),
)
: CachedNetworkImage(
imageUrl: src,
width: size.toDouble(),
),
),
);
case MessageTypes.Audio:
case MessageTypes.File:
case MessageTypes.Video:
2020-01-05 12:27:03 +01:00
if (textOnly) {
return Text(
"${event.sender.calcDisplayname()} sent a file",
maxLines: maxLines,
style: TextStyle(
color: textColor,
decoration:
event.redacted ? TextDecoration.lineThrough : null,
),
);
}
return Container(
width: 200,
child: RaisedButton(
color: Colors.blueGrey,
child: Text(
2020-01-14 12:45:52 +01:00
"Download ${event.body}",
overflow: TextOverflow.fade,
softWrap: false,
maxLines: 1,
),
onPressed: () => launch(
MxContent(event.content["url"])
.getDownloadLink(Matrix.of(context).client),
),
),
);
case MessageTypes.Text:
case MessageTypes.Reply:
case MessageTypes.Location:
case MessageTypes.None:
case MessageTypes.Notice:
final String senderPrefix =
textOnly && event.senderId != event.room.directChatMatrixID
? event.senderId == Matrix.of(context).client.userID
? "You: "
: "${event.sender.calcDisplayname()}: "
: "";
2020-01-08 20:01:52 +01:00
final String body = event.redacted
? "Redacted by ${event.redactedBecause.sender.calcDisplayname()}"
2020-01-14 12:45:52 +01:00
: senderPrefix + event.body;
2020-01-06 20:36:11 +01:00
if (textOnly) {
return Text(
2020-01-08 20:01:52 +01:00
body,
2020-01-06 20:36:11 +01:00
maxLines: maxLines,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: textColor,
decoration:
event.redacted ? TextDecoration.lineThrough : null,
),
);
}
return LinkText(
2020-01-08 20:01:52 +01:00
text: body,
2020-01-06 20:36:11 +01:00
textStyle: TextStyle(
color: textColor,
decoration: event.redacted ? TextDecoration.lineThrough : null,
),
);
2020-01-06 20:36:11 +01:00
case MessageTypes.Emote:
2020-01-06 20:36:11 +01:00
if (textOnly) {
return Text(
2020-01-14 12:45:52 +01:00
"* " + event.body,
2020-01-06 20:36:11 +01:00
maxLines: maxLines,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: textColor,
fontStyle: FontStyle.italic,
decoration:
event.redacted ? TextDecoration.lineThrough : null,
),
);
}
return LinkText(
2020-01-14 12:45:52 +01:00
text: "* " + event.body,
2020-01-06 20:36:11 +01:00
textStyle: TextStyle(
color: textColor,
fontStyle: FontStyle.italic,
decoration: event.redacted ? TextDecoration.lineThrough : null,
),
);
2020-01-04 09:37:09 +01:00
}
return unknown;
2020-01-02 23:38:46 +01:00
case EventTypes.RoomCreate:
return Text(
"${event.sender.calcDisplayname()} has created the chat",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomAvatar:
return Text(
"${event.sender.calcDisplayname()} has changed the chat avatar",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomName:
return Text(
"${event.sender.calcDisplayname()} has changed the chat name to '${event.content['name']}'",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomMember: // Display what has changed
String text = "Failed to parse member event";
// Has the membership changed?
final String newMembership = event.content["membership"] ?? "";
final String oldMembership =
event.unsigned["prev_content"] is Map<String, dynamic>
? event.unsigned["prev_content"]["membership"] ?? ""
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
text =
"${event.stateKeyUser.calcDisplayname()} has accepted the invitation";
} else if (oldMembership == "leave" && newMembership == "join") {
text =
"${event.stateKeyUser.calcDisplayname()} has joined the chat";
} else if (oldMembership == "join" && newMembership == "ban") {
text =
"${event.sender.calcDisplayname()} has kicked and banned ${event.stateKeyUser.calcDisplayname()}";
} else if (oldMembership == "join" &&
newMembership == "leave" &&
event.stateKey != event.senderId) {
text =
"${event.sender.calcDisplayname()} has kicked ${event.stateKeyUser.calcDisplayname()}";
} else if (oldMembership == "join" &&
newMembership == "leave" &&
event.stateKey == event.senderId) {
text = "${event.stateKeyUser.calcDisplayname()} has left the room";
} else if (oldMembership == "invite" && newMembership == "ban") {
text =
"${event.sender.calcDisplayname()} has banned ${event.stateKeyUser.calcDisplayname()}";
} else if (oldMembership == "leave" && newMembership == "ban") {
text =
"${event.sender.calcDisplayname()} has banned ${event.stateKeyUser.calcDisplayname()}";
} else if (oldMembership == "ban" && newMembership == "leave") {
text =
"${event.sender.calcDisplayname()} has unbanned ${event.stateKeyUser.calcDisplayname()}";
} else if (newMembership == "invite") {
text =
"${event.sender.calcDisplayname()} has invited ${event.stateKeyUser.calcDisplayname()}";
} else if (newMembership == "join") {
text = "${event.stateKeyUser.calcDisplayname()} has joined";
}
} else if (newMembership == "join") {
final String newAvatar = event.content["avatar_url"] ?? "";
final String oldAvatar =
event.unsigned["prev_content"] is Map<String, dynamic>
? event.unsigned["prev_content"]["avatar_url"] ?? ""
: "";
final String newDisplayname = event.content["displayname"] ?? "";
final String oldDisplayname =
event.unsigned["prev_content"] is Map<String, dynamic>
? event.unsigned["prev_content"]["displayname"] ?? ""
: "";
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
text =
"${event.stateKeyUser.calcDisplayname()} has changed the profile avatar";
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
text =
"${event.stateKeyUser.calcDisplayname()} has changed the displayname to '$newDisplayname'";
}
}
return Text(
text,
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomTopic:
return Text(
"${event.sender.calcDisplayname()} has changed the chat topic to '${event.content['topic']}'",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomPowerLevels:
return Text(
"${event.sender.calcDisplayname()} has changed the power levels of the chat",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.HistoryVisibility:
return Text(
"${event.sender.calcDisplayname()} has changed the history visibility of the chat to '${event.content['history_visibility']}'",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomJoinRules:
return Text(
"${event.sender.calcDisplayname()} has changed the join rules of the chat to '${event.content['join_rule']}'",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
case EventTypes.RoomCanonicalAlias:
if (event.content['canonical_alias']?.isEmpty ?? true) {
return Text(
"${event.sender.calcDisplayname()} has removed the canonical alias.",
maxLines: maxLines,
style: TextStyle(
color: textColor,
),
);
}
return Text(
"${event.sender.calcDisplayname()} has changed the canonical alias to: ${event.content['canonical_alias']}",
maxLines: maxLines,
2020-01-03 14:44:39 +01:00
overflow: textOnly ? TextOverflow.ellipsis : null,
2020-01-02 23:38:46 +01:00
style: TextStyle(
color: textColor,
),
);
2020-01-01 19:10:13 +01:00
default:
return unknown;
2020-01-01 19:10:13 +01:00
}
}
}