<commit_before><?php
/**
 * Minds messenger messages
 */

namespace Minds\Core\Messenger;

use Minds\Core\Di\Di;
use Minds\Core\Session;
use Minds\Entities;
use Minds\Core\Messenger;
use Minds\Core\Security\ACL;

class Messages
{
    private $indexes;
    private $db;
    private $conversation;
    private $participants = [];
    private $acl;

    public function __construct(
        $db = null,
        $indexes = null,
        $acl = null
    )
    {
        $this->db = $db ?: Di::_()->get('Database\Cassandra\Entities');
        $this->indexes = $indexes ?: Di::_()->get('Database\Cassandra\Indexes');
        $this->acl = $acl ?: ACL::_();
    }

    public function setConversation($conversation)
    {
        $this->conversation = $conversation;
        return $this;
    }

    public function getMessages($limit = 12, $offset = "", $finish = "")
    {
        $this->conversation->setGuid(null); //legacy messages get confused here
        $guid = $this->conversation->getGuid();

        $opts = [
          'limit' => $limit,
          'offset'=> $offset,
          'finish'=> $finish,
          'reversed'=> true
        ];

        $messages = $this->indexes->get("object:gathering:conversation:$guid", $opts) ?: [];

        $entities = [];

        foreach ($messages as $guid => $json) {
            $message = json_decode($json, true);
            $entity = new Entities\Message();
            $entity->loadFromArray($message);

            if ($this->acl->read($entity)) {
                $entities[$guid] = $entity;
            }
        }

        return $entities;
    }
}<commit_msg>(fix): very hacky fix due to 18 digits ids ticking over to 10s<commit_after><?php
/**
 * Minds messenger messages
 */

namespace Minds\Core\Messenger;

use Minds\Core\Di\Di;
use Minds\Core\Session;
use Minds\Entities;
use Minds\Core\Messenger;
use Minds\Core\Security\ACL;

class Messages
{
    private $indexes;
    private $db;
    private $conversation;
    private $participants = [];
    private $acl;

    public function __construct(
        $db = null,
        $indexes = null,
        $acl = null
    )
    {
        $this->db = $db ?: Di::_()->get('Database\Cassandra\Entities');
        $this->indexes = $indexes ?: Di::_()->get('Database\Cassandra\Indexes');
        $this->acl = $acl ?: ACL::_();
    }

    public function setConversation($conversation)
    {
        $this->conversation = $conversation;
        return $this;
    }

    public function getMessages($limit = 12, $offset = "", $finish = "", $idFix = false)
    {
        $this->conversation->setGuid(null); //legacy messages get confused here
        $guid = $this->conversation->getGuid();

        $cassandraOffset = $offset;
        if ($cassandraOffset) {
            $idFix = true;
        }

        if (!$idFix) {
            $cassandraOffset = "1900076691505463296";
        }

        $opts = [
          'limit' => $limit,
          'offset'=> $cassandraOffset,
          'finish'=> $finish,
          'reversed'=> true, 
        ];

        $messages = $this->indexes->get("object:gathering:conversation:$guid", $opts) ?: [];

        $entities = [];

        foreach ($messages as $guid => $json) {
            $message = json_decode($json, true);
            $entity = new Entities\Message();
            $entity->loadFromArray($message);

            if ($this->acl->read($entity)) {
                $entities[$guid] = $entity;
            }
        }

        if ((!$idFix || (int) $cassandraOffset > 999999999999999999) && count($messages) < $limit) {
            $olderEntities = $this->getMessages($limit - count($messages), $offset, "", true);
            foreach ($olderEntities as $guid => $entity) {
                $entities[$guid] = $entity;
            }
        }

        return $entities;
    }
}
{'fraction_non_alphanumeric': 0.1599370739381227, 'fraction_numerical': 0.011798636601992658, 'mean_word_length': 2.134757600657354, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2614988', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1149, 'n_words': 239}