2017-04-20 17:40:52 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-12 10:06:26 -05:00
|
|
|
package types
|
2017-04-11 05:52:26 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-22 05:34:54 -05:00
|
|
|
"encoding/json"
|
2020-01-23 11:51:10 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-30 08:52:21 -05:00
|
|
|
"sort"
|
2017-04-13 10:56:46 -05:00
|
|
|
"strconv"
|
2020-01-23 11:51:10 -06:00
|
|
|
"strings"
|
2017-04-19 10:04:01 -05:00
|
|
|
|
2020-01-23 11:51:10 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-04-19 10:04:01 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-04-24 10:30:25 -05:00
|
|
|
"github.com/tidwall/gjson"
|
2017-04-11 05:52:26 -05:00
|
|
|
)
|
|
|
|
|
2020-01-23 11:51:10 -06:00
|
|
|
var (
|
2020-05-13 06:14:50 -05:00
|
|
|
// ErrInvalidSyncTokenType is returned when an attempt at creating a
|
|
|
|
// new instance of SyncToken with an invalid type (i.e. neither "s"
|
2020-01-23 11:51:10 -06:00
|
|
|
// nor "t").
|
2020-05-13 06:14:50 -05:00
|
|
|
ErrInvalidSyncTokenType = fmt.Errorf("Sync token has an unknown prefix (should be either s or t)")
|
|
|
|
// ErrInvalidSyncTokenLen is returned when the pagination token is an
|
2020-01-23 11:51:10 -06:00
|
|
|
// invalid length
|
2020-05-13 06:14:50 -05:00
|
|
|
ErrInvalidSyncTokenLen = fmt.Errorf("Sync token has an invalid length")
|
2020-01-23 11:51:10 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// StreamPosition represents the offset in the sync stream a client is at.
|
|
|
|
type StreamPosition int64
|
|
|
|
|
2020-07-29 13:00:04 -05:00
|
|
|
// LogPosition represents the offset in a Kafka log a client is at.
|
|
|
|
type LogPosition struct {
|
|
|
|
Partition int32
|
|
|
|
Offset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAfter returns true if this position is after `lp`.
|
|
|
|
func (p *LogPosition) IsAfter(lp *LogPosition) bool {
|
|
|
|
if lp == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.Partition != lp.Partition {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return p.Offset > lp.Offset
|
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
// StreamEvent is the same as gomatrixserverlib.Event but also has the PDU stream position for this event.
|
2020-01-23 11:51:10 -06:00
|
|
|
type StreamEvent struct {
|
2020-03-19 07:07:01 -05:00
|
|
|
gomatrixserverlib.HeaderedEvent
|
2020-01-23 11:51:10 -06:00
|
|
|
StreamPosition StreamPosition
|
|
|
|
TransactionID *api.TransactionID
|
|
|
|
ExcludeFromSync bool
|
2019-07-12 09:59:53 -05:00
|
|
|
}
|
2017-04-11 05:52:26 -05:00
|
|
|
|
2020-05-15 03:41:12 -05:00
|
|
|
// Range represents a range between two stream positions.
|
|
|
|
type Range struct {
|
|
|
|
// From is the position the client has already received.
|
|
|
|
From StreamPosition
|
|
|
|
// To is the position the client is going towards.
|
|
|
|
To StreamPosition
|
|
|
|
// True if the client is going backwards
|
|
|
|
Backwards bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Low returns the low number of the range.
|
|
|
|
// This represents the position the client already has and hence is exclusive.
|
|
|
|
func (r *Range) Low() StreamPosition {
|
|
|
|
if !r.Backwards {
|
|
|
|
return r.From
|
|
|
|
}
|
|
|
|
return r.To
|
|
|
|
}
|
|
|
|
|
|
|
|
// High returns the high number of the range
|
|
|
|
// This represents the position the client is going towards and hence is inclusive.
|
|
|
|
func (r *Range) High() StreamPosition {
|
|
|
|
if !r.Backwards {
|
|
|
|
return r.To
|
|
|
|
}
|
|
|
|
return r.From
|
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
// SyncTokenType represents the type of a sync token.
|
2020-01-23 11:51:10 -06:00
|
|
|
// It can be either "s" (representing a position in the whole stream of events)
|
|
|
|
// or "t" (representing a position in a room's topology/depth).
|
2020-05-13 06:14:50 -05:00
|
|
|
type SyncTokenType string
|
2020-01-23 11:51:10 -06:00
|
|
|
|
|
|
|
const (
|
2020-05-13 06:14:50 -05:00
|
|
|
// SyncTokenTypeStream represents a position in the server's whole
|
2020-01-23 11:51:10 -06:00
|
|
|
// stream of events
|
2020-05-13 06:14:50 -05:00
|
|
|
SyncTokenTypeStream SyncTokenType = "s"
|
|
|
|
// SyncTokenTypeTopology represents a position in a room's topology.
|
|
|
|
SyncTokenTypeTopology SyncTokenType = "t"
|
2020-01-23 11:51:10 -06:00
|
|
|
)
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
type StreamingToken struct {
|
|
|
|
syncToken
|
2020-07-29 13:00:04 -05:00
|
|
|
logs map[string]*LogPosition
|
|
|
|
}
|
|
|
|
|
2020-07-30 05:15:46 -05:00
|
|
|
func (t *StreamingToken) SetLog(name string, lp *LogPosition) {
|
|
|
|
t.logs[name] = lp
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:00:04 -05:00
|
|
|
func (t *StreamingToken) Log(name string) *LogPosition {
|
|
|
|
l, ok := t.logs[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return l
|
2020-05-13 06:14:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *StreamingToken) PDUPosition() StreamPosition {
|
|
|
|
return t.Positions[0]
|
|
|
|
}
|
|
|
|
func (t *StreamingToken) EDUPosition() StreamPosition {
|
|
|
|
return t.Positions[1]
|
|
|
|
}
|
2020-06-19 07:29:27 -05:00
|
|
|
func (t *StreamingToken) String() string {
|
2020-07-30 08:52:21 -05:00
|
|
|
var logStrings []string
|
2020-07-29 13:00:04 -05:00
|
|
|
for name, lp := range t.logs {
|
|
|
|
logStr := fmt.Sprintf("%s-%d-%d", name, lp.Partition, lp.Offset)
|
|
|
|
logStrings = append(logStrings, logStr)
|
|
|
|
}
|
2020-07-30 08:52:21 -05:00
|
|
|
sort.Strings(logStrings)
|
2020-07-29 13:00:04 -05:00
|
|
|
// E.g s11_22_33.dl0-134.ab1-441
|
2020-07-30 08:52:21 -05:00
|
|
|
return strings.Join(append([]string{t.syncToken.String()}, logStrings...), ".")
|
2020-06-19 07:29:27 -05:00
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
|
|
|
|
// IsAfter returns true if ANY position in this token is greater than `other`.
|
|
|
|
func (t *StreamingToken) IsAfter(other StreamingToken) bool {
|
|
|
|
for i := range other.Positions {
|
|
|
|
if t.Positions[i] > other.Positions[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 13:00:04 -05:00
|
|
|
for name := range t.logs {
|
|
|
|
otherLog := other.Log(name)
|
|
|
|
if otherLog == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.logs[name].IsAfter(otherLog) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUpdates returns a copy of the StreamingToken with updates applied from another StreamingToken.
|
|
|
|
// If the latter StreamingToken contains a field that is not 0, it is considered an update,
|
|
|
|
// and its value will replace the corresponding value in the StreamingToken on which WithUpdates is called.
|
2020-07-29 13:00:04 -05:00
|
|
|
// If the other token has a log, they will replace any existing log on this token.
|
2020-05-13 06:14:50 -05:00
|
|
|
func (t *StreamingToken) WithUpdates(other StreamingToken) (ret StreamingToken) {
|
|
|
|
ret.Type = t.Type
|
|
|
|
ret.Positions = make([]StreamPosition, len(t.Positions))
|
|
|
|
for i := range t.Positions {
|
|
|
|
ret.Positions[i] = t.Positions[i]
|
|
|
|
if other.Positions[i] == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Positions[i] = other.Positions[i]
|
|
|
|
}
|
2020-07-29 13:00:04 -05:00
|
|
|
for name := range t.logs {
|
|
|
|
otherLog := other.Log(name)
|
|
|
|
if otherLog == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.logs[name] = otherLog
|
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type TopologyToken struct {
|
|
|
|
syncToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TopologyToken) Depth() StreamPosition {
|
|
|
|
return t.Positions[0]
|
|
|
|
}
|
|
|
|
func (t *TopologyToken) PDUPosition() StreamPosition {
|
|
|
|
return t.Positions[1]
|
|
|
|
}
|
|
|
|
func (t *TopologyToken) StreamToken() StreamingToken {
|
2020-07-29 13:00:04 -05:00
|
|
|
return NewStreamToken(t.PDUPosition(), 0, nil)
|
2020-05-13 06:14:50 -05:00
|
|
|
}
|
|
|
|
func (t *TopologyToken) String() string {
|
|
|
|
return t.syncToken.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the topology token to one event earlier.
|
|
|
|
func (t *TopologyToken) Decrement() {
|
|
|
|
depth := t.Positions[0]
|
|
|
|
pduPos := t.Positions[1]
|
|
|
|
if depth-1 <= 0 {
|
2020-05-14 11:30:16 -05:00
|
|
|
// nothing can be lower than this
|
2020-05-13 06:14:50 -05:00
|
|
|
depth = 1
|
|
|
|
} else {
|
2020-05-14 11:30:16 -05:00
|
|
|
// this assumes that we will never have 1000 events all with the same
|
|
|
|
// depth. TODO: work out what the right PDU position is to use, probably needs a db hit.
|
2020-05-13 06:14:50 -05:00
|
|
|
depth--
|
|
|
|
pduPos += 1000
|
|
|
|
}
|
|
|
|
// The lowest token value is 1, therefore we need to manually set it to that
|
|
|
|
// value if we're below it.
|
|
|
|
if depth < 1 {
|
|
|
|
depth = 1
|
|
|
|
}
|
|
|
|
t.Positions = []StreamPosition{
|
|
|
|
depth, pduPos,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSyncTokenFromString takes a string of the form "xyyyy..." where "x"
|
2020-01-23 11:51:10 -06:00
|
|
|
// represents the type of a pagination token and "yyyy..." the token itself, and
|
2020-05-13 06:14:50 -05:00
|
|
|
// parses it in order to create a new instance of SyncToken. Returns an
|
2020-01-23 11:51:10 -06:00
|
|
|
// error if the token couldn't be parsed into an int64, or if the token type
|
2020-05-13 06:14:50 -05:00
|
|
|
// isn't a known type (returns ErrInvalidSyncTokenType in the latter
|
2020-01-23 11:51:10 -06:00
|
|
|
// case).
|
2020-07-29 13:00:04 -05:00
|
|
|
func newSyncTokenFromString(s string) (token *syncToken, categories []string, err error) {
|
2020-01-23 11:51:10 -06:00
|
|
|
if len(s) == 0 {
|
2020-07-29 13:00:04 -05:00
|
|
|
return nil, nil, ErrInvalidSyncTokenLen
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
token = new(syncToken)
|
2020-01-23 11:51:10 -06:00
|
|
|
var positions []string
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
switch t := SyncTokenType(s[:1]); t {
|
|
|
|
case SyncTokenTypeStream, SyncTokenTypeTopology:
|
2020-01-23 11:51:10 -06:00
|
|
|
token.Type = t
|
2020-07-29 13:00:04 -05:00
|
|
|
categories = strings.Split(s[1:], ".")
|
|
|
|
positions = strings.Split(categories[0], "_")
|
2020-01-23 11:51:10 -06:00
|
|
|
default:
|
2020-07-29 13:00:04 -05:00
|
|
|
return nil, nil, ErrInvalidSyncTokenType
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
for _, pos := range positions {
|
|
|
|
if posInt, err := strconv.ParseInt(pos, 10, 64); err != nil {
|
2020-07-29 13:00:04 -05:00
|
|
|
return nil, nil, err
|
2020-05-13 06:14:50 -05:00
|
|
|
} else if posInt < 0 {
|
2020-07-29 13:00:04 -05:00
|
|
|
return nil, nil, errors.New("negative position not allowed")
|
2020-01-23 11:51:10 -06:00
|
|
|
} else {
|
2020-05-13 06:14:50 -05:00
|
|
|
token.Positions = append(token.Positions, StreamPosition(posInt))
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
// NewTopologyToken creates a new sync token for /messages
|
|
|
|
func NewTopologyToken(depth, streamPos StreamPosition) TopologyToken {
|
|
|
|
if depth < 0 {
|
|
|
|
depth = 1
|
|
|
|
}
|
|
|
|
return TopologyToken{
|
|
|
|
syncToken: syncToken{
|
|
|
|
Type: SyncTokenTypeTopology,
|
|
|
|
Positions: []StreamPosition{depth, streamPos},
|
|
|
|
},
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
2019-07-12 09:59:53 -05:00
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
func NewTopologyTokenFromString(tok string) (token TopologyToken, err error) {
|
2020-07-29 13:00:04 -05:00
|
|
|
t, _, err := newSyncTokenFromString(tok)
|
2020-05-13 06:14:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.Type != SyncTokenTypeTopology {
|
|
|
|
err = fmt.Errorf("token %s is not a topology token", tok)
|
|
|
|
return
|
|
|
|
}
|
2020-06-19 07:29:27 -05:00
|
|
|
if len(t.Positions) < 2 {
|
|
|
|
err = fmt.Errorf("token %s wrong number of values, got %d want at least 2", tok, len(t.Positions))
|
2020-05-13 06:14:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return TopologyToken{
|
|
|
|
syncToken: *t,
|
|
|
|
}, nil
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
// NewStreamToken creates a new sync token for /sync
|
2020-07-29 13:00:04 -05:00
|
|
|
func NewStreamToken(pduPos, eduPos StreamPosition, logs map[string]*LogPosition) StreamingToken {
|
|
|
|
if logs == nil {
|
|
|
|
logs = make(map[string]*LogPosition)
|
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
return StreamingToken{
|
|
|
|
syncToken: syncToken{
|
|
|
|
Type: SyncTokenTypeStream,
|
|
|
|
Positions: []StreamPosition{pduPos, eduPos},
|
|
|
|
},
|
2020-07-29 13:00:04 -05:00
|
|
|
logs: logs,
|
2020-05-13 06:14:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func NewStreamTokenFromString(tok string) (token StreamingToken, err error) {
|
2020-07-29 13:00:04 -05:00
|
|
|
t, categories, err := newSyncTokenFromString(tok)
|
2020-05-13 06:14:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
2019-07-12 09:59:53 -05:00
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
if t.Type != SyncTokenTypeStream {
|
|
|
|
err = fmt.Errorf("token %s is not a streaming token", tok)
|
|
|
|
return
|
2019-07-12 09:59:53 -05:00
|
|
|
}
|
2020-06-19 07:29:27 -05:00
|
|
|
if len(t.Positions) < 2 {
|
|
|
|
err = fmt.Errorf("token %s wrong number of values, got %d want at least 2", tok, len(t.Positions))
|
2020-05-13 06:14:50 -05:00
|
|
|
return
|
|
|
|
}
|
2020-07-29 13:00:04 -05:00
|
|
|
logs := make(map[string]*LogPosition)
|
|
|
|
if len(categories) > 1 {
|
|
|
|
// dl-0-1234
|
|
|
|
// $log_name-$partition-$offset
|
|
|
|
for _, logStr := range categories[1:] {
|
|
|
|
segments := strings.Split(logStr, "-")
|
|
|
|
if len(segments) != 3 {
|
|
|
|
err = fmt.Errorf("token %s - invalid log: %s", tok, logStr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var partition int64
|
|
|
|
partition, err = strconv.ParseInt(segments[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var offset int64
|
|
|
|
offset, err = strconv.ParseInt(segments[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logs[segments[0]] = &LogPosition{
|
|
|
|
Partition: int32(partition),
|
|
|
|
Offset: offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 06:14:50 -05:00
|
|
|
return StreamingToken{
|
|
|
|
syncToken: *t,
|
2020-07-29 13:00:04 -05:00
|
|
|
logs: logs,
|
2020-05-13 06:14:50 -05:00
|
|
|
}, nil
|
2017-04-11 05:52:26 -05:00
|
|
|
}
|
|
|
|
|
2020-05-13 06:14:50 -05:00
|
|
|
// syncToken represents a syncapi token, used for interactions with
|
|
|
|
// /sync or /messages, for example.
|
|
|
|
type syncToken struct {
|
|
|
|
Type SyncTokenType
|
|
|
|
// A list of stream positions, their meanings vary depending on the token type.
|
|
|
|
Positions []StreamPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
// String translates a SyncToken to a string of the "xyyyy..." (see
|
|
|
|
// NewSyncToken to know what it represents).
|
|
|
|
func (p *syncToken) String() string {
|
|
|
|
posStr := make([]string, len(p.Positions))
|
|
|
|
for i := range p.Positions {
|
|
|
|
posStr[i] = strconv.FormatInt(int64(p.Positions[i]), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s", p.Type, strings.Join(posStr, "_"))
|
2020-01-23 11:51:10 -06:00
|
|
|
}
|
|
|
|
|
2017-09-22 05:34:54 -05:00
|
|
|
// PrevEventRef represents a reference to a previous event in a state event upgrade
|
|
|
|
type PrevEventRef struct {
|
|
|
|
PrevContent json.RawMessage `json:"prev_content"`
|
|
|
|
ReplacesState string `json:"replaces_state"`
|
|
|
|
PrevSender string `json:"prev_sender"`
|
|
|
|
}
|
|
|
|
|
2017-04-11 05:52:26 -05:00
|
|
|
// Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
|
|
|
|
type Response struct {
|
|
|
|
NextBatch string `json:"next_batch"`
|
|
|
|
AccountData struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2020-06-26 05:07:52 -05:00
|
|
|
} `json:"account_data,omitempty"`
|
2017-04-11 05:52:26 -05:00
|
|
|
Presence struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2020-06-26 05:07:52 -05:00
|
|
|
} `json:"presence,omitempty"`
|
2017-04-11 05:52:26 -05:00
|
|
|
Rooms struct {
|
|
|
|
Join map[string]JoinResponse `json:"join"`
|
|
|
|
Invite map[string]InviteResponse `json:"invite"`
|
|
|
|
Leave map[string]LeaveResponse `json:"leave"`
|
|
|
|
} `json:"rooms"`
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
ToDevice struct {
|
|
|
|
Events []gomatrixserverlib.SendToDeviceEvent `json:"events"`
|
|
|
|
} `json:"to_device"`
|
2020-07-27 03:19:55 -05:00
|
|
|
DeviceLists struct {
|
|
|
|
Changed []string `json:"changed,omitempty"`
|
|
|
|
Left []string `json:"left,omitempty"`
|
|
|
|
} `json:"device_lists,omitempty"`
|
2020-08-03 06:29:58 -05:00
|
|
|
DeviceListsOTKCount map[string]int `json:"device_one_time_keys_count"`
|
2017-04-11 05:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponse creates an empty response with initialised maps.
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
func NewResponse() *Response {
|
|
|
|
res := Response{}
|
2017-06-12 12:30:47 -05:00
|
|
|
// Pre-initialise the maps. Synapse will return {} even if there are no rooms under a specific section,
|
2017-04-11 05:52:26 -05:00
|
|
|
// so let's do the same thing. Bonus: this means we can't get dreaded 'assignment to entry in nil map' errors.
|
|
|
|
res.Rooms.Join = make(map[string]JoinResponse)
|
|
|
|
res.Rooms.Invite = make(map[string]InviteResponse)
|
|
|
|
res.Rooms.Leave = make(map[string]LeaveResponse)
|
|
|
|
|
|
|
|
// Also pre-intialise empty slices or else we'll insert 'null' instead of '[]' for the value.
|
|
|
|
// TODO: We really shouldn't have to do all this to coerce encoding/json to Do The Right Thing. We should
|
|
|
|
// really be using our own Marshal/Unmarshal implementations otherwise this may prove to be a CPU bottleneck.
|
|
|
|
// This also applies to NewJoinResponse, NewInviteResponse and NewLeaveResponse.
|
2017-04-13 10:56:46 -05:00
|
|
|
res.AccountData.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
|
|
|
res.Presence.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
res.ToDevice.Events = make([]gomatrixserverlib.SendToDeviceEvent, 0)
|
2020-08-03 06:29:58 -05:00
|
|
|
res.DeviceListsOTKCount = make(map[string]int)
|
2017-04-11 05:52:26 -05:00
|
|
|
|
|
|
|
return &res
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:34:08 -05:00
|
|
|
// IsEmpty returns true if the response is empty, i.e. used to decided whether
|
|
|
|
// to return the response immediately to the client or to wait for more data.
|
|
|
|
func (r *Response) IsEmpty() bool {
|
|
|
|
return len(r.Rooms.Join) == 0 &&
|
|
|
|
len(r.Rooms.Invite) == 0 &&
|
|
|
|
len(r.Rooms.Leave) == 0 &&
|
|
|
|
len(r.AccountData.Events) == 0 &&
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
len(r.Presence.Events) == 0 &&
|
|
|
|
len(r.ToDevice.Events) == 0
|
2017-10-16 07:34:08 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 05:52:26 -05:00
|
|
|
// JoinResponse represents a /sync response for a room which is under the 'join' key.
|
|
|
|
type JoinResponse struct {
|
|
|
|
State struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"timeline"`
|
|
|
|
Ephemeral struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"ephemeral"`
|
|
|
|
AccountData struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"account_data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJoinResponse creates an empty response with initialised arrays.
|
|
|
|
func NewJoinResponse() *JoinResponse {
|
|
|
|
res := JoinResponse{}
|
2017-04-13 10:56:46 -05:00
|
|
|
res.State.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
|
|
|
res.Timeline.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
|
|
|
res.Ephemeral.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
|
|
|
res.AccountData.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
2017-04-11 05:52:26 -05:00
|
|
|
return &res
|
|
|
|
}
|
|
|
|
|
|
|
|
// InviteResponse represents a /sync response for a room which is under the 'invite' key.
|
|
|
|
type InviteResponse struct {
|
|
|
|
InviteState struct {
|
2020-04-24 10:30:25 -05:00
|
|
|
Events json.RawMessage `json:"events"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"invite_state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInviteResponse creates an empty response with initialised arrays.
|
2020-04-24 10:30:25 -05:00
|
|
|
func NewInviteResponse(event gomatrixserverlib.HeaderedEvent) *InviteResponse {
|
2017-04-11 05:52:26 -05:00
|
|
|
res := InviteResponse{}
|
2020-04-24 10:30:25 -05:00
|
|
|
res.InviteState.Events = json.RawMessage{'[', ']'}
|
|
|
|
if inviteRoomState := gjson.GetBytes(event.Unsigned(), "invite_room_state"); inviteRoomState.Exists() {
|
|
|
|
res.InviteState.Events = json.RawMessage(inviteRoomState.Raw)
|
|
|
|
}
|
2017-04-11 05:52:26 -05:00
|
|
|
return &res
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveResponse represents a /sync response for a room which is under the 'leave' key.
|
|
|
|
type LeaveResponse struct {
|
|
|
|
State struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2017-04-13 10:56:46 -05:00
|
|
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2017-04-11 05:52:26 -05:00
|
|
|
} `json:"timeline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLeaveResponse creates an empty response with initialised arrays.
|
|
|
|
func NewLeaveResponse() *LeaveResponse {
|
|
|
|
res := LeaveResponse{}
|
2017-04-13 10:56:46 -05:00
|
|
|
res.State.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
|
|
|
res.Timeline.Events = make([]gomatrixserverlib.ClientEvent, 0)
|
2017-04-11 05:52:26 -05:00
|
|
|
return &res
|
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
|
|
|
|
type SendToDeviceNID int
|
|
|
|
|
|
|
|
type SendToDeviceEvent struct {
|
|
|
|
gomatrixserverlib.SendToDeviceEvent
|
|
|
|
ID SendToDeviceNID
|
|
|
|
UserID string
|
|
|
|
DeviceID string
|
|
|
|
SentByToken *StreamingToken
|
|
|
|
}
|