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"
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-07-03 17:24:51 +01:00
|
|
|
"github.com/matrix-org/util"
|
2022-08-24 14:03:06 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
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(
|
2022-05-05 13:17:38 +01:00
|
|
|
ctx context.Context, rsAPI InputRoomEventsAPI,
|
2023-04-27 12:54:20 +01:00
|
|
|
kind Kind, events []*types.HeaderedEvent,
|
2023-04-19 15:50:33 +01:00
|
|
|
virtualHost, origin spec.ServerName,
|
|
|
|
sendAsServer spec.ServerName, txnID *TransactionID,
|
2022-01-05 17:44:49 +00:00
|
|
|
async bool,
|
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,
|
2022-01-27 14:29:14 +00:00
|
|
|
Origin: origin,
|
2017-12-04 18:07:52 +00:00
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-15 15:05:23 +00:00
|
|
|
return SendInputRoomEvents(ctx, rsAPI, virtualHost, ires, async)
|
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(
|
2022-11-15 15:05:23 +00:00
|
|
|
ctx context.Context, rsAPI InputRoomEventsAPI,
|
2023-04-19 15:50:33 +01:00
|
|
|
virtualHost spec.ServerName, kind Kind,
|
2023-04-27 12:54:20 +01:00
|
|
|
state gomatrixserverlib.StateResponse, event *types.HeaderedEvent,
|
2023-04-19 15:50:33 +01:00
|
|
|
origin spec.ServerName, haveEventIDs map[string]bool, async bool,
|
2017-09-13 13:37:50 +01:00
|
|
|
) error {
|
2023-05-02 15:03:16 +01:00
|
|
|
outliers := gomatrixserverlib.LineariseStateResponse(event.Version(), state)
|
2022-02-28 14:51:40 +00:00
|
|
|
ires := make([]InputRoomEvent, 0, len(outliers))
|
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{
|
2022-01-27 14:29:14 +00:00
|
|
|
Kind: KindOutlier,
|
2023-05-02 15:03:16 +01:00
|
|
|
Event: &types.HeaderedEvent{PDU: outlier},
|
2022-01-27 14:29:14 +00:00
|
|
|
Origin: origin,
|
2020-03-27 16:28:22 +00:00
|
|
|
})
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
stateEvents := state.GetStateEvents().UntrustedEvents(event.Version())
|
2022-02-09 20:31:24 +00:00
|
|
|
stateEventIDs := make([]string, len(stateEvents))
|
|
|
|
for i := range stateEvents {
|
|
|
|
stateEventIDs[i] = stateEvents[i].EventID()
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:03:06 +01:00
|
|
|
logrus.WithContext(ctx).WithFields(logrus.Fields{
|
2023-09-15 15:39:06 +01:00
|
|
|
"room_id": event.RoomID().String(),
|
2022-08-24 14:03:06 +01:00
|
|
|
"event_id": event.EventID(),
|
|
|
|
"outliers": len(ires),
|
|
|
|
"state_ids": len(stateEventIDs),
|
|
|
|
}).Infof("Submitting %q event to roomserver with state snapshot", event.Type())
|
|
|
|
|
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,
|
2022-01-27 14:29:14 +00:00
|
|
|
Origin: origin,
|
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
|
|
|
|
2022-11-15 15:05:23 +00:00
|
|
|
return SendInputRoomEvents(ctx, rsAPI, virtualHost, ires, async)
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:17:54 +01:00
|
|
|
// SendInputRoomEvents to the roomserver.
|
|
|
|
func SendInputRoomEvents(
|
2022-05-05 13:17:38 +01:00
|
|
|
ctx context.Context, rsAPI InputRoomEventsAPI,
|
2023-04-19 15:50:33 +01:00
|
|
|
virtualHost spec.ServerName,
|
2022-01-05 17:44:49 +00:00
|
|
|
ires []InputRoomEvent, async bool,
|
2020-09-03 15:22:16 +01:00
|
|
|
) error {
|
2022-01-05 17:44:49 +00:00
|
|
|
request := InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: ires,
|
|
|
|
Asynchronous: async,
|
2022-11-15 15:05:23 +00:00
|
|
|
VirtualHost: virtualHost,
|
2022-01-05 17:44:49 +00:00
|
|
|
}
|
2020-06-10 12:17:54 +01:00
|
|
|
var response InputRoomEventsResponse
|
2023-05-09 23:46:49 +01:00
|
|
|
rsAPI.InputRoomEvents(ctx, &request, &response)
|
2020-09-16 13:00:52 +01:00
|
|
|
return response.Err()
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
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.
|
2023-04-27 12:54:20 +01:00
|
|
|
func GetEvent(ctx context.Context, rsAPI QueryEventsAPI, roomID, eventID string) *types.HeaderedEvent {
|
2020-07-03 17:24:51 +01:00
|
|
|
var res QueryEventsByIDResponse
|
|
|
|
err := rsAPI.QueryEventsByID(ctx, &QueryEventsByIDRequest{
|
2023-03-01 16:06:47 +00:00
|
|
|
RoomID: roomID,
|
2020-07-03 17:24:51 +01:00
|
|
|
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.
|
2023-04-27 12:54:20 +01:00
|
|
|
func GetStateEvent(ctx context.Context, rsAPI QueryEventsAPI, roomID string, tuple gomatrixserverlib.StateKeyTuple) *types.HeaderedEvent {
|
2020-09-03 17:20:54 +01:00
|
|
|
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.
|
2023-04-19 15:50:33 +01:00
|
|
|
func IsServerBannedFromRoom(ctx context.Context, rsAPI FederationRoomserverAPI, roomID string, serverName spec.ServerName) bool {
|
2020-09-03 17:20:54 +01:00
|
|
|
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
|
2023-04-06 09:55:01 +01:00
|
|
|
func PopulatePublicRooms(ctx context.Context, roomIDs []string, rsAPI QueryBulkStateContentAPI) ([]fclient.PublicRoom, error) {
|
2020-09-03 17:20:54 +01:00
|
|
|
avatarTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.avatar", StateKey: ""}
|
|
|
|
nameTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.name", StateKey: ""}
|
2023-04-19 15:50:33 +01:00
|
|
|
canonicalTuple := gomatrixserverlib.StateKeyTuple{EventType: spec.MRoomCanonicalAlias, StateKey: ""}
|
2020-09-03 17:20:54 +01:00
|
|
|
topicTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.topic", StateKey: ""}
|
|
|
|
guestTuple := gomatrixserverlib.StateKeyTuple{EventType: "m.room.guest_access", StateKey: ""}
|
2023-04-19 15:50:33 +01:00
|
|
|
visibilityTuple := gomatrixserverlib.StateKeyTuple{EventType: spec.MRoomHistoryVisibility, StateKey: ""}
|
|
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{EventType: spec.MRoomJoinRules, StateKey: ""}
|
2020-09-03 17:20:54 +01:00
|
|
|
|
|
|
|
var stateRes QueryBulkStateContentResponse
|
|
|
|
err := rsAPI.QueryBulkStateContent(ctx, &QueryBulkStateContentRequest{
|
|
|
|
RoomIDs: roomIDs,
|
|
|
|
AllowWildcards: true,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
nameTuple, canonicalTuple, topicTuple, guestTuple, visibilityTuple, joinRuleTuple, avatarTuple,
|
2023-04-19 15:50:33 +01:00
|
|
|
{EventType: spec.MRoomMember, StateKey: "*"},
|
2020-09-03 17:20:54 +01:00
|
|
|
},
|
|
|
|
}, &stateRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("QueryBulkStateContent failed")
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
chunk := make([]fclient.PublicRoom, len(roomIDs))
|
2020-09-03 17:20:54 +01:00
|
|
|
i := 0
|
|
|
|
for roomID, data := range stateRes.Rooms {
|
2023-04-06 09:55:01 +01:00
|
|
|
pub := fclient.PublicRoom{
|
2020-09-03 17:20:54 +01:00
|
|
|
RoomID: roomID,
|
|
|
|
}
|
|
|
|
joinCount := 0
|
2024-03-28 19:40:45 +00:00
|
|
|
var guestAccess string
|
2020-09-03 17:20:54 +01:00
|
|
|
for tuple, contentVal := range data {
|
2023-04-19 15:50:33 +01:00
|
|
|
if tuple.EventType == spec.MRoomMember && contentVal == "join" {
|
2020-09-03 17:20:54 +01:00
|
|
|
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:
|
2024-03-28 19:40:45 +00:00
|
|
|
pub.JoinRule = contentVal
|
2020-09-03 17:20:54 +01:00
|
|
|
case guestTuple:
|
|
|
|
guestAccess = contentVal
|
|
|
|
}
|
|
|
|
}
|
2024-03-28 19:40:45 +00:00
|
|
|
if pub.JoinRule == spec.Public && guestAccess == "can_join" {
|
2020-09-03 17:20:54 +01:00
|
|
|
pub.GuestCanJoin = true
|
|
|
|
}
|
|
|
|
pub.JoinedMembersCount = joinCount
|
|
|
|
chunk[i] = pub
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return chunk, nil
|
|
|
|
}
|