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

Method PerformDeviceUpdate

userapi/internal/api.go:244–293  ·  view source on GitHub ↗
(ctx context.Context, req *api.PerformDeviceUpdateRequest, res *api.PerformDeviceUpdateResponse)

Source from the content-addressed store, hash-verified

242}
243
244func (a *UserInternalAPI) PerformDeviceUpdate(ctx context.Context, req *api.PerformDeviceUpdateRequest, res *api.PerformDeviceUpdateResponse) error {
245 localpart, _, err := gomatrixserverlib.SplitID('@', req.RequestingUserID)
246 if err != nil {
247 util.GetLogger(ctx).WithError(err).Error("gomatrixserverlib.SplitID failed")
248 return err
249 }
250 dev, err := a.DB.GetDeviceByID(ctx, localpart, req.DeviceID)
251 if err == sql.ErrNoRows {
252 res.DeviceExists = false
253 return nil
254 } else if err != nil {
255 util.GetLogger(ctx).WithError(err).Error("deviceDB.GetDeviceByID failed")
256 return err
257 }
258 res.DeviceExists = true
259
260 if dev.UserID != req.RequestingUserID {
261 res.Forbidden = true
262 return nil
263 }
264
265 err = a.DB.UpdateDevice(ctx, localpart, req.DeviceID, req.DisplayName)
266 if err != nil {
267 util.GetLogger(ctx).WithError(err).Error("deviceDB.UpdateDevice failed")
268 return err
269 }
270 if req.DisplayName != nil && dev.DisplayName != *req.DisplayName {
271 // display name has changed: update the device key
272 var uploadRes keyapi.PerformUploadKeysResponse
273 a.KeyAPI.PerformUploadKeys(context.Background(), &keyapi.PerformUploadKeysRequest{
274 UserID: req.RequestingUserID,
275 DeviceKeys: []keyapi.DeviceKeys{
276 {
277 DeviceID: dev.ID,
278 DisplayName: *req.DisplayName,
279 KeyJSON: nil,
280 UserID: dev.UserID,
281 },
282 },
283 OnlyDisplayNameUpdates: true,
284 }, &uploadRes)
285 if uploadRes.Error != nil {
286 return fmt.Errorf("failed to update device key display name: %v", uploadRes.Error)
287 }
288 if len(uploadRes.KeyErrors) > 0 {
289 return fmt.Errorf("failed to update device key display name, key errors: %+v", uploadRes.KeyErrors)
290 }
291 }
292 return nil
293}
294
295func (a *UserInternalAPI) QueryProfile(ctx context.Context, req *api.QueryProfileRequest, res *api.QueryProfileResponse) error {
296 local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)

Callers

nothing calls this directly

Calls 2

PerformUploadKeysMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected