MCPcopy Create free account
hub / github.com/daodst/chat / queueInputRoomEvents

Method queueInputRoomEvents

roomserver/internal/input/input.go:283–333  ·  view source on GitHub ↗

queueInputRoomEvents queues events into the roomserver input stream in NATS.

(
	ctx context.Context,
	request *api.InputRoomEventsRequest,
)

Source from the content-addressed store, hash-verified

281// queueInputRoomEvents queues events into the roomserver input
282// stream in NATS.
283func (r *Inputer) queueInputRoomEvents(
284 ctx context.Context,
285 request *api.InputRoomEventsRequest,
286) (replySub *nats.Subscription, err error) {
287 // If the request is synchronous then we need to create a
288 // temporary inbox to wait for responses on, and then create
289 // a subscription to it. If it's asynchronous then we won't
290 // bother, so these values will remain empty.
291 var replyTo string
292 if !request.Asynchronous {
293 replyTo = nats.NewInbox()
294 replySub, err = r.NATSClient.SubscribeSync(replyTo)
295 if err != nil {
296 return nil, fmt.Errorf("r.NATSClient.SubscribeSync: %w", err)
297 }
298 if replySub == nil {
299 // This shouldn't ever happen, but it doesn't hurt to check
300 // because we can potentially avoid a nil pointer panic later
301 // if it did for some reason.
302 return nil, fmt.Errorf("expected a subscription to the temporary inbox")
303 }
304 }
305
306 // For each event, marshal the input room event and then
307 // send it into the input queue.
308 for _, e := range request.InputRoomEvents {
309 roomID := e.Event.RoomID()
310 subj := r.Cfg.Matrix.JetStream.Prefixed(jetstream.InputRoomEventSubj(roomID))
311 msg := &nats.Msg{
312 Subject: subj,
313 Header: nats.Header{},
314 }
315 msg.Header.Set("room_id", roomID)
316 if replyTo != "" {
317 msg.Header.Set("sync", replyTo)
318 }
319 msg.Data, err = json.Marshal(e)
320 if err != nil {
321 return nil, fmt.Errorf("json.Marshal: %w", err)
322 }
323 if _, err = r.JetStream.PublishMsg(msg, nats.Context(ctx)); err != nil {
324 logrus.WithError(err).WithFields(logrus.Fields{
325 "room_id": roomID,
326 "event_id": e.Event.EventID(),
327 "subj": subj,
328 }).Error("Roomserver failed to queue async event")
329 return nil, fmt.Errorf("r.JetStream.PublishMsg: %w", err)
330 }
331 }
332 return
333}
334
335// InputRoomEvents implements api.RoomserverInternalAPI
336func (r *Inputer) InputRoomEvents(

Callers 1

InputRoomEventsMethod · 0.95

Calls 5

PrefixedMethod · 0.80
PublishMsgMethod · 0.80
ContextMethod · 0.80
SetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected