2023-04-04 18:16:53 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package synctypes
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
import (
|
2023-08-31 16:33:38 +01:00
|
|
|
"encoding/json"
|
2023-08-24 16:43:51 +01:00
|
|
|
"fmt"
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-08-31 16:33:38 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-04-19 15:50:33 +01:00
|
|
|
)
|
2023-04-04 18:16:53 +01:00
|
|
|
|
2023-08-31 16:33:38 +01: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"`
|
|
|
|
PrevSenderID string `json:"prev_sender"`
|
|
|
|
}
|
|
|
|
|
2023-04-04 18:16:53 +01:00
|
|
|
type ClientEventFormat int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FormatAll will include all client event keys
|
|
|
|
FormatAll ClientEventFormat = iota
|
|
|
|
// FormatSync will include only the event keys required by the /sync API. Notably, this
|
|
|
|
// means the 'room_id' will be missing from the events.
|
|
|
|
FormatSync
|
2023-08-31 16:33:38 +01:00
|
|
|
// FormatSyncFederation will include all event keys normally included in federated events.
|
|
|
|
// This allows clients to request federated formatted events via the /sync API.
|
|
|
|
FormatSyncFederation
|
2023-04-04 18:16:53 +01:00
|
|
|
)
|
|
|
|
|
2023-08-31 16:33:38 +01:00
|
|
|
// ClientFederationFields extends a ClientEvent to contain the additional fields present in a
|
|
|
|
// federation event. Used when the client requests `event_format` of type `federation`.
|
|
|
|
type ClientFederationFields struct {
|
|
|
|
Depth int64 `json:"depth,omitempty"`
|
|
|
|
PrevEvents []string `json:"prev_events,omitempty"`
|
|
|
|
AuthEvents []string `json:"auth_events,omitempty"`
|
|
|
|
Signatures spec.RawJSON `json:"signatures,omitempty"`
|
|
|
|
Hashes spec.RawJSON `json:"hashes,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-04-04 18:16:53 +01:00
|
|
|
// ClientEvent is an event which is fit for consumption by clients, in accordance with the specification.
|
|
|
|
type ClientEvent struct {
|
2023-04-19 15:50:33 +01:00
|
|
|
Content spec.RawJSON `json:"content"`
|
|
|
|
EventID string `json:"event_id,omitempty"` // EventID is omitted on receipt events
|
|
|
|
OriginServerTS spec.Timestamp `json:"origin_server_ts,omitempty"` // OriginServerTS is omitted on receipt events
|
|
|
|
RoomID string `json:"room_id,omitempty"` // RoomID is omitted on /sync responses
|
|
|
|
Sender string `json:"sender,omitempty"` // Sender is omitted on receipt events
|
2023-06-13 11:50:22 +01:00
|
|
|
SenderKey spec.SenderID `json:"sender_key,omitempty"` // The SenderKey for events in pseudo ID rooms
|
2023-04-19 15:50:33 +01:00
|
|
|
StateKey *string `json:"state_key,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Unsigned spec.RawJSON `json:"unsigned,omitempty"`
|
|
|
|
Redacts string `json:"redacts,omitempty"`
|
2023-08-31 16:33:38 +01:00
|
|
|
|
|
|
|
// Only sent to clients when `event_format` == `federation`.
|
|
|
|
ClientFederationFields
|
2023-04-04 18:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToClientEvents converts server events to client events.
|
2023-06-06 21:55:18 +01:00
|
|
|
func ToClientEvents(serverEvs []gomatrixserverlib.PDU, format ClientEventFormat, userIDForSender spec.UserIDForSender) []ClientEvent {
|
2023-04-04 18:16:53 +01:00
|
|
|
evs := make([]ClientEvent, 0, len(serverEvs))
|
|
|
|
for _, se := range serverEvs {
|
|
|
|
if se == nil {
|
|
|
|
continue // TODO: shouldn't happen?
|
|
|
|
}
|
2023-08-31 16:33:38 +01:00
|
|
|
if format == FormatSyncFederation {
|
|
|
|
evs = append(evs, ToClientEvent(se, format, string(se.SenderID()), se.StateKey(), spec.RawJSON(se.Unsigned())))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:55:18 +01:00
|
|
|
sender := spec.UserID{}
|
2023-06-14 15:23:46 +01:00
|
|
|
validRoomID, err := spec.NewRoomID(se.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
userID, err := userIDForSender(*validRoomID, se.SenderID())
|
2023-06-06 21:55:18 +01:00
|
|
|
if err == nil && userID != nil {
|
|
|
|
sender = *userID
|
|
|
|
}
|
2023-06-12 12:19:25 +01:00
|
|
|
|
|
|
|
sk := se.StateKey()
|
|
|
|
if sk != nil && *sk != "" {
|
2023-06-14 15:23:46 +01:00
|
|
|
skUserID, err := userIDForSender(*validRoomID, spec.SenderID(*sk))
|
2023-06-12 12:19:25 +01:00
|
|
|
if err == nil && skUserID != nil {
|
|
|
|
skString := skUserID.String()
|
|
|
|
sk = &skString
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 16:33:38 +01:00
|
|
|
|
|
|
|
unsigned := se.Unsigned()
|
|
|
|
var prev PrevEventRef
|
|
|
|
if err := json.Unmarshal(se.Unsigned(), &prev); err == nil && prev.PrevSenderID != "" {
|
|
|
|
prevUserID, err := userIDForSender(*validRoomID, spec.SenderID(prev.PrevSenderID))
|
|
|
|
if err == nil && userID != nil {
|
|
|
|
prev.PrevSenderID = prevUserID.String()
|
|
|
|
} else {
|
|
|
|
errString := "userID unknown"
|
|
|
|
if err != nil {
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
logrus.Warnf("Failed to find userID for prev_sender in ClientEvent: %s", errString)
|
|
|
|
// NOTE: Not much can be done here, so leave the previous value in place.
|
|
|
|
}
|
|
|
|
unsigned, err = json.Marshal(prev)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to marshal unsigned content for ClientEvent: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
evs = append(evs, ToClientEvent(se, format, sender.String(), sk, spec.RawJSON(unsigned)))
|
2023-04-04 18:16:53 +01:00
|
|
|
}
|
|
|
|
return evs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToClientEvent converts a single server event to a client event.
|
2023-08-31 16:33:38 +01:00
|
|
|
func ToClientEvent(se gomatrixserverlib.PDU, format ClientEventFormat, sender string, stateKey *string, unsigned spec.RawJSON) ClientEvent {
|
2023-04-04 18:16:53 +01:00
|
|
|
ce := ClientEvent{
|
2023-04-19 15:50:33 +01:00
|
|
|
Content: spec.RawJSON(se.Content()),
|
2023-08-31 16:33:38 +01:00
|
|
|
Sender: sender,
|
2023-04-04 18:16:53 +01:00
|
|
|
Type: se.Type(),
|
2023-06-12 12:19:25 +01:00
|
|
|
StateKey: stateKey,
|
2023-08-31 16:33:38 +01:00
|
|
|
Unsigned: unsigned,
|
2023-04-04 18:16:53 +01:00
|
|
|
OriginServerTS: se.OriginServerTS(),
|
|
|
|
EventID: se.EventID(),
|
|
|
|
Redacts: se.Redacts(),
|
|
|
|
}
|
2023-08-31 16:33:38 +01:00
|
|
|
|
|
|
|
switch format {
|
|
|
|
case FormatAll:
|
|
|
|
ce.RoomID = se.RoomID()
|
|
|
|
case FormatSync:
|
|
|
|
case FormatSyncFederation:
|
2023-04-04 18:16:53 +01:00
|
|
|
ce.RoomID = se.RoomID()
|
2023-08-31 16:33:38 +01:00
|
|
|
ce.AuthEvents = se.AuthEventIDs()
|
|
|
|
ce.PrevEvents = se.PrevEventIDs()
|
|
|
|
ce.Depth = se.Depth()
|
|
|
|
// TODO: Set Signatures & Hashes fields
|
2023-04-04 18:16:53 +01:00
|
|
|
}
|
2023-08-31 16:33:38 +01:00
|
|
|
|
|
|
|
if format != FormatSyncFederation {
|
|
|
|
if se.Version() == gomatrixserverlib.RoomVersionPseudoIDs {
|
|
|
|
ce.SenderKey = se.SenderID()
|
|
|
|
}
|
2023-06-13 11:50:22 +01:00
|
|
|
}
|
2023-04-04 18:16:53 +01:00
|
|
|
return ce
|
|
|
|
}
|
2023-06-12 12:19:25 +01:00
|
|
|
|
|
|
|
// ToClientEvent converts a single server event to a client event.
|
|
|
|
// It provides default logic for event.SenderID & event.StateKey -> userID conversions.
|
|
|
|
func ToClientEventDefault(userIDQuery spec.UserIDForSender, event gomatrixserverlib.PDU) ClientEvent {
|
|
|
|
sender := spec.UserID{}
|
2023-06-14 15:23:46 +01:00
|
|
|
validRoomID, err := spec.NewRoomID(event.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
return ClientEvent{}
|
|
|
|
}
|
|
|
|
userID, err := userIDQuery(*validRoomID, event.SenderID())
|
2023-06-12 12:19:25 +01:00
|
|
|
if err == nil && userID != nil {
|
|
|
|
sender = *userID
|
|
|
|
}
|
|
|
|
|
|
|
|
sk := event.StateKey()
|
|
|
|
if sk != nil && *sk != "" {
|
2023-06-14 15:23:46 +01:00
|
|
|
skUserID, err := userIDQuery(*validRoomID, spec.SenderID(*event.StateKey()))
|
2023-06-12 12:19:25 +01:00
|
|
|
if err == nil && skUserID != nil {
|
|
|
|
skString := skUserID.String()
|
|
|
|
sk = &skString
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 16:33:38 +01:00
|
|
|
return ToClientEvent(event, FormatAll, sender.String(), sk, event.Unsigned())
|
2023-06-12 12:19:25 +01:00
|
|
|
}
|
2023-08-24 16:43:51 +01:00
|
|
|
|
|
|
|
// If provided state key is a user ID (state keys beginning with @ are reserved for this purpose)
|
|
|
|
// fetch it's associated sender ID and use that instead. Otherwise returns the same state key back.
|
|
|
|
//
|
|
|
|
// # This function either returns the state key that should be used, or an error
|
|
|
|
//
|
|
|
|
// TODO: handle failure cases better (e.g. no sender ID)
|
|
|
|
func FromClientStateKey(roomID spec.RoomID, stateKey string, senderIDQuery spec.SenderIDForUser) (*string, error) {
|
|
|
|
if len(stateKey) >= 1 && stateKey[0] == '@' {
|
|
|
|
parsedStateKey, err := spec.NewUserID(stateKey, true)
|
|
|
|
if err != nil {
|
|
|
|
// If invalid user ID, then there is no associated state event.
|
|
|
|
return nil, fmt.Errorf("Provided state key begins with @ but is not a valid user ID: %s", err.Error())
|
|
|
|
}
|
|
|
|
senderID, err := senderIDQuery(roomID, *parsedStateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to query sender ID: %s", err.Error())
|
|
|
|
}
|
|
|
|
if senderID == nil {
|
|
|
|
// If no sender ID, then there is no associated state event.
|
|
|
|
return nil, fmt.Errorf("No associated sender ID found.")
|
|
|
|
}
|
|
|
|
newStateKey := string(*senderID)
|
|
|
|
return &newStateKey, nil
|
|
|
|
} else {
|
|
|
|
return &stateKey, nil
|
|
|
|
}
|
|
|
|
}
|