mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-16 14:12:24 +03:00
implement AS timestamp massaging (#542)
This commit is contained in:
parent
99005d6a91
commit
a56752f3f6
12 changed files with 82 additions and 50 deletions
|
@ -18,6 +18,8 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/common/config"
|
||||
|
@ -38,18 +40,18 @@ var ErrRoomNoExists = errors.New("Room does not exist")
|
|||
// the room doesn't exist
|
||||
// Returns an error if something else went wrong
|
||||
func BuildEvent(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
builder *gomatrixserverlib.EventBuilder, cfg config.Dendrite,
|
||||
queryAPI api.RoomserverQueryAPI, queryRes *api.QueryLatestEventsAndStateResponse,
|
||||
) (*gomatrixserverlib.Event, error) {
|
||||
err := AddPrevEventsToEvent(ctx, builder, queryAPI, queryRes)
|
||||
err := AddPrevEventsToEvent(req.Context(), builder, queryAPI, queryRes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
|
||||
now := time.Now()
|
||||
event, err := builder.Build(eventID, now, cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey)
|
||||
eventTime := ParseTSParam(req)
|
||||
event, err := builder.Build(eventID, eventTime, cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -57,6 +59,25 @@ 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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue