Skip to content

Commit

Permalink
Add wrapper for MSC2716 batch send
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 26, 2021
1 parent 9cdc8dd commit 6b5ccdf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,21 @@ func (cli *Client) PutPushRule(scope string, kind pushrules.PushRuleType, ruleID
return err
}

// BatchSend sends a batch of historical events into a room. This is only available for appservices.
//
// See https://github.com/matrix-org/matrix-doc/pull/2716 for more info.
func (cli *Client) BatchSend(roomID id.RoomID, req *ReqBatchSend) (resp *RespBatchSend, err error) {
path := URLPath{"_matrix", "client", "unstable", "org.matrix.msc2716", "rooms", roomID, "batch_send"}
query := map[string]string{
"prev_event_id": req.PrevEventID.String(),
}
if len(req.BatchID) > 0 {
query["batch_id"] = req.BatchID.String()
}
_, err = cli.MakeRequest("POST", cli.BuildBaseURLWithQuery(path, query), req, &resp)
return
}

// TxnID returns the next transaction ID.
func (cli *Client) TxnID() string {
txnID := atomic.AddInt32(&cli.txnID, 1)
Expand Down
8 changes: 8 additions & 0 deletions id/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func NewRoomAlias(localpart, server string) RoomAlias {
// https://matrix.org/docs/spec/rooms/v4#event-ids
type EventID string

// A BatchID is a string identifying a batch of events being backfilled to a room.
// https://github.com/matrix-org/matrix-doc/pull/2716
type BatchID string

func (roomID RoomID) String() string {
return string(roomID)
}
Expand All @@ -39,3 +43,7 @@ func (roomAlias RoomAlias) String() string {
func (eventID EventID) String() string {
return string(eventID)
}

func (batchID BatchID) String() string {
return string(batchID)
}
8 changes: 8 additions & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,11 @@ type ReqPutPushRule struct {
Conditions []pushrules.PushCondition `json:"conditions"`
Pattern string `json:"pattern"`
}

type ReqBatchSend struct {
PrevEventID id.EventID `json:"-"`
BatchID id.BatchID `json:"-"`

StateEventsAtStart []*event.Event `json:"state_events_at_start"`
Events []*event.Event `json:"events"`
}
11 changes: 11 additions & 0 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,14 @@ type RespDeviceInfo struct {
LastSeenIP string `json:"last_seen_ip"`
LastSeenTS int64 `json:"last_seen_ts"`
}

type RespBatchSend struct {
StateEventIDs []id.EventID `json:"state_event_ids"`
EventIDs []id.EventID `json:"event_ids"`

InsertionEventID id.EventID `json:"insertion_event_id"`
BatchEventID id.EventID `json:"batch_event_id"`
BaseInsertionEventID id.EventID `json:"base_insertion_event_id"`

NextBatchID id.BatchID `json:"next_batch_id"`
}

0 comments on commit 6b5ccdf

Please sign in to comment.