2017-04-20 22:40:52 +00: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.
|
|
|
|
|
2017-03-15 13:36:26 +00:00
|
|
|
package producers
|
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
|
|
|
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverProducer produces events for the roomserver to consume.
|
|
|
|
type RoomserverProducer struct {
|
2017-07-18 12:40:03 +00:00
|
|
|
InputAPI api.RoomserverInputAPI
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoomserverProducer creates a new RoomserverProducer
|
2017-08-04 12:12:36 +00:00
|
|
|
func NewRoomserverProducer(inputAPI api.RoomserverInputAPI) *RoomserverProducer {
|
2017-03-15 13:36:26 +00:00
|
|
|
return &RoomserverProducer{
|
2017-08-04 12:12:36 +00:00
|
|
|
InputAPI: inputAPI,
|
2017-07-18 12:40:03 +00:00
|
|
|
}
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendEvents writes the given events to the roomserver input log. The events are written with KindNew.
|
2017-09-13 12:37:50 +00:00
|
|
|
func (c *RoomserverProducer) SendEvents(
|
|
|
|
ctx context.Context, events []gomatrixserverlib.Event, sendAsServer gomatrixserverlib.ServerName,
|
2017-12-04 18:07:52 +00:00
|
|
|
txnID *api.TransactionID,
|
2018-05-26 11:03:35 +00:00
|
|
|
) (string, error) {
|
2017-03-15 13:36:26 +00:00
|
|
|
ires := make([]api.InputRoomEvent, len(events))
|
2017-05-25 15:08:28 +00:00
|
|
|
for i, event := range events {
|
2020-03-17 11:01:25 +00:00
|
|
|
roomVersion := gomatrixserverlib.RoomVersionV1
|
|
|
|
|
2017-05-25 15:08:28 +00:00
|
|
|
ires[i] = api.InputRoomEvent{
|
2017-12-04 18:07:52 +00:00
|
|
|
Kind: api.KindNew,
|
2020-03-17 11:01:25 +00:00
|
|
|
Event: event.Headered(roomVersion),
|
2017-12-04 18:07:52 +00:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
SendAsServer: string(sendAsServer),
|
|
|
|
TransactionID: txnID,
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 12:37:50 +00:00
|
|
|
return c.SendInputRoomEvents(ctx, ires)
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:08:28 +00:00
|
|
|
// SendEventWithState writes an event with KindNew to the roomserver input log
|
|
|
|
// with the state at the event as KindOutlier before it.
|
2017-09-13 12:37:50 +00:00
|
|
|
func (c *RoomserverProducer) SendEventWithState(
|
|
|
|
ctx context.Context, state gomatrixserverlib.RespState, event gomatrixserverlib.Event,
|
|
|
|
) error {
|
2017-05-25 15:08:28 +00:00
|
|
|
outliers, err := state.Events()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-17 11:01:25 +00:00
|
|
|
// TODO: Room version here
|
|
|
|
roomVersion := gomatrixserverlib.RoomVersionV1
|
|
|
|
|
2017-05-25 15:08:28 +00:00
|
|
|
ires := make([]api.InputRoomEvent, len(outliers)+1)
|
|
|
|
for i, outlier := range outliers {
|
|
|
|
ires[i] = api.InputRoomEvent{
|
|
|
|
Kind: api.KindOutlier,
|
2020-03-17 11:01:25 +00:00
|
|
|
Event: outlier.Headered(roomVersion),
|
2017-07-07 13:11:32 +00:00
|
|
|
AuthEventIDs: outlier.AuthEventIDs(),
|
2017-05-25 15:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEventIDs := make([]string, len(state.StateEvents))
|
|
|
|
for i := range state.StateEvents {
|
|
|
|
stateEventIDs[i] = state.StateEvents[i].EventID()
|
|
|
|
}
|
|
|
|
|
|
|
|
ires[len(outliers)] = api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
2020-03-17 11:01:25 +00:00
|
|
|
Event: event.Headered(roomVersion),
|
2017-07-07 13:11:32 +00:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
2017-05-25 15:08:28 +00:00
|
|
|
HasState: true,
|
|
|
|
StateEventIDs: stateEventIDs,
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:03:35 +00:00
|
|
|
_, err = c.SendInputRoomEvents(ctx, ires)
|
|
|
|
return err
|
2017-05-25 15:08:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 14:13:47 +00:00
|
|
|
// SendInputRoomEvents writes the given input room events to the roomserver input API.
|
2018-05-26 11:03:35 +00:00
|
|
|
func (c *RoomserverProducer) SendInputRoomEvents(
|
|
|
|
ctx context.Context, ires []api.InputRoomEvent,
|
|
|
|
) (eventID string, err error) {
|
2017-07-18 12:40:03 +00:00
|
|
|
request := api.InputRoomEventsRequest{InputRoomEvents: ires}
|
|
|
|
var response api.InputRoomEventsResponse
|
2018-05-26 11:03:35 +00:00
|
|
|
err = c.InputAPI.InputRoomEvents(ctx, &request, &response)
|
|
|
|
eventID = response.EventID
|
|
|
|
return
|
2017-03-15 13:36:26 +00:00
|
|
|
}
|
2017-08-23 14:13:47 +00:00
|
|
|
|
|
|
|
// SendInvite writes the invite event to the roomserver input API.
|
2017-09-12 15:54:37 +00: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.
|
2018-05-26 11:03:35 +00:00
|
|
|
func (c *RoomserverProducer) SendInvite(
|
|
|
|
ctx context.Context, inviteEvent gomatrixserverlib.Event,
|
|
|
|
) error {
|
2020-03-17 11:01:25 +00:00
|
|
|
// TODO: Room version here
|
|
|
|
roomVersion := gomatrixserverlib.RoomVersionV1
|
|
|
|
|
2017-08-23 14:13:47 +00:00
|
|
|
request := api.InputRoomEventsRequest{
|
2020-03-17 11:01:25 +00:00
|
|
|
InputInviteEvents: []api.InputInviteEvent{{
|
|
|
|
Event: inviteEvent.Headered(roomVersion),
|
|
|
|
}},
|
2017-08-23 14:13:47 +00:00
|
|
|
}
|
|
|
|
var response api.InputRoomEventsResponse
|
2017-09-13 12:37:50 +00:00
|
|
|
return c.InputAPI.InputRoomEvents(ctx, &request, &response)
|
2017-08-23 14:13:47 +00:00
|
|
|
}
|