roomserver.go 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16 17
package producers

import (
18 19
	"context"

20 21 22 23 24 25
	"github.com/matrix-org/dendrite/roomserver/api"
	"github.com/matrix-org/gomatrixserverlib"
)

// RoomserverProducer produces events for the roomserver to consume.
type RoomserverProducer struct {
26
	InputAPI api.RoomserverInputAPI
27 28 29
}

// NewRoomserverProducer creates a new RoomserverProducer
30
func NewRoomserverProducer(inputAPI api.RoomserverInputAPI) *RoomserverProducer {
31
	return &RoomserverProducer{
32
		InputAPI: inputAPI,
33
	}
34 35 36
}

// SendEvents writes the given events to the roomserver input log. The events are written with KindNew.
37 38 39
func (c *RoomserverProducer) SendEvents(
	ctx context.Context, events []gomatrixserverlib.Event, sendAsServer gomatrixserverlib.ServerName,
) error {
40
	ires := make([]api.InputRoomEvent, len(events))
41 42
	for i, event := range events {
		ires[i] = api.InputRoomEvent{
43
			Kind:         api.KindNew,
44
			Event:        event,
45
			AuthEventIDs: event.AuthEventIDs(),
46
			SendAsServer: string(sendAsServer),
47 48
		}
	}
49
	return c.SendInputRoomEvents(ctx, ires)
50 51
}

52 53
// SendEventWithState writes an event with KindNew to the roomserver input log
// with the state at the event as KindOutlier before it.
54 55 56
func (c *RoomserverProducer) SendEventWithState(
	ctx context.Context, state gomatrixserverlib.RespState, event gomatrixserverlib.Event,
) error {
57 58 59 60 61 62 63 64 65
	outliers, err := state.Events()
	if err != nil {
		return err
	}

	ires := make([]api.InputRoomEvent, len(outliers)+1)
	for i, outlier := range outliers {
		ires[i] = api.InputRoomEvent{
			Kind:         api.KindOutlier,
66
			Event:        outlier,
67
			AuthEventIDs: outlier.AuthEventIDs(),
68 69 70 71 72 73 74 75 76 77
		}
	}

	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,
78
		Event:         event,
79
		AuthEventIDs:  event.AuthEventIDs(),
80 81 82 83
		HasState:      true,
		StateEventIDs: stateEventIDs,
	}

84
	return c.SendInputRoomEvents(ctx, ires)
85 86
}

87
// SendInputRoomEvents writes the given input room events to the roomserver input API.
88
func (c *RoomserverProducer) SendInputRoomEvents(ctx context.Context, ires []api.InputRoomEvent) error {
89 90
	request := api.InputRoomEventsRequest{InputRoomEvents: ires}
	var response api.InputRoomEventsResponse
91
	return c.InputAPI.InputRoomEvents(ctx, &request, &response)
92
}
93 94

// SendInvite writes the invite event to the roomserver input API.
95 96
// 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.
97
func (c *RoomserverProducer) SendInvite(ctx context.Context, inviteEvent gomatrixserverlib.Event) error {
98 99 100 101
	request := api.InputRoomEventsRequest{
		InputInviteEvents: []api.InputInviteEvent{{Event: inviteEvent}},
	}
	var response api.InputRoomEventsResponse
102
	return c.InputAPI.InputRoomEvents(ctx, &request, &response)
103
}