types.go 12.6 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
// 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.
//
//
// Modifications copyright (C) 2020 Finogeeks Co., Ltd

package syncapitypes

import (
	jsonRaw "encoding/json"
	"strconv"
	"sync"

	"github.com/finogeeks/ligase/model/types"
	"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
	"github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// StreamPosition represents the offset in the sync stream a client is at.
type StreamPosition int64

// String implements the Stringer interface.
func (sp StreamPosition) String() string {
	return strconv.FormatInt(int64(sp), 10)
}

// PrevEventRef represents a reference to a previous event in a state event upgrade
type PrevEventRef struct {
	PrevContent   jsonRaw.RawMessage `json:"prev_content"`
	ReplacesState string             `json:"replaces_state"`
	PrevSender    string             `json:"prev_sender"`
	PreOffset     int64              `json:"prev_offset"`
}

// 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 {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"account_data"`
	Presence struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"presence"`
	Rooms struct {
		Join   map[string]JoinResponse   `json:"join"`
		Invite map[string]InviteResponse `json:"invite"`
		Leave  map[string]LeaveResponse  `json:"leave"`
	} `json:"rooms"`
	/* extensions */
	// ToDevice send to device extension
	ToDevice ToDevice `json:"to_device"`
	// DeviceList encryptoapi key management /keyChange extension
	DeviceList DeviceLists `json:"device_lists"`
	// compatibility with no definition todo: del it
	SignNum map[string]int `json:"device_one_time_keys_count"`
	Lock    *sync.Mutex    `json:"-"`
}

func (p *Response) Encode() ([]byte, error) {
	return json.Marshal(p)
}

func (p *Response) Decode(input []byte) error {
	return json.Unmarshal(input, p)
}

type SyncServerResponse struct {
	Rooms struct {
		Join   map[string]JoinResponse   `json:"join,omitempty"`
		Invite map[string]InviteResponse `json:"invite,omitempty"`
		Leave  map[string]LeaveResponse  `json:"leave,omitempty"`
	} `json:"rooms,omitempty"`
	MaxReceiptOffset int64            `json:"max_receipt_offset,omitempty"`
	AllLoaded        bool             `json:"all_loaded,omitempty"`
	NewUsers         []string         `json:"new_users,omitempty"`
	Ready            bool             `json:"ready,omitempty"`
	MaxRoomOffset    map[string]int64 `json:"max_room_offset,omitempty"`
}

type ToDevice struct {
	StdEvent []types.StdEvent `json:"events"`
}

type DeviceLists struct {
	Changed []string `json:"changed"`
}

type PaginationChunk struct {
	Start string                          `json:"start"`
	Chunk []gomatrixserverlib.ClientEvent `json:"chunk"`
	End   string                          `json:"end"`
}

func (p *PaginationChunk) Encode() ([]byte, error) {
	return json.Marshal(p)
}

func (p *PaginationChunk) Decode(input []byte) error {
	return json.Unmarshal(input, p)
}

type RoomInfo struct {
	RoomID     string `json:"room_id"`
	Membership string `json:"membership"`
	//Invite      gomatrixserverlib.ClientEvent   `json:"invite"`
	Messages    PaginationChunk                 `json:"messages"`
	State       []gomatrixserverlib.ClientEvent `json:"state"`
	Visibility  string                          `json:"visibility", omitempty`
	AccountData []gomatrixserverlib.ClientEvent `json:"account_data"`
	Presence    struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"presence"`
}

func (r *RoomInfo) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *RoomInfo) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type StdHolder struct {
	StreamID int64  `json:"stream_id"`
	Sender   string `json:"sender"`
	EventTyp string `json:"event_type"`
	Event    []byte `json:"event"`
}

// NewResponse creates an empty response with initialised maps.
func NewResponse(pos StreamPosition) *Response {
	res := Response{}
	// Make sure we send the next_batch as a string. We don't want to confuse clients by sending this
	// as an integer even though (at the moment) it is.
	res.NextBatch = pos.String()
	// Pre-initialise the maps. Synapse will return {} even if there are no rooms under a specific section,
	// 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.
	res.AccountData.Events = make([]gomatrixserverlib.ClientEvent, 0)
	res.Presence.Events = make([]gomatrixserverlib.ClientEvent, 0)
	res.ToDevice.StdEvent = make([]types.StdEvent, 0)
	res.DeviceList.Changed = make([]string, 0)
	res.Lock = new(sync.Mutex)
	return &res
}

// JoinResponse represents a /sync response for a room which is under the 'join' key.
type JoinResponse struct {
	State struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"state"`
	Timeline struct {
		Events    []gomatrixserverlib.ClientEvent `json:"events"`
		Limited   bool                            `json:"limited"`
		PrevBatch string                          `json:"prev_batch"`
	} `json:"timeline"`
	Ephemeral struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"ephemeral"`
	AccountData struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"account_data"`
	Unread *UnreadNotifications `json:"unread_notifications,omitempty"`
}

type UnreadNotifications struct {
	HighLightCount    int64 `json:"highlight_count"`
	NotificationCount int64 `json:"notification_count"`
}

// NewJoinResponse creates an empty response with initialised arrays.
func NewJoinResponse() *JoinResponse {
	res := JoinResponse{}
	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)
	return &res
}

// InviteResponse represents a /sync response for a room which is under the 'invite' key.
type InviteResponse struct {
	InviteState struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"invite_state"`
}

// NewInviteResponse creates an empty response with initialised arrays.
func NewInviteResponse() *InviteResponse {
	res := InviteResponse{}
	res.InviteState.Events = make([]gomatrixserverlib.ClientEvent, 0)
	return &res
}

// LeaveResponse represents a /sync response for a room which is under the 'leave' key.
type LeaveResponse struct {
	State struct {
		Events []gomatrixserverlib.ClientEvent `json:"events"`
	} `json:"state"`
	Timeline struct {
		Events    []gomatrixserverlib.ClientEvent `json:"events"`
		Limited   bool                            `json:"limited"`
		PrevBatch string                          `json:"prev_batch"`
	} `json:"timeline"`
}

// NewLeaveResponse creates an empty response with initialised arrays.
func NewLeaveResponse() *LeaveResponse {
	res := LeaveResponse{}
	res.State.Events = make([]gomatrixserverlib.ClientEvent, 0)
	res.Timeline.Events = make([]gomatrixserverlib.ClientEvent, 0)
	return &res
}

type KeyChanged struct {
	Changes []string `json:"changed"`
}

func (r *KeyChanged) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *KeyChanged) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type JoinedRoomsResp struct {
	JoinedRooms []string `json:"joined_rooms,omitempty"`
}

func (r *JoinedRoomsResp) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *JoinedRoomsResp) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type StateEventInState struct {
	gomatrixserverlib.ClientEvent
	PrevContent   jsonRaw.RawMessage `json:"prev_content,omitempty"`
	ReplacesState string             `json:"replaces_state,omitempty"`
}

type StateEventInStateResp []StateEventInState

func (r StateEventInStateResp) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r StateEventInStateResp) Decode(input []byte) error {
	return json.Unmarshal(input, &r)
}

type MemberResponse struct {
	Chunk []gomatrixserverlib.ClientEvent `json:"chunk"`
}

func (r *MemberResponse) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *MemberResponse) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type MessageEventResp struct {
	Start string                          `json:"start,omitempty"`
	Chunk []gomatrixserverlib.ClientEvent `json:"chunk"`
	End   string                          `json:"end,omitempty"`
}

func (r *MessageEventResp) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *MessageEventResp) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type ContextEventResp struct {
	Start     string                           `json:"start"`
	End       string                           `json:"end"`
	EvsBefore []gomatrixserverlib.ClientEvent  `json:"events_before"`
	Event     gomatrixserverlib.ClientEvent    `json:"event"`
	EvsAfter  []gomatrixserverlib.ClientEvent  `json:"events_after"`
	State     []*gomatrixserverlib.ClientEvent `json:"state"`
}

func (r *ContextEventResp) Encode() ([]byte, error) {
	return json.Marshal(r)
}

func (r *ContextEventResp) Decode(input []byte) error {
	return json.Unmarshal(input, r)
}

type ReceiptUpdate struct {
	Users  []string `json:"users"`
	Offset int64    `json:"offset"`
	RoomID string   `json:"room_id"`
}

type TypingUpdate struct {
	Type      string   `json:"type,omitempty"`
	RoomID    string   `json:"room_id,omitempty"`
	UserID    string   `json:"user_id,omitempty"`
	DeviceID  string   `json:"device_id,omitempty"`
	RoomUsers []string `json:"users"`
}

type SyncServerRequest struct {
	RequestType      string     `json:"request_type,omitempty"`
	InviteRooms      []SyncRoom `json:"invite_rooms,omitempty"`
	JoinRooms        []SyncRoom `json:"join_rooms,omitempty"`
	LeaveRooms       []SyncRoom `json:"leave_rooms,omitempty"`
	JoinedRooms      []string   `json:"joined_rooms,omitempty"`
	UserID           string     `json:"user_id,omitempty"`
	DeviceID         string     `json:"device_id,omitempty"`
	ReceiptOffset    int64      `json:"receipt_offset,omitempty"`
	MaxReceiptOffset int64      `json:"max_receipt_offset,omitempty"`
	IsHuman          bool       `json:"is_human,omitempty"`
	Limit            int        `json:"limit,omitempty"`
	LoadReady        bool       `json:"load_ready,omitempty"`
	SyncReady        bool       `json:"sync_ready,omitempty"`
	SyncInstance     uint32     `json:"sync_instance"`
	IsFullSync       bool       `json:"is_full_sync"`
	Reply            string
	TraceID          string `json:"trace_id"`
	Slot             uint32 `json:"slot"`
	RSlot            uint32 `json:"rslot"`
}

type SyncRoom struct {
	RoomID    string `json:"room_id,omitempty"`
	RoomState string `json:"room_state,omitempty"`
	Start     int64  `json:"start,omitempty"`
	End       int64  `json:"end,omitempty"`
}

type SyncUnreadRequest struct {
	JoinRooms    []string `json:"join_rooms,omitempty"`
	UserID       string   `json:"user_id,omitempty"`
	SyncInstance uint32   `json:"sync_instance"`
	Reply        string
}

type SyncUnreadResponse struct {
	Count int64 `json:"count,omitempty"`
}

type UserTimeLineStream struct {
	Offset     int64  `json:"offset,omitempty"`
	UserID     string `json:"user_id,omitempty"`
	RoomID     string `json:"room_id,omitempty"`
	RoomOffset int64  `json:"room_offset,omitempty"`
	RoomState  string `json:"room_state,omitempty"`
}

type ClientEvents []gomatrixserverlib.ClientEvent

func (es ClientEvents) Len() int { return len(es) }

func (es ClientEvents) Less(i, j int) bool {
	return es[i].Depth < es[j].Depth
}

func (es ClientEvents) Swap(i, j int) { es[i], es[j] = es[j], es[i] }