<commit_before><?php

class Feed extends WidgetCommon {
    function WidgetLoad()
    {
        $this->addcss('feed.css');
        $this->addjs('feed.js');
        $this->registerEvent('post', 'onStream');
        $this->registerEvent('postdeleted', 'onPostDelete');
        $this->registerEvent('postdeleteerror', 'onPostDeleteError');
        
        $this->registerEvent('comment', 'onComment');
        $this->registerEvent('nocomment', 'onNoComment');
        $this->registerEvent('nocommentstream', 'onNoCommentStream');
        
		$this->registerEvent('stream', 'onStream');
        $this->registerEvent('postpublished', 'onPostPublished');
        $this->registerEvent('postpublisherror', 'onPostPublishError');
        
        $this->registerEvent('nodecreated', 'onNodeCreated');
        $this->registerEvent('nodecreationerror', 'onNodeCreationError');
        
        $this->registerEvent('config', 'onConfig');

        $this->cached = false;
    }
    
    function onConfig(array $data)
    {
        $this->user->setConfig($data);
        RPC::call('movim_fill', 'feedhead', RPC::cdata($this->prepareHead()));
    }
    
    function onPost($id) {
        $query = Post::query()
                            ->where(array('key' => $this->user->getLogin(), 'nodeid' => $id));
        $post = Post::run_query($query);

        if($post != false) {  
            $html = $this->preparePost($post[0]);
            RPC::call('movim_prepend', 'feedcontent', RPC::cdata($html));
        }
    }
    
    function onNodeCreated() {
        $config = $this->user->getConfig();
        $config['feed'] = 'created';
        
        $s = new moxl\StorageSet();
        $s->setXmlns('movim:prefs')
          ->setData(serialize($config))
          ->request();
    }
    
    function onNodeCreationError() {
        $config = $this->user->getConfig();
        $config['feed'] = 'error';
        
        $s = new moxl\StorageSet();
        $s->setXmlns('movim:prefs')
          ->setData(serialize($config))
          ->request();
        
        $html .=
            '<div class="message error">'.
                t("Your server doesn't support post publication, you can only read contact's feeds").'
             </div>';
        RPC::call('movim_fill', 'feednotifs', RPC::cdata($html));
        RPC::commit();
    }

    function onPostPublished($post) {        
        $query = Post::query()
                            ->join('Contact', array('Post.jid' => 'Contact.jid'))
                            ->where(
                                array(
                                    'Post`.`nodeid' => $post->nodeid->getval()))
                            ->limit(0, 1);
        $messages = Post::run_query($query);

        // We ask for the HTML of all the posts
        $html = $this->preparePosts($messages);

        RPC::call('createCommentNode', $post->nodeid->getval());            
        RPC::call('movim_prepend', 'feedcontent', RPC::cdata($html));
    }  
    
    function ajaxCreateCommentNode($parentid) {
        $n = new moxl\MicroblogCommentCreateNode();
        $n->setTo($this->user->getLogin())
          ->setParentId($parentid)
          ->request();
    }
    
    function onPostPublishError($error) {
        $html .=
            '<div class="message error">'.t('An error occured : ').$error.'</div>';
        RPC::call('movim_fill', 'feednotifs', RPC::cdata($html));
    }
    
    function prepareHead() {
        $html = '';
        
        global $session;

        if($session['config']['config'] == false) {
            $html .= 
                '<div class="message warning" style="margin: 1.5em;">'.
                    t("Your server doesn't support post publication, you can only read contact's feeds").
                '</div>';
        } elseif(!isset($session['config']['feed'])) {
            $html .= '
                <div id="feednotifs">
                    <div class="message info">'.
                     t("Creating your feed...").
                    '</div>
                </div>
                <script type="text/javascript">'.
                        $this->genCallAjax('ajaxCreateNode').
                '</script>';
        } else {
            $html .= '
                <script type="text/javascript">
                    function createCommentNode(parentid) {'.
                        $this->genCallAjax('ajaxCreateCommentNode', 'parentid[0]').
                '}
                </script>
                <table id="feedsubmitform">
                    <tbody>
                        <tr>
                            <td>
                                <textarea 
                                    placeholder="'.t("What's new ?").'" 
                                    id="feedmessagecontent" 
                                    class="steditor"
                                    onkeyup="movim_textarea_autoheight(this);"></textarea>
                            </td>
                        </tr>
                        
                        <script type="text/javascript">
                            var ste = new SimpleTextEditor("feedmessagecontent", "ste");
                            ste.init();
                        </script>
                        
                        <tr id="feedsubmitrow">
                            <td>
                                <a 
                                    title="Plus"
                                    href="#" 
                                    onclick="frameHeight(this);"
                                    style="float: left;"
                                    class="button tiny icon add merged left">'.t("Size").'
                                </a>
                                <a 
                                    title="Rich"
                                    href="#" 
                                    onclick="richText(this);"
                                    style="float: left;"
                                    class="button tiny icon yes merged right">'.t("Rich Text").'
                                </a>
                                <a 
                                    title="'.t("Submit").'"
                                    href="#" 
                                    id="feedmessagesubmit" 
                                    onclick="ste.submit();'.$this->genCallAjax('ajaxPublishItem', 'getFeedMessage()').'; ste.clearContent();"
                                    class="button tiny icon submit">'.t("Submit").'
                                </a>
                            </td>
                        </tr>
                    </tbody>
                </table>



                <div id="feednotifs"></div>';
        }
        
        return $html;
    }
    
    function prepareFeed($start) {
        $query = RosterLink::query()->where(
                                        array(
                                            'RosterLink`.`key' => $this->user->getLogin(),
                                            array(
                                                'RosterLink`.`rostersubscription!' => 'none',
                                                'RosterLink`.`rostersubscription!' => '',
                                                'RosterLink`.`rostersubscription!' => 'vcard',
                                                '|RosterLink`.`rosterask' => 'subscribe')))
                                     ->orderby('RosterLink.group', true);

        $contacts = RosterLink::run_query($query);
        
        $rosterc = array();
        
        //foreach($contactsq as $c)
        //    array_push($rosterc, $c->getData('jid'));
            
        // We create the array for the comments request
        $commentid = array();
        $i = 0;
            
        foreach($contacts as $c) {
            if($i == 0)
                array_push($rosterc, $c->getData('jid'));

            else
                array_push($rosterc, '|'.$c->getData('jid'));
            $i++;
        }
        
        // We query the last messages
        $query = Post::query()
                            ->join('Contact', array('Post.jid' => 'Contact.jid'))
                            ->where(
                                array(
                                    'Post`.`parentid' => '',
                                    'Post`.`key' => $this->user->getLogin(),
                                    array('Post`.`jid' => $rosterc)))
                            ->orderby('Post.updated', true)
                            ->limit($start+1, '10');
        $messages = Post::run_query($query);

        // We ask for the HTML of all the posts
        $html = $this->preparePosts($messages);
        
        $next = $start + 10;
            
        if(sizeof($messages) > 9 && $html != '') {
            $html .= '
                <div class="post">
                    <div 
                        class="older" 
                        onclick="'.$this->genCallAjax('ajaxGetFeed', "'".$next."'").'; this.parentNode.style.display = \'none\'">'.
                            t('Get older posts').'
                    </div>
                </div>';
        }
        return $html;
    }

    function ajaxGetFeed($start) {
        RPC::call('movim_append', 'feedcontent', RPC::cdata($this->prepareFeed($start)));
        RPC::commit();
    }
        
    function onStream($payload) {
        $html = '';
        $html = $this->prepareFeed(0);
        <commit_msg>- Fix an error on account first login<commit_after><?php

class Feed extends WidgetCommon {
    function WidgetLoad()
    {
        $this->addcss('feed.css');
        $this->addjs('feed.js');
        $this->registerEvent('post', 'onStream');
        $this->registerEvent('postdeleted', 'onPostDelete');
        $this->registerEvent('postdeleteerror', 'onPostDeleteError');
        
        $this->registerEvent('comment', 'onComment');
        $this->registerEvent('nocomment', 'onNoComment');
        $this->registerEvent('nocommentstream', 'onNoCommentStream');
        
		$this->registerEvent('stream', 'onStream');
        $this->registerEvent('postpublished', 'onPostPublished');
        $this->registerEvent('postpublisherror', 'onPostPublishError');
        
        $this->registerEvent('nodecreated', 'onNodeCreated');
        $this->registerEvent('nodecreationerror', 'onNodeCreationError');
        
        $this->registerEvent('config', 'onConfig');

        $this->cached = false;
    }
    
    function onConfig(array $data)
    {
        $this->user->setConfig($data);
        RPC::call('movim_fill', 'feedhead', RPC::cdata($this->prepareHead()));
    }
    
    function onPost($id) {
        $query = Post::query()
                            ->where(array('key' => $this->user->getLogin(), 'nodeid' => $id));
        $post = Post::run_query($query);

        if($post != false) {  
            $html = $this->preparePost($post[0]);
            RPC::call('movim_prepend', 'feedcontent', RPC::cdata($html));
        }
    }
    
    function onNodeCreated() {
        $config = $this->user->getConfig();
        $config['feed'] = 'created';
        
        $s = new moxl\StorageSet();
        $s->setXmlns('movim:prefs')
          ->setData(serialize($config))
          ->request();
    }
    
    function onNodeCreationError() {
        $config = $this->user->getConfig();
        $config['feed'] = 'error';
        
        $s = new moxl\StorageSet();
        $s->setXmlns('movim:prefs')
          ->setData(serialize($config))
          ->request();
        
        $html .=
            '<div class="message error">'.
                t("Your server doesn't support post publication, you can only read contact's feeds").'
             </div>';
        RPC::call('movim_fill', 'feednotifs', RPC::cdata($html));
        RPC::commit();
    }

    function onPostPublished($post) {        
        $query = Post::query()
                            ->join('Contact', array('Post.jid' => 'Contact.jid'))
                            ->where(
                                array(
                                    'Post`.`nodeid' => $post->nodeid->getval()))
                            ->limit(0, 1);
        $messages = Post::run_query($query);

        // We ask for the HTML of all the posts
        $html = $this->preparePosts($messages);

        RPC::call('createCommentNode', $post->nodeid->getval());            
        RPC::call('movim_prepend', 'feedcontent', RPC::cdata($html));
    }  
    
    function ajaxCreateCommentNode($parentid) {
        $n = new moxl\MicroblogCommentCreateNode();
        $n->setTo($this->user->getLogin())
          ->setParentId($parentid)
          ->request();
    }
    
    function onPostPublishError($error) {
        $html .=
            '<div class="message error">'.t('An error occured : ').$error.'</div>';
        RPC::call('movim_fill', 'feednotifs', RPC::cdata($html));
    }
    
    function prepareHead() {
        $html = '';
        
        global $session;

        if($session['config']['config'] == false) {
            $html .= 
                '<div class="message warning" style="margin: 1.5em;">'.
                    t("Your server doesn't support post publication, you can only read contact's feeds").
                '</div>';
        } elseif(!isset($session['config']['feed'])) {
            $html .= '
                <div id="feednotifs">
                    <div class="message info">'.
                     t("Creating your feed...").
                    '</div>
                </div>
                <script type="text/javascript">'.
                        $this->genCallAjax('ajaxCreateNode').
                '</script>';
        } else {
            $html .= '
                <script type="text/javascript">
                    function createCommentNode(parentid) {'.
                        $this->genCallAjax('ajaxCreateCommentNode', 'parentid[0]').
                '}
                </script>
                <table id="feedsubmitform">
                    <tbody>
                        <tr>
                            <td>
                                <textarea 
                                    placeholder="'.t("What's new ?").'" 
                                    id="feedmessagecontent" 
                                    class="steditor"
                                    onkeyup="movim_textarea_autoheight(this);"></textarea>
                            </td>
                        </tr>
                        
                        <script type="text/javascript">
                            var ste = new SimpleTextEditor("feedmessagecontent", "ste");
                            ste.init();
                        </script>
                        
                        <tr id="feedsubmitrow">
                            <td>
                                <a 
                                    title="Plus"
                                    href="#" 
                                    onclick="frameHeight(this);"
                                    style="float: left;"
                                    class="button tiny icon add merged left">'.t("Size").'
                                </a>
                                <a 
                                    title="Rich"
                                    href="#" 
                                    onclick="richText(this);"
                                    style="float: left;"
                                    class="button tiny icon yes merged right">'.t("Rich Text").'
                                </a>
                                <a 
                                    title="'.t("Submit").'"
                                    href="#" 
                                    id="feedmessagesubmit" 
                                    onclick="ste.submit();'.$this->genCallAjax('ajaxPublishItem', 'getFeedMessage()').'; ste.clearContent();"
                                    class="button tiny icon submit">'.t("Submit").'
                                </a>
                            </td>
                        </tr>
                    </tbody>
                </table>



                <div id="feednotifs"></div>';
        }
        
        return $html;
    }
    
    function prepareFeed($start) {
        $query = RosterLink::query()->where(
                                        array(
                                            'RosterLink`.`key' => $this->user->getLogin(),
                                            array(
                                                'RosterLink`.`rostersubscription!' => 'none',
                                                'RosterLink`.`rostersubscription!' => '',
                                                'RosterLink`.`rostersubscription!' => 'vcard',
                                                '|RosterLink`.`rosterask' => 'subscribe')))
                                     ->orderby('RosterLink.group', true);

        $contacts = RosterLink::run_query($query);
        
        $rosterc = array();
        
        //foreach($contactsq as $c)
        //    array_push($rosterc, $c->getData('jid'));
            
        // We create the array for the comments request
        $commentid = array();
        $i = 0;
            
        foreach($contacts as $c) {
            if($i == 0)
                array_push($rosterc, $c->getData('jid'));

            else
                array_push($rosterc, '|'.$c->getData('jid'));
            $i++;
        }
        
        if(empty($rosterc))
            $where = array(
                        'Post`.`parentid' => '',
                        'Post`.`key' => $this->user->getLogin());
        else
            $where = array(
                        'Post`.`parentid' => '',
                        'Post`.`key' => $this->user->getLogin(),
                        array('Post`.`jid' => $rosterc));
        
        // We query the last messages
        $query = Post::query()
                            ->join('Contact', array('Post.jid' => 'Contact.jid'))
                            ->where($where)
                            ->orderby('Post.updated', true)
                            ->limit($start+1, '10');
        $messages = Post::run_query($query);

        // We ask for the HTML of all the posts
        $html = $this->preparePosts($messages);
        
        $next = $start + 10;
            
        if(sizeof($messages) > 9 && $html != '') {
            $html .= '
                <div class="post">
                    <div 
                        class="older" 
                        onclick="'.$this->genCallAjax('ajaxGetFeed', "'".$next."'").'; this.parentNode.style.display = \'none\'">'.
                            t('Get older posts').'
                    </div>
                </div>';
        }
        return $html;
    }

    function ajaxGetFeed($start) {
        RPC::call('movim_append', 'feedcontent', RPC::cdata($this->prepareFeed($start)));
        RPC::commit();
    }
        
    function onStream($payload) {
        $html = '';
        $html = $this->prepareFeed(0);
        
{'fraction_non_alphanumeric': 0.14798813179948989, 'fraction_numerical': 0.0015616053302795274, 'mean_word_length': 1.145393634840871, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 249, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 73, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086900', 'n_tokens_mistral': 4898, 'n_tokens_neox': 4650, 'n_words': 803}