<commit_before><?php

namespace Moxl\Xec\Payload;

use Moxl\Xec\Action\Vcard\Get;

use Movim\Session;
use App\Presence as DBPresence;
use App\PresenceBuffer;

class Presence extends Payload
{
    public function handle($stanza, $parent = false)
    {
        // Subscribe request
        if ((string)$stanza->attributes()->type == 'subscribe') {
            $session = Session::start();
            $notifs = $session->get('activenotifs');
            $notifs[(string)$stanza->attributes()->from] = 'sub';
            $session->set('activenotifs', $notifs);

            $this->event('subscribe', (string)$stanza->attributes()->from);
        } elseif((string)$stanza->attributes()->type === 'error'
            && isset($stanza->attributes()->id)) {
            // Let's drop errors with an id, useless for us
        } else {
            $presence = DBPresence::findByStanza($stanza);
            $presence->set($stanza);

            $refreshable = $presence->refreshable;
            if ($refreshable) {
                $r = new Get;
                $r->setAvatarhash($presence->avatarhash)
                  ->setTo((string)$refreshable)
                  ->request();
            }

            PresenceBuffer::getInstance()->append($presence, function () use ($presence, $stanza) {
                if ($presence->muc
                && isset($stanza->x)) {
                    foreach ($stanza->x as $x) {
                        if ($x->attributes()->xmlns == 'http://jabber.org/protocol/muc#user'
                        && isset($stanza->x->status)
                        && \in_array((int)$stanza->x->status->attributes()->code, [110, 332, 307, 301])) {
                            if ($presence->value != 5 && $presence->value != 6) {
                                $this->method('muc_handle');
                                $this->pack($presence);
                            } elseif ($presence->value == 5) {
                                $this->method('unavailable_handle');
                                $this->pack($presence);
                            }

                            $this->deliver();
                            break;
                        }
                    }
                } else {
                    $this->pack($presence->roster);

                    if ($presence->value == 5 /*|| $p->value == 6*/) {
                        $presence->delete();
                    }
                }

                $this->deliver();
            });
        }
    }
}
<commit_msg>Add a workaround for Spectrum2 multi-presences<commit_after><?php

namespace Moxl\Xec\Payload;

use Moxl\Xec\Action\Vcard\Get;

use Movim\Session;
use App\Presence as DBPresence;
use App\PresenceBuffer;

class Presence extends Payload
{
    public function handle($stanza, $parent = false)
    {
        // Subscribe request
        if ((string)$stanza->attributes()->type == 'subscribe') {
            $session = Session::start();
            $notifs = $session->get('activenotifs');
            $notifs[(string)$stanza->attributes()->from] = 'sub';
            $session->set('activenotifs', $notifs);

            $this->event('subscribe', (string)$stanza->attributes()->from);
        } elseif((string)$stanza->attributes()->type === 'error'
            && isset($stanza->attributes()->id)) {
            // Let's drop errors with an id, useless for us
        } else {
            $presence = DBPresence::findByStanza($stanza);
            $presence->set($stanza);

            $refreshable = $presence->refreshable;
            if ($refreshable) {
                $r = new Get;
                $r->setAvatarhash($presence->avatarhash)
                  ->setTo((string)$refreshable)
                  ->request();
            }

            PresenceBuffer::getInstance()->append($presence, function () use ($presence, $stanza) {
                if ($presence->muc
                && isset($stanza->x)) {
                    foreach ($stanza->x as $x) {
                        if ($x->attributes()->xmlns == 'http://jabber.org/protocol/muc#user'
                        && isset($stanza->x->status)
                        && \in_array((int)$stanza->x->status->attributes()->code, [110, 332, 307, 301])) {
                            // Spectrum2 specific bug, we can receive two self-presences, one with several caps items
                            $cCount = 0;
                            foreach ($stanza->children() as $key => $content) {
                                if ($key == 'c') $cCount++;
                            }

                            if ($cCount > 1) {
                                $presence->delete();
                                break;
                            }
                            // So we drop it

                            \Utils::debug('CONNECTED 2 '.$presence->jid. ' '.$stanza->x->status->attributes()->code);
                            \Utils::debug($stanza->asXML());
                            if ($presence->value != 5 && $presence->value != 6) {
                                $this->method('muc_handle');
                                $this->pack($presence);
                            } elseif ($presence->value == 5) {
                                $this->method('unavailable_handle');
                                $this->pack($presence);
                            }

                            $this->deliver();
                            break;
                        }
                    }
                } else {
                    $this->pack($presence->roster);

                    if ($presence->value == 5 /*|| $p->value == 6*/) {
                        $presence->delete();
                    }
                }

                $this->deliver();
            });
        }
    }
}

{'fraction_non_alphanumeric': 0.15069671426113881, 'fraction_numerical': 0.006709100292447962, 'mean_word_length': 1.2310053722179586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708322', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1460, 'n_words': 257}