Browse Source

Add method to rank beatmapset

pull/4481/head
nanaya 3 weeks ago
parent
commit
59ea647f98

+ 19
- 5
app/Jobs/BroadcastNotification.php View File

@@ -50,7 +50,7 @@ class BroadcastNotification implements ShouldQueue
50 50
             ->all();
51 51
     }
52 52
 
53
-    public function __construct($name, $object, $source)
53
+    public function __construct($name, $object, $source = null)
54 54
     {
55 55
         $this->name = $name;
56 56
         $this->object = $object;
@@ -69,14 +69,16 @@ class BroadcastNotification implements ShouldQueue
69 69
 
70 70
         $this->notifiable = $this->notifiable ?? $this->object;
71 71
         $this->params['name'] = $this->name;
72
-        $this->params['details']['username'] = $this->source->username;
72
+        if ($this->source !== null) {
73
+            $this->params['details']['username'] = $this->source->username;
74
+        }
73 75
 
74 76
         if (is_array($this->receiverIds)) {
75 77
             switch (count($this->receiverIds)) {
76 78
                 case 0:
77 79
                     return;
78 80
                 case 1:
79
-                    if ($this->receiverIds[0] === $this->source->getKey()) {
81
+                    if ($this->receiverIds[0] === optional($this->source)->getKey()) {
80 82
                         return;
81 83
                     }
82 84
             }
@@ -84,7 +86,9 @@ class BroadcastNotification implements ShouldQueue
84 86
 
85 87
         $notification = new Notification($this->params);
86 88
         $notification->notifiable()->associate($this->notifiable);
87
-        $notification->source()->associate($this->source);
89
+        if ($this->source !== null) {
90
+            $notification->source()->associate($this->source);
91
+        }
88 92
 
89 93
         $notification->save();
90 94
 
@@ -95,7 +99,7 @@ class BroadcastNotification implements ShouldQueue
95 99
                 $receivers = User::whereIn('user_id', $this->receiverIds)->get();
96 100
 
97 101
                 foreach ($receivers as $receiver) {
98
-                    if ($receiver->getKey() !== $this->source->getKey()) {
102
+                    if ($receiver->getKey() !== optional($this->source)->getKey()) {
99 103
                         $notification->userNotifications()->create(['user_id' => $receiver->getKey()]);
100 104
                     }
101 105
                 }
@@ -177,6 +181,16 @@ class BroadcastNotification implements ShouldQueue
177 181
         ];
178 182
     }
179 183
 
184
+    private function onBeatmapsetRank()
185
+    {
186
+        $this->receiverIds = static::beatmapsetReceiverIds($this->object);
187
+
188
+        $this->params['details'] = [
189
+            'title' => $this->object->title,
190
+            'cover_url' => $this->object->coverURL('card'),
191
+        ];
192
+    }
193
+
180 194
     private function onBeatmapsetResetNominations()
181 195
     {
182 196
         $this->receiverIds = static::beatmapsetReceiverIds($this->object);

+ 27
- 1
app/Models/Beatmapset.php View File

@@ -552,7 +552,9 @@ class Beatmapset extends Model implements AfterCommit
552 552
 
553 553
         if ($this->approved > 0) {
554 554
             $this->approved_date = $currentTime;
555
-            $this->approvedby_id = $user->user_id;
555
+            if ($user !== null) {
556
+                $this->approvedby_id = $user->user_id;
557
+            }
556 558
         } else {
557 559
             $this->approved_date = null;
558 560
             $this->approvedby_id = null;
@@ -700,6 +702,30 @@ class Beatmapset extends Model implements AfterCommit
700 702
         ];
701 703
     }
702 704
 
705
+    public function rank()
706
+    {
707
+        if (!$this->isQualified()) {
708
+            return false;
709
+        }
710
+
711
+        DB::transaction(function () {
712
+            $this->events()->create(['type' => BeatmapsetEvent::QUALIFY]);
713
+
714
+            $this->setApproved('ranked', null);
715
+
716
+            // global event
717
+            Event::generate('beatmapsetApprove', ['beatmapset' => $this]);
718
+
719
+            // enqueue a cover check job to ensure cover images are all present
720
+            $job = (new CheckBeatmapsetCovers($this))->onQueue('beatmap_high');
721
+            dispatch($job);
722
+
723
+            broadcast_notification(Notification::BEATMAPSET_RANK, $this);
724
+        });
725
+
726
+        return true;
727
+    }
728
+
703 729
     public function favourite($user)
704 730
     {
705 731
         DB::transaction(function () use ($user) {

+ 1
- 0
app/Models/Notification.php View File

@@ -31,6 +31,7 @@ class Notification extends Model
31 31
     const BEATMAPSET_LOVE = 'beatmapset_love';
32 32
     const BEATMAPSET_NOMINATE = 'beatmapset_nominate';
33 33
     const BEATMAPSET_QUALIFY = 'beatmapset_qualify';
34
+    const BEATMAPSET_RANK = 'beatmapset_rank';
34 35
     const BEATMAPSET_RESET_NOMINATIONS = 'beatmapset_reset_nominations';
35 36
     const CHANNEL_MESSAGE = 'channel_message';
36 37
     const FORUM_TOPIC_REPLY = 'forum_topic_reply';

+ 1
- 0
resources/assets/lib/notification-maps/category.ts View File

@@ -28,6 +28,7 @@ export const nameToCategory: CategoryMap = {
28 28
   beatmapset_love: 'beatmapset_state',
29 29
   beatmapset_nominate: 'beatmapset_state',
30 30
   beatmapset_qualify: 'beatmapset_state',
31
+  beatmapset_rank: 'beatmapset_state',
31 32
   beatmapset_reset_nominations: 'beatmapset_state',
32 33
   channel_message: 'channel',
33 34
   forum_topic_reply: 'forum_topic_reply',

+ 2
- 0
resources/assets/lib/notification-maps/icons.ts View File

@@ -36,6 +36,7 @@ export const nameToIcons: IconsMap = {
36 36
   beatmapset_love: ['fas fa-drafting-compass', 'fas fa-heart'],
37 37
   beatmapset_nominate: ['fas fa-drafting-compass', 'fas fa-vote-yea'],
38 38
   beatmapset_qualify: ['fas fa-drafting-compass', 'fas fa-check'],
39
+  beatmapset_rank: ['fas fa-drafting-compass', 'fas fa-check-double'],
39 40
   beatmapset_reset_nominations: ['fas fa-drafting-compass', 'fas fa-undo'],
40 41
   channel_message: ['fas fa-comments'],
41 42
   forum_topic_reply: ['fas fa-comment-medical'],
@@ -50,6 +51,7 @@ export const nameToIconsCompact: IconsMap = {
50 51
   beatmapset_love: ['fas fa-heart'],
51 52
   beatmapset_nominate: ['fas fa-vote-yea'],
52 53
   beatmapset_qualify: ['fas fa-check'],
54
+  beatmapset_rank: ['fas fa-check-double'],
53 55
   beatmapset_reset_nominations: ['fas fa-undo'],
54 56
   channel_message: ['fas fa-comments'],
55 57
   forum_topic_reply: ['fas fa-comment-medical'],

+ 2
- 0
resources/assets/lib/notification-maps/url.ts View File

@@ -50,6 +50,8 @@ export function urlSingular(item: Notification) {
50 50
         beatmapsetId: item.objectId,
51 51
         discussionId: item.details.discussionId,
52 52
       });
53
+    case 'beatmapset_rank':
54
+      return laroute.route('beatmapsets.show', { beatmapset: item.objectId });
53 55
     case 'channel_message':
54 56
       return laroute.route('chat.index', { sendto: item.sourceUserId });
55 57
     case 'forum_topic_reply':

+ 2
- 0
resources/lang/en/notifications.php View File

@@ -46,6 +46,8 @@ return [
46 46
                 'beatmapset_nominate_compact' => 'Beatmap was nominated',
47 47
                 'beatmapset_qualify' => '":title" has gained enough nominations and entered the ranking queue',
48 48
                 'beatmapset_qualify_compact' => 'Beatmap entered ranking queue',
49
+                'beatmapset_rank' => '":title" has been ranked',
50
+                'beatmapset_rank_compact' => 'Beatmap was ranked',
49 51
                 'beatmapset_reset_nominations' => 'Nomination of ":title" has been reset',
50 52
                 'beatmapset_reset_nominations_compact' => 'Nomination was reset',
51 53
             ],

+ 19
- 0
tests/Models/BeatmapsetTest.php View File

@@ -120,6 +120,25 @@ class BeatmapsetTest extends TestCase
120 120
         priv_check_user($nominator, 'BeatmapsetNominate', $beatmapset)->ensureCan();
121 121
     }
122 122
 
123
+    public function testRank()
124
+    {
125
+        $beatmapset = $this->createBeatmapset([
126
+            'approved' => Beatmapset::STATES['qualified'],
127
+        ]);
128
+
129
+        $notifications = Notification::count();
130
+        $userNotifications = UserNotification::count();
131
+
132
+        $otherUser = factory(User::class)->create();
133
+        $beatmapset->watches()->create(['user_id' => $otherUser->getKey()]);
134
+
135
+        $beatmapset->rank();
136
+
137
+        $this->assertTrue($beatmapset->fresh()->isRanked());
138
+        $this->assertSame($notifications + 1, UserNotification::count());
139
+        $this->assertSame($notifications + 1, Notification::count());
140
+    }
141
+
123 142
     private function createBeatmapset($params = []) : Beatmapset
124 143
     {
125 144
         $defaultParams = [

Loading…
Cancel
Save