Passed
Push — master ( 2343b7...2a99ce )
by Darko
03:16
created

Release::movieInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\ElasticSearchSiteSearch;
6
use Blacklight\ManticoreSearch;
7
use Blacklight\NZB;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\Relations\HasOne;
14
use Illuminate\Support\Arr;
15
use Illuminate\Support\Facades\Cache;
16
use Illuminate\Support\Facades\DB;
17
18
class Release extends Model
19
{
20
    use HasFactory;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $dateFormat = false;
26
27
    /**
28
     * @var bool
29
     */
30
    public $timestamps = false;
31
32
    /**
33
     * @var array
34
     */
35
    protected $guarded = [];
36
37
    public function group(): BelongsTo
38
    {
39
        return $this->belongsTo(UsenetGroup::class, 'groups_id');
40
    }
41
42
    public function download(): HasMany
43
    {
44
        return $this->hasMany(UserDownload::class, 'releases_id');
45
    }
46
47
    public function userRelease(): HasMany
48
    {
49
        return $this->hasMany(UsersRelease::class, 'releases_id');
50
    }
51
52
    public function file(): HasMany
53
    {
54
        return $this->hasMany(ReleaseFile::class, 'releases_id');
55
    }
56
57
    public function category(): BelongsTo
58
    {
59
        return $this->belongsTo(Category::class, 'categories_id');
60
    }
61
62
    public function predb(): BelongsTo
63
    {
64
        return $this->belongsTo(Predb::class, 'predb_id');
65
    }
66
67
    public function failed(): HasMany
68
    {
69
        return $this->hasMany(DnzbFailure::class, 'release_id');
70
    }
71
72
    public function nfo(): HasOne
73
    {
74
        return $this->hasOne(ReleaseNfo::class, 'releases_id');
75
    }
76
77
    public function comment(): HasMany
78
    {
79
        return $this->hasMany(ReleaseComment::class, 'releases_id');
80
    }
81
82
    public function releaseGroup(): HasMany
83
    {
84
        return $this->hasMany(ReleasesGroups::class, 'releases_id');
85
    }
86
87
    public function video(): BelongsTo
88
    {
89
        return $this->belongsTo(Video::class, 'videos_id');
90
    }
91
92
    public function videoData(): HasOne
93
    {
94
        return $this->hasOne(VideoData::class, 'releases_id');
95
    }
96
97
    public function episode(): BelongsTo
98
    {
99
        return $this->belongsTo(TvEpisode::class, 'tv_episodes_id');
100
    }
101
102
    public function movieInfo(): BelongsTo
103
    {
104
        return $this->belongsTo(MovieInfo::class, 'movieinfo_id');
105
    }
106
107
    /**
108
     * Insert a single release returning the ID on success or false on failure.
109
     *
110
     * @param  array  $parameters  Insert parameters, must be escaped if string.
111
     * @return bool|int
112
     *
113
     * @throws \Exception
114
     */
115
    public static function insertRelease(array $parameters = [])
116
    {
117
        $passwordStatus = config('nntmux_settings.check_passworded_rars') === true ? -1 : 0;
118
        $parameters['id'] = self::query()
119
            ->insertGetId(
120
                [
121
                    'name' => $parameters['name'],
122
                    'searchname' => $parameters['searchname'],
123
                    'totalpart' => $parameters['totalpart'],
124
                    'groups_id' => $parameters['groups_id'],
125
                    'adddate' => now(),
126
                    'guid' => $parameters['guid'],
127
                    'leftguid' => $parameters['guid'][0],
128
                    'postdate' => $parameters['postdate'],
129
                    'fromname' => $parameters['fromname'],
130
                    'size' => $parameters['size'],
131
                    'passwordstatus' => $passwordStatus,
132
                    'haspreview' => -1,
133
                    'categories_id' => $parameters['categories_id'],
134
                    'nfostatus' => -1,
135
                    'nzbstatus' => $parameters['nzbstatus'],
136
                    'isrenamed' => $parameters['isrenamed'],
137
                    'iscategorized' => 1,
138
                    'predb_id' => $parameters['predb_id'],
139
                ]
140
            );
141
142
        if (config('nntmux.elasticsearch_enabled') === true) {
143
            (new ElasticSearchSiteSearch)->insertRelease($parameters);
144
        } else {
145
            (new ManticoreSearch)->insertRelease($parameters);
146
        }
147
148
        return $parameters['id'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parameters['id'] also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type boolean|integer.
Loading history...
149
    }
150
151
    /**
152
     * @throws \Exception
153
     */
154
    public static function updateRelease($id, $name, $searchName, $fromName, $categoryId, $parts, $grabs, $size, $postedDate, $addedDate, $videoId, $episodeId, $imDbId, $aniDbId): void
155
    {
156
        $movieInfoId = null;
157
        if (! empty($imDbId)) {
158
            $movieInfoId = MovieInfo::whereImdbid($imDbId)->first(['id']);
159
        }
160
        self::whereId($id)->update(
161
            [
162
                'name' => $name,
163
                'searchname' => $searchName,
164
                'fromname' => $fromName,
165
                'categories_id' => $categoryId,
166
                'totalpart' => $parts,
167
                'grabs' => $grabs,
168
                'size' => $size,
169
                'postdate' => $postedDate,
170
                'adddate' => $addedDate,
171
                'videos_id' => $videoId,
172
                'tv_episodes_id' => $episodeId,
173
                'imdbid' => $imDbId,
174
                'anidbid' => $aniDbId,
175
                'movieinfo_id' => $movieInfoId !== null ? $movieInfoId->id : $movieInfoId,
176
            ]
177
        );
178
179
        if (config('nntmux.elasticsearch_enabled') === true) {
180
            (new ElasticSearchSiteSearch)->updateRelease($id);
181
        } else {
182
            (new ManticoreSearch)->updateRelease($id);
183
        }
184
    }
185
186
    /**
187
     * @throws \Exception
188
     */
189
    public static function updateGrab(string $guid): void
190
    {
191
        $updateGrabs = ((int) Settings::settingValue('grabstatus') !== 0);
192
        if ($updateGrabs) {
193
            self::whereGuid($guid)->increment('grabs');
194
        }
195
    }
196
197
    /**
198
     * @return Model|null|static
199
     */
200
    public static function getCatByRelId($id)
201
    {
202
        return self::whereId($id)->first(['categories_id']);
203
    }
204
205
    public static function removeVideoIdFromReleases($videoId): int
206
    {
207
        return self::whereVideosId($videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]);
208
    }
209
210
    public static function removeAnidbIdFromReleases($anidbID): int
211
    {
212
        return self::whereAnidbid($anidbID)->update(['anidbid' => -1]);
213
    }
214
215
    /**
216
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
217
     */
218
    public static function getTopDownloads()
219
    {
220
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
221
        $releases = Cache::get(md5('topDownloads'));
222
        if ($releases !== null) {
223
            return $releases;
224
        }
225
        $releases = self::query()
226
            ->where('grabs', '>', 0)
227
            ->select(['id', 'searchname', 'guid', 'adddate'])
228
            ->selectRaw('SUM(grabs) as grabs')
229
            ->groupBy('id', 'searchname', 'adddate')
230
            ->havingRaw('SUM(grabs) > 0')
231
            ->orderByDesc('grabs')
0 ignored issues
show
Bug introduced by
'grabs' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

231
            ->orderByDesc(/** @scrutinizer ignore-type */ 'grabs')
Loading history...
232
            ->limit(10)
233
            ->get();
234
235
        Cache::put(md5('topDownloads'), $releases, $expiresAt);
236
237
        return $releases;
238
    }
239
240
    /**
241
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
242
     */
243
    public static function getTopComments()
244
    {
245
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
246
        $releases = Cache::get(md5('topComments'));
247
        if ($releases !== null) {
248
            return $releases;
249
        }
250
        $releases = self::query()
251
            ->where('comments', '>', 0)
252
            ->select(['id', 'guid', 'searchname'])
253
            ->selectRaw('SUM(comments) AS comments')
254
            ->groupBy('id', 'searchname', 'adddate')
255
            ->havingRaw('SUM(comments) > 0')
256
            ->orderByDesc('comments')
0 ignored issues
show
Bug introduced by
'comments' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
            ->orderByDesc(/** @scrutinizer ignore-type */ 'comments')
Loading history...
257
            ->limit(10)
258
            ->get();
259
260
        Cache::put(md5('topComments'), $releases, $expiresAt);
261
262
        return $releases;
263
    }
264
265
    /**
266
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed
267
     */
268
    public static function getReleases()
269
    {
270
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
271
        $releases = Cache::get(md5('releases'));
272
        if ($releases !== null) {
273
            return $releases;
274
        }
275
276
        $releases = self::query()
277
278
            ->where('nzbstatus', '=', NZB::NZB_ADDED)
279
            ->select(['releases.*', 'g.name as group_name', 'c.title as category_name'])
280
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
281
            ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
282
            ->get();
283
284
        Cache::put(md5('releases'), $releases, $expiresAt);
285
286
        return $releases;
287
    }
288
289
    /**
290
     * Used for admin page release-list.
291
     *
292
     *
293
     * @return LengthAwarePaginator|mixed
294
     */
295
    public static function getReleasesRange()
296
    {
297
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
298
        $releases = Cache::get(md5('releasesRange'));
299
        if ($releases !== null) {
300
            return $releases;
301
        }
302
303
        $releases = self::query()
304
            ->where('nzbstatus', '=', NZB::NZB_ADDED)
305
            ->select(
306
                [
307
                    'releases.id',
308
                    'releases.name',
309
                    'releases.searchname',
310
                    'releases.size',
311
                    'releases.guid',
312
                    'releases.totalpart',
313
                    'releases.postdate',
314
                    'releases.adddate',
315
                    'releases.grabs',
316
                    'cp.title as parent_category',
317
                    'c.title as sub_category',
318
                    DB::raw('CONCAT(cp.title, ' > ', c.title) AS category_name'),
319
                ]
320
            )
321
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
322
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
323
            ->orderByDesc('releases.postdate')
0 ignored issues
show
Bug introduced by
'releases.postdate' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

323
            ->orderByDesc(/** @scrutinizer ignore-type */ 'releases.postdate')
Loading history...
324
            ->paginate(config('nntmux.items_per_page'));
325
326
        Cache::put(md5('releasesRange'), $releases, $expiresAt);
327
328
        return $releases;
329
    }
330
331
    public static function getByGuid($guid)
332
    {
333
        $query = self::with([
334
            'group:id,name',
335
            'category:id,title,root_categories_id',
336
            'category.parent:id,title',
337
            'video:id,title,tvdb,trakt,tvrage,tvmaze,source',
338
            'video.tvInfo:videos_id,summary,image',
339
            'episode:id,title,firstaired,se_complete',
340
        ]);
341
342
        if (is_array($guid)) {
343
            $query->whereIn('guid', $guid);
344
        } else {
345
            $query->where('guid', $guid);
346
        }
347
348
        $releases = $query->get();
349
350
        $releases->each(function ($release) {
351
            $release->group_name = $release->group->name ?? null;
352
            $release->showtitle = $release->video->title ?? null;
353
            $release->tvdb = $release->video->tvdb ?? null;
354
            $release->trakt = $release->video->trakt ?? null;
355
            $release->tvrage = $release->video->tvrage ?? null;
356
            $release->tvmaze = $release->video->tvmaze ?? null;
357
            $release->source = $release->video->source ?? null;
358
            $release->summary = $release->video->tvInfo->summary ?? null;
359
            $release->image = $release->video->tvInfo->image ?? null;
360
            $release->title = $release->episode->title ?? null;
361
            $release->firstaired = $release->episode->firstaired ?? null;
362
            $release->se_complete = $release->episode->se_complete ?? null;
363
            $release->parent_category = $release->category->parent->title ?? null;
364
            $release->sub_category = $release->category->title ?? null;
365
            $release->category_name = $release->parent_category.' > '.$release->sub_category;
366
            $release->category_ids = $release->category->parentid.','.$release->category->id;
367
            $release->group_names = $release->releaseGroup->pluck('group.name')->implode(',');
368
        });
369
370
        return is_array($guid) ? $releases : $releases->first();
371
    }
372
373
    /**
374
     * Get a range of releases. used in admin manage list.
375
     */
376
    public static function getFailedRange(): LengthAwarePaginator
377
    {
378
        $failedList = self::query()
379
            ->select(['name', 'searchname', 'size', 'guid', 'totalpart', 'postdate', 'adddate', 'grabs', 'cp.title as parent_category', 'c.title as sub_category', DB::raw("CONCAT(cp.title, ' > ', c.title) AS category_name")])
380
            ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id')
381
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
382
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
383
            ->orderByDesc('postdate');
0 ignored issues
show
Bug introduced by
'postdate' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

383
            ->orderByDesc(/** @scrutinizer ignore-type */ 'postdate');
Loading history...
384
385
        return $failedList->paginate(config('nntmux.items_per_page'));
386
    }
387
388
    /**
389
     * @return Release|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
390
     */
391
    public static function getAlternate(string $guid, int $userid)
392
    {
393
        $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']);
394
395
        if ($rel === null) {
396
            return false;
397
        }
398
        DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]);
399
400
        preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', $rel['searchname'], $similar);
401
402
        if (! empty($similar)) {
403
            if (config('nntmux.elasticsearch_enabled') === true) {
404
                $searchResult = (new ElasticSearchSiteSearch)->indexSearch($similar[1], 10);
405
            } else {
406
                $searchResult = (new ManticoreSearch)->searchIndexes('releases_rt', $similar[1]);
407
                if (! empty($searchResult)) {
408
                    $searchResult = Arr::wrap(Arr::get($searchResult, 'id'));
409
                }
410
            }
411
412
            if (empty($searchResult)) {
413
                return false;
414
            }
415
416
            return self::query()->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'releases.id')->whereIn('releases.id', $searchResult)->where('df.release_id', '=', null)->where('releases.categories_id', $rel['categories_id'])->where('id', '<>', $rel['id'])->orderByDesc('releases.postdate')->first(['guid']);
0 ignored issues
show
Bug introduced by
'releases.postdate' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

416
            return self::query()->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'releases.id')->whereIn('releases.id', $searchResult)->where('df.release_id', '=', null)->where('releases.categories_id', $rel['categories_id'])->where('id', '<>', $rel['id'])->orderByDesc(/** @scrutinizer ignore-type */ 'releases.postdate')->first(['guid']);
Loading history...
417
        }
418
419
        return false;
420
    }
421
422
    public static function checkGuidForApi($guid): bool
423
    {
424
        $check = self::whereGuid($guid)->first();
425
426
        return $check !== null;
427
    }
428
}
429