mirror of
https://github.com/matrix-org/sliding-sync.git
synced 2025-03-10 13:37:11 +00:00
only use the hero avatar if the room is a DM
This commit is contained in:
parent
8d300dc206
commit
08d3ec9745
@ -264,11 +264,11 @@ const noAvatar = ""
|
|||||||
// CalculateAvatar computes the avatar for the room, based on the global room metadata.
|
// 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
|
// Assumption: metadata.RemoveHero has been called to remove the user who is syncing
|
||||||
// from the list of heroes.
|
// from the list of heroes.
|
||||||
func CalculateAvatar(metadata *RoomMetadata) string {
|
func CalculateAvatar(metadata *RoomMetadata, isDM bool) string {
|
||||||
if metadata.AvatarEvent != "" {
|
if metadata.AvatarEvent != "" {
|
||||||
return metadata.AvatarEvent
|
return metadata.AvatarEvent
|
||||||
}
|
}
|
||||||
if len(metadata.Heroes) == 1 {
|
if len(metadata.Heroes) == 1 && isDM {
|
||||||
return metadata.Heroes[0].Avatar
|
return metadata.Heroes[0].Avatar
|
||||||
}
|
}
|
||||||
return noAvatar
|
return noAvatar
|
||||||
|
@ -115,7 +115,7 @@ func NewInviteData(ctx context.Context, userID, roomID string, inviteState []jso
|
|||||||
Timestamp: uint64(ts),
|
Timestamp: uint64(ts),
|
||||||
AlwaysProcess: true,
|
AlwaysProcess: true,
|
||||||
}
|
}
|
||||||
id.IsDM = j.Get("is_direct").Bool()
|
id.IsDM = j.Get("content.is_direct").Bool()
|
||||||
} else if target == j.Get("sender").Str {
|
} else if target == j.Get("sender").Str {
|
||||||
id.Heroes = append(id.Heroes, internal.Hero{
|
id.Heroes = append(id.Heroes, internal.Hero{
|
||||||
ID: target,
|
ID: target,
|
||||||
|
@ -675,7 +675,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
|
|||||||
roomName, calculated := internal.CalculateRoomName(metadata, 5) // TODO: customisable?
|
roomName, calculated := internal.CalculateRoomName(metadata, 5) // TODO: customisable?
|
||||||
room := sync3.Room{
|
room := sync3.Room{
|
||||||
Name: roomName,
|
Name: roomName,
|
||||||
AvatarChange: sync3.NewAvatarChange(internal.CalculateAvatar(metadata)),
|
AvatarChange: sync3.NewAvatarChange(internal.CalculateAvatar(metadata, userRoomData.IsDM)),
|
||||||
NotificationCount: int64(userRoomData.NotificationCount),
|
NotificationCount: int64(userRoomData.NotificationCount),
|
||||||
HighlightCount: int64(userRoomData.HighlightCount),
|
HighlightCount: int64(userRoomData.HighlightCount),
|
||||||
Timeline: roomToTimeline[roomID],
|
Timeline: roomToTimeline[roomID],
|
||||||
|
@ -277,7 +277,7 @@ func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update,
|
|||||||
if delta.RoomAvatarChanged {
|
if delta.RoomAvatarChanged {
|
||||||
metadata := roomUpdate.GlobalRoomMetadata()
|
metadata := roomUpdate.GlobalRoomMetadata()
|
||||||
metadata.RemoveHero(s.userID)
|
metadata.RemoveHero(s.userID)
|
||||||
thisRoom.AvatarChange = sync3.NewAvatarChange(internal.CalculateAvatar(metadata))
|
thisRoom.AvatarChange = sync3.NewAvatarChange(internal.CalculateAvatar(metadata, roomUpdate.UserRoomMetadata().IsDM))
|
||||||
}
|
}
|
||||||
if delta.InviteCountChanged {
|
if delta.InviteCountChanged {
|
||||||
thisRoom.InvitedCount = &roomUpdate.GlobalRoomMetadata().InviteCount
|
thisRoom.InvitedCount = &roomUpdate.GlobalRoomMetadata().InviteCount
|
||||||
|
@ -87,7 +87,7 @@ func (s *InternalRequestLists) SetRoom(r RoomConnMetadata) (delta RoomDelta) {
|
|||||||
}
|
}
|
||||||
delta.RoomAvatarChanged = !existing.SameRoomAvatar(&r.RoomMetadata)
|
delta.RoomAvatarChanged = !existing.SameRoomAvatar(&r.RoomMetadata)
|
||||||
if delta.RoomAvatarChanged {
|
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
|
// 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(
|
r.CanonicalisedName = strings.ToLower(
|
||||||
strings.Trim(roomName, "#!():_@"),
|
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
|
// We'll automatically use the LastInterestedEventTimestamps provided by the
|
||||||
// caller, so that recency sorts work.
|
// caller, so that recency sorts work.
|
||||||
}
|
}
|
||||||
|
@ -1376,6 +1376,11 @@ func TestAvatarFieldInRoomResponse(t *testing.T) {
|
|||||||
"invite": []string{bob.UserID, chris.UserID},
|
"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.Logf("Rooms:\npublic=%s\ndmBob=%s\ndmChris=%s\ndmBobChris=%s", public, dmBob, dmChris, dmBobChris)
|
||||||
t.Log("Bob accepts his invites. Chris accepts none.")
|
t.Log("Bob accepts his invites. Chris accepts none.")
|
||||||
bob.JoinRoom(t, dmBob, nil)
|
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(
|
m.MatchResponse(
|
||||||
t,
|
t,
|
||||||
res,
|
res,
|
||||||
m.MatchRoomSubscription(public, m.MatchRoomUnsetAvatar()),
|
m.MatchRoomSubscription(public, m.MatchRoomUnsetAvatar(), m.MatchRoomIsDM(false)),
|
||||||
m.MatchRoomSubscription(dmBob, m.MatchRoomAvatar(bob.AvatarURL)),
|
m.MatchRoomSubscription(dmBob, m.MatchRoomAvatar(bob.AvatarURL), m.MatchRoomIsDM(true)),
|
||||||
m.MatchRoomSubscription(dmChris, m.MatchRoomAvatar(chris.AvatarURL)),
|
m.MatchRoomSubscription(dmChris, m.MatchRoomAvatar(chris.AvatarURL), m.MatchRoomIsDM(true)),
|
||||||
m.MatchRoomSubscription(dmBobChris, m.MatchRoomUnsetAvatar()),
|
m.MatchRoomSubscription(dmBobChris, m.MatchRoomUnsetAvatar(), m.MatchRoomIsDM(true)),
|
||||||
)
|
)
|
||||||
|
|
||||||
t.Run("Avatar not resent on message", func(t *testing.T) {
|
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) {
|
func TestAvatarUnsetInTwoPersonRoom(t *testing.T) {
|
||||||
alice := registerNamedUser(t, "alice")
|
alice := registerNamedUser(t, "alice")
|
||||||
bob := registerNamedUser(t, "bob")
|
bob := registerNamedUser(t, "bob")
|
||||||
|
bobAvatar := alice.UploadContent(t, smallPNG, "bob.png", "image/png")
|
||||||
|
bob.SetAvatar(t, bobAvatar)
|
||||||
roomID := alice.MustCreateRoom(t, map[string]interface{}{
|
roomID := alice.MustCreateRoom(t, map[string]interface{}{
|
||||||
"preset": "trusted_private_chat",
|
"preset": "trusted_private_chat",
|
||||||
"name": "Nice test room",
|
"name": "Nice test room",
|
||||||
|
@ -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 {
|
func MatchJoinCount(count int) RoomMatcher {
|
||||||
return func(r sync3.Room) error {
|
return func(r sync3.Room) error {
|
||||||
if r.JoinedCount != count {
|
if r.JoinedCount != count {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user