2020-06-10 12:17:54 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 23:40:52 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
package api
|
2017-03-15 13:36:26 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 13:37:50 +01:00
|
|
|
"context"
|
2020-08-17 11:40:49 +01:00
|
|
|
"fmt"
|
2017-09-13 13:37:50 +01:00
|
|
|
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-03 17:24:51 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-03-15 13:36:26 +00:00
|
|
|
)
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
// SendEvents to the roomserver The events are written with KindNew.
|
|
|
|
func SendEvents(
|
2020-10-19 14:59:13 +01:00
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI,
|
2020-11-16 15:44:53 +00:00
|
|
|
kind Kind, events []*gomatrixserverlib.HeaderedEvent,
|
2020-06-10 12:17:54 +01:00
|
|
|
sendAsServer gomatrixserverlib.ServerName, txnID *TransactionID,
|
2020-09-03 15:22:16 +01:00
|
|
|
) error {
|
2020-06-10 12:17:54 +01:00
|
|
|
ires := make([]InputRoomEvent, len(events))
|
2017-05-25 16:08:28 +01:00
|
|
|
for i, event := range events {
|
2020-06-10 12:17:54 +01:00
|
|
|
ires[i] = InputRoomEvent{
|
2020-10-19 14:59:13 +01:00
|
|
|
Kind: kind,
|
2020-03-27 16:28:22 +00:00
|
|
|
Event: event,
|
2017-12-04 18:07:52 +00:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 12:17:54 +01:00
|
|
|
return SendInputRoomEvents(ctx, rsAPI, ires)
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:59:13 +01:00
|
|
|
// SendEventWithState writes an event with the specified kind to the roomserver
|
2020-05-06 18:03:25 +01:00
|
|
|
// with the state at the event as KindOutlier before it. Will not send any event that is
|
2021-01-22 14:55:08 +00:00
|
|
|
// marked as `true` in haveEventIDs.
|
2020-06-10 12:17:54 +01:00
|
|
|
func SendEventWithState(
|
2020-10-19 14:59:13 +01:00
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI, kind Kind,
|
2020-11-16 15:44:53 +00:00
|
|
|
state *gomatrixserverlib.RespState, event *gomatrixserverlib.HeaderedEvent,
|
2020-10-19 14:59:13 +01:00
|
|
|
haveEventIDs map[string]bool,
|
2017-09-13 13:37:50 +01:00
|
|
|
) error {
|
2017-05-25 16:08:28 +01:00
|
|
|
outliers, err := state.Events()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
var ires []InputRoomEvent
|
2020-03-27 16:28:22 +00:00
|
|
|
for _, outlier := range outliers {
|
2020-05-06 18:03:25 +01:00
|
|
|
if haveEventIDs[outlier.EventID()] {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-10 12:17:54 +01:00
|
|
|
ires = append(ires, InputRoomEvent{
|
|
|
|
Kind: KindOutlier,
|
2020-03-27 16:28:22 +00:00
|
|
|
Event: outlier.Headered(event.RoomVersion),
|
2017-07-07 14:11:32 +01:00
|
|
|
AuthEventIDs: outlier.AuthEventIDs(),
|
2020-03-27 16:28:22 +00:00
|
|
|
})
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stateEventIDs := make([]string, len(state.StateEvents))
|
|
|
|
for i := range state.StateEvents {
|
|
|
|
stateEventIDs[i] = state.StateEvents[i].EventID()
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
ires = append(ires, InputRoomEvent{
|
2020-10-19 14:59:13 +01:00
|
|
|
Kind: kind,
|
2020-03-27 16:28:22 +00:00
|
|
|
Event: event,
|
2017-07-07 14:11:32 +01:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
2017-05-25 16:08:28 +01:00
|
|
|
HasState: true,
|
|
|
|
StateEventIDs: stateEventIDs,
|
2020-03-27 16:28:22 +00:00
|
|
|
})
|
2017-05-25 16:08:28 +01:00
|
|
|
|
2020-09-03 15:22:16 +01:00
|
|
|
return SendInputRoomEvents(ctx, rsAPI, ires)
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
// SendInputRoomEvents to the roomserver.
|
|
|
|
func SendInputRoomEvents(
|
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI, ires []InputRoomEvent,
|
2020-09-03 15:22:16 +01:00
|
|
|
) error {
|
2020-06-10 12:17:54 +01:00
|
|
|
request := InputRoomEventsRequest{InputRoomEvents: ires}
|
|
|
|
var response InputRoomEventsResponse
|
2020-09-16 13:00:52 +01:00
|
|
|
rsAPI.InputRoomEvents(ctx, &request, &response)
|
|
|
|
return response.Err()
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
2017-08-23 15:13:47 +01:00
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
// SendInvite event to the roomserver.
|
2017-09-12 16:54:37 +01:00
|
|
|
// This should only be needed for invite events that occur outside of a known room.
|
|
|
|
// If we are in the room then the event should be sent using the SendEvents method.
|
2020-06-10 12:17:54 +01:00
|
|
|
func SendInvite(
|
|
|
|
ctx context.Context,
|
2020-11-16 15:44:53 +00:00
|
|
|
rsAPI RoomserverInternalAPI, inviteEvent *gomatrixserverlib.HeaderedEvent,
|
2020-04-03 14:29:06 +01:00
|
|
|
inviteRoomState []gomatrixserverlib.InviteV2StrippedState,
|
2020-06-10 12:17:54 +01:00
|
|
|
sendAsServer gomatrixserverlib.ServerName, txnID *TransactionID,
|
2020-08-17 11:40:49 +01:00
|
|
|
) error {
|
|
|
|
// Start by sending the invite request into the roomserver. This will
|
|
|
|
// trigger the federation request amongst other things if needed.
|
|
|
|
request := &PerformInviteRequest{
|
2020-06-24 15:06:14 +01:00
|
|
|
Event: inviteEvent,
|
|
|
|
InviteRoomState: inviteRoomState,
|
|
|
|
RoomVersion: inviteEvent.RoomVersion,
|
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-08-23 15:13:47 +01:00
|
|
|
}
|
2020-08-17 11:40:49 +01:00
|
|
|
response := &PerformInviteResponse{}
|
|
|
|
if err := rsAPI.PerformInvite(ctx, request, response); err != nil {
|
|
|
|
return fmt.Errorf("rsAPI.PerformInvite: %w", err)
|
|
|
|
}
|
|
|
|
if response.Error != nil {
|
2020-06-24 15:06:14 +01:00
|
|
|
return response.Error
|
|
|
|
}
|
2020-08-17 11:40:49 +01:00
|
|
|
|
2020-06-24 15:06:14 +01:00
|
|
|
return nil
|
2017-08-23 15:13:47 +01:00
|
|
|
}
|
2020-07-03 17:24:51 +01:00
|
|
|
|
|
|
|
// GetEvent returns the event or nil, even on errors.
|
|
|
|
func GetEvent(ctx context.Context, rsAPI RoomserverInternalAPI, eventID string) *gomatrixserverlib.HeaderedEvent {
|
|
|
|
var res QueryEventsByIDResponse
|
|
|
|
err := rsAPI.QueryEventsByID(ctx, &QueryEventsByIDRequest{
|
|
|
|
EventIDs: []string{eventID},
|
|
|
|
}, &res)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryEventsByID")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(res.Events) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 15:44:53 +00:00
|
|
|
return res.Events[0]
|
2020-07-03 17:24:51 +01:00
|
|
|
}
|
2020-09-03 17:20:54 +01:00
|
|
|
|
|
|
|
// GetStateEvent returns the current state event in the room or nil.
|
|
|
|
func GetStateEvent(ctx context.Context, rsAPI RoomserverInternalAPI, roomID string, tuple gomatrixserverlib.StateKeyTuple) *gomatrixserverlib.HeaderedEvent {
|
|
|
|
var res QueryCurrentStateResponse
|
|
|
|
err := rsAPI.QueryCurrentState(ctx, &QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{tuple},
|
|
|
|
}, &res)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryCurrentState")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ev, ok := res.StateEvents[tuple]
|
|
|
|
if ok {
|
|
|
|
return ev
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsServerBannedFromRoom returns whether the server is banned from a room by server ACLs.
|
|
|
|
func IsServerBannedFromRoom(ctx context.Context, rsAPI RoomserverInternalAPI, roomID string, serverName gomatrixserverlib.ServerName) bool {
|
|
|
|
req := &QueryServerBannedFromRoomRequest{
|
|
|
|
ServerName: serverName,
|
|
|
|
RoomID: roomID,
|
|
|
|
}
|
|
|
|
res := &QueryServerBannedFromRoomResponse{}
|
|
|
|
if err := rsAPI.QueryServerBannedFromRoom(ctx, req, res); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryServerBannedFromRoom")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return res.Banned
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopulatePublicRooms extracts PublicRoom information for all the provided room IDs. The IDs are not checked to see if they are visible in the
|
|
|
|
// published room directory.
|
|
|
|
// due to lots of switches
|
|
|
|
func PopulatePublicRooms(ctx context.Context, roomIDs []string, rsAPI RoomserverInternalAPI) ([]gomatrixserverlib.PublicRoom, error) {
|
|
|
|
avatarTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.avatar", StateKey: ""}
|
|
|
|
nameTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.name", StateKey: ""}
|
|
|
|
canonicalTuple := gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomCanonicalAlias, StateKey: ""}
|
|
|
|
topicTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.topic", StateKey: ""}
|
|
|
|
guestTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.guest_access", StateKey: ""}
|
|
|
|
visibilityTuple := gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomHistoryVisibility, StateKey: ""}
|
|
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomJoinRules, StateKey: ""}
|
|
|
|
|
|
|
|
var stateRes QueryBulkStateContentResponse
|
|
|
|
err := rsAPI.QueryBulkStateContent(ctx, &QueryBulkStateContentRequest{
|
|
|
|
RoomIDs: roomIDs,
|
|
|
|
AllowWildcards: true,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
nameTuple, canonicalTuple, topicTuple, guestTuple, visibilityTuple, joinRuleTuple, avatarTuple,
|
|
|
|
{EventType: gomatrixserverlib.MRoomMember, StateKey: "*"},
|
|
|
|
},
|
|
|
|
}, &stateRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("QueryBulkStateContent failed")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chunk := make([]gomatrixserverlib.PublicRoom, len(roomIDs))
|
|
|
|
i := 0
|
|
|
|
for roomID, data := range stateRes.Rooms {
|
|
|
|
pub := gomatrixserverlib.PublicRoom{
|
|
|
|
RoomID: roomID,
|
|
|
|
}
|
|
|
|
joinCount := 0
|
|
|
|
var joinRule, guestAccess string
|
|
|
|
for tuple, contentVal := range data {
|
|
|
|
if tuple.EventType == gomatrixserverlib.MRoomMember && contentVal == "join" {
|
|
|
|
joinCount++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch tuple {
|
|
|
|
case avatarTuple:
|
|
|
|
pub.AvatarURL = contentVal
|
|
|
|
case nameTuple:
|
|
|
|
pub.Name = contentVal
|
|
|
|
case topicTuple:
|
|
|
|
pub.Topic = contentVal
|
|
|
|
case canonicalTuple:
|
2021-08-27 16:50:30 +01:00
|
|
|
if _, _, err := gomatrixserverlib.SplitID('#', contentVal); err == nil {
|
|
|
|
pub.CanonicalAlias = contentVal
|
|
|
|
}
|
2020-09-03 17:20:54 +01:00
|
|
|
case visibilityTuple:
|
|
|
|
pub.WorldReadable = contentVal == "world_readable"
|
|
|
|
// need both of these to determine whether guests can join
|
|
|
|
case joinRuleTuple:
|
|
|
|
joinRule = contentVal
|
|
|
|
case guestTuple:
|
|
|
|
guestAccess = contentVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if joinRule == gomatrixserverlib.Public && guestAccess == "can_join" {
|
|
|
|
pub.GuestCanJoin = true
|
|
|
|
}
|
|
|
|
pub.JoinedMembersCount = joinCount
|
|
|
|
chunk[i] = pub
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return chunk, nil
|
|
|
|
}
|