Exemple #1
0
        public async Task <IActionResult> AddDevice(AddDeviceAddressModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (_dbContext.Devices.Any(t => t.PushP256DH == model.PushP256DH))
            {
                return(this.Protocol(ErrorType.HasDoneAlready, "已经有一个带有重复Push 256dh的设备: " + model.PushP256DH));
            }
            var devicesExists = await _dbContext.Devices.Where(t => t.UserID == user.Id).ToListAsync();

            if (devicesExists.Count >= 100)
            {
                var toDrop = devicesExists.OrderBy(t => t.AddTime).First();
                _dbContext.Devices.Remove(toDrop);
                await _dbContext.SaveChangesAsync();
            }
            var device = new Device
            {
                Name         = model.Name,
                UserID       = user.Id,
                PushAuth     = model.PushAuth,
                PushEndpoint = model.PushEndpoint,
                PushP256DH   = model.PushP256DH,
                IPAddress    = HttpContext.Connection.RemoteIpAddress.ToString()
            };

            _dbContext.Devices.Add(device);
            await _dbContext.SaveChangesAsync();

            return(this.ChatJson(new AiurValue <long>(device.Id)
            {
                Code = ErrorType.Success,
                Message = "已成功创建新设备 ID: " + device.Id
            }));
        }
Exemple #2
0
 public async Task<IActionResult> AddDevice(AddDeviceAddressModel model)
 {
     var user = await GetKahlaUser();
     if (_dbContext.Devices.Any(t => t.PushP256DH == model.PushP256DH))
     {
         return this.Protocol(ErrorType.HasDoneAlready, "There is already an device with push 256DH: " + model.PushP256DH);
     }
     var devicesExists = await _dbContext.Devices.Where(t => t.UserId == user.Id).ToListAsync();
     if (devicesExists.Count >= 10)
     {
         var toDrop = devicesExists.OrderBy(t => t.AddTime).First();
         _dbContext.Devices.Remove(toDrop);
         await _dbContext.SaveChangesAsync();
     }
     var device = new Device
     {
         Name = model.Name,
         UserId = user.Id,
         PushAuth = model.PushAuth,
         PushEndpoint = model.PushEndpoint,
         PushP256DH = model.PushP256DH,
         IPAddress = HttpContext.Connection.RemoteIpAddress.ToString()
     };
     _dbContext.Devices.Add(device);
     await _dbContext.SaveChangesAsync();
     //ErrorType.Success, 
     return Json(new AiurValue<long>(device.Id)
     {
         Code = ErrorType.Success,
         Message = "Successfully created your new device with id: " + device.Id
     });
 }
Exemple #3
0
        public async Task <IActionResult> AddDevice(AddDeviceAddressModel model)
        {
            var user = await GetKahlaUser();

            var existingDevice = await _dbContext.Devices.FirstOrDefaultAsync(t => t.PushP256DH == model.PushP256DH);

            if (existingDevice != null)
            {
                return(Json(new AiurValue <long>(existingDevice.Id)
                {
                    Code = ErrorType.HasDoneAlready,
                    Message = "There is already a device with the same `PushP256DH`. Please check the value in the response."
                }));
            }
            var devicesExists = await _dbContext.Devices.Where(t => t.UserId == user.Id).ToListAsync();

            if (devicesExists.Count >= 10)
            {
                var toDrop = devicesExists.OrderBy(t => t.AddTime).First();
                _dbContext.Devices.Remove(toDrop);
                await _dbContext.SaveChangesAsync();
            }
            var device = new Device
            {
                Name         = model.Name,
                UserId       = user.Id,
                PushAuth     = model.PushAuth,
                PushEndpoint = model.PushEndpoint,
                PushP256DH   = model.PushP256DH,
                IPAddress    = HttpContext.Connection.RemoteIpAddress.ToString()
            };
            await _dbContext.Devices.AddAsync(device);

            await _dbContext.SaveChangesAsync();

            //ErrorType.Success,
            return(Json(new AiurValue <long>(device.Id)
            {
                Code = ErrorType.Success,
                Message = "Successfully created your new device with id: " + device.Id
            }));
        }