Ejemplo n.º 1
0
        public void ProcessKeyword(ChatMessage chatMessage, ProcessKeywordMode mode)
        {
            if (chatMessage == null)
            {
                return;
            }

            ChatMessageCollection messages = new ChatMessageCollection();

            messages.Add(chatMessage);

            ProcessKeyword(messages, mode);
        }
Ejemplo n.º 2
0
        public ChatMessageCollection GetLastChatMessages(int userID, int targetUserID, int lastMessageID, int messageCount)
        {
            if (!AllSettings.Current.ChatSettings.EnableChatFunction)
            {
                return(new ChatMessageCollection());
            }

            messageCount = messageCount <= 0 ? 200 : messageCount;

#if !Passport
            PassportClientConfig settings = Globals.PassportClient;
            if (settings.EnablePassport)
            {
                ChatMessageProxy[] messages = settings.PassportService.Chat_GetLastChatMessages(userID, targetUserID, lastMessageID, messageCount);


                ChatMessageCollection result = new ChatMessageCollection();
                foreach (ChatMessageProxy m in messages)
                {
                    result.Add(GetChatMessage(m));
                }

                return(result);
            }
            else
#endif
            {
                ChatMessageCollection messages = ChatDao.Instance.GetLastChatMessages(userID, targetUserID, lastMessageID, messageCount);
                ProcessKeyword(messages, ProcessKeywordMode.TryUpdateKeyword);

                if (messages.Count > 0)
                {
                    if (OnUsersMessageCountChanged != null)
                    {
                        AuthUser user = UserBO.Instance.GetAuthUser(userID);
                        if (user != null)
                        {
                            Dictionary <int, int> counts = new Dictionary <int, int>();
                            counts.Add(userID, user.UnreadMessages);
                            OnUsersMessageCountChanged(counts);
                        }
                    }
                }
                return(messages);
            }

            //if (OnChatUserMessageCountChanged != null) OnChatUserMessageCountChanged(userID);
        }
Ejemplo n.º 3
0
        private static IEnumerable <ChatMessage> GetUnreadMessages(ISkype skype)
        {
            ChatMessageCollection messages = null;

            foreach (var name in Names)
            {
                messages = skype.Messages[name];
            }
            foreach (string chatName in ChatNames)
            {
                var chatMessages = skype.Chat[chatName].Messages;
                if (messages == null)
                {
                    break;
                }
                foreach (ChatMessage chatMessage in chatMessages)
                {
                    messages.Add(chatMessage);
                }
            }

            var unreaded = messages?.OfType <ChatMessage>().Where(o => (int)o.Status == 2);

            return(unreaded);

            //            foreach (Chat chat in skype.Chats)
            //            {
            //                if (chat.ActiveMembers.Count > 2)
            //                {
            //                    Console.WriteLine(chat.Name);
            //                    foreach (User activeMember in chat.ActiveMembers)
            //                    {
            //                        Console.WriteLine(activeMember.FullName);
            //                    }
            //                    Console.WriteLine(chat.ActiveMembers.Count);
            //                }
            //            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 发送一个对话消息,并且根据指定参数决定是否需要同时返回最新消息
        /// </summary>
        /// <param name="operatorID">发送者</param>
        /// <param name="targetUserID">接受者</param>
        /// <param name="contentWithoutEncode">未经html编码的内容</param>
        /// <param name="ip">发送者IP</param>
        /// <param name="processContent">是否处理内容</param>
        /// <param name="getNewMessages">是否需要同时返回最新消息</param>
        /// <param name="lastMessageID">如果需要返回最新消息,则传入客户端</param>
        /// <returns></returns>
        public ChatMessageCollection SendMessage(int operatorID, int targetUserID, string contentWithoutEncode, string ip, bool processContent, bool getNewMessages, int lastMessageID)
        {
#if !Passport
            PassportClientConfig settings = Globals.PassportClient;
            if (settings.EnablePassport)
            {
                ChatMessageProxy[] chatMessages;
                APIResult          result  = null;
                string             content = ProcessChatContent(operatorID, contentWithoutEncode);
                content = content.Replace("{$root}", Globals.FullAppRoot);

                try
                {
                    result = settings.PassportService.Chat_SendMessage(operatorID, targetUserID, content, ip, getNewMessages, lastMessageID, out chatMessages);
                }
                catch (Exception ex)
                {
                    ThrowError(new APIError(ex.Message));
                    return(new ChatMessageCollection());
                }

                if (result.ErrorCode == Consts.ExceptionCode)
                {
                    if (result.Messages.Length > 0)
                    {
                        ThrowError(new CustomError("远程服务器错误, 请稍后重试." + (result.Messages.Length > 0 ? result.Messages[0] : string.Empty)));
                    }
                    return(new ChatMessageCollection());
                }
                else if (result.IsSuccess == false)
                {
                    ThrowError <CustomError>(new CustomError("", result.Messages[0]));
                    return(new ChatMessageCollection());
                }

                ChatMessageCollection messages = new ChatMessageCollection();
                foreach (ChatMessageProxy m in chatMessages)
                {
                    messages.Add(GetChatMessage(m));
                }
                return(messages);
            }
            else
#endif
            {
                if (!AllSettings.Current.ChatSettings.EnableChatFunction)
                {
                    return(new ChatMessageCollection());
                }

                if (operatorID <= 0)
                {
                    ThrowError(new NotLoginError());
                    return(new ChatMessageCollection());
                }

                if (targetUserID <= 0)
                {
                    ThrowError(new UserNotExistsError("targetUserID", targetUserID));
                    return(new ChatMessageCollection());
                }

                if (operatorID == targetUserID)
                {
                    ThrowError(new CustomError("targetUserID", "您不能和自己交谈"));  //临时的
                    return(new ChatMessageCollection());
                }

                if (string.IsNullOrEmpty(contentWithoutEncode) || (contentWithoutEncode = contentWithoutEncode.Trim()) == string.Empty)
                {
                    ThrowError(new EmptyMessageContentError("contentWithoutEncode"));
                    return(new ChatMessageCollection());
                }

                if (contentWithoutEncode.Length > MaxContentLength)
                {
                    ThrowError(new MessageContentLengthError("contentWithoutEncode", contentWithoutEncode, MaxContentLength));
                    return(new ChatMessageCollection());
                }

                if (FriendBO.Instance.InMyBlacklist(targetUserID, operatorID))
                {
                    ThrowError(new InBlackListError());
                    return(new ChatMessageCollection());
                }



                //if (AllSettings.Current.UserPermissionSet.Can(operatorID, UserPermissionSet.Action.UseMessage) == false)
                //{
                //    ThrowError(new NoPermissionUseMessageError());
                //    return false;
                //}
                string content = contentWithoutEncode;
                if (processContent)
                {
                    content = ProcessChatContent(operatorID, content);
                }

                ContentKeywordSettings keywords = AllSettings.Current.ContentKeywordSettings;

                string keyword = null;

                if (keywords.BannedKeywords.IsMatch(content, out keyword))
                {
                    ThrowError(new ChatMessageBannedKeywordsError("content", keyword));
                    return(null);
                }
                ChatMessageCollection newMessages = ChatDao.Instance.SendMessage(operatorID, targetUserID, content, ip, getNewMessages, lastMessageID);

                AuthUser user = UserBO.Instance.GetUserFromCache <AuthUser>(targetUserID);
                if (user != null)
                {
                    user.UnreadMessages++;
                }
                if (OnUsersMessageCountChanged != null)
                {
                    if (user == null)
                    {
                        user = UserBO.Instance.GetAuthUser(targetUserID);
                    }
                    if (user != null)
                    {
                        Dictionary <int, int> counts = new Dictionary <int, int>();
                        counts.Add(user.UserID, user.UnreadMessages);

                        OnUsersMessageCountChanged(counts);
                    }
                }

                ProcessKeyword(newMessages, ProcessKeywordMode.TryUpdateKeyword);

                return(newMessages);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取和某人的聊天记录,如果pageNumber为0则取最后一页
        /// </summary>
        /// <param name="operatorID"></param>
        /// <param name="targetUserID"></param>
        /// <param name="pageNumber">0表示取最后一页</param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        private ChatMessageCollection GetChatMessages(int operatorID, int targetUserID, int pageNumber, int pageSize, bool processKeyword, bool updateIsReaded)
        {
            if (!AllSettings.Current.ChatSettings.EnableChatFunction)
            {
                return(new ChatMessageCollection());
            }

            if (operatorID <= 0)
            {
                ThrowError(new NotLoginError());
                return(new ChatMessageCollection());
            }

            if (targetUserID <= 0)
            {
                return(new ChatMessageCollection());
            }

#if !Passport
            PassportClientConfig settings = Globals.PassportClient;
            if (settings.EnablePassport)
            {
                int totalCount;
                ChatMessageProxy[] messages = settings.PassportService.Chat_GetChatMessages(operatorID, targetUserID, pageNumber, pageSize, processKeyword, updateIsReaded, out totalCount);

                ChatMessageCollection result = new ChatMessageCollection();
                foreach (ChatMessageProxy m in messages)
                {
                    result.Add(GetChatMessage(m));
                }
                result.TotalRecords = totalCount;
                return(result);
            }
            else
#endif
            {
                ChatMessageCollection Messages = ChatDao.Instance.GetChatMessages(operatorID, targetUserID, pageNumber, pageSize, updateIsReaded);

                if (updateIsReaded)
                {
                    if (OnUsersMessageCountChanged != null)
                    {
                        AuthUser user = UserBO.Instance.GetAuthUser(operatorID);
                        if (user != null)
                        {
                            Dictionary <int, int> counts = new Dictionary <int, int>();
                            counts.Add(operatorID, user.UnreadMessages);
                            OnUsersMessageCountChanged(counts);
                        }
                    }
                }

                if (processKeyword)
                {
                    ProcessKeyword(Messages, ProcessKeywordMode.TryUpdateKeyword);
                }
                else
                {
                    ProcessKeyword(Messages, ProcessKeywordMode.FillOriginalText);
                }

                return(Messages);
            }
        }