<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers\Chat;

use App\Libraries\Chat;
use App\Models\Chat\Channel;
use App\Models\Chat\Message;
use App\Models\Chat\UserChannel;
use App\Models\User;
use Auth;

/**
 * @group Chat
 */
class ChatController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');

        return parent::__construct();
    }

    /**
     * Get Updates
     *
     * This endpoint returns new messages since the given `message_id` along with updated channel 'presence' data.
     *
     * ---
     *
     * ### Response Format
     *
     * Field            | Type
     * ---------------- | -----------------
     * presence         | array of [ChatChannel](#chatchannel)
     * messages         | array of [ChatMessage](#chatmessage)
     *
     * <aside class="notice">
     *   Note that this returns messages for all channels the user has joined.
     * </aside>
     *
     * @authenticated
     *
     * @queryParam since required The `message_id` of the last message to retrieve messages since
     * @queryParam channel_id If provided, will only return messages for the given channel
     * @queryParam limit number of messages to return (max of 50)
     *
     * @response {
     *   "presence": [
     *     {
     *       "channel_id": 5,
     *       "name": "#osu",
     *       "description": "The official osu! channel (english only).",
     *       "type": "public",
     *       "last_read_id": 9150005005,
     *       "last_message_id": 9150005005
     *     },
     *     {
     *       "channel_id": 12345,
     *       "type": "PM",
     *       "name": "peppy",
     *       "icon": "https://a.ppy.sh/2?1519081077.png",
     *       "users": [
     *         2,
     *         102
     *       ],
     *       "last_read_id": 9150001235,
     *       "last_message_id": 9150001234
     *     }
     *   ],
     *   "messages": [
     *     {
     *       "message_id": 9150005004,
     *       "sender_id": 2,
     *       "channel_id": 5,
     *       "timestamp": "2018-07-06T06:33:34+00:00",
     *       "content": "i am a lazerface",
     *       "is_action": 0,
     *       "sender": {
     *         "id": 2,
     *         "username": "peppy",
     *         "profile_colour": "#3366FF",
     *         "avatar_url": "https://a.ppy.sh/2?1519081077.png",
     *         "country_code": "AU",
     *         "is_active": true,
     *         "is_bot": false,
     *         "is_online": true,
     *         "is_supporter": true
     *       }
     *     },
     *     {
     *       "message_id": 9150005005,
     *       "sender_id": 102,
     *       "channel_id": 5,
     *       "timestamp": "2018-07-06T06:33:42+00:00",
     *       "content": "uh ok then",
     *       "is_action": 0,
     *       "sender": {
     *         "id": 102,
     *         "username": "nekodex",
     *         "profile_colour": "#333333",
     *         "avatar_url": "https://a.ppy.sh/102?1500537068",
     *         "country_code": "AU",
     *         "is_active": true,
     *         "is_bot": false,
     *         "is_online": true,
     *         "is_supporter": true
     *       }
     *     }
     *   ]
     * }
     */
    public function updates()
    {
        $params = request()->all();

        if (!present($params['since'] ?? null)) {
            abort(422);
        }

        $presence = self::presence();

        $since = $params['since'];
        $limit = clamp(get_int($params['limit'] ?? null) ?? 50, 1, 50);

        // this is used to filter out messages from restricted users/etc
        $channelIds = array_map(function ($e) {
            return $e['channel_id'];
        }, $presence);

        $messages = Message::forUser(Auth::user())
            ->with('sender')
            ->whereIn('channel_id', $channelIds)
            ->since($since)
            ->limit($limit);

        if (present($params['channel_id'] ?? null)) {
            $messages->where('channel_id', get_int($params['channel_id']));
        }

        $messages = $messages->get()->reverse();

        if ($messages->isEmpty()) {
            return response([], 204);
        }

        $response = [
            'presence' => $presence,
            'messages' => json_collection(
                $messages,
                'Chat\Message',
                ['sender']
            ),
        ];

        return $response;
    }

    /**
     * @group Undocumented
     */
    public function presence()
    {
        return UserChannel::presenceForUser(Auth::user());
    }<commit_msg>Return straight away<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers\Chat;

use App\Libraries\Chat;
use App\Models\Chat\Channel;
use App\Models\Chat\Message;
use App\Models\Chat\UserChannel;
use App\Models\User;
use Auth;

/**
 * @group Chat
 */
class ChatController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');

        return parent::__construct();
    }

    /**
     * Get Updates
     *
     * This endpoint returns new messages since the given `message_id` along with updated channel 'presence' data.
     *
     * ---
     *
     * ### Response Format
     *
     * Field            | Type
     * ---------------- | -----------------
     * presence         | array of [ChatChannel](#chatchannel)
     * messages         | array of [ChatMessage](#chatmessage)
     *
     * <aside class="notice">
     *   Note that this returns messages for all channels the user has joined.
     * </aside>
     *
     * @authenticated
     *
     * @queryParam since required The `message_id` of the last message to retrieve messages since
     * @queryParam channel_id If provided, will only return messages for the given channel
     * @queryParam limit number of messages to return (max of 50)
     *
     * @response {
     *   "presence": [
     *     {
     *       "channel_id": 5,
     *       "name": "#osu",
     *       "description": "The official osu! channel (english only).",
     *       "type": "public",
     *       "last_read_id": 9150005005,
     *       "last_message_id": 9150005005
     *     },
     *     {
     *       "channel_id": 12345,
     *       "type": "PM",
     *       "name": "peppy",
     *       "icon": "https://a.ppy.sh/2?1519081077.png",
     *       "users": [
     *         2,
     *         102
     *       ],
     *       "last_read_id": 9150001235,
     *       "last_message_id": 9150001234
     *     }
     *   ],
     *   "messages": [
     *     {
     *       "message_id": 9150005004,
     *       "sender_id": 2,
     *       "channel_id": 5,
     *       "timestamp": "2018-07-06T06:33:34+00:00",
     *       "content": "i am a lazerface",
     *       "is_action": 0,
     *       "sender": {
     *         "id": 2,
     *         "username": "peppy",
     *         "profile_colour": "#3366FF",
     *         "avatar_url": "https://a.ppy.sh/2?1519081077.png",
     *         "country_code": "AU",
     *         "is_active": true,
     *         "is_bot": false,
     *         "is_online": true,
     *         "is_supporter": true
     *       }
     *     },
     *     {
     *       "message_id": 9150005005,
     *       "sender_id": 102,
     *       "channel_id": 5,
     *       "timestamp": "2018-07-06T06:33:42+00:00",
     *       "content": "uh ok then",
     *       "is_action": 0,
     *       "sender": {
     *         "id": 102,
     *         "username": "nekodex",
     *         "profile_colour": "#333333",
     *         "avatar_url": "https://a.ppy.sh/102?1500537068",
     *         "country_code": "AU",
     *         "is_active": true,
     *         "is_bot": false,
     *         "is_online": true,
     *         "is_supporter": true
     *       }
     *     }
     *   ]
     * }
     */
    public function updates()
    {
        $params = request()->all();

        if (!present($params['since'] ?? null)) {
            abort(422);
        }

        $presence = self::presence();

        $since = $params['since'];
        $limit = clamp(get_int($params['limit'] ?? null) ?? 50, 1, 50);

        // this is used to filter out messages from restricted users/etc
        $channelIds = array_map(function ($e) {
            return $e['channel_id'];
        }, $presence);

        $messages = Message::forUser(Auth::user())
            ->with('sender')
            ->whereIn('channel_id', $channelIds)
            ->since($since)
            ->limit($limit);

        if (present($params['channel_id'] ?? null)) {
            $messages->where('channel_id', get_int($params['channel_id']));
        }

        $messages = $messages->get()->reverse();

        if ($messages->isEmpty()) {
            return response([], 204);
        }

        return [
            'presence' => $presence,
            'messages' => json_collection(
                $messages,
                'Chat\Message',
                ['sender']
            ),
        ];
    }

    /**
     * @group Undocumented
     */
    public function presence()
    {
        return UserChannel::presenceForUser(Auth::user());
    }
{'fraction_non_alphanumeric': 0.15674246430460073, 'fraction_numerical': 0.03765203595980963, 'mean_word_length': 1.5772690106295995, 'pattern_counts': {'":': 94, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 6, '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': '3510376', 'n_tokens_mistral': 3176, 'n_tokens_neox': 2895, 'n_words': 614}