public string GetAllChats(string guid) { // Check current user var userElasticResult = _userRepository.Get(guid); if (!userElasticResult.Success || userElasticResult.Value == null) { return(JsonConvert.SerializeObject(new { error = true, message = ReloginMessage })); } var chatUserlasticResult = _chatUserRepository.GetAllByUserGuid(guid); if (!chatUserlasticResult.Success) { return(JsonConvert.SerializeObject(new List <ElasticChat>())); } var chatElasticResult = _chatRepository.GetByGuids(chatUserlasticResult.Value.Select(c => c.ChatGuid).ToArray()); if (!chatElasticResult.Success) { return(JsonConvert.SerializeObject(new List <ElasticChat>())); } return(JsonConvert.SerializeObject(chatElasticResult.Value.OrderBy(u => u.Name))); }
public void Login() { // Get user by cookie var user = GetUserFromCookies(Context.RequestCookies); if (user == null) { Clients.Caller.onLoginCaller(JsonConvert.SerializeObject(new { error = true, message = UserErrorMessage })); return; } // Update connection ids user.ConnectionIds.Add(Context.ConnectionId); var elasticResult = _userRepository.Update(user); if (!elasticResult.Success || elasticResult.Value == null) { Clients.Caller.onLoginCaller( JsonConvert.SerializeObject(new { error = true, message = ServerErrorMessage })); return; } #region Add current connection to all user's chats var chatUserElasticResult = _chatUserRepository.GetAllByUserGuid(user.Guid); if (!elasticResult.Success || elasticResult.Value == null) { Clients.Caller.onLoginCaller( JsonConvert.SerializeObject(new { error = true, message = ServerErrorMessage })); return; } foreach (var chatUser in chatUserElasticResult.Value) { Groups.Add(Context.ConnectionId, chatUser.ChatGuid); } #endregion user = elasticResult.Value; if (user.ConnectionIds.Count() == 1) { Clients.Others.onLoginOthers(JsonConvert.SerializeObject(user)); } Clients.Caller.onLoginCaller(JsonConvert.SerializeObject(user)); }