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

Function GetAccountData

clientapi/routing/account_data.go:34–74  ·  view source on GitHub ↗

GetAccountData implements GET /user/{userId}/[rooms/{roomid}/]account_data/{type}

(
	req *http.Request, userAPI api.ClientUserAPI, device *api.Device,
	userID string, roomID string, dataType string,
)

Source from the content-addressed store, hash-verified

32
33// GetAccountData implements GET /user/{userId}/[rooms/{roomid}/]account_data/{type}
34func GetAccountData(
35 req *http.Request, userAPI api.ClientUserAPI, device *api.Device,
36 userID string, roomID string, dataType string,
37) util.JSONResponse {
38 if userID != device.UserID {
39 return util.JSONResponse{
40 Code: http.StatusForbidden,
41 JSON: jsonerror.Forbidden("userID does not match the current user"),
42 }
43 }
44
45 dataReq := api.QueryAccountDataRequest{
46 UserID: userID,
47 DataType: dataType,
48 RoomID: roomID,
49 }
50 dataRes := api.QueryAccountDataResponse{}
51 if err := userAPI.QueryAccountData(req.Context(), &dataReq, &dataRes); err != nil {
52 util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryAccountData failed")
53 return util.ErrorResponse(fmt.Errorf("userAPI.QueryAccountData: %w", err))
54 }
55
56 var data json.RawMessage
57 var ok bool
58 if roomID != "" {
59 data, ok = dataRes.RoomAccountData[roomID][dataType]
60 } else {
61 data, ok = dataRes.GlobalAccountData[dataType]
62 }
63 if ok {
64 return util.JSONResponse{
65 Code: http.StatusOK,
66 JSON: data,
67 }
68 }
69
70 return util.JSONResponse{
71 Code: http.StatusNotFound,
72 JSON: jsonerror.NotFound("data not found"),
73 }
74}
75
76// SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type}
77func SaveAccountData(

Callers 1

SetupFunction · 0.85

Calls 3

ContextMethod · 0.80
QueryAccountDataMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected