1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Http\Controllers;
7
8use App\Libraries\Chat;
9use App\Libraries\UserChannelList;
10use App\Models\Chat\Channel;
11use App\Models\Chat\Message;
12use App\Models\User;
13
14class ChatController extends Controller
15{
16 public function __construct()
17 {
18 $this->middleware('auth');
19
20 // TODO: notification server and chat client needs some updating
21 // to handle verification_requirement_change properly.
22 if ($GLOBALS['cfg']['osu']['user']['post_action_verification']) {
23 $this->middleware('verify-user');
24 }
25
26 parent::__construct();
27 }
28
29 public function index()
30 {
31 $user = auth()->user();
32 Chat::ack($user);
33
34 $params = get_params(request()->all(), null, [
35 'channel_id:int',
36 'sendto:int',
37 ], ['null_missing' => true]);
38
39 // rejoin any existing channel first, so it will be in the user channel list.
40 if ($params['sendto'] !== null) {
41 $targetUser = User::lookup($params['sendto'], 'id');
42 if ($targetUser !== null) {
43 $channel = Channel::findPM($targetUser, $user);
44 $channel?->addUser($user);
45
46 $sendToJson = [
47 'can_message_error' => ($channel?->checkCanMessage($user) ?? priv_check('ChatPmStart', $targetUser))->message(),
48 'channel_id' => $channel?->getKey(),
49 'target' => json_item($targetUser, 'UserCompact'),
50 ];
51 }
52 } elseif ($params['channel_id'] !== null) {
53 // This is only for rejoining / unhiding channels the user is already in.
54 $channel = Channel::find($params['channel_id']);
55 $channel?->unhide($user);
56 }
57
58 $json = [
59 'current_user_attributes' => [
60 'can_chat_announce' => priv_check('ChatAnnounce')->can(),
61 ],
62 'last_message_id' => optional(Message::last())->getKey(),
63 'presence' => (new UserChannelList($user))->get(),
64 ];
65
66 if (isset($sendToJson)) {
67 $json['send_to'] = $sendToJson;
68 }
69
70 return ext_view('chat.index', compact('json'));
71 }
72}