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

Function UploadKeys

clientapi/routing/keys.go:34–88  ·  view source on GitHub ↗
(req *http.Request, keyAPI api.ClientKeyAPI, device *userapi.Device)

Source from the content-addressed store, hash-verified

32}
33
34func UploadKeys(req *http.Request, keyAPI api.ClientKeyAPI, device *userapi.Device) util.JSONResponse {
35 var r uploadKeysRequest
36 resErr := httputil.UnmarshalJSONRequest(req, &r)
37 if resErr != nil {
38 return *resErr
39 }
40
41 uploadReq := &api.PerformUploadKeysRequest{
42 DeviceID: device.ID,
43 UserID: device.UserID,
44 }
45 if r.DeviceKeys != nil {
46 uploadReq.DeviceKeys = []api.DeviceKeys{
47 {
48 DeviceID: device.ID,
49 UserID: device.UserID,
50 KeyJSON: r.DeviceKeys,
51 },
52 }
53 }
54 if r.OneTimeKeys != nil {
55 uploadReq.OneTimeKeys = []api.OneTimeKeys{
56 {
57 DeviceID: device.ID,
58 UserID: device.UserID,
59 KeyJSON: r.OneTimeKeys,
60 },
61 }
62 }
63
64 var uploadRes api.PerformUploadKeysResponse
65 keyAPI.PerformUploadKeys(req.Context(), uploadReq, &uploadRes)
66 if uploadRes.Error != nil {
67 util.GetLogger(req.Context()).WithError(uploadRes.Error).Error("Failed to PerformUploadKeys")
68 return jsonerror.InternalServerError()
69 }
70 if len(uploadRes.KeyErrors) > 0 {
71 util.GetLogger(req.Context()).WithField("key_errors", uploadRes.KeyErrors).Error("Failed to upload one or more keys")
72 return util.JSONResponse{
73 Code: 400,
74 JSON: uploadRes.KeyErrors,
75 }
76 }
77 keyCount := make(map[string]int)
78 // we only return key counts when the client uploads OTKs
79 if len(uploadRes.OneTimeKeyCounts) > 0 {
80 keyCount = uploadRes.OneTimeKeyCounts[0].KeyCount
81 }
82 return util.JSONResponse{
83 Code: 200,
84 JSON: struct {
85 OTKCounts interface{} `json:"one_time_key_counts"`
86 }{keyCount},
87 }
88}
89
90type queryKeysRequest struct {
91 Timeout int `json:"timeout"`

Callers 1

SetupFunction · 0.85

Calls 3

ContextMethod · 0.80
PerformUploadKeysMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected