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
6declare(strict_types=1);
7
8namespace App\Libraries\BeatmapsetDiscussion;
9
10use App\Jobs\Notifications\BeatmapsetDiscussionPostNew;
11use App\Libraries\BeatmapsetDiscussion\Traits\HandlesProblem;
12use App\Models\BeatmapDiscussion;
13use App\Models\Beatmapset;
14use App\Models\User;
15
16class Discussion
17{
18 use HandlesProblem;
19
20 private BeatmapDiscussion $discussion;
21
22 public function __construct(private User $user, private Beatmapset $beatmapset, array $discussionParams, private ?string $message)
23 {
24 $this->discussion = $beatmapset->beatmapDiscussions()->make($discussionParams);
25 $this->discussion->beatmapset()->associate($beatmapset);
26 $this->discussion->user()->associate($user);
27
28 priv_check_user($user, 'BeatmapsetDiscussionNew', $this->discussion)->ensureCan();
29
30 $this->maybeSetProblemDiscussion($this->discussion);
31 }
32
33 public function handle(): array
34 {
35 $newPost = $this->discussion->getConnection()->transaction(function () {
36 $this->discussion->saveOrExplode();
37
38 $post = $this->discussion->beatmapDiscussionPosts()->make(['message' => $this->message]);
39 $post->beatmapDiscussion()->associate($this->discussion);
40 $post->user()->associate($this->user);
41 $post->saveOrExplode();
42
43 $this->handleProblemDiscussion();
44
45 return $post;
46 });
47
48 // TODO: make transactional
49 (new BeatmapsetDiscussionPostNew($newPost, $this->user))->dispatch();
50
51 return [$this->discussion, [$newPost]];
52 }
53}