Skip to content

Commit

Permalink
Move NewEventData to free function
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Oct 10, 2023
1 parent d08f82a commit 32479e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
22 changes: 22 additions & 0 deletions sync3/caches/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ type EventData struct {
ForceInitial bool
}

func NewEventData(event json.RawMessage, roomID string, latestPos int64) *EventData {
// parse the event to pull out fields we care about
var stateKey *string
ev := gjson.ParseBytes(event)
if sk := ev.Get("state_key"); sk.Exists() {
stateKey = &sk.Str
}
eventType := ev.Get("type").Str

return &EventData{
Event: event,
RoomID: roomID,
EventType: eventType,
StateKey: stateKey,
Content: ev.Get("content"),
NID: latestPos,
Timestamp: ev.Get("origin_server_ts").Uint(),
Sender: ev.Get("sender").Str,
TransactionID: ev.Get("unsigned.transaction_id").Str,
}
}

var logger = zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "15:04:05",
Expand Down
27 changes: 2 additions & 25 deletions sync3/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/matrix-org/sliding-sync/internal"
"github.com/matrix-org/sliding-sync/sync3/caches"
"github.com/rs/zerolog"
"github.com/tidwall/gjson"
)

var logger = zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{
Expand Down Expand Up @@ -78,28 +77,6 @@ func (d *Dispatcher) ReceiverForUser(userID string) Receiver {
return d.userToReceiver[userID]
}

func (d *Dispatcher) newEventData(event json.RawMessage, roomID string, latestPos int64) *caches.EventData {
// parse the event to pull out fields we care about
var stateKey *string
ev := gjson.ParseBytes(event)
if sk := ev.Get("state_key"); sk.Exists() {
stateKey = &sk.Str
}
eventType := ev.Get("type").Str

return &caches.EventData{
Event: event,
RoomID: roomID,
EventType: eventType,
StateKey: stateKey,
Content: ev.Get("content"),
NID: latestPos,
Timestamp: ev.Get("origin_server_ts").Uint(),
Sender: ev.Get("sender").Str,
TransactionID: ev.Get("unsigned.transaction_id").Str,
}
}

// Called by v2 pollers when we receive an initial state block. Very similar to OnNewEvents but
// done in bulk for speed.
func (d *Dispatcher) OnNewInitialRoomState(ctx context.Context, roomID string, state []json.RawMessage) {
Expand All @@ -117,7 +94,7 @@ func (d *Dispatcher) OnNewInitialRoomState(ctx context.Context, roomID string, s
eventDatas := make([]*caches.EventData, len(state))
var joined, invited []string
for i, event := range state {
ed := d.newEventData(event, roomID, 0)
ed := caches.NewEventData(event, roomID, 0)
eventDatas[i] = ed
if ed.EventType == "m.room.member" && ed.StateKey != nil {
membership := ed.Content.Get("membership").Str
Expand Down Expand Up @@ -153,7 +130,7 @@ func (d *Dispatcher) OnNewInitialRoomState(ctx context.Context, roomID string, s
func (d *Dispatcher) OnNewEvent(
ctx context.Context, roomID string, event json.RawMessage, nid int64,
) {
ed := d.newEventData(event, roomID, nid)
ed := caches.NewEventData(event, roomID, nid)

// update the tracker
targetUser := ""
Expand Down

0 comments on commit 32479e2

Please sign in to comment.