Example #1
0
        async void _channel_OnDataReceived(object sender, JArray rawdata)
        {
            //Parse channel array and call the appropriate events.
            if (rawdata[0].ToString() == "noop")
            {
                // set active client if more than 120 sec of inactivity
                if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null)
                {
                    SetActiveClientAsync();
                    _last_response_date = DateTime.UtcNow;
                }
            }
            else if (rawdata[0].ToString() == "resync")
            {
                SyncAllNewEventsAsync(long.Parse(rawdata[1].ToString()));
            }
            else if (rawdata[0]["p"] != null)
            {
                JObject wrapper = JObject.Parse(rawdata[0]["p"].ToString());
                if (wrapper["3"] != null && wrapper["3"]["2"] != null)
                {
                    _client_id     = wrapper["3"]["2"].ToString();
                    _requestHeader = null;

                    _channel.SendAck(0);

                    if (_channel.Connected && !_wasConnected)
                    {
                        _wasConnected = _channel.Connected;
                        if (ConnectionEstablished != null)
                        {
                            ConnectionEstablished(this, null);
                        }

                        // check if contacts are loaded
                        if (_contacts == null || _contacts.Count == 0)
                        {
                            if (ContactListLoaded != null)
                            {
                                ContactListLoaded(this, _contacts.Values.ToList());
                            }

                            string[] participants_id = _active_conversations.Values.SelectMany(c => c._conversation.current_participant.Where(p => p.gaia_id != CurrentUser.Id).Select(p => p.gaia_id)).Distinct().ToArray();
                            await GetEntityByIdAsync(participants_id);

                            if (ConversationHistoryLoaded != null)
                            {
                                ConversationHistoryLoaded(this, _active_conversations.Values.ToList());
                            }
                        }
                    }
                }

                if (wrapper["2"] != null && wrapper["2"]["2"] != null)
                {
                    JArray cbu = JArray.Parse(wrapper["2"]["2"].ToString());
                    cbu.RemoveAt(0);
                    BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as JArray);

                    foreach (StateUpdate state_update in batchUpdate.state_update)
                    {
                        if (state_update.event_notification != null)
                        {
                            switch (state_update.event_notification.current_event.event_type)
                            {
                            case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE:
                            case EventType.EVENT_TYPE_UNKNOWN:
                                if (state_update.event_notification.current_event.conversation_id == null)
                                {
                                    break;
                                }

                                if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id))
                                {
                                    _active_conversations[state_update.event_notification.current_event.conversation_id.id].AddNewMessage(state_update.event_notification.current_event);
                                    if (NewMessageReceived != null)
                                    {
                                        NewMessageReceived(this, _active_conversations[state_update.event_notification.current_event.conversation_id.id]);
                                    }
                                }
                                else
                                {
                                    ConversationState s = new ConversationState()
                                    {
                                        conversation_id = state_update.event_notification.current_event.conversation_id,
                                        conversation    = state_update.conversation,
                                        events          = new List <Event>()
                                        {
                                            state_update.event_notification.current_event
                                        }
                                    };

                                    _active_conversations.Add(s.conversation_id.id, new Model.Conversation(s));
                                    if (NewConversationCreated != null)
                                    {
                                        NewConversationCreated(this, _active_conversations.Last().Value);
                                    }
                                }
                                break;

                            case EventType.EVENT_TYPE_OTR_MODIFICATION:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id]._conversation.otr_status = state_update.event_notification.current_event.otr_status;
                                break;

                            case EventType.EVENT_TYPE_CONVERSATION_RENAME:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id]._conversation.name = state_update.event_notification.current_event.conversation_rename.new_name;
                                break;
                            }
                        }


                        if (state_update.presence_notification != null)
                        {
                            foreach (var presence in state_update.presence_notification.presence)
                            {
                                if (_contacts.ContainsKey(presence.user_id.gaia_id))
                                {
                                    setPresence(_contacts[presence.user_id.gaia_id], presence.presence);
                                }
                            }
                        }


                        if (state_update.self_presence_notification != null)
                        {
                            CurrentUser.SetPresence(new Presence()
                            {
                                available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE
                            }
                                                    );
                        }

                        if (state_update.watermark_notification != null)
                        {
                            if (state_update.watermark_notification.sender_id.gaia_id == CurrentUser.Id)
                            {
                                _active_conversations[state_update.watermark_notification.conversation_id.id].SelfReadState = state_update.watermark_notification.latest_read_timestamp.FromUnixTime();
                            }
                            else
                            {
                                _active_conversations[state_update.watermark_notification.conversation_id.id].ReadState = state_update.watermark_notification.latest_read_timestamp.FromUnixTime();
                            }
                        }
                    }

                    _timestamp = long.Parse(wrapper["1"]["4"].ToString());
                }
            }
        }
Example #2
0
        async void _channel_OnDataReceived(object sender, DynamicJson rawdata)
        {
            //Parse channel array and call the appropriate events.
            if (rawdata[0].ToString() == "noop")
            {
                // set active client if more than 120 sec of inactivity
                if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null)
                {
                    SetActiveClientAsync();
                    _last_response_date = DateTime.UtcNow;
                }
            }
            else if (rawdata[0].ToString() == "resync") // internal handling
            {
                SyncAllNewEventsAsync(long.Parse(rawdata[1].ToString()));
            }
            else if (rawdata[0]["p"] != null)
            {
                DynamicJson wrapper = new DynamicJson(rawdata[0]["p"].ToString());
                if (wrapper["4"] != null && wrapper["4"]["2"] != null)
                {
                    if (_channel.Connected && !_wasConnected)
                    {
                        _client_id     = wrapper["4"]["2"].ToString();
                        _requestHeader = null;
                        _channel.SendAck(0);

                        _wasConnected = _channel.Connected;

                        // load self info
                        await GetSelfInfoAsync();

                        // load conversations if not loaded
                        await SyncRecentConversationsAsync();

                        if (OnConnectionEstablished != null)
                        {
                            OnConnectionEstablished(this, null);
                        }
                    }
                }

                if (wrapper["2"] != null && wrapper["2"]["2"] != null)
                {
                    DynamicJson cbu = new DynamicJson(wrapper["2"]["2"].ToString());
                    cbu.RemoveAt(0);
                    BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as DynamicJson);

                    foreach (StateUpdate state_update in batchUpdate.state_update)
                    {
                        if (state_update.event_notification != null)
                        {
                            switch (state_update.event_notification.current_event.event_type)
                            {
                            case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE:
                            case EventType.EVENT_TYPE_UNKNOWN:
                                if (state_update.event_notification.current_event.conversation_id == null)
                                {
                                    break;
                                }

                                if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id))
                                {
                                    _active_conversations[state_update.event_notification.current_event.conversation_id.id].events.Add(state_update.event_notification.current_event);
                                    if (OnNewMessageReceived != null)
                                    {
                                        OnNewMessageReceived(this, state_update.event_notification.current_event);
                                    }
                                }
                                else
                                {
                                    // get conversation
                                    await GetConversationAsync(state_update.event_notification.current_event.conversation_id.id);

                                    //ConversationState s = new ConversationState()
                                    //{
                                    //    conversation_id = state_update.event_notification.current_event.conversation_id,
                                    //    conversation = state_update.conversation,
                                    //    events = new List<Event>() { state_update.event_notification.current_event }
                                    //};

                                    //_active_conversations.Add(s.conversation_id.id, new Model.Conversation(s));
                                    //if (NewConversationCreated != null)
                                    //    NewConversationCreated(this, _active_conversations.Last().Value);
                                }
                                break;

                            case EventType.EVENT_TYPE_OTR_MODIFICATION:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.otr_status = state_update.event_notification.current_event.otr_status;
                                break;

                            case EventType.EVENT_TYPE_CONVERSATION_RENAME:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.name = state_update.event_notification.current_event.conversation_rename.new_name;
                                break;
                            }
                        }

                        if (state_update.presence_notification != null)
                        {
                            foreach (var presence in state_update.presence_notification.presence)
                            {
                                if (_contacts.ContainsKey(presence.user_id.gaia_id))
                                {
                                    _contacts[presence.user_id.gaia_id].presence = presence.presence;
                                }
                            }
                        }


                        if (state_update.self_presence_notification != null)
                        {
                            CurrentUser.presence = new Presence()
                            {
                                available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE
                            }
                        }
                        ;


                        if (state_update.watermark_notification != null)
                        {
                            if (state_update.watermark_notification.sender_id.gaia_id == CurrentUser.id.gaia_id)
                            {
                                _active_conversations[state_update.watermark_notification.conversation_id.id].conversation.self_conversation_state.self_read_state.latest_read_timestamp = state_update.watermark_notification.latest_read_timestamp;
                            }
                            //else
                            //    _active_conversations[state_update.watermark_notification.conversation_id.id].read_state. = state_update.watermark_notification.latest_read_timestamp.FromUnixTime();
                        }
                    }

                    _timestamp = long.Parse(wrapper["1"]["4"].ToString());
                }
            }
        }
Example #3
0
        void _channel_OnDataReceived(object sender, JArray rawdata)
        {
            //Parse channel array and call the appropriate events.
            if (rawdata[0].ToString() == "noop")
            {
                // set active client if more than 120 sec of inactivity
                if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null)
                {
                    SetActiveClient();
                    _last_response_date = DateTime.UtcNow;
                }
            }
            else if (rawdata[0]["p"] != null)
            {
                JObject wrapper = JObject.Parse(rawdata[0]["p"].ToString());
                if (wrapper["3"] != null && wrapper["3"]["2"] != null)
                {
                    _client_id     = wrapper["3"]["2"].ToString();
                    _requestHeader = null;

                    _channel.SendAck(0);

                    if (_channel.Connected && !_wasConnected)
                    {
                        _wasConnected = _channel.Connected;
                        if (ConnectionEstablished != null)
                        {
                            ConnectionEstablished(this, null);
                        }
                    }
                }

                if (wrapper["2"] != null && wrapper["2"]["2"] != null)
                {
                    JArray cbu = JArray.Parse(wrapper["2"]["2"].ToString());
                    cbu.RemoveAt(0);
                    BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as JArray);

                    foreach (StateUpdate state_update in batchUpdate.state_update)
                    {
                        if (state_update.event_notification != null)
                        {
                            switch (state_update.event_notification.current_event.event_type)
                            {
                            case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE:
                                if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id))
                                {
                                    _active_conversations[state_update.event_notification.current_event.conversation_id.id].NewEventReceived(this, state_update.event_notification.current_event);
                                }
                                else
                                {
                                    ConversationState s = new ConversationState()
                                    {
                                        conversation_id = state_update.event_notification.current_event.conversation_id,
                                        conversation    = state_update.conversation,
                                        events          = new List <Event>()
                                        {
                                            state_update.event_notification.current_event
                                        }
                                    };

                                    _active_conversations.Add(s.conversation_id.id, new Model.Conversation(s));
                                    if (NewConversationCreated != null)
                                    {
                                        NewConversationCreated(this, _active_conversations.Last().Value);
                                    }
                                }
                                break;

                            case EventType.EVENT_TYPE_OTR_MODIFICATION:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id]._conversation.otr_status = state_update.event_notification.current_event.otr_status;
                                break;
                            }
                        }


                        if (state_update.presence_notification != null)
                        {
                            foreach (var presence in state_update.presence_notification.presence)
                            {
                                if (_contacts.ContainsKey(presence.user_id.chat_id))
                                {
                                    _contacts[presence.user_id.chat_id].SetPresence(presence.presence);
                                }
                            }
                        }


                        if (state_update.self_presence_notification != null)
                        {
                            CurrentUser.SetPresence(new Presence()
                            {
                                available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE
                            }
                                                    );
                        }
                    }

                    this._timestamp = long.Parse(wrapper["1"]["4"].ToString());
                }
            }
        }