<commit_before><?php
namespace Minds\Core\Matrix;

use Minds\Api\Exportable;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\User;
use Minds\Exceptions\UserErrorException;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequest;

/**
 * Matrix Controller
 * @package Minds\Core\Matrix
 */
class Controller
{
    /** @var Manager */
    protected $manager;

    /** @var EntitiesBuilder */
    protected $entitiesBuilder;

    public function __construct(Manager $manager = null, EntitiesBuilder $entitiesBuilder = null)
    {
        $this->manager = $manager ?? new Manager();
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getAccount(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        $matrixAccount = $this->manager->getAccountByUser($user);

        $this->manager->syncAccount($user);

        return new JsonResponse([
           'status' => 'success',
           'account' => $matrixAccount->export(),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getRooms(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        $joinedRooms = $this->manager->getJoinedRooms($user);
        return new JsonResponse([
           'status' => 'success',
           'rooms' => Exportable::_($joinedRooms),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getTotalUnread(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        $joinedRooms = $this->manager->getJoinedRooms($user);

        $sum = 0;

        foreach ($joinedRooms as $room) {
            $sum += $room->getUnreadCount();
        }

        return new JsonResponse([
           'status' => 'success',
           'total_unread' => $sum,
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function createDirectRoom(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');

        $receiverGuid = $request->getAttribute('parameters')['receiverGuid'] ?? null;

        $receiver = $this->entitiesBuilder->single($receiverGuid);

        if (!$receiver || !$receiver instanceof User) {
            throw new UserErrorException('User not found');
        }

        $newRoom = $this->manager->createDirectRoom($user, $receiver);
        return new JsonResponse([
           'status' => 'success',
           'room' => $newRoom->export(),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getRawState(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        return  new JsonResponse([
            'status' => 'success',
            'state' => $this->manager->getState($user)]);
    }
}
<commit_msg>(fix): do not log exception if user has not rooms or not setup on messenger<commit_after><?php
namespace Minds\Core\Matrix;

use GuzzleHttp\Exception\ClientException;
use Minds\Api\Exportable;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\User;
use Minds\Exceptions\UserErrorException;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequest;

/**
 * Matrix Controller
 * @package Minds\Core\Matrix
 */
class Controller
{
    /** @var Manager */
    protected $manager;

    /** @var EntitiesBuilder */
    protected $entitiesBuilder;

    public function __construct(Manager $manager = null, EntitiesBuilder $entitiesBuilder = null)
    {
        $this->manager = $manager ?? new Manager();
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getAccount(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        $matrixAccount = $this->manager->getAccountByUser($user);

        $this->manager->syncAccount($user);

        return new JsonResponse([
           'status' => 'success',
           'account' => $matrixAccount->export(),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getRooms(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        $joinedRooms = $this->manager->getJoinedRooms($user);
        return new JsonResponse([
           'status' => 'success',
           'rooms' => Exportable::_($joinedRooms),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getTotalUnread(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');

        try {
            $joinedRooms = $this->manager->getJoinedRooms($user);

            $sum = 0;

            foreach ($joinedRooms as $room) {
                $sum += $room->getUnreadCount();
            }
        } catch (ClientException $e) {
            $sum = 0;
        }

        return new JsonResponse([
           'status' => 'success',
           'total_unread' => $sum,
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function createDirectRoom(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');

        $receiverGuid = $request->getAttribute('parameters')['receiverGuid'] ?? null;

        $receiver = $this->entitiesBuilder->single($receiverGuid);

        if (!$receiver || !$receiver instanceof User) {
            throw new UserErrorException('User not found');
        }

        $newRoom = $this->manager->createDirectRoom($user, $receiver);
        return new JsonResponse([
           'status' => 'success',
           'room' => $newRoom->export(),
        ]);
    }

    /**
     * @param ServerRequest $request
     * @return JsonResponse
     */
    public function getRawState(ServerRequest $request): JsonResponse
    {
        $user = $request->getAttribute('_user');
        return  new JsonResponse([
            'status' => 'success',
            'state' => $this->manager->getState($user)]);
    }
}

{'fraction_non_alphanumeric': 0.14126738794435859, 'fraction_numerical': 0.0004636785162287481, 'mean_word_length': 2.4959481361426255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '5869213', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1803, 'n_words': 370}