-
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathGenerate.php
More file actions
128 lines (117 loc) · 3.99 KB
/
Generate.php
File metadata and controls
128 lines (117 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2026 LycheeOrg.
*/
namespace App\Actions\RSS;
use App\Constants\PhotoAlbum as PA;
use App\Contracts\Exceptions\InternalLycheeException;
use App\Enum\SizeVariantType;
use App\Exceptions\Internal\FrameworkException;
use App\Models\Extensions\UTCBasedTimes;
use App\Models\Photo;
use App\Policies\AlbumPolicy;
use App\Policies\PhotoQueryPolicy;
use App\Repositories\ConfigManager;
use App\Services\UrlGenerator;
use Carbon\Exceptions\InvalidFormatException;
use Carbon\Exceptions\UnitException;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Spatie\Feed\FeedItem;
/**
* @template T of object{id:string,title:string,description:?string,type:string,created_at:string,updated_at:string,album_id:string,album_title:string,short_path:string,filesize:int,storage_disk:string,size_variant_type:string,username:string}
*/
class Generate
{
use UTCBasedTimes;
public function __construct(
protected PhotoQueryPolicy $photo_query_policy,
protected readonly ConfigManager $config_manager,
protected readonly UrlGenerator $url_generator,
) {
}
/**
* @param T $data
*
* @return FeedItem
*
* @throws BindingResolutionException
*/
private function toFeedItem(object $data): FeedItem
{
$page_link = route('gallery', ['albumId' => $data->album_id, 'photoId' => $data->id]);
$feed_item = [
'id' => $page_link,
'title' => $data->title,
'summary' => Markdown::convert($data->description ?? '')->getContent(),
'updated' => $this->asDateTime($data->updated_at),
'link' => $page_link,
'enclosure' => $this->url_generator->pathToUrl($data->short_path, $data->storage_disk, SizeVariantType::ORIGINAL),
'enclosureType' => $data->type,
'enclosureLength' => $data->filesize,
'authorName' => ($data->display_name !== null && $data->display_name !== '')
? $data->display_name
: $data->username,
'category' => [$data->album_title],
];
return FeedItem::create($feed_item);
}
/**
* @return Collection<int,FeedItem>
*
* @throws InternalLycheeException
*/
public function do(): Collection
{
$user = Auth::user();
$unlocked_album_ids = AlbumPolicy::getUnlockedAlbumIDs();
$rss_recent = $this->config_manager->getValueAsInt('rss_recent_days');
$rss_max = $this->config_manager->getValueAsInt('rss_max_items');
try {
$now_minus = Carbon::now()->subDays($rss_recent)->toDateTimeString();
} catch (UnitException|InvalidFormatException $e) {
throw new FrameworkException('Date/Time component (Carbon)', $e);
}
/** @var Collection<int,T> $photos */
$photos = $this->photo_query_policy
->applySearchabilityFilter(
query: Photo::query(),
user: $user,
unlocked_album_ids: $unlocked_album_ids,
origin: null,
include_nsfw: !$this->config_manager->getValueAsBool('hide_nsfw_in_rss')
)
->joinSub(DB::table(PA::PHOTO_ALBUM), 'outer_' . PA::PHOTO_ALBUM, 'photos.id', '=', 'outer_' . PA::PHOTO_ID)
->join('base_albums', 'base_albums.id', '=', 'outer_' . PA::ALBUM_ID)
->join('size_variants', 'size_variants.photo_id', '=', 'photos.id')
->join('users', 'users.id', '=', 'photos.owner_id')
->where('size_variants.type', '=', SizeVariantType::ORIGINAL->value)
->select([
'photos.id',
'photos.title',
'photos.description',
'photos.type',
'photos.updated_at',
'outer_' . PA::ALBUM_ID,
'base_albums.title as album_title',
'size_variants.short_path',
'size_variants.filesize',
'size_variants.storage_disk',
'users.username',
'users.display_name',
]
)
->where('photos.created_at', '>=', $now_minus)
->limit($rss_max)
->orderBy('photos.created_at', 'desc')
->toBase() // We use toBase() to avoid the use of the Eloquent casts etc.
->get();
return $photos->map(fn (object $p) => $this->toFeedItem($p));
}
}