public void AddChatRequest(ChatRequest request)
 {
     lock (lChatRequests)
     {
         lChatRequests.Add(request);
     }
 }
Exemplo n.º 2
0
        public JoinChatRoomResult JoinChatRoom(string chatRoomId, string href)
        {
            if (href != null) HttpContext.Current.Items["href"] = href;

            var result = new JoinChatRoomResult
                             {
                                 FileTransferEnabled = chatSettingsProvider.EnableFileTransfer,
                                 VideoChatEnabled = chatSettingsProvider.EnableVideoChat,
                                 FlashMediaServer = chatSettingsProvider.FlashMediaServer
                             };

            User user;
            try
            {
                user = chatUserProvider.GetCurrentlyLoggedUser();
            }
            catch (System.Security.SecurityException err)
            {
                result.Error = err.Message;
                return result;
            }

            if (user == null)
            {
                string loginUrl = chatUserProvider.GetLoginUrl(href);
                result.Error = "You need to login in order to use the chat!";
                if (loginUrl != null)
                {
                    result.Error += " Redirecting to login page...";
                    result.RedirectUrl = loginUrl;
                }
                return result;
            }

            Room room = chatRoomProvider.GetChatRoom(chatRoomId);

            if (room == null)
            {
                result.Error = "The specified chat room does not exist!";
                return result;
            }

            #region Messenger
            var url = (new Uri(href));
            var queryKeyValues = HttpUtility.ParseQueryString(url.Query);
            var isInitiator = queryKeyValues["init"];
            var targetUserId = queryKeyValues["target"];
            bool alreadyConnected = href.IndexOf('#') == -1 ? false : (href.Substring(href.IndexOf('#')) == "#connected");
            #endregion

            string reason;

            //if messenger
            if (room.Id == "-2")
            {
                //check chat access only for the initiators
                if (isInitiator == "1" && !chatRoomProvider.HasChatAccess(user.Id, room.Id, out reason))
                {
                    if (String.IsNullOrEmpty(reason))
                        result.Error = "You don't have access to the messenger.";
                    else
                        result.Error = reason;

                    return result;
                }
            }
            else
            {
                if (!chatRoomProvider.HasChatAccess(user.Id, room.Id, out reason))
                {
                    if (String.IsNullOrEmpty(reason))
                        result.Error = String.Format("You don't have access to chat room '{0}'.", room.Name);
                    else
                        result.Error = reason;

                    return result;
                }
            }

            // Ignore max users limit for messengerRoom (roomId = -2)
            if (room.Id != "-2" && chatRoomStorage.GetUsersInRoom(room.Id).Length >= room.MaxUsers)
            {
                result.Error = String.Format("The chat room '{0}' is full. Please try again later.", room.Name);

                return result;
            }

            chatRoomStorage.AddUserToRoom(room.Id, user.Id);
            chatRoomStorage.AddMessage(room.Id, new Message
                                                    {
                                                        Content =
                                                            string.Format("User {0} has joined the chat.",
                                                                          user.DisplayName),
                                                        FromUserId = user.Id,
                                                        ToUserId = String.IsNullOrEmpty(targetUserId) ? null : targetUserId,
                                                        MessageType = MessageTypeEnum.UserJoined,
                                                        Timestamp = Miscellaneous.GetTimestamp()
                                                    });

            #region Messenger

            //delete all messages when starting messenger (not reloading it)
            if (isInitiator != null && !alreadyConnected)
            {
                chatRoomStorage.DeleteAllMessagesFor(room.Id, user.Id);
            }

            if (!alreadyConnected && isInitiator == "1" && !String.IsNullOrWhiteSpace(targetUserId))
            {
                try
                {
                    var targetUser = chatUserProvider.GetUser(targetUserId);

                    //Not sure that MessengerUrl should be constructed here?! timestamp and hash are tied to
                    //the implementation
                    var timestamp = DateTime.Now.ToFileTimeUtc();
                    var hash = Miscellaneous.CalculateChatAuthHash(targetUserId, user.Id, timestamp.ToString());

                    var request = new ChatRequest
                                      {
                                          FromThumbnailUrl = user.ThumbnailUrl,
                                          FromProfileUrl = user.ProfileUrl,
                                          FromUserId = user.Id,
                                          FromUsername = user.DisplayName,
                                          ToUserId = targetUserId,
                                          ToUsername = targetUser.DisplayName,
                                          MessengerUrl =
                                              String.Format("{0}?init=0&id={1}&target={2}&timestamp={3}&hash={4}",
                                                            url.GetLeftPart(UriPartial.Path), targetUserId, user.Id,
                                                            timestamp, hash)
                                      };

                    messengerProvider.AddChatRequest(request);
                }
                catch (System.Security.SecurityException) { }
            }
            #endregion

            result.ChatRoomName = room.Name;
            result.ChatRoomTopic = room.Topic;
            result.Users = chatRoomStorage.GetUsersInRoom(room.Id).Select(chatUserProvider.GetUser).ToArray();
            result.Token = chatRoomStorage.GenerateUserToken(user.Id);
            result.UserId = user.Id;
            result.IsAdmin = chatUserProvider.IsChatAdmin(user.Id, room.Id);

            StopVideoBroadcast(user.Id, room.Id);
            result.Broadcasts = chatRoomStorage.GetBroadcasts(room.Id, user.Id);

            return result;
        }