only use the hero avatar if the room is a DM

This commit is contained in:
Kegan Dougal 2024-01-22 10:34:06 +00:00
parent 8d300dc206
commit 08d3ec9745
7 changed files with 28 additions and 12 deletions

View File

@ -264,11 +264,11 @@ const noAvatar = ""
// CalculateAvatar computes the avatar for the room, based on the global room metadata.
// Assumption: metadata.RemoveHero has been called to remove the user who is syncing
// from the list of heroes.
func CalculateAvatar(metadata *RoomMetadata) string {
func CalculateAvatar(metadata *RoomMetadata, isDM bool) string {
if metadata.AvatarEvent != "" {
return metadata.AvatarEvent
}
if len(metadata.Heroes) == 1 {
if len(metadata.Heroes) == 1 && isDM {
return metadata.Heroes[0].Avatar
}
return noAvatar

View File

@ -115,7 +115,7 @@ func NewInviteData(ctx context.Context, userID, roomID string, inviteState []jso
Timestamp: uint64(ts),
AlwaysProcess: true,
}
id.IsDM = j.Get("is_direct").Bool()
id.IsDM = j.Get("content.is_direct").Bool()
} else if target == j.Get("sender").Str {
id.Heroes = append(id.Heroes, internal.Hero{
ID: target,

View File

@ -675,7 +675,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
roomName, calculated := internal.CalculateRoomName(metadata, 5) // TODO: customisable?
room := sync3.Room{
Name: roomName,
AvatarChange: sync3.NewAvatarChange(internal.CalculateAvatar(metadata)),
AvatarChange: sync3.NewAvatarChange(internal.CalculateAvatar(metadata, userRoomData.IsDM)),
NotificationCount: int64(userRoomData.NotificationCount),
HighlightCount: int64(userRoomData.HighlightCount),
Timeline: roomToTimeline[roomID],

View File

@ -277,7 +277,7 @@ func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update,
if delta.RoomAvatarChanged {
metadata := roomUpdate.GlobalRoomMetadata()
metadata.RemoveHero(s.userID)
thisRoom.AvatarChange = sync3.NewAvatarChange(internal.CalculateAvatar(metadata))
thisRoom.AvatarChange = sync3.NewAvatarChange(internal.CalculateAvatar(metadata, roomUpdate.UserRoomMetadata().IsDM))
}
if delta.InviteCountChanged {
thisRoom.InvitedCount = &roomUpdate.GlobalRoomMetadata().InviteCount

View File

@ -87,7 +87,7 @@ func (s *InternalRequestLists) SetRoom(r RoomConnMetadata) (delta RoomDelta) {
}
delta.RoomAvatarChanged = !existing.SameRoomAvatar(&r.RoomMetadata)
if delta.RoomAvatarChanged {
r.ResolvedAvatarURL = internal.CalculateAvatar(&r.RoomMetadata)
r.ResolvedAvatarURL = internal.CalculateAvatar(&r.RoomMetadata, r.IsDM)
}
// Interpret the timestamp map on r as the changes we should apply atop the
@ -114,7 +114,7 @@ func (s *InternalRequestLists) SetRoom(r RoomConnMetadata) (delta RoomDelta) {
r.CanonicalisedName = strings.ToLower(
strings.Trim(roomName, "#!():_@"),
)
r.ResolvedAvatarURL = internal.CalculateAvatar(&r.RoomMetadata)
r.ResolvedAvatarURL = internal.CalculateAvatar(&r.RoomMetadata, r.IsDM)
// We'll automatically use the LastInterestedEventTimestamps provided by the
// caller, so that recency sorts work.
}

View File

@ -1376,6 +1376,11 @@ func TestAvatarFieldInRoomResponse(t *testing.T) {
"invite": []string{bob.UserID, chris.UserID},
})
alice.MustSetGlobalAccountData(t, "m.direct", map[string]any{
bob.UserID: []string{dmBob, dmBobChris},
chris.UserID: []string{dmChris, dmBobChris},
})
t.Logf("Rooms:\npublic=%s\ndmBob=%s\ndmChris=%s\ndmBobChris=%s", public, dmBob, dmChris, dmBobChris)
t.Log("Bob accepts his invites. Chris accepts none.")
bob.JoinRoom(t, dmBob, nil)
@ -1390,14 +1395,14 @@ func TestAvatarFieldInRoomResponse(t *testing.T) {
},
})
t.Log("Alice should see each room in the sync response with an appropriate avatar")
t.Log("Alice should see each room in the sync response with an appropriate avatar and DM flag")
m.MatchResponse(
t,
res,
m.MatchRoomSubscription(public, m.MatchRoomUnsetAvatar()),
m.MatchRoomSubscription(dmBob, m.MatchRoomAvatar(bob.AvatarURL)),
m.MatchRoomSubscription(dmChris, m.MatchRoomAvatar(chris.AvatarURL)),
m.MatchRoomSubscription(dmBobChris, m.MatchRoomUnsetAvatar()),
m.MatchRoomSubscription(public, m.MatchRoomUnsetAvatar(), m.MatchRoomIsDM(false)),
m.MatchRoomSubscription(dmBob, m.MatchRoomAvatar(bob.AvatarURL), m.MatchRoomIsDM(true)),
m.MatchRoomSubscription(dmChris, m.MatchRoomAvatar(chris.AvatarURL), m.MatchRoomIsDM(true)),
m.MatchRoomSubscription(dmBobChris, m.MatchRoomUnsetAvatar(), m.MatchRoomIsDM(true)),
)
t.Run("Avatar not resent on message", func(t *testing.T) {
@ -1736,6 +1741,8 @@ func TestAvatarFieldInRoomResponse(t *testing.T) {
func TestAvatarUnsetInTwoPersonRoom(t *testing.T) {
alice := registerNamedUser(t, "alice")
bob := registerNamedUser(t, "bob")
bobAvatar := alice.UploadContent(t, smallPNG, "bob.png", "image/png")
bob.SetAvatar(t, bobAvatar)
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"preset": "trusted_private_chat",
"name": "Nice test room",

View File

@ -73,6 +73,15 @@ func MatchRoomUnchangedAvatar() RoomMatcher {
}
}
func MatchRoomIsDM(wantDM bool) RoomMatcher {
return func(r sync3.Room) error {
if r.IsDM != wantDM {
return fmt.Errorf("MatchRoomIsDM: got %t want %t", r.IsDM, wantDM)
}
return nil
}
}
func MatchJoinCount(count int) RoomMatcher {
return func(r sync3.Room) error {
if r.JoinedCount != count {