Exemplo n.º 1
0
        private void Poll()
        {
            var jObject = new JObject();

            jObject["chat_token"] = Token;
            jObject["usernames"]  = JArray.FromObject(AccountData.Users.Where(x => x.Update).Select(x => x.Name).ToArray());
            jObject["after"]      = LastPollTime + 0.005;

            try
            {
                var response = Requester.PostRequest(Methods.ChatHistory, jObject);
                var obj      = JsonConvert.DeserializeObject(response) as JObject;

                var chatHistory = new ChatHistory();
                var chatObject  = obj["chats"] as JObject;
                foreach (var kvp in chatObject)
                {
                    foreach (JObject msgObject in kvp.Value as JArray)
                    {
                        var channel = string.Empty;
                        if (msgObject["channel"] != null)
                        {
                            channel = msgObject["channel"].ToString();
                        }

                        if (msgObject["is_join"] != null)
                        {
                            var presence = new Presence(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                bool.Parse(msgObject["is_join"].ToString())
                                );
                            chatHistory.Push(presence);
                        }
                        else
                        {
                            var message = new Message(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                msgObject["msg"].ToString()
                                );
                            chatHistory.Push(message);
                        }
                    }
                }

                if (!chatHistory.IsEmpty)
                {
                    LastPollTime = chatHistory.Last.Timestamp;
                }

                while (!chatHistory.IsEmpty)
                {
                    var element = chatHistory.Pop();

                    if (element is Presence)
                    {
                        var pres    = element as Presence;
                        var channel = AccountData.FindUser(pres.To).FindChannel(pres.Channel);

                        if (channel != null)
                        {
                            if (!pres.Joined)
                            {
                                if (channel.MemberExists(pres.From))
                                {
                                    channel.Members.Remove(pres.From);
                                }
                            }
                            else
                            {
                                if (!channel.MemberExists(pres.From))
                                {
                                    channel.Members.Add(pres.From);
                                }
                            }
                        }
                        PresenceReceived?.Invoke(this, new PresenceEventArgs(pres));
                        continue;
                    }
                    else if (element is Message)
                    {
                        if (element.From != element.To)
                        {
                            if (!RelayedIDs.Contains(element.ID))
                            {
                                RelayedIDs.Add(element.ID);
                                MessageReceived?.Invoke(this, new MessageEventArgs(element as Message));
                            }
                        }
                        else
                        {
                            if (!RelayedIDs.Contains(element.ID))
                            {
                                RelayedIDs.Add(element.ID);
                                MessageSent?.Invoke(this, new MessageEventArgs(element as Message));
                            }
                        }


                        if (RelayedIDs.Count > 100)
                        {
                            RelayedIDs.Clear();
                        }
                    }
                }
            }
            catch (RequestException e)
            {
                ErrorOccured?.Invoke(this, new ErrorEventArgs(e));
            }
        }
Exemplo n.º 2
0
        public void RequestMessageHistory()
        {
            if (!IsAuthenticated)
            {
                throw new InvalidOperationException("Not authenticated. Request/set token first.");
            }

            if (AccountData == null)
            {
                throw new InvalidOperationException("No account data requested. Request account data first.");
            }

            var jObject = new JObject();

            jObject["chat_token"] = Token;
            jObject["usernames"]  = JArray.FromObject(AccountData.Users.Where(x => x.Update).Select(x => x.Name).ToArray());

            try
            {
                var response = Requester.PostRequest(Methods.ChatHistory, jObject);
                var obj      = JsonConvert.DeserializeObject(response) as JObject;

                var chatHistory = new ChatHistory();

                var chatObject = obj["chats"] as JObject;
                foreach (var kvp in chatObject)
                {
                    foreach (JObject msgObject in kvp.Value as JArray)
                    {
                        var channel = string.Empty;
                        if (msgObject["channel"] != null)
                        {
                            channel = msgObject["channel"].ToString();
                        }

                        if (msgObject["is_join"] != null)
                        {
                            var presence = new Presence(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                bool.Parse(msgObject["is_join"].ToString())
                                );
                            chatHistory.Push(presence);
                        }
                        else
                        {
                            var message = new Message(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                msgObject["msg"].ToString()
                                );
                            chatHistory.Push(message);
                        }
                    }
                }
                LastPollTime = chatHistory.Last.Timestamp;
                ChatHistoryReceived?.Invoke(this, new ChatHistoryEventArgs(chatHistory));
            }
            catch (RequestException e)
            {
                ErrorOccured?.Invoke(this, new ErrorEventArgs(e));
            }
        }