Skip to content

Commit 77f3ce7

Browse files
committed
content: Update Mention widget to render user group mentions dynamically
Fixes #1259. Look up current user group name from PerAccountStore when rendering mentions, while respecting "isSilent" flag. This ensures mentions display correctly even if the user group name has been changed since the message was sent. Falls back to original HTML text when user group not found. Added 'user group mention dynamic name resolution' tests group.
1 parent 09ed709 commit 77f3ce7

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lib/widgets/content.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,12 +1197,22 @@ class Mention extends StatelessWidget {
11971197
final store = PerAccountStoreWidget.of(context);
11981198
final contentTheme = ContentTheme.of(context);
11991199
var nodes = node.nodes;
1200-
if (node case UserMentionNode(:final userId?)) {
1201-
final user = store.getUser(userId);
1202-
if (user case User(:final fullName)) {
1203-
nodes = [TextNode(node.isSilent ? fullName : '@$fullName')];
1204-
}
1200+
1201+
switch (node) {
1202+
case UserMentionNode(:final userId?):
1203+
final user = store.getUser(userId);
1204+
if (user case User(:final fullName)) {
1205+
nodes = [TextNode(node.isSilent ? fullName : '@$fullName')];
1206+
}
1207+
case UserGroupMentionNode(:final userGroupId):
1208+
final userGroup = store.getGroup(userGroupId);
1209+
if (userGroup case UserGroup(:final name)) {
1210+
// TODO(#1260) Get display name for system groups using localization
1211+
nodes = [TextNode(node.isSilent ? name : '@$name')];
1212+
}
1213+
default:
12051214
}
1215+
12061216
return Container(
12071217
decoration: BoxDecoration(
12081218
// TODO(#646) different for wildcard mentions

test/widgets/content_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,45 @@ void main() {
11111111
check(find.text('@New Name')).findsNothing();
11121112
});
11131113
});
1114+
1115+
group('user group mention dynamic name resolution', () {
1116+
Future<void> prepare({
1117+
required WidgetTester tester,
1118+
required String html,
1119+
List<UserGroup>? userGroups,
1120+
}) async {
1121+
final initialSnapshot = eg.initialSnapshot(realmUserGroups: userGroups);
1122+
await prepareContent(tester,
1123+
wrapWithPerAccountStoreWidget: true,
1124+
initialSnapshot: initialSnapshot,
1125+
plainContent(html));
1126+
}
1127+
1128+
testWidgets('resolves current user group name from store', (tester) async {
1129+
await prepare(
1130+
tester: tester,
1131+
html: '<p><span class="user-group-mention" data-user-group-id="186">@old-name</span></p>',
1132+
userGroups: [eg.userGroup(id: 186, name: 'new-name')]);
1133+
check(find.text('@new-name')).findsOne();
1134+
check(find.text('@old-name')).findsNothing();
1135+
});
1136+
1137+
testWidgets('falls back to original text when user group not found', (tester) async {
1138+
await prepare(
1139+
tester: tester,
1140+
html: '<p><span class="user-group-mention" data-user-group-id="999">@Unknown Group</span></p>');
1141+
check(find.text('@Unknown Group')).findsOne();
1142+
});
1143+
1144+
testWidgets('handles silent mentions correctly', (tester) async {
1145+
await prepare(
1146+
tester: tester,
1147+
html: '<p><span class="user-group-mention silent" data-user-group-id="186">old-name</span></p>',
1148+
userGroups: [eg.userGroup(id: 186, name: 'new-name')]);
1149+
check(find.text('new-name')).findsOne();
1150+
check(find.text('@new-name')).findsNothing();
1151+
});
1152+
});
11141153
});
11151154

11161155
Future<void> tapText(WidgetTester tester, Finder textFinder) async {

0 commit comments

Comments
 (0)