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\Models;
7
8use Carbon\Carbon;
9
10/**
11 * @property \Illuminate\Database\Eloquent\Collection $builds Build
12 * @property \Illuminate\Database\Eloquent\Collection $changelogEntries ChangelogEntry
13 * @property \Illuminate\Database\Eloquent\Collection $changelogs Changelog
14 * @property string $name
15 * @property string|null $pretty_name
16 * @property string|null $repository
17 * @property int $stream_id
18 */
19class UpdateStream extends Model
20{
21 public $timestamps = false;
22
23 protected $connection = 'mysql-updates';
24 protected $table = 'streams';
25 protected $primaryKey = 'stream_id';
26
27 public function builds()
28 {
29 return $this->hasMany(Build::class);
30 }
31
32 public function changelogs()
33 {
34 return $this->hasMany(Changelog::class);
35 }
36
37 public function changelogEntries()
38 {
39 return $this->hasManyThrough(ChangelogEntry::class, Repository::class);
40 }
41
42 public function scopeWhereHasBuilds($query)
43 {
44 $query->whereHas('builds', function ($q) {
45 $buildInstance = new Build();
46 $table = $buildInstance->getTable();
47 $database = $buildInstance->dbName();
48 $qualifiedTable = "{$database}.{$table}";
49
50 $q->from($qualifiedTable)->default()->whereRaw("{$qualifiedTable}.stream_id = stream_id");
51 });
52 }
53
54 public function createBuild()
55 {
56 $entryIds = model_pluck(
57 $this->changelogEntries()->orphans($this->getKey()),
58 'id',
59 ChangelogEntry::class
60 );
61
62 if (empty($entryIds)) {
63 return;
64 }
65
66 $version = Carbon::now()->format('Y.nd.0');
67 $build = $this->builds()->firstOrCreate(compact('version'));
68 $build->changelogEntries()->attach($entryIds);
69
70 return $build;
71 }
72
73 public function latestBuild()
74 {
75 return $this->builds()->orderBy('build_id', 'DESC')->first();
76 }
77
78 public function userCount()
79 {
80 return (int) $this->builds()->where('allow_bancho', '=', true)->sum('users');
81 }
82
83 public function isFeatured()
84 {
85 return $this->getKey() === $GLOBALS['cfg']['osu']['changelog']['featured_stream'];
86 }
87}