Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit f3534b4

Browse files
authored
Add string for membership event where both displayname & avatar change (#10880)
* Add string for membership event where both displayname & avatar change * i18n and tests
1 parent 64733e5 commit f3534b4

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

src/TextForEvent.tsx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ function textForCallInviteEvent(event: MatrixEvent): (() => string) | null {
8282
return null;
8383
}
8484

85+
enum Modification {
86+
None,
87+
Unset,
88+
Set,
89+
Changed,
90+
}
91+
92+
function getModification(prev?: string, value?: string): Modification {
93+
if (prev && value && prev !== value) {
94+
return Modification.Changed;
95+
}
96+
if (prev && !value) {
97+
return Modification.Unset;
98+
}
99+
if (!prev && value) {
100+
return Modification.Set;
101+
}
102+
103+
return Modification.None;
104+
}
105+
85106
function textForMemberEvent(ev: MatrixEvent, allowJSX: boolean, showHiddenEvents?: boolean): (() => string) | null {
86107
// XXX: SYJS-16 "sender is sometimes null for join messages"
87108
const senderName = ev.sender?.name || getRoomMemberDisplayname(ev);
@@ -114,36 +135,44 @@ function textForMemberEvent(ev: MatrixEvent, allowJSX: boolean, showHiddenEvents
114135
: _t("%(senderName)s banned %(targetName)s", { senderName, targetName });
115136
case "join":
116137
if (prevContent && prevContent.membership === "join") {
117-
if (prevContent.displayname && content.displayname && prevContent.displayname !== content.displayname) {
138+
const modDisplayname = getModification(prevContent.displayname, content.displayname);
139+
const modAvatarUrl = getModification(prevContent.avatar_url, content.avatar_url);
140+
141+
if (modDisplayname !== Modification.None && modAvatarUrl !== Modification.None) {
142+
// Compromise to provide the user with more context without needing 16 translations
118143
return () =>
119-
_t("%(oldDisplayName)s changed their display name to %(displayName)s", {
144+
_t("%(oldDisplayName)s changed their display name and profile picture", {
120145
// We're taking the display namke directly from the event content here so we need
121146
// to strip direction override chars which the js-sdk would normally do when
122147
// calculating the display name
123148
oldDisplayName: removeDirectionOverrideChars(prevContent.displayname!),
149+
});
150+
} else if (modDisplayname === Modification.Changed) {
151+
return () =>
152+
_t("%(oldDisplayName)s changed their display name to %(displayName)s", {
153+
// We're taking the display name directly from the event content here so we need
154+
// to strip direction override chars which the js-sdk would normally do when
155+
// calculating the display name
156+
oldDisplayName: removeDirectionOverrideChars(prevContent.displayname!),
124157
displayName: removeDirectionOverrideChars(content.displayname!),
125158
});
126-
} else if (!prevContent.displayname && content.displayname) {
159+
} else if (modDisplayname === Modification.Set) {
127160
return () =>
128161
_t("%(senderName)s set their display name to %(displayName)s", {
129162
senderName: ev.getSender(),
130163
displayName: removeDirectionOverrideChars(content.displayname!),
131164
});
132-
} else if (prevContent.displayname && !content.displayname) {
165+
} else if (modDisplayname === Modification.Unset) {
133166
return () =>
134167
_t("%(senderName)s removed their display name (%(oldDisplayName)s)", {
135168
senderName,
136169
oldDisplayName: removeDirectionOverrideChars(prevContent.displayname!),
137170
});
138-
} else if (prevContent.avatar_url && !content.avatar_url) {
171+
} else if (modAvatarUrl === Modification.Unset) {
139172
return () => _t("%(senderName)s removed their profile picture", { senderName });
140-
} else if (
141-
prevContent.avatar_url &&
142-
content.avatar_url &&
143-
prevContent.avatar_url !== content.avatar_url
144-
) {
173+
} else if (modAvatarUrl === Modification.Changed) {
145174
return () => _t("%(senderName)s changed their profile picture", { senderName });
146-
} else if (!prevContent.avatar_url && content.avatar_url) {
175+
} else if (modAvatarUrl === Modification.Set) {
147176
return () => _t("%(senderName)s set a profile picture", { senderName });
148177
} else if (showHiddenEvents ?? SettingsStore.getValue("showHiddenEventsInTimeline")) {
149178
// This is a null rejoin, it will only be visible if using 'show hidden events' (labs)

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
"%(senderName)s invited %(targetName)s": "%(senderName)s invited %(targetName)s",
502502
"%(senderName)s banned %(targetName)s: %(reason)s": "%(senderName)s banned %(targetName)s: %(reason)s",
503503
"%(senderName)s banned %(targetName)s": "%(senderName)s banned %(targetName)s",
504+
"%(oldDisplayName)s changed their display name and profile picture": "%(oldDisplayName)s changed their display name and profile picture",
504505
"%(oldDisplayName)s changed their display name to %(displayName)s": "%(oldDisplayName)s changed their display name to %(displayName)s",
505506
"%(senderName)s set their display name to %(displayName)s": "%(senderName)s set their display name to %(displayName)s",
506507
"%(senderName)s removed their display name (%(oldDisplayName)s)": "%(senderName)s removed their display name (%(oldDisplayName)s)",

test/TextForEvent-test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,32 @@ describe("TextForEvent", () => {
480480
});
481481
});
482482
});
483+
484+
describe("textForMemberEvent()", () => {
485+
beforeEach(() => {
486+
stubClient();
487+
});
488+
489+
it("should handle both displayname and avatar changing in one event", () => {
490+
expect(
491+
textForEvent(
492+
new MatrixEvent({
493+
type: "m.room.member",
494+
sender: "@a:foo",
495+
content: {
496+
membership: "join",
497+
avatar_url: "b",
498+
displayname: "Bob",
499+
},
500+
prev_content: {
501+
membership: "join",
502+
avatar_url: "a",
503+
displayname: "Andy",
504+
},
505+
state_key: "@a:foo",
506+
}),
507+
),
508+
).toMatchInlineSnapshot(`"Andy changed their display name and profile picture"`);
509+
});
510+
});
483511
});

0 commit comments

Comments
 (0)