Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamp to room responses #247

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sync3/handler/connstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
JoinedCount: metadata.JoinCount,
InvitedCount: &metadata.InviteCount,
PrevBatch: userRoomData.RequestedLatestEvents.PrevBatch,
Timestamp: metadata.LastMessageTimestamp,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't know if this is what they want. getInitialRoomData is a central point called for multiple lists: you need to know the bump_event_types values to know what timestamp to pull here. Add that as a param to getInitialRoomData

}
}

Expand Down
8 changes: 8 additions & 0 deletions sync3/handler/connstate_live.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update,
// include this update in the rooms response TODO: filters on event type?
userRoomData := roomUpdate.UserRoomMetadata()
r := response.Rooms[roomUpdate.RoomID()]

conMeta := s.lists.ReadOnlyRoom(roomUpdate.RoomID())
for _, ts := range conMeta.LastInterestedEventTimestamps {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs comments. This is using the highest timestamp from each list for this room I guess?

if ts >= r.Timestamp {
r.Timestamp = ts
}
}

r.HighlightCount = int64(userRoomData.HighlightCount)
r.NotificationCount = int64(userRoomData.NotificationCount)
if roomEventUpdate != nil && roomEventUpdate.EventData.Event != nil {
Expand Down
1 change: 1 addition & 0 deletions sync3/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Room struct {
InvitedCount *int `json:"invited_count,omitempty"`
PrevBatch string `json:"prev_batch,omitempty"`
NumLive int `json:"num_live,omitempty"`
Timestamp uint64 `json:"timestamp,omitempty"`
}

// RoomConnMetadata represents a room as seen by one specific connection (hence one
Expand Down
126 changes: 126 additions & 0 deletions tests-e2e/timestamp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package syncv3_test

import (
"testing"

"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils/m"
)

func TestTimestamp(t *testing.T) {
alice := registerNewUser(t)
bob := registerNewUser(t)

roomID := alice.CreateRoom(t, map[string]interface{}{
"preset": "public_chat",
})

// Init sync to get the latest timestamp
res := alice.SlidingSync(t, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 10,
},
},
})
m.MatchResponse(t, res, m.MatchRoomSubscription(roomID, m.MatchRoomInitial(true)))
timestampBeforeBobJoined := res.Rooms[roomID].Timestamp

bob.JoinRoom(t, roomID, nil)
res = alice.SlidingSyncUntilMembership(t, res.Pos, roomID, bob, "join")

resBob := bob.SlidingSync(t, sync3.Request{
Lists: map[string]sync3.RequestList{
"myFirstList": {
Ranges: [][2]int64{{0, 1}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
BumpEventTypes: []string{"m.room.message"}, // only messages bump the timestamp
},
"mySecondList": {
Ranges: [][2]int64{{0, 1}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
BumpEventTypes: []string{"m.reaction"}, // only reactions bump the timestamp
},
},
})

// Bob should see a different timestamp than alice, as he just joined
bobTimestampBefore := resBob.Rooms[roomID].Timestamp
if bobTimestampBefore == timestampBeforeBobJoined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really this should equal res.Rooms[roomID].Timestamp as that is going to be bob's join due to :30.

t.Fatalf("expected timestamp to differ: %v vs %v", timestampBeforeBobJoined, bobTimestampBefore)
}

// Send an event which should NOT bump Bobs timestamp, because it is not listed it
// any BumpEventTypes
emptyStateKey := ""
eventID := alice.SendEventSynced(t, roomID, Event{
Type: "m.room.topic",
StateKey: &emptyStateKey,
Content: map[string]interface{}{
"topic": "random topic",
},
})

bobTimestampAfter := resBob.Rooms[roomID].Timestamp

resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
t.Logf("bobTimestampBefore vs bobTimestampAfter: %v %v", bobTimestampBefore, bobTimestampAfter)
if bobTimestampBefore != bobTimestampAfter {
t.Fatalf("expected timestamps to be the same, but they aren't: %v vs %v", bobTimestampBefore, bobTimestampAfter)
}
bobTimestampBefore = bobTimestampAfter

// Now send a message which bumps the timestamp in myFirstList
eventID = alice.SendEventSynced(t, roomID, Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Hello, world!",
},
})

resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
bobTimestampAfter = resBob.Rooms[roomID].Timestamp
if bobTimestampBefore == bobTimestampAfter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we need to know exactly what timestamp this should be - use alice to figure this out.

t.Fatalf("expected timestamps to be different, but they aren't: %v vs %v", bobTimestampBefore, bobTimestampAfter)
}
bobTimestampBefore = bobTimestampAfter

// Now send a message which bumps the timestamp in mySecondList
eventID = alice.SendEventSynced(t, roomID, Event{
Type: "m.reaction",
Content: map[string]interface{}{
"m.relates.to": map[string]interface{}{
"event_id": eventID,
"key": "✅",
"rel_type": "m.annotation",
},
},
})

resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
bobTimestampAfter = resBob.Rooms[roomID].Timestamp
if bobTimestampBefore == bobTimestampAfter {
t.Fatalf("expected timestamps to be different, but they aren't: %v vs %v", bobTimestampBefore, bobTimestampAfter)
}
bobTimestampBefore = bobTimestampAfter

// Send another event which should NOT bump Bobs timestamp
eventID = alice.SendEventSynced(t, roomID, Event{
Type: "m.room.name",
StateKey: &emptyStateKey,
Content: map[string]interface{}{
"name": "random name",
},
})

resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
bobTimestampAfter = resBob.Rooms[roomID].Timestamp
if bobTimestampBefore != bobTimestampAfter {
t.Fatalf("expected timestamps to be the same, but they aren't: %v vs %v", bobTimestampBefore, bobTimestampAfter)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs more tests. Specifically:

  • If you now do an initial sync (no pos) as Bob with the same lists, I don't think you'll get the right value. You'll get the latest timestamp (m.room.name) and not the m.reaction timestamp.
  • Ensure you time.Sleep(time.Millisecond) after each send to ensure that timestamps will change.
  • If Charlie now joins the room with the same bump_event_types as bob, he should see the timestamp of his join.

Loading