diff --git a/src/github.com/matrix-org/dendrite/appservice/appservice.go b/src/github.com/matrix-org/dendrite/appservice/appservice.go index 5d2fc085..3c3bd59a 100644 --- a/src/github.com/matrix-org/dendrite/appservice/appservice.go +++ b/src/github.com/matrix-org/dendrite/appservice/appservice.go @@ -17,6 +17,7 @@ package appservice import ( "context" "net/http" + "strconv" "sync" "time" @@ -132,3 +133,22 @@ func generateAppServiceAccount( _, err = deviceDB.CreateDevice(ctx, as.SenderLocalpart, nil, as.ASToken, &as.SenderLocalpart) return err } + +// ParseTSParam takes a req from an application service and parses a Time object +// from the req if it exists in the query parameters. If it doesn't exist, the +// current time is returned. +func ParseTSParam(req *http.Request) time.Time { + // Use the ts parameter's value for event time if present + tsStr := req.URL.Query().Get("ts") + if tsStr == "" { + return time.Now() + } + + // The parameter exists, parse into a Time object + ts, err := strconv.ParseInt(tsStr, 10, 64) + if err != nil { + return time.Unix(ts/1000, 0) + } + + return time.Unix(ts/1000, 0) +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go index 84c1f24c..a209fe6c 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go @@ -214,7 +214,8 @@ func (r joinRoomReq) joinRoomUsingServers( } var queryRes roomserverAPI.QueryLatestEventsAndStateResponse - event, err := common.BuildEvent(r.req, &eb, r.cfg, r.queryAPI, &queryRes) + eventTime := appservice.ParseTSParam(r.req) + event, err := common.BuildEvent(r.req.Context(), eventTime, &eb, r.cfg, r.queryAPI, &queryRes) if err == nil { if _, err = r.producer.SendEvents(r.req.Context(), []gomatrixserverlib.Event{*event}, r.cfg.Matrix.ServerName, nil); err != nil { return httputil.LogThenError(r.req, err) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go index cf4d075e..285dae67 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go @@ -19,6 +19,7 @@ import ( "errors" "net/http" + "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -147,7 +148,8 @@ func buildMembershipEvent( return nil, err } - return common.BuildEvent(req, &builder, cfg, queryAPI, nil) + eventTime := appservice.ParseTSParam(r.req) + return common.BuildEvent(req.Context(), eventTime, &builder, cfg, queryAPI, nil) } // loadProfile lookups the profile of a given user from the database and returns diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go index 35b7226a..f61e1673 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go @@ -18,6 +18,7 @@ import ( "database/sql" "net/http" + "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -284,7 +285,8 @@ func buildMembershipEvents( return nil, err } - event, err := common.BuildEvent(req, &builder, *cfg, queryAPI, nil) + eventTime := appservice.ParseTSParam(req) + event, err := common.BuildEvent(req.Context(), eventTime, &builder, *cfg, queryAPI, nil) if err != nil { return nil, err } diff --git a/src/github.com/matrix-org/dendrite/common/events.go b/src/github.com/matrix-org/dendrite/common/events.go index 41022c7c..70729a31 100644 --- a/src/github.com/matrix-org/dendrite/common/events.go +++ b/src/github.com/matrix-org/dendrite/common/events.go @@ -18,8 +18,6 @@ import ( "context" "errors" "fmt" - "net/http" - "strconv" "time" "github.com/matrix-org/dendrite/common/config" @@ -40,7 +38,8 @@ var ErrRoomNoExists = errors.New("Room does not exist") // the room doesn't exist // Returns an error if something else went wrong func BuildEvent( - req *http.Request, + ctx context.Context, + eventTime time.Time, builder *gomatrixserverlib.EventBuilder, cfg config.Dendrite, queryAPI api.RoomserverQueryAPI, queryRes *api.QueryLatestEventsAndStateResponse, ) (*gomatrixserverlib.Event, error) { @@ -50,7 +49,6 @@ func BuildEvent( } eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName) - eventTime := ParseTSParam(req) event, err := builder.Build(eventID, eventTime, cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey) if err != nil { return nil, err @@ -59,25 +57,6 @@ func BuildEvent( return &event, nil } -// ParseTSParam takes a req from an application service and parses a Time object -// from the req if it exists in the query parameters. If it doesn't exist, the -// current time is returned. -func ParseTSParam(req *http.Request) time.Time { - // Use the ts parameter's value for event time if present - tsStr := req.URL.Query().Get("ts") - if tsStr == "" { - return time.Now() - } - - // The parameter exists, parse into a Time object - ts, err := strconv.ParseInt(tsStr, 10, 64) - if err != nil { - return time.Unix(ts/1000, 0) - } - - return time.Unix(ts/1000, 0) -} - // AddPrevEventsToEvent fills out the prev_events and auth_events fields in builder func AddPrevEventsToEvent( ctx context.Context,