public async Task <IActionResult> UpdateMessageLifeTime(UpdateMessageLifeTimeAddressModel model) { var user = await GetKahlaUser(); var target = await _dbContext.Conversations.FindAsync(model.Id); if (!await _dbContext.VerifyJoined(user.Id, target)) { return(this.Protocol(ErrorType.Unauthorized, "You don't have any relationship with that conversation.")); } // Do update. target.MaxLiveSeconds = model.NewLifeTime; await _dbContext.SaveChangesAsync(); // Delete outdated for current. var outdatedMessages = _dbContext .Messages .Include(t => t.Conversation) .Where(t => t.ConversationId == target.Id) .Where(t => DateTime.UtcNow > t.SendTime + TimeSpan.FromSeconds(t.Conversation.MaxLiveSeconds)); _dbContext.Messages.RemoveRange(outdatedMessages); await _dbContext.SaveChangesAsync(); // Push event. if (target is PrivateConversation privateConversation) { var requester = await _userManager.FindByIdAsync(privateConversation.RequesterId); await _pusher.TimerUpdatedEvent(requester, model.NewLifeTime, target.Id); if (privateConversation.RequesterId != privateConversation.TargetId) { var targetUser = await _userManager.FindByIdAsync(privateConversation.TargetId); await _pusher.TimerUpdatedEvent(targetUser, model.NewLifeTime, target.Id); } } else if (target is GroupConversation g) { var usersJoined = await _dbContext .UserGroupRelations .Include(t => t.User) .Where(t => t.GroupId == g.Id) .ToListAsync(); var taskList = new List <Task>(); foreach (var relation in usersJoined) { var pushTask = _pusher.TimerUpdatedEvent(relation.User, model.NewLifeTime, target.Id); taskList.Add(pushTask); } await Task.WhenAll(taskList); } return(this.Protocol(ErrorType.Success, "Successfully updated your life time. Your current message life time is: " + TimeSpan.FromSeconds(target.MaxLiveSeconds))); }
public async Task <IActionResult> UpdateMessageLifeTime(UpdateMessageLifeTimeAddressModel model) { var user = await GetKahlaUser(); var target = await _dbContext .Conversations .Include(t => (t as GroupConversation).Users) .ThenInclude(t => t.User) .SingleOrDefaultAsync(t => t.Id == model.Id); if (target == null) { return(this.Protocol(ErrorType.NotFound, $"Can not find conversation with id: {model.Id}.")); } if (!target.HasUser(user.Id)) { return(this.Protocol(ErrorType.Unauthorized, "You don't have any relationship with that conversation.")); } if (target is GroupConversation g && g.OwnerId != user.Id) { return(this.Protocol(ErrorType.Unauthorized, "You are not the owner of that group.")); } var oldestAliveTime = DateTime.UtcNow - TimeSpan.FromSeconds(Math.Min(target.MaxLiveSeconds, model.NewLifeTime)); // Delete outdated for current. var toDelete = await _dbContext .Messages .Where(t => t.ConversationId == target.Id) .Where(t => t.SendTime < oldestAliveTime) .ToListAsync(); _dbContext.Messages.RemoveRange(toDelete); await _dbContext.SaveChangesAsync(); // Update current. target.MaxLiveSeconds = model.NewLifeTime; await _dbContext.SaveChangesAsync(); var taskList = new List <Task>(); await target.ForEachUserAsync((eachUser, relation) => { taskList.Add(_pusher.TimerUpdatedEvent(eachUser, model.NewLifeTime, target.Id)); return(Task.CompletedTask); }); await Task.WhenAll(taskList); return(this.Protocol(ErrorType.Success, "Successfully updated your life time. Your current message life time is: " + TimeSpan.FromSeconds(target.MaxLiveSeconds))); }
public async Task <IActionResult> UpdateMessageLifeTime(UpdateMessageLifeTimeAddressModel model) { var user = await GetKahlaUser(); var target = await _dbContext.Conversations.FindAsync(model.Id); if (!await _dbContext.VerifyJoined(user.Id, target)) { return(this.Protocol(ErrorType.Unauthorized, "You don't have any relationship with that conversation.")); } if (target is GroupConversation g && g.OwnerId != user.Id) { return(this.Protocol(ErrorType.Unauthorized, "You are not the owner of that group.")); } var oldestAliveTime = DateTime.UtcNow - TimeSpan.FromSeconds(Math.Min(target.MaxLiveSeconds, model.NewLifeTime)); // Delete outdated for current. var outdatedMessages = await _dbContext .Messages .Where(t => t.ConversationId == target.Id) .Where(t => t.SendTime < oldestAliveTime) .BatchDeleteAsync(); await _dbContext.SaveChangesAsync(); // Update current. target.MaxLiveSeconds = model.NewLifeTime; await _dbContext.SaveChangesAsync(); var taskList = new List <Task>(); await target.ForEachUserAsync((eachUser, relation) => { taskList.Add(_pusher.TimerUpdatedEvent(eachUser, model.NewLifeTime, target.Id)); return(Task.CompletedTask); }, _userManager); await Task.WhenAll(taskList); return(this.Protocol(ErrorType.Success, "Successfully updated your life time. Your current message life time is: " + TimeSpan.FromSeconds(target.MaxLiveSeconds))); }