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

Function GetDeviceByID

clientapi/routing/device.go:52–87  ·  view source on GitHub ↗

GetDeviceByID handles /devices/{deviceID}

(
	req *http.Request, userAPI api.ClientUserAPI, device *api.Device,
	deviceID string,
)

Source from the content-addressed store, hash-verified

50
51// GetDeviceByID handles /devices/{deviceID}
52func GetDeviceByID(
53 req *http.Request, userAPI api.ClientUserAPI, device *api.Device,
54 deviceID string,
55) util.JSONResponse {
56 var queryRes api.QueryDevicesResponse
57 err := userAPI.QueryDevices(req.Context(), &api.QueryDevicesRequest{
58 UserID: device.UserID,
59 }, &queryRes)
60 if err != nil {
61 util.GetLogger(req.Context()).WithError(err).Error("QueryDevices failed")
62 return jsonerror.InternalServerError()
63 }
64 var targetDevice *api.Device
65 for _, device := range queryRes.Devices {
66 if device.ID == deviceID {
67 targetDevice = &device
68 break
69 }
70 }
71 if targetDevice == nil {
72 return util.JSONResponse{
73 Code: http.StatusNotFound,
74 JSON: jsonerror.NotFound("Unknown device"),
75 }
76 }
77
78 return util.JSONResponse{
79 Code: http.StatusOK,
80 JSON: deviceJSON{
81 DeviceID: targetDevice.ID,
82 DisplayName: targetDevice.DisplayName,
83 LastSeenIP: stripIPPort(targetDevice.LastSeenIP),
84 LastSeenTS: targetDevice.LastSeenTS,
85 },
86 }
87}
88
89// GetDevicesByLocalpart handles /devices
90func GetDevicesByLocalpart(

Callers 1

SetupFunction · 0.85

Calls 4

stripIPPortFunction · 0.85
ContextMethod · 0.80
QueryDevicesMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected