Skip to content

Commit 3407bac

Browse files
committed
setup device creation api to do the right thing
1 parent 913f0c4 commit 3407bac

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

userapi/internal/user_api.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,20 @@ func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.Pe
288288
if !a.Config.Global.IsLocalServerName(serverName) {
289289
return fmt.Errorf("server name %s is not local", serverName)
290290
}
291+
292+
var dev *api.Device
293+
var err error
294+
291295
// If a device ID was specified, check if it already exists and
292296
// avoid sending an empty device list update which would remove
293297
// existing device keys.
294298
isExisting := false
295299
if req.DeviceID != nil && *req.DeviceID != "" {
296-
existingDev, err := a.DB.GetDeviceByID(ctx, req.Localpart, req.ServerName, *req.DeviceID)
300+
dev, err = a.DB.GetDeviceByID(ctx, req.Localpart, req.ServerName, *req.DeviceID)
297301
if err != nil && !frame.ErrIsNotFound(err) {
298302
return err
299303
}
300-
isExisting = existingDev.ID == *req.DeviceID
304+
isExisting = dev.ID == *req.DeviceID
301305
}
302306
log := util.Log(ctx).WithField("localpart", req.Localpart)
303307

@@ -308,10 +312,14 @@ func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.Pe
308312
if req.DeviceDisplayName != nil {
309313
log = log.WithField("display_name", *req.DeviceDisplayName)
310314
}
311-
log.Debug("PerformDeviceCreation")
312-
dev, err := a.DB.CreateDevice(ctx, req.Localpart, serverName, req.DeviceID, req.AccessToken, req.ExtraData, req.DeviceDisplayName, req.IPAddr, req.UserAgent)
313-
if err != nil {
314-
return err
315+
316+
if !isExisting {
317+
log.Debug("PerformDeviceCreation")
318+
319+
dev, err = a.DB.CreateDevice(ctx, req.Localpart, serverName, req.DeviceID, req.AccessToken, req.ExtraData, req.DeviceDisplayName, req.IPAddr, req.UserAgent)
320+
if err != nil {
321+
return err
322+
}
315323
}
316324
res.DeviceCreated = true
317325
res.Device = dev

0 commit comments

Comments
 (0)