private void DeleteAllExpiredSessionsAndMessages() { CryptoChatEntities context = new CryptoChatEntities(); DateTime minAllowedLastActivityTime = DateTime.Now.AddSeconds(-Settings.Default.HttpSessionTimeoutSeconds); // Delete any expired chat sessions in the DB string deleteChatSessionsSQL = "DELETE FROM ChatSessions " + "FROM Users u JOIN ChatSessions s " + " ON (u.UserId = s.FromUserId or u.UserId = s.ToUserId) " + "WHERE LastActivity < {0}"; context.ExecuteStoreCommand(deleteChatSessionsSQL, new object[] { minAllowedLastActivityTime }); // Delete any expired messages in the DB string deleteMessagesSQL = "DELETE FROM Messages " + "FROM Users u JOIN Messages m ON (u.UserId = m.ToUserId) " + "WHERE LastActivity < {0}"; context.ExecuteStoreCommand(deleteMessagesSQL, new object[] { minAllowedLastActivityTime }); // Send notifications about all users whose sessions were expired var usersWithExpiredSession = from u in context.Users where u.LastActivity < minAllowedLastActivityTime && u.SessionKey != null select u; foreach (var user in usersWithExpiredSession) { user.SessionKey = null; MobileCryptoChatService.SendMessageToAllOnlineUsers(user, MessageType.MSG_USER_OFFLINE, "User lost connection with the server."); } context.SaveChanges(); }
private void DeleteChatSession(User senderUser, User recipientUser) { CryptoChatEntities context = new CryptoChatEntities(); // Delete any existing chat sessions between the users string deleteChatSessionsSQL = "DELETE FROM ChatSessions WHERE " + "(FromUserId={0} and ToUserId={1}) or (FromUserId={1} and ToUserId={0})"; context.ExecuteStoreCommand(deleteChatSessionsSQL, new object[] { senderUser.UserId, recipientUser.UserId }); // Delete any waiting chat messages between the users string msgTypesToDelete = "'" + MessageType.MSG_CHALLENGE.ToString() + "'," + "'" + MessageType.MSG_RESPONSE.ToString() + "'," + "'" + MessageType.MSG_START_CHAT.ToString() + "'," + "'" + MessageType.MSG_CANCEL_CHAT.ToString() + "'," + "'" + MessageType.MSG_CHAT_MESSAGE.ToString() + "'"; string deleteMessagesSQL = "DELETE FROM Messages WHERE " + "((FromUserId={0} and ToUserId={1}) or (FromUserId={1} and ToUserId={0})) AND " + "(MsgType in (" + msgTypesToDelete + "))"; context.ExecuteStoreCommand(deleteMessagesSQL, new object[] { senderUser.UserId, recipientUser.UserId }); }
private void DeleteWaitingMessagesAndSessions(User recipientUser) { CryptoChatEntities context = new CryptoChatEntities(); // Delete any waiting messages for the user string deleteMessagesSQL = "DELETE FROM Messages WHERE ToUserId={0}"; context.ExecuteStoreCommand(deleteMessagesSQL, new object[] { recipientUser.UserId }); // Delete any existing chat sessions associated with the user string deleteChatSessionsSQL = "DELETE FROM ChatSessions WHERE " + "FromUserId={0} or ToUserId={0}"; context.ExecuteStoreCommand(deleteChatSessionsSQL, new object[] { recipientUser.UserId }); }
public StatusResponse LogoutUser(string sessionID) { ExecuteAndHandleExceptions(() => { User user = CheckUserSession(sessionID); CryptoChatEntities context = new CryptoChatEntities(); int updatedSessionsCount = context.ExecuteStoreCommand( "UPDATE Users SET SessionKey=null WHERE SessionKey={0}", new object[] { sessionID }); if (updatedSessionsCount != 1) { throw new ErrorResponseException(HttpStatusCode.InternalServerError, "ERR_LOGOUT_FAIL", "Logout failed unexpectedly."); } // Session sucessfully removed from DB (logout was successfull) DeleteWaitingMessagesAndSessions(user); // Send the "user online" notification to all online users SendMessageToAllOnlineUsers(user, MessageType.MSG_USER_OFFLINE, "User left the system."); }); return new StatusResponse(); }