roomserver.go 2.9 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 18 19 20 21 22 23
package producers

import (
	"github.com/matrix-org/dendrite/roomserver/api"
	"github.com/matrix-org/gomatrixserverlib"
)

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

// NewRoomserverProducer creates a new RoomserverProducer
28
func NewRoomserverProducer(roomserverURI string) *RoomserverProducer {
29
	return &RoomserverProducer{
30 31
		InputAPI: api.NewRoomserverInputAPIHTTP(roomserverURI, nil),
	}
32 33 34
}

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

48 49 50 51 52 53 54 55 56 57 58 59
// SendEventWithState writes an event with KindNew to the roomserver input log
// with the state at the event as KindOutlier before it.
func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespState, event gomatrixserverlib.Event) error {
	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,
60
			Event:        outlier,
61
			AuthEventIDs: outlier.AuthEventIDs(),
62 63 64 65 66 67 68 69 70 71
		}
	}

	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,
72
		Event:         event,
73
		AuthEventIDs:  event.AuthEventIDs(),
74 75 76 77
		HasState:      true,
		StateEventIDs: stateEventIDs,
	}

78
	return c.SendInputRoomEvents(ires)
79 80
}

81 82
// SendInputRoomEvents writes the given input room events to the roomserver input log. The length of both
// arrays must match, and each element must correspond to the same event.
83 84 85 86
func (c *RoomserverProducer) SendInputRoomEvents(ires []api.InputRoomEvent) error {
	request := api.InputRoomEventsRequest{InputRoomEvents: ires}
	var response api.InputRoomEventsResponse
	return c.InputAPI.InputRoomEvents(&request, &response)
87
}