public async Task BanDialog(string userToBanId, string whoAccessedId) { if (userToBanId == whoAccessedId) { throw new InvalidDataException("Can't ban yourself."); } var bannedBy = await usersRepository.GetByIdAsync(whoAccessedId); var banned = await usersRepository.GetByIdAsync(userToBanId); if (banned == null || bannedBy == null) { throw new KeyNotFoundException("Wrong id of person to ban."); } try { await UsersBansRepository.AddAsync(UsersBansDatamodel.Create(banned, bannedBy)); } catch { throw new InvalidDataException("User is already banned."); } UsersConversationDataModel dialog; if ((dialog = await usersConversationsRepository.GetDialog(userToBanId, whoAccessedId)) != null) { await ConversationsBansRepository.AddAsync(ConversationsBansDataModel.Create(banned, dialog.Conversation)); } await unitOfWork.Commit(); }
public async Task UnbanDialog(string userId, string whoUnbans) { try { await UsersBansRepository.DeleteAsync(await UsersBansRepository.GetByIdAsync(userId, whoUnbans)); } catch { throw new InvalidDataException("Wrong id of a person to unban."); } UsersConversationDataModel dialog; if ((dialog = await usersConversationsRepository.GetDialog(userId, whoUnbans)) != null) { var entry = await ConversationsBansRepository.GetByIdAsync(userId, dialog.Conversation.Id); await ConversationsBansRepository.DeleteAsync(entry); } await unitOfWork.Commit(); }
public bool IsBannedFromMessagingWith(string who, string byWho) { // check if allowed return(UsersBansRepository.IsBanned(who, byWho).GetAwaiter().GetResult()); }