public object GetRequestData([FromQuery] TokenQueryModel tokenInfo, [FromQuery] RequestIdentityModel requestIdentity) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); var request = requestRepository.Select(requestIdentity.RequestId); return(RequestModel.Create(request)); }
public UserModel GetMe([FromQuery] TokenQueryModel tokenInfo) { var token = tokenRepository.ValidateToken(tokenInfo.Token); var user = userRepository.Select(token.UserUuid); return(UserModel.Create(user)); }
public object LeaveGroup([FromQuery] TokenQueryModel tokenData, [FromQuery] GroupIdentityModel groupIdentity) { var userContext = tokenRepository.ValidateToken(userRepository, tokenData.Token); var left = false; var exception = default(Exception); try { left = groupRepository.DeleteMember(userContext.user.Uuid, groupIdentity.GroupId); } catch (Exception e) { exception = e; } finally { if (!left) { throw new BasicControllerException("Failed to leave group.", "failed to leave group", exception); } } return(new OperationResultModel() { Result = left }); }
public object GetUsersInfo([FromQuery] TokenQueryModel tokenInfo, [FromBody] UsersListModel usersList) { var result = new List <SimpleUserModel>(); foreach (var user in userRepository.Select(usersList.Users)) { result.Add(SimpleUserModel.Create(user)); } return(result); }
public object NotifyOnline([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity) { var userContext = ValidateGroupMember(tokenInfo.Token, groupIdentity.GroupId); var count = chatManager.GetConnectUsersCount(groupIdentity.GroupId); return(new OperationResultModel <int>() { Result = count }); }
public object FillRequest([FromQuery] TokenQueryModel tokenInfo, [FromQuery] RequestIdentityModel requestIdentity) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); if (requestRepository.FillRequest(requestIdentity.RequestId)) { return(RequestModel.Create(requestRepository.Select(requestIdentity.RequestId))); } throw new BasicControllerException("Failed to fill request", "failed to fill request"); }
public async Task CreateSocketConnection([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity) { var userContext = ValidateGroupMember(tokenInfo.Token, groupIdentity.GroupId); if (ControllerContext.HttpContext.WebSockets.IsWebSocketRequest) { var socket = await ControllerContext.HttpContext.WebSockets.AcceptWebSocketAsync(); var ct = ControllerContext.HttpContext.RequestAborted; await chatManager.Listen(groupIdentity.GroupId, userContext.user.Uuid, socket, ct); } }
public object GetMembers([FromQuery] TokenQueryModel tokenData, [FromQuery] GroupIdentityModel groupIdentity) { var token = tokenRepository.ValidateToken(tokenData.Token); var members = groupRepository.SelectMembers(groupIdentity.GroupId); var result = new List <GroupMemberModel>(); foreach (var member in members) { result.Add(GroupMemberModel.Create(member)); } return(result); }
public object NotifyOnline([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); if (!groupRepository.IsMember(userContext.user.Uuid, groupIdentity.GroupId)) { throw new AuthorizationException($"User {userContext.user.Uuid} isn't group member", "not a group member"); } chatManager.NotifyConnectedAsync(groupIdentity.GroupId, userContext.user.Uuid); return(new OperationResultModel() { Result = true }); }
public object SendMessage([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity, [FromBody] MessageDataModel messageData) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); if (!groupRepository.IsMember(userContext.user.Uuid, groupIdentity.GroupId)) { throw new AuthorizationException($"User {userContext.user.Uuid} isn't group member", "not a group member"); } messageRepository.Send(userContext.user.Uuid, groupIdentity.GroupId, messageData.Text); chatManager.NotifyNewMessageAsync(groupIdentity.GroupId); return(new OperationResultModel() { Result = true }); }
public object JoinGroup([FromQuery] TokenQueryModel tokenData, [FromQuery] GroupIdentityModel groupIdentity) { var userContext = tokenRepository.ValidateToken(userRepository, tokenData.Token); var group = groupRepository.Select(groupIdentity.GroupId); var membersCount = groupRepository.SelectMembersCount(groupIdentity.GroupId); if (membersCount >= platformConfig.GroupMaxMembers) { throw new BasicControllerException("Group is full.", "group is full"); } var joined = false; var exception = default(Exception); try { joined = groupRepository.InsertMember(userContext.user.Uuid, group.Id, (byte)GroupMemberRole.Default); } catch (MySqlException e) { if (e.Number == 1062) { throw new BasicControllerException(e.Message, "already joined", e); } else { exception = e; } } catch (Exception e) { exception = e; } finally { if (!joined && exception != null) { throw new BasicControllerException("Failed to insert new group member.", "failed to join group", exception); } } return(new OperationResultModel() { Result = joined }); }
public object CreateGroup([FromQuery] NewGroupInfoQueryModel groupInfo, [FromQuery] TokenQueryModel tokenData) { if (groupInfo.Name.Length > MaxGroupNameLength) { throw new Exceptions.FormatException("Group name is too long.", "group name is too long"); } if (!Enum.IsDefined(typeof(GroupType), groupInfo.Type)) { throw new Exceptions.FormatException("Unknown group type.", "unknown group type"); } var token = tokenRepository.ValidateToken(tokenData.Token); var group = groupRepository.CreateGroup(groupInfo.Name, groupInfo.Type, token.UserUuid); return(GroupModel.Create(group)); }
public object CreateRequest([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity, [FromQuery] RequestQueryModel requestInfo) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); if (!groupRepository.IsMember(userContext.user.Uuid, groupIdentity.GroupId)) { throw new AuthorizationException($"User {userContext.user.Uuid} isn't group member", "not a group member"); } if (requestInfo.Type == 0 || !Enum.IsDefined(typeof(RequestType), requestInfo.Type)) { throw new BasicControllerException("Request type not defined.", "invalid request type", statusCode: System.Net.HttpStatusCode.UnprocessableEntity); } var type = (RequestType)requestInfo.Type; var request = default(Request); switch (type) { case RequestType.Lives: request = new Request() { Type = (byte)RequestType.Lives, Amount = 5, Filled = 0, Sender = userContext.user.Uuid }; break; default: throw new BasicControllerException($"Handler for {type} reuqest type not implemented.", "server error", statusCode: System.Net.HttpStatusCode.InternalServerError); } requestRepository.Insert(request); messageRepository.Send(userContext.user.Uuid, groupIdentity.GroupId, String.Empty, (byte)AttachmentType.Request, request.Id); chatManager.NotifyNewMessageAsync(groupIdentity.GroupId); return(RequestModel.Create(request)); }
public object FetchMessages([FromQuery] TokenQueryModel tokenInfo, [FromQuery] GroupIdentityModel groupIdentity, [FromQuery] MessageRangeInfoModel rangeInfo) { var userContext = tokenRepository.ValidateToken(userRepository, tokenInfo.Token); if (!groupRepository.IsMember(userContext.user.Uuid, groupIdentity.GroupId)) { throw new AuthorizationException($"User {userContext.user.Uuid} isn't group member", "not a group member"); } var ascendingOrder = rangeInfo.Ascending; var offset = rangeInfo.Offset; var limit = rangeInfo.Limit > MaxFetchLimit ? MaxFetchLimit : rangeInfo.Limit == 0 ? DefaultFetchLimit : rangeInfo.Limit; var result = new List <MessageModel>(); var forward = rangeInfo.Direction >= 0; if (rangeInfo.StartId == 0) { var range = messageRepository.SelectRange(groupIdentity.GroupId, offset, limit); foreach (var messageData in (ascendingOrder ? range.Reverse() : range)) { result.Add(MessageModel.Create(messageData)); } } else { var range = forward ? messageRepository.SelectMessagesAfterGiven(groupIdentity.GroupId, offset, limit, rangeInfo.StartId) : messageRepository.SelectMessagesBeforeGiven(groupIdentity.GroupId, offset, limit, rangeInfo.StartId); foreach (var messageData in (ascendingOrder ? range : range.Reverse())) { result.Add(MessageModel.Create(messageData)); } } return(result); }