public ChatSession JoinChatRoom(int chatRoomId) { if (ApplicationInstance == null) ApplicationInstance = Context.ApplicationInstance; CleanTimedOutUsers.Ping(); if (ChatSession.Current == null) ChatSession.Current = new ChatSession(); ChatSession sess = ChatSession.Current; HttpApplication app = Context.ApplicationInstance; if (app is IHttpApplicationUserAdapter) { string username = ((IHttpApplicationUserAdapter) app).GetCurrentlyLoggedUsername(); if (username == null) { sess.Authorized = false; if (app is IHttpApplicationSupportLogin) { sess.AuthorizeUrl = ((IHttpApplicationSupportLogin) app).GetLoginUrl(); } } else { sess.ChatUserInstance = ChatUser.FetchByUsername(username); List<ChatBan> bans = null; if (sess.ChatUserInstance != null) { bans = ChatBan.FetchBans(null, chatRoomId, sess.ChatUserInstance.Id, null, DateTime.Now); } if (bans != null && bans.Count > 0) { sess.Authorized = false; sess.Banned = true; } else { sess.Authorized = true; sess.ChatUserInstance = ChatUser.FetchByUsername(username); if (sess.ChatUserInstance == null) { ChatUser user = new ChatUser(); user.Username = username; user.DisplayName = username; user.BeginTime = DateTime.Now; user.ActiveTime = DateTime.Now; user.Ip = Context.Request.UserHostAddress; user.Save(); sess.ChatUserInstance = user; } if (sess.JoinedChatRooms.IndexOf(chatRoomId) < 0) { sess.JoinedChatRooms.Add(chatRoomId); } ChatRoomEvents.JoinUser(chatRoomId, sess); string joinedRoomMessage = "User {0} joined the room"; if (Context.ApplicationInstance is IHttpApplicationSupportTranslations) joinedRoomMessage = ((IHttpApplicationSupportTranslations) Context.ApplicationInstance) .Translate(joinedRoomMessage); ChatMessage msg = ChatMessage.Create("system", String.Format( joinedRoomMessage, sess.ChatUserInstance.DisplayName)); msg.MessageType = ChatMessage.MessageTypeEnum.JoinRoom; msg.SenderUserId = sess.ChatUserInstance.Id; msg.ChatRoomId = chatRoomId; msg.Save(); ChatRoomEvents.AddEvent(msg); } } } else { sess.Authorized = true; } return sess; }
private static List<ChatUser> Fetch(int? id, string username, string displayName, string ip) { List<ChatUser> users = new List<ChatUser>(); using (SqlConnection conn = DB.Open()) { SqlDataReader reader = SqlHelper.ExecuteReader(conn, "FetchChatUsers", id, username, displayName, ip); while (reader.Read()) { ChatUser user = new ChatUser(); user.id = (int)reader["Id"]; user.username = (string) reader["Username"]; user.displayName = (string)reader["DisplayName"]; user.beginTime = (DateTime)reader["BeginTime"]; user.activeTime = (DateTime)reader["ActiveTime"]; user.ip = (string)reader["Ip"]; users.Add(user); } } return users; }