feat: Start implementing Sync method (WIP)

This commit is contained in:
ChronosX88 2019-08-17 12:42:56 +04:00
parent c694841cb1
commit e009c054cb
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
6 changed files with 107 additions and 54 deletions

View File

@ -222,7 +222,7 @@ func extractEventsFromNodes(nodes []*sortedset.SortedSetNode) []events.Event {
func isEventRelatedToUser(event events.Event, user internal.User) bool {
if roomEvent, ok := event.(*events.RoomEvent); ok {
if internal.InArray(roomEvent.RoomID, extractRoomIDsFromModel(user.JoinedRooms())) {
if internal.InArray(roomEvent.RoomID, extractRoomIDsFromModel(user.JoinedRooms())) { // TODO check for invited or archived rooms
return true
}
}

View File

@ -354,5 +354,54 @@ func (user *User) GetFilterByID(filterID string) *common.Filter {
}
func (user *User) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err models.ApiError) {
return nil, nil
response = mSync.BuildEmptySyncReply()
eventsList := user.backend.GetEventsSince(user, request.Since, 0) // TODO filtering
var eventListEmpty bool
if len(eventsList) != 0 {
eventListEmpty = false
}
if !eventListEmpty {
for _, room := range user.JoinedRooms() {
filteredEventList := filterEventsByRoom(room.ID(), eventsList)
var prevBatch string
if len(filteredEventList) != 0 {
prevBatch = filteredEventList[0].ID()
}
response.Rooms.Join[room.ID()] = mSync.JoinedRoom{
RoomSummary: mSync.RoomSummary{
Heroes: nil,
JoinedMemberCount: 0,
InvitedMemberCount: 0,
},
State: events.State{
Events: nil,
},
Timeline: mSync.Timeline{
Events: filteredEventList,
Limited: false, // TODO filtering
PrevBatch: prevBatch,
},
}
}
response.NextBatch = eventsList[len(eventsList)-1].ID()
} else {
// TODO wait for new events or just return empty response when timeout is reached
}
return response, nil
}
func filterEventsByRoom(roomID string, eventList []events.Event) []events.RoomEvent {
var filteredEventList []events.RoomEvent
for _, event := range eventList {
if roomEvent, ok := event.(events.RoomEvent); ok {
if roomEvent.RoomID == roomID {
filteredEventList = append(filteredEventList, event.(events.RoomEvent))
}
}
}
return filteredEventList
}

View File

@ -300,9 +300,6 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) {
response, _ := user.Sync(token, request) // TODO: handle error
response.NextBatch = "123"
response.Rooms = mSync.RoomsSyncReply{}
sendJsonResponse(w, http.StatusOK, response)
}

View File

@ -92,7 +92,7 @@ type signed struct {
}
type State struct {
events []StateEvent `json:"events"` // List of events.
Events []StateEvent `json:"events"` // List of events.
}
type Invite struct {

View File

@ -15,51 +15,14 @@ type RoomEvent struct {
RoomID string `json:"room_id"` // Required. The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else.
}
func (roomEvent *RoomEvent) Content() json.RawMessage {
func (roomEvent RoomEvent) Content() json.RawMessage {
return roomEvent.ContentData
}
func (roomEvent *RoomEvent) ID() string {
func (roomEvent RoomEvent) ID() string {
return roomEvent.EventID
}
func (roomEvent *RoomEvent) Type() EventType {
func (roomEvent RoomEvent) Type() EventType {
return roomEvent.EType
}
type JoinedRoom struct {
State State `json:"state"` // Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).
Timeline Timeline `json:"timeline"` // The timeline of messages and state changes in the room.
Ephemeral Ephemeral `json:"ephemeral"` // The ephemeral events in the room that aren't recorded in the timeline or state of the room. e.g. typing.
AccountData AccountData `json:"account_data"` // The private data that this user has attached to this room.
UnreadNotifications UnreadNotificationCounts `json:"unread_notifications"` // Counts of unread notifications for this room
}
type AccountData struct {
Events []Event `json:"events"` // List of events.
}
type UnreadNotificationCounts struct {
HighlightCount int `json:"highlight_count"` // The number of unread notifications for this room with the highlight flag set
NotificationCount int `json:"notification_count"` // The total number of unread notifications for this room
}
type LeftRoom struct {
State State `json:"state"` // The state updates for the room up to the start of the timeline.
Timeline Timeline `json:"timeline"` // The timeline of messages and state changes in the room up to the point when the user left.
AccountData AccountData `json:"account_data"` // The private data that this user has attached to this room.
}
type InvitedRoom struct {
InviteState InviteState `json:"invite_state"` // The state of a room that the user has been invited to. These state events may only have the sender, type, state_key and content keys present. These events do not replace any state that the client already has for the room, for example if the client has archived the room. Instead the client should keep two separate copies of the state: the one from the invite_state and one from the archived state. If the client joins the room then the current state will be given as a delta against the archived state not the invite_state.
}
type InviteState struct {
Events []StrippedState `json:"events"` // The StrippedState events that form the invite state.
}
type Timeline struct {
Events []RoomEvent `json:"events"` // List of events.
Limited bool `json:"limited"` // True if the number of events returned was limited by the limit on the filter.
PrevBatch string `json:"prev_batch"` // A token that can be supplied to the from parameter of the rooms/{roomId}/messages endpoint.
}

View File

@ -9,16 +9,60 @@ type SyncReply struct {
NextBatch string `json:"next_batch"` // Required. The batch token to supply in the since param of the next /sync request.
Rooms RoomsSyncReply `json:"rooms"` // Updates to rooms.
Presence events.Presence `json:"presence"` // The updates to the presence status of other users.
AccountData events.AccountData `json:"account_data"` // The global private data created by this user.
AccountData AccountData `json:"account_data"` // The global private data created by this user.
ToDevice events.ToDevice `json:"to_device"` // Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging.
DeviceLists events.DeviceLists `json:"device_lists"` // Information on end-to-end device updates, as specified in End-to-end encryption.
DeviceOneTimeKeysCount map[string]int `json:"device_one_time_keys_count"` // Information on end-to-end encryption keys, as specified in End-to-end encryption.
}
type RoomsSyncReply struct {
Join map[string]events.JoinedRoom `json:"join"` // The rooms that the user has joined.
Invite map[string]events.InvitedRoom `json:"invite"` // The rooms that the user has been invited to.
Leave map[string]events.LeftRoom `json:"leave"` // The rooms that the user has left or been banned from.
Join map[string]JoinedRoom `json:"join"` // The rooms that the user has joined.
Invite map[string]InvitedRoom `json:"invite"` // The rooms that the user has been invited to.
Leave map[string]LeftRoom `json:"leave"` // The rooms that the user has left or been banned from.
}
type RoomSummary struct {
Heroes []string `json:"m.heroes"`
JoinedMemberCount int `json:"m.joined_member_count"`
InvitedMemberCount int `json:"m.invited_member_count"`
}
type JoinedRoom struct {
RoomSummary RoomSummary `json:"summary"`
State events.State `json:"state"` // Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).
Timeline Timeline `json:"timeline"` // The timeline of messages and state changes in the room.
Ephemeral events.Ephemeral `json:"ephemeral"` // The ephemeral events in the room that aren't recorded in the timeline or state of the room. e.g. typing.
AccountData AccountData `json:"account_data"` // The private data that this user has attached to this room.
UnreadNotifications UnreadNotificationCounts `json:"unread_notifications"` // Counts of unread notifications for this room
}
type AccountData struct {
Events []events.Event `json:"events"` // List of events.
}
type UnreadNotificationCounts struct {
HighlightCount int `json:"highlight_count"` // The number of unread notifications for this room with the highlight flag set
NotificationCount int `json:"notification_count"` // The total number of unread notifications for this room
}
type LeftRoom struct {
State events.State `json:"state"` // The state updates for the room up to the start of the timeline.
Timeline Timeline `json:"timeline"` // The timeline of messages and state changes in the room up to the point when the user left.
AccountData AccountData `json:"account_data"` // The private data that this user has attached to this room.
}
type InvitedRoom struct {
InviteState InviteState `json:"invite_state"` // The state of a room that the user has been invited to. These state events may only have the sender, type, state_key and content keys present. These events do not replace any state that the client already has for the room, for example if the client has archived the room. Instead the client should keep two separate copies of the state: the one from the invite_state and one from the archived state. If the client joins the room then the current state will be given as a delta against the archived state not the invite_state.
}
type InviteState struct {
Events []events.StrippedState `json:"events"` // The StrippedState events that form the invite state.
}
type Timeline struct {
Events []events.RoomEvent `json:"events"` // List of events.
Limited bool `json:"limited"` // True if the number of events returned was limited by the limit on the filter.
PrevBatch string `json:"prev_batch"` // A token that can be supplied to the from parameter of the rooms/{roomId}/messages endpoint.
}
// BuildEmptySyncReply is function which builds empty SyncReply model
@ -26,14 +70,14 @@ func BuildEmptySyncReply() *SyncReply {
return &SyncReply{
NextBatch: "",
Rooms: RoomsSyncReply{
Join: make(map[string]events.JoinedRoom),
Invite: make(map[string]events.InvitedRoom),
Leave: make(map[string]events.LeftRoom),
Join: make(map[string]JoinedRoom),
Invite: make(map[string]InvitedRoom),
Leave: make(map[string]LeftRoom),
},
Presence: events.Presence{
Events: nil,
},
AccountData: events.AccountData{
AccountData: AccountData{
Events: nil,
},
ToDevice: events.ToDevice{