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, )
| 27 | // SendToDevice handles PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId} |
| 28 | // sends the device events to the syncapi & federationsender |
| 29 | func 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 | } |
no test coverage detected