diff options
author | Kegsay <kegan@matrix.org> | 2020-06-10 12:17:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-10 12:17:54 +0100 |
commit | b7187a9a354530c1846c2a97be701ea484f91c2c (patch) | |
tree | 6d14903a444fa8bca964247dbe85ae8d9709d4c8 /roomserver | |
parent | d9d6f4568ce891ae0ae9d2a3449974d3777bd21d (diff) |
Remove clientapi producers which aren't actually producers (#1111)
* Remove clientapi producers which aren't actually producers
They are actually just convenience wrappers around the internal APIs
for roomserver/eduserver. Move their logic to their respective `api`
packages and call them directly.
* Remove TODO
* unbreak ygg
Diffstat (limited to 'roomserver')
-rw-r--r-- | roomserver/api/wrapper.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/roomserver/api/wrapper.go b/roomserver/api/wrapper.go new file mode 100644 index 00000000..97940e0c --- /dev/null +++ b/roomserver/api/wrapper.go @@ -0,0 +1,113 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// 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 api + +import ( + "context" + + "github.com/matrix-org/gomatrixserverlib" +) + +// 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, +) (string, error) { + ires := make([]InputRoomEvent, len(events)) + for i, event := range events { + ires[i] = InputRoomEvent{ + Kind: KindNew, + Event: event, + AuthEventIDs: event.AuthEventIDs(), + SendAsServer: string(sendAsServer), + TransactionID: txnID, + } + } + return SendInputRoomEvents(ctx, rsAPI, ires) +} + +// SendEventWithState writes an event with KindNew to the roomserver +// with the state at the event as KindOutlier before it. Will not send any event that is +// marked as `true` in haveEventIDs +func SendEventWithState( + ctx context.Context, rsAPI RoomserverInternalAPI, state *gomatrixserverlib.RespState, + event gomatrixserverlib.HeaderedEvent, haveEventIDs map[string]bool, +) error { + outliers, err := state.Events() + if err != nil { + return err + } + + var ires []InputRoomEvent + for _, outlier := range outliers { + if haveEventIDs[outlier.EventID()] { + continue + } + ires = append(ires, InputRoomEvent{ + Kind: KindOutlier, + Event: outlier.Headered(event.RoomVersion), + AuthEventIDs: outlier.AuthEventIDs(), + }) + } + + stateEventIDs := make([]string, len(state.StateEvents)) + for i := range state.StateEvents { + stateEventIDs[i] = state.StateEvents[i].EventID() + } + + ires = append(ires, InputRoomEvent{ + Kind: KindNew, + Event: event, + AuthEventIDs: event.AuthEventIDs(), + HasState: true, + StateEventIDs: stateEventIDs, + }) + + _, err = SendInputRoomEvents(ctx, rsAPI, ires) + return err +} + +// SendInputRoomEvents to the roomserver. +func SendInputRoomEvents( + ctx context.Context, rsAPI RoomserverInternalAPI, ires []InputRoomEvent, +) (eventID string, err error) { + request := InputRoomEventsRequest{InputRoomEvents: ires} + var response InputRoomEventsResponse + err = rsAPI.InputRoomEvents(ctx, &request, &response) + eventID = response.EventID + return +} + +// SendInvite event to the roomserver. +// 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. +func SendInvite( + ctx context.Context, + rsAPI RoomserverInternalAPI, inviteEvent gomatrixserverlib.HeaderedEvent, + inviteRoomState []gomatrixserverlib.InviteV2StrippedState, + sendAsServer gomatrixserverlib.ServerName, txnID *TransactionID, +) error { + request := InputRoomEventsRequest{ + InputInviteEvents: []InputInviteEvent{{ + Event: inviteEvent, + InviteRoomState: inviteRoomState, + RoomVersion: inviteEvent.RoomVersion, + SendAsServer: string(sendAsServer), + TransactionID: txnID, + }}, + } + var response InputRoomEventsResponse + return rsAPI.InputRoomEvents(ctx, &request, &response) +} |