2020-06-10 06:17:54 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 17:40:52 -05: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 06:17:54 -05:00
|
|
|
package api
|
2017-03-15 08:36:26 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 07:37:50 -05:00
|
|
|
"context"
|
|
|
|
|
2017-03-15 08:36:26 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
// SendEvents to the roomserver The events are written with KindNew.
|
|
|
|
func SendEvents(
|
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI, events []gomatrixserverlib.HeaderedEvent,
|
|
|
|
sendAsServer gomatrixserverlib.ServerName, txnID *TransactionID,
|
2018-05-26 06:03:35 -05:00
|
|
|
) (string, error) {
|
2020-06-10 06:17:54 -05:00
|
|
|
ires := make([]InputRoomEvent, len(events))
|
2017-05-25 10:08:28 -05:00
|
|
|
for i, event := range events {
|
2020-06-10 06:17:54 -05:00
|
|
|
ires[i] = InputRoomEvent{
|
|
|
|
Kind: KindNew,
|
2020-03-27 11:28:22 -05:00
|
|
|
Event: event,
|
2017-12-04 12:07:52 -06:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-03-15 08:36:26 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 06:17:54 -05:00
|
|
|
return SendInputRoomEvents(ctx, rsAPI, ires)
|
2017-03-15 08:36:26 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
// SendEventWithState writes an event with KindNew to the roomserver
|
2020-05-06 12:03:25 -05:00
|
|
|
// with the state at the event as KindOutlier before it. Will not send any event that is
|
|
|
|
// marked as `true` in haveEventIDs
|
2020-06-10 06:17:54 -05:00
|
|
|
func SendEventWithState(
|
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI, state *gomatrixserverlib.RespState,
|
|
|
|
event gomatrixserverlib.HeaderedEvent, haveEventIDs map[string]bool,
|
2017-09-13 07:37:50 -05:00
|
|
|
) error {
|
2017-05-25 10:08:28 -05:00
|
|
|
outliers, err := state.Events()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
var ires []InputRoomEvent
|
2020-03-27 11:28:22 -05:00
|
|
|
for _, outlier := range outliers {
|
2020-05-06 12:03:25 -05:00
|
|
|
if haveEventIDs[outlier.EventID()] {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-10 06:17:54 -05:00
|
|
|
ires = append(ires, InputRoomEvent{
|
|
|
|
Kind: KindOutlier,
|
2020-03-27 11:28:22 -05:00
|
|
|
Event: outlier.Headered(event.RoomVersion),
|
2017-07-07 08:11:32 -05:00
|
|
|
AuthEventIDs: outlier.AuthEventIDs(),
|
2020-03-27 11:28:22 -05:00
|
|
|
})
|
2017-05-25 10:08:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stateEventIDs := make([]string, len(state.StateEvents))
|
|
|
|
for i := range state.StateEvents {
|
|
|
|
stateEventIDs[i] = state.StateEvents[i].EventID()
|
|
|
|
}
|
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
ires = append(ires, InputRoomEvent{
|
|
|
|
Kind: KindNew,
|
2020-03-27 11:28:22 -05:00
|
|
|
Event: event,
|
2017-07-07 08:11:32 -05:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
2017-05-25 10:08:28 -05:00
|
|
|
HasState: true,
|
|
|
|
StateEventIDs: stateEventIDs,
|
2020-03-27 11:28:22 -05:00
|
|
|
})
|
2017-05-25 10:08:28 -05:00
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
_, err = SendInputRoomEvents(ctx, rsAPI, ires)
|
2018-05-26 06:03:35 -05:00
|
|
|
return err
|
2017-05-25 10:08:28 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
// SendInputRoomEvents to the roomserver.
|
|
|
|
func SendInputRoomEvents(
|
|
|
|
ctx context.Context, rsAPI RoomserverInternalAPI, ires []InputRoomEvent,
|
2018-05-26 06:03:35 -05:00
|
|
|
) (eventID string, err error) {
|
2020-06-10 06:17:54 -05:00
|
|
|
request := InputRoomEventsRequest{InputRoomEvents: ires}
|
|
|
|
var response InputRoomEventsResponse
|
|
|
|
err = rsAPI.InputRoomEvents(ctx, &request, &response)
|
2018-05-26 06:03:35 -05:00
|
|
|
eventID = response.EventID
|
|
|
|
return
|
2017-03-15 08:36:26 -05:00
|
|
|
}
|
2017-08-23 09:13:47 -05:00
|
|
|
|
2020-06-10 06:17:54 -05:00
|
|
|
// SendInvite event to the roomserver.
|
2017-09-12 10:54:37 -05: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 06:17:54 -05:00
|
|
|
func SendInvite(
|
|
|
|
ctx context.Context,
|
|
|
|
rsAPI RoomserverInternalAPI, inviteEvent gomatrixserverlib.HeaderedEvent,
|
2020-04-03 08:29:06 -05:00
|
|
|
inviteRoomState []gomatrixserverlib.InviteV2StrippedState,
|
2020-06-10 06:17:54 -05:00
|
|
|
sendAsServer gomatrixserverlib.ServerName, txnID *TransactionID,
|
2020-06-24 09:06:14 -05:00
|
|
|
) *PerformError {
|
|
|
|
request := PerformInviteRequest{
|
|
|
|
Event: inviteEvent,
|
|
|
|
InviteRoomState: inviteRoomState,
|
|
|
|
RoomVersion: inviteEvent.RoomVersion,
|
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|
2020-06-24 09:06:14 -05:00
|
|
|
var response PerformInviteResponse
|
|
|
|
rsAPI.PerformInvite(ctx, &request, &response)
|
|
|
|
// we need to do this because many places people will use `var err error` as the return
|
|
|
|
// arg and a nil interface != nil pointer to a concrete interface (in this case PerformError)
|
|
|
|
if response.Error != nil && response.Error.Msg != "" {
|
|
|
|
return response.Error
|
|
|
|
}
|
|
|
|
return nil
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|