Exemplo n.º 1
0
 public PresenceManager(XmppClientEx xmppClient, ICaps clientCaps, IMapper mapper)
 {
     this.clientCaps = clientCaps;
     this.xmppClient = xmppClient;
     this.mapper     = mapper;
 }
Exemplo n.º 2
0
        public XmppSession(
            XmppClientEx xmppClient,
            IMapper mapper,
            MyViewModel myViewModel,
            Contacts contacts,
            IPresenceManager presenceManager,
            IAvatarStorage avatarStorage,
            IUserAvatarStorage userAvatarStorage)
        {
            this.xmppClient        = xmppClient;
            this.mapper            = mapper;
            this.viewModel         = myViewModel;
            this.contacts          = contacts;
            this.presenceManager   = presenceManager;
            this.avatarStorage     = avatarStorage;
            this.userAvatarStorage = userAvatarStorage;

            xmppClient.XmppSessionStateObserver.Subscribe(async state =>
            {
                if (state == SessionState.Disconnected && SessionState != SessionState.Disconnected)
                {
                    //Task.Run(() => DoReconnectAsync(Connect, TimeSpan.FromSeconds(30), 100));
                }

                await SetSessionState(state);

                System.Diagnostics.Debug.WriteLine(state);
            });

            xmppClient
            .XmppXElementStreamObserver
            .Where(el =>
                   el.OfType <MatrixPresence>() &&
                   el.Cast <MatrixPresence>().Type == MatrixPresenceType.Available &&
                   contacts.Any(c => c.Jid == el.Cast <MatrixPresence>().From.Bare)
                   )
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(el =>
            {
                // received online presence from a contact
                System.Diagnostics.Debug.WriteLine($"online presence from: {el.Cast<MatrixPresence>().From}");
                var pres     = el.Cast <MatrixPresence>();
                var contact  = contacts.FirstOrDefault(ct => ct.Jid == pres.From.Bare);
                var viewPres = mapper.Map <ViewModel.Presence>(pres);
                contact.Presences.AddOrReplace(viewPres, p => p.Resource == viewPres.Resource);
            });


            xmppClient
            .XmppXElementStreamObserver
            .Where(el =>
                   el.OfType <MatrixPresence>() &&
                   el.Cast <MatrixPresence>().Type == MatrixPresenceType.Unavailable &&
                   el.Cast <MatrixPresence>().From != null &&
                   el.Cast <MatrixPresence>().From.Resource != null &&
                   contacts.Any(c => c.Jid == el.Cast <MatrixPresence>().From.Bare)
                   )
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(el =>
            {
                // received offline presence from a contact
                System.Diagnostics.Debug.WriteLine($"offline presence from: {el.Cast<MatrixPresence>().From}");
                var pres    = el.Cast <MatrixPresence>();
                var contact = contacts.FirstOrDefault(ct => ct.Jid == pres.From.Bare);

                contact.Presences.Remove(p => p.Resource == pres.From.Resource);
            });


            xmppClient
            .XmppXElementStreamObserver
            .Where(el =>
                   el.OfType <MatrixMessage>() &&
                   el.Cast <MatrixMessage>().Type == MatriXMessageType.Chat &&
                   el.Cast <MatrixMessage>().From != null &&
                   el.Cast <MatrixMessage>().Body != null
                   )
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(el =>
            {
                var msg = el.Cast <MatrixMessage>();
                Contact contact;

                // handle incoming chat message
                if (!viewModel.Chats.Any(c => c.Jid == msg.From.Bare))
                {
                    if (contacts.Contains(msg.From.Bare))
                    {
                        contact = contacts[msg.From.Bare];
                    }
                    else
                    {
                        contact = new Contact(msg.From.Bare);
                    }
                    viewModel.AddChatMenu(contact, false);
                }
                else
                {
                    contact = contacts[msg.From.Bare];
                }

                contact.LastMessageResource = msg.From.Resource;
                var viewMessage             = mapper.Map <Message>(msg);
                viewMessage.Nickname        = contact.Name;
                viewMessage.IsSelf          = false;

                if (msg.Id != null)
                {
                    viewMessage.Id = msg.Id;
                }

                viewModel.GetChatMenu(msg.From.Bare).Messages.Add(viewMessage);
            });


            /*
             * <presence>
             *  <show>away</show>
             *  <status>Kann nicht chatten. Muss arbeiten.</status>
             *  <priority>40</priority>
             *  <c xmlns="http://jabber.org/protocol/caps" ver="r75qsgpbcZtkumlpddBcZZCeDco=" hash="sha-1" node="http://psi-im.org"/>
             *  <x xmlns="vcard-temp:x:update">
             *      <photo>9b33b369b378be852b8487e23105dd4bd880a0f0</photo>
             *  </x>
             *  </presence>
             *
             */
        }