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

Function SendToDevice

clientapi/routing/sendtodevice.go:29–70  ·  view source on GitHub ↗

SendToDevice handles PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId} sends the device events to the syncapi & federationsender

(
	req *http.Request, device *userapi.Device,
	syncProducer *producers.SyncAPIProducer,
	txnCache *transactions.Cache,
	eventType string, txnID *string,
)

Source from the content-addressed store, hash-verified

27// SendToDevice handles PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId}
28// sends the device events to the syncapi & federationsender
29func SendToDevice(
30 req *http.Request, device *userapi.Device,
31 syncProducer *producers.SyncAPIProducer,
32 txnCache *transactions.Cache,
33 eventType string, txnID *string,
34) util.JSONResponse {
35 if txnID != nil {
36 if res, ok := txnCache.FetchTransaction(device.AccessToken, *txnID); ok {
37 return *res
38 }
39 }
40
41 var httpReq struct {
42 Messages map[string]map[string]json.RawMessage `json:"messages"`
43 }
44 resErr := httputil.UnmarshalJSONRequest(req, &httpReq)
45 if resErr != nil {
46 return *resErr
47 }
48
49 for userID, byUser := range httpReq.Messages {
50 for deviceID, message := range byUser {
51 if err := syncProducer.SendToDevice(
52 req.Context(), device.UserID, userID, deviceID, eventType, message,
53 ); err != nil {
54 util.GetLogger(req.Context()).WithError(err).Error("eduProducer.SendToDevice failed")
55 return jsonerror.InternalServerError()
56 }
57 }
58 }
59
60 res := util.JSONResponse{
61 Code: http.StatusOK,
62 JSON: struct{}{},
63 }
64
65 if txnID != nil {
66 txnCache.AddTransaction(device.AccessToken, *txnID, &res)
67 }
68
69 return res
70}

Callers 1

SetupFunction · 0.70

Calls 5

FetchTransactionMethod · 0.80
ContextMethod · 0.80
AddTransactionMethod · 0.80
SendToDeviceMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected