. 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; use App\Libraries\Notification\BatchIdentities; use App\Libraries\NotificationsBundle; use App\Models\UserNotification; /** * @group Notification */ class NotificationsController extends Controller { const LIMIT = 51; public function __construct() { parent::__construct(); $this->middleware('auth'); } public function batchDestroy() { UserNotification::batchDestroy( auth()->id(), BatchIdentities::fromParams(request()->all()) ); return response(null, 204); } public function endpoint() { return ['url' => $this->endpointUrl()]; } /** * Get Notifications * * This endpoint returns a list of the user's unread notifications. Sorted descending by `id` with limit of 50. * * --- * * ### Response Format * * Returns an object containing [Notification](#notification) and other related attributes. * * Field | Type * --------------------- | --------------------------------------------------- * has_more | boolean whether or not there are more notifications * notifications | array of [Notification](#notification) * unread_count | total unread notifications * notification_endpoint | url to connect to websocket server * * @queryParam max_id Maximum `id` fetched. Can be used to load earlier notifications. Defaults to no limit (fetch latest notifications) * * @response { * "has_more": true, * "notifications": [ * { * "id": 1, * "name": "forum_topic_reply", * "created_at": "2019-04-24T07:12:43+00:00", * "object_type": "forum_topic", * "object_id": 1, * "source_user_id": 1, * "is_read": false, * "details": { * "title": "A topic", * "post_id": 2, * "username": "User", * "cover_url": "https://..." * } * } * ], * "unread_count": 100, * "notification_endpoint": "wss://notify.ppy.sh" * } */ public function index() { $bundle = new NotificationsBundle(auth()->user(), request()->all()); $bundleJson = $bundle->toArray(); if (is_json_request()) { $bundleJson['notification_endpoint'] = $this->endpointUrl(); return response($bundleJson)->header('Cache-Control', 'no-store'); } return ext_view('notifications.index', compact('bundleJson')); } /** * Mark Notifications as Read * * This endpoint allows you to mark notifications read. * * --- * * ### Response Format * * _empty response_ * * @bodyParam identities[].category string Notification category. Example: beatmapset_state * @bodyParam identities[].object_id string Id of the object triggered the notification. Example: 1 * @bodyParam identities[].object_type string Type of the object triggered the notification. Example: beatmapset * @bodyParam notifications[].category string Notification category. Example: beatmapset_state * @bodyParam notifications[].id integer Id of notifications to be marked as read. Example: 1 * @bodyParam notifications[].object_id string Id of the object triggered the notification. Example: 1 * @bodyParam notifications[].object_type string Type of the object triggered the notification. Example: beatmapset * * @response 204 */ public function markRead() { UserNotification::batchMarkAsRead( auth()->user(), BatchIdentities::fromParams(request()->all()) ); return response(null, 204); } private function endpointUrl() { $url = $GLOBALS['cfg']['osu']['notification']['endpoint']; if (($url[0] ?? null) === '/') { $host = request()->getHttpHost(); $protocol = request()->secure() ? 'wss' : 'ws'; $url = "{$protocol}://{$host}{$url}"; } return $url; } }