From 7df0115707ce0ffb8471edb9a8c4be1e75d8a39f Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 14:25:51 +0900 Subject: [PATCH 01/39] add toOpengraph method --- app/Http/Controllers/UsersController.php | 5 +++-- app/Models/Traits/HasOpengraph.php | 13 +++++++++++++ app/Models/User.php | 12 +++++++++++- resources/views/layout/metadata.blade.php | 8 +++++--- resources/views/users/show.blade.php | 4 ---- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 app/Models/Traits/HasOpengraph.php diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 2d8f3543877..6a7ac38d7f5 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -654,9 +654,10 @@ public function show($id, $mode = null) 'user' => $userArray, ]; - $pageDescription = blade_safe($user->toMetaDescription(['ruleset' => $currentMode])); - return ext_view('users.show', compact('initialData', 'pageDescription', 'mode', 'user')); + $opengraph = $user->toOpengraph(['ruleset' => $currentMode]); + + return ext_view('users.show', compact('initialData', 'mode', 'opengraph', 'user')); } } diff --git a/app/Models/Traits/HasOpengraph.php b/app/Models/Traits/HasOpengraph.php new file mode 100644 index 00000000000..ac8df20fdcf --- /dev/null +++ b/app/Models/Traits/HasOpengraph.php @@ -0,0 +1,13 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Models\Traits; + +interface HasOpengraph +{ + public function toOpengraph(?array $options): array; +} diff --git a/app/Models/User.php b/app/Models/User.php index 3c81fc5e2e8..335a14b2c36 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -20,6 +20,7 @@ use App\Libraries\UsernameValidation; use App\Models\Forum\TopicWatch; use App\Models\OAuth\Client; +use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use App\Traits\Validatable; use Cache; @@ -210,7 +211,7 @@ * @method static Builder eagerloadForListing() * @method static Builder online() */ -class User extends Model implements AfterCommit, AuthenticatableContract, HasLocalePreference, Indexable, Traits\ReportableInterface +class User extends Model implements AfterCommit, AuthenticatableContract, HasLocalePreference, HasOpengraph, Indexable, Traits\ReportableInterface { use Authenticatable, HasApiTokens, Memoizes, Traits\Es\UserSearch, Traits\Reportable, Traits\UserAvatar, Traits\UserScoreable, Traits\UserStore, Validatable; @@ -1847,6 +1848,15 @@ public function toMetaDescription(array $options = []): string return osu_trans('users.ogp.description._', $replacements); } + public function toOpengraph(?array $options = []): array + { + return [ + 'description' => blade_safe($this->toMetaDescription($options)), + 'image' => $this->user_avatar, + 'title' => blade_safe(osu_trans('users.show.title', ['username' => $this->username])), + ]; + } + public function hasProfile() { return $this->getKey() !== null diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 6bf5014e56e..3ff0ea44b4c 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -26,10 +26,12 @@ - - - @if (isset($pageDescription)) + @foreach ($opengraph as $key => $value) + + @endforeach + + @if (!isset($opengraph['description']) && isset($pageDescription)) @endif @endif diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index c1a8269adf1..8fd3a063eef 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -6,10 +6,6 @@ @extends('master', [ 'canonicalUrl' => $user->url($mode), 'titlePrepend' => blade_safe(str_replace(' ', ' ', e($user->username))), - 'opengraph' => [ - 'title' => blade_safe(osu_trans('users.show.title', ['username' => $user->username])), - 'image' => $user->user_avatar, - ] ]) @section('content') From 6a3bf2bafa2451ca6f1312832edcf79b2c8b4529 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 16:09:01 +0900 Subject: [PATCH 02/39] more toOpengraph methods --- app/Http/Controllers/ArtistsController.php | 1 + app/Http/Controllers/ContestsController.php | 4 ++++ app/Http/Controllers/NewsController.php | 1 + app/Models/Artist.php | 11 ++++++++++- app/Models/Contest.php | 11 ++++++++++- app/Models/NewsPost.php | 11 ++++++++++- resources/views/artists/show.blade.php | 4 ---- resources/views/contests/base.blade.php | 4 ---- resources/views/news/show.blade.php | 9 +-------- 9 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/ArtistsController.php b/app/Http/Controllers/ArtistsController.php index 1a63b4680f3..407dfe927d0 100644 --- a/app/Http/Controllers/ArtistsController.php +++ b/app/Http/Controllers/ArtistsController.php @@ -120,6 +120,7 @@ public function show($id) 'tracks' => json_collection($tracks, new ArtistTrackTransformer()), ], 'links' => $links, + 'opengraph' => $artist->toOpengraph(), ]); } } diff --git a/app/Http/Controllers/ContestsController.php b/app/Http/Controllers/ContestsController.php index 3ed5d131fd4..d28a7119915 100644 --- a/app/Http/Controllers/ContestsController.php +++ b/app/Http/Controllers/ContestsController.php @@ -42,6 +42,8 @@ public function show($id) $contests = collect([$contest]); } + $opengraph = $contest->toOpengraph(); + if ($contest->isVotingStarted()) { if ($contest->isVotingOpen()) { // TODO: add support for $contests requirement instead of at parent @@ -56,11 +58,13 @@ public function show($id) 'contestMeta' => $contest, 'contests' => $contests, 'noVoteReason' => $noVoteReason ?? null, + 'opengraph' => $opengraph, ]); } else { return ext_view('contests.enter', [ 'contestMeta' => $contest, 'contest' => $contests->first(), + 'opengraph' => $opengraph, ]); } } diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index 4bae26a0579..7d1d0b51684 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -196,6 +196,7 @@ public function show($slug) return ext_view('news.show', [ 'commentBundle' => CommentBundle::forEmbed($post), + 'opengraph' => $post->toOpengraph(), 'post' => $post, 'postJson' => $postJson, 'sidebarMeta' => $this->sidebarMeta($post), diff --git a/app/Models/Artist.php b/app/Models/Artist.php index 2505f299d84..c26da06ff9b 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -5,6 +5,7 @@ namespace App\Models; +use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; /** @@ -31,7 +32,7 @@ * @property string|null $website * @property string|null $youtube */ -class Artist extends Model +class Artist extends Model implements HasOpengraph { use Memoizes; @@ -69,6 +70,14 @@ public function hasNewTracks() return $date !== null && $date->addMonths(1)->isFuture(); } + public function toOpengraph(?array $options = []): array + { + return [ + 'title' => $this->name, + 'image' => $this->cover_url, + ]; + } + public function url() { return route('artists.show', $this); diff --git a/app/Models/Contest.php b/app/Models/Contest.php index 4fd36f7d884..d63a61e7a9a 100644 --- a/app/Models/Contest.php +++ b/app/Models/Contest.php @@ -6,6 +6,7 @@ namespace App\Models; use App\Exceptions\InvariantException; +use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use App\Transformers\ContestEntryTransformer; use App\Transformers\ContestTransformer; @@ -38,7 +39,7 @@ * @property \Carbon\Carbon|null $voting_ends_at * @property \Carbon\Carbon|null $voting_starts_at */ -class Contest extends Model +class Contest extends Model implements HasOpengraph { use Memoizes; @@ -330,6 +331,14 @@ public function defaultJson($user = null) ]); } + public function toOpengraph(?array $options = []): array + { + return [ + 'title' => $this->name, + 'image' => $this->header_url, + ]; + } + public function votesForUser($user = null) { if ($user === null) { diff --git a/app/Models/NewsPost.php b/app/Models/NewsPost.php index 66d83085ac4..8fd3a56a6b7 100644 --- a/app/Models/NewsPost.php +++ b/app/Models/NewsPost.php @@ -9,6 +9,7 @@ use App\Libraries\Commentable; use App\Libraries\Markdown\OsuMarkdown; use App\Libraries\OsuWiki; +use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use Carbon\Carbon; use Exception; @@ -26,7 +27,7 @@ * @property \Carbon\Carbon|null $updated_at * @property string|null $version */ -class NewsPost extends Model implements Commentable, Wiki\WikiObject +class NewsPost extends Model implements Commentable, HasOpengraph, Wiki\WikiObject { use Memoizes, Traits\CommentableDefaults, Traits\WithDbCursorHelper; @@ -329,6 +330,14 @@ public function sync($force = false) return $this; } + public function toOpengraph(?array $options = []): array + { + return [ + 'title' => $this->title(), + 'image' => $this->firstImage(true), + ]; + } + public function pagePublishedAt() { $date = $this->page['header']['date'] ?? null; diff --git a/resources/views/artists/show.blade.php b/resources/views/artists/show.blade.php index 2da9ce799df..058919ad4df 100644 --- a/resources/views/artists/show.blade.php +++ b/resources/views/artists/show.blade.php @@ -22,10 +22,6 @@ 'titlePrepend' => $artist->name, 'pageDescription' => $artist->description, 'canonicalUrl' => $artist->url(), - 'opengraph' => [ - 'title' => $artist->name, - 'image' => $artist->cover_url, - ], ]) @section('content') diff --git a/resources/views/contests/base.blade.php b/resources/views/contests/base.blade.php index 5878a9f63cf..1e02d812bf3 100644 --- a/resources/views/contests/base.blade.php +++ b/resources/views/contests/base.blade.php @@ -19,10 +19,6 @@ 'titlePrepend' => $contestMeta->name, 'pageDescription' => strip_tags(markdown($contestMeta->currentDescription())), 'canonicalUrl' => $contestMeta->url(), - 'opengraph' => [ - 'title' => $contestMeta->name, - 'image' => $contestMeta->header_url, - ], ]) @section('content') diff --git a/resources/views/news/show.blade.php b/resources/views/news/show.blade.php index a0ca40ecb02..9b6cf2e1a8e 100644 --- a/resources/views/news/show.blade.php +++ b/resources/views/news/show.blade.php @@ -2,17 +2,10 @@ Copyright (c) ppy Pty Ltd . Licensed under the GNU Affero General Public License v3.0. See the LICENCE file in the repository root for full licence text. --}} -@php - $title = $post->title(); -@endphp @extends('master', [ - 'titlePrepend' => $title, + 'titlePrepend' => $post->title(), 'canonicalUrl' => $post->url(), 'pageDescription' => blade_safe($post->previewText()), - 'opengraph' => [ - 'title' => $title, - 'image' => $post->firstImage(true), - ], ]) @section('content') From 3e84b70fc8a5631a8cf30fd94f71807db26a51d0 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 17:27:22 +0900 Subject: [PATCH 03/39] move shared descriptions --- app/Models/Artist.php | 3 ++- app/Models/Contest.php | 3 ++- app/Models/NewsPost.php | 3 ++- resources/views/artists/show.blade.php | 1 - resources/views/beatmapsets/show.blade.php | 1 - resources/views/contests/base.blade.php | 1 - resources/views/layout/metadata.blade.php | 2 ++ resources/views/news/show.blade.php | 1 - 8 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/Models/Artist.php b/app/Models/Artist.php index c26da06ff9b..90a4b384b87 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -73,8 +73,9 @@ public function hasNewTracks() public function toOpengraph(?array $options = []): array { return [ - 'title' => $this->name, + 'description' => $this->description, 'image' => $this->cover_url, + 'title' => $this->name, ]; } diff --git a/app/Models/Contest.php b/app/Models/Contest.php index d63a61e7a9a..5c312f60368 100644 --- a/app/Models/Contest.php +++ b/app/Models/Contest.php @@ -334,8 +334,9 @@ public function defaultJson($user = null) public function toOpengraph(?array $options = []): array { return [ - 'title' => $this->name, + 'description' => strip_tags(markdown($this->currentDescription())), 'image' => $this->header_url, + 'title' => $this->name, ]; } diff --git a/app/Models/NewsPost.php b/app/Models/NewsPost.php index 8fd3a56a6b7..2160a89abca 100644 --- a/app/Models/NewsPost.php +++ b/app/Models/NewsPost.php @@ -333,8 +333,9 @@ public function sync($force = false) public function toOpengraph(?array $options = []): array { return [ - 'title' => $this->title(), + 'description' => blade_safe($this->previewText()), 'image' => $this->firstImage(true), + 'title' => $this->title(), ]; } diff --git a/resources/views/artists/show.blade.php b/resources/views/artists/show.blade.php index 058919ad4df..44ae241fb03 100644 --- a/resources/views/artists/show.blade.php +++ b/resources/views/artists/show.blade.php @@ -20,7 +20,6 @@ @extends('master', [ 'titlePrepend' => $artist->name, - 'pageDescription' => $artist->description, 'canonicalUrl' => $artist->url(), ]) diff --git a/resources/views/beatmapsets/show.blade.php b/resources/views/beatmapsets/show.blade.php index 14e3036f4b0..0adb9b9c84d 100644 --- a/resources/views/beatmapsets/show.blade.php +++ b/resources/views/beatmapsets/show.blade.php @@ -10,7 +10,6 @@ @endphp @endif @extends('master', [ - 'pageDescription' => $beatmapset->toMetaDescription(), 'titlePrepend' => "{$beatmapset->getDisplayArtist(auth()->user())} - {$beatmapset->getDisplayTitle(auth()->user())}", 'extraFooterLinks' => $extraFooterLinks ?? [], ]) diff --git a/resources/views/contests/base.blade.php b/resources/views/contests/base.blade.php index 1e02d812bf3..325ec0f0a54 100644 --- a/resources/views/contests/base.blade.php +++ b/resources/views/contests/base.blade.php @@ -17,7 +17,6 @@ @extends('master', [ 'titlePrepend' => $contestMeta->name, - 'pageDescription' => strip_tags(markdown($contestMeta->currentDescription())), 'canonicalUrl' => $contestMeta->url(), ]) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 3ff0ea44b4c..840ecf0c3f8 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -6,6 +6,8 @@ $appUrl = config('app.url'); $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); + + $pageDescription ??= $opengraph['description'] ?? null; @endphp diff --git a/resources/views/news/show.blade.php b/resources/views/news/show.blade.php index 9b6cf2e1a8e..6c24b760ba2 100644 --- a/resources/views/news/show.blade.php +++ b/resources/views/news/show.blade.php @@ -5,7 +5,6 @@ @extends('master', [ 'titlePrepend' => $post->title(), 'canonicalUrl' => $post->url(), - 'pageDescription' => blade_safe($post->previewText()), ]) @section('content') From 13d01015d842c1c89f789c41f8d61bf5decfbfe5 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 17:44:47 +0900 Subject: [PATCH 04/39] add toOpengraph method for forum and topic show --- app/Http/Controllers/Forum/ForumsController.php | 2 ++ app/Http/Controllers/Forum/TopicsController.php | 2 ++ app/Models/Forum/Forum.php | 10 +++++++++- app/Models/Forum/Topic.php | 10 +++++++++- resources/views/forum/forums/show.blade.php | 1 - resources/views/forum/topics/show.blade.php | 1 - resources/views/layout/metadata.blade.php | 9 +++++++-- 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Forum/ForumsController.php b/app/Http/Controllers/Forum/ForumsController.php index d5450dcbf9b..b68c9925cbf 100644 --- a/app/Http/Controllers/Forum/ForumsController.php +++ b/app/Http/Controllers/Forum/ForumsController.php @@ -104,12 +104,14 @@ public function show($id) ->keyBy('topic_id'); $noindex = !$forum->enable_indexing; + $opengraph = $forum->toOpengraph(); return ext_view('forum.forums.show', compact( 'cover', 'forum', 'lastTopics', 'noindex', + 'opengraph', 'pinnedTopics', 'sort', 'topicReadStatus', diff --git a/app/Http/Controllers/Forum/TopicsController.php b/app/Http/Controllers/Forum/TopicsController.php index 184b9f2e485..b1ef0dcedc4 100644 --- a/app/Http/Controllers/Forum/TopicsController.php +++ b/app/Http/Controllers/Forum/TopicsController.php @@ -437,6 +437,7 @@ public function show($id) $featureVotes = $this->groupFeatureVotes($topic); $noindex = !$topic->forum->enable_indexing; + $opengraph = $topic->toOpengraph(); return ext_view('forum.topics.show', compact( 'canEditPoll', @@ -449,6 +450,7 @@ public function show($id) 'firstPostPosition', 'navUrls', 'noindex', + 'opengraph', 'topic', 'userCanModerate', 'showDeleted' diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index cf61fb0a7a4..e708756552c 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -5,6 +5,7 @@ namespace App\Models\Forum; +use App\Models\Traits\HasOpengraph; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; @@ -59,7 +60,7 @@ * @property \Illuminate\Database\Eloquent\Collection $subforums static * @property \Illuminate\Database\Eloquent\Collection $topics Topic */ -class Forum extends Model +class Forum extends Model implements HasOpengraph { public $timestamps = false; @@ -385,4 +386,11 @@ public function toMetaDescription() return implode(' » ', $stack); } + + public function toOpengraph(?array $options = []): array + { + return [ + 'description' => $this->toMetaDescription(), + ]; + } } diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index 15832714316..0bf16b0d039 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -13,6 +13,7 @@ use App\Models\Beatmapset; use App\Models\Log; use App\Models\Notification; +use App\Models\Traits\HasOpengraph; use App\Models\User; use App\Traits\Memoizes; use App\Traits\Validatable; @@ -72,7 +73,7 @@ * @property \Illuminate\Database\Eloquent\Collection $userTracks TopicTrack * @property \Illuminate\Database\Eloquent\Collection $watches TopicWatch */ -class Topic extends Model implements AfterCommit +class Topic extends Model implements AfterCommit, HasOpengraph { use Memoizes, Validatable; use SoftDeletes { @@ -829,6 +830,13 @@ public function toMetaDescription() return "{$this->forum->toMetaDescription()} » {$this->topic_title}"; } + public function toOpengraph(?array $options = []): array + { + return [ + 'description' => $this->toMetaDescription(), + ]; + } + public function afterCommit() { if ($this->exists && $this->firstPost !== null) { diff --git a/resources/views/forum/forums/show.blade.php b/resources/views/forum/forums/show.blade.php index 0c621e36a0f..50b8c228290 100644 --- a/resources/views/forum/forums/show.blade.php +++ b/resources/views/forum/forums/show.blade.php @@ -4,7 +4,6 @@ --}} @extends('master', [ 'bodyAdditionalClasses' => "t-forum-{$forum->categorySlug()}", - 'pageDescription' => $forum->toMetaDescription(), 'searchParams' => [ 'forum_id' => $forum->getKey(), 'mode' => 'forum_post', diff --git a/resources/views/forum/topics/show.blade.php b/resources/views/forum/topics/show.blade.php index 7008cdc8940..f397db7e6c8 100644 --- a/resources/views/forum/topics/show.blade.php +++ b/resources/views/forum/topics/show.blade.php @@ -5,7 +5,6 @@ @extends('master', [ 'titlePrepend' => $topic->topic_title, 'canonicalUrl' => route('forum.topics.show', $topic->topic_id), - 'pageDescription' => $topic->toMetaDescription(), ]) @php diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 840ecf0c3f8..921c7aafa7a 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -27,10 +27,15 @@ @if (isset($opengraph)) - + + @if (isset($canonicalUrl)) + + @endif @foreach ($opengraph as $key => $value) - + @if (present($value)) + + @endif @endforeach @if (!isset($opengraph['description']) && isset($pageDescription)) From d0c15f02280458cea79b50c9fb53a6dc2d40e7c3 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 17:49:37 +0900 Subject: [PATCH 05/39] ogp description always same as meta description --- resources/views/layout/metadata.blade.php | 28 +++++++++-------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 921c7aafa7a..c84f173f6bc 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -7,7 +7,7 @@ $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); - $pageDescription ??= $opengraph['description'] ?? null; + $opengraph['description'] ??= $opengraph['description'] ?? $pageDescription ?? null; @endphp @@ -18,30 +18,24 @@ {{-- @osu-colour-b1 --}} - + -@if (isset($opengraph)) - - + + - @if (isset($canonicalUrl)) - - @endif - - @foreach ($opengraph as $key => $value) - @if (present($value)) - - @endif - @endforeach +@if (isset($canonicalUrl)) + +@endif - @if (!isset($opengraph['description']) && isset($pageDescription)) - +@foreach ($opengraph as $key => $value) + @if (present($value)) + @endif -@endif +@endforeach @if ($noindex ?? false) From 12bb5cb60bb47c2d57125f0b633a61132929f023 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 17:54:50 +0900 Subject: [PATCH 06/39] remove markdown/up from featured artist description; also don't put every line in --- app/Models/Artist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Artist.php b/app/Models/Artist.php index 90a4b384b87..47252bb72c4 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -73,7 +73,7 @@ public function hasNewTracks() public function toOpengraph(?array $options = []): array { return [ - 'description' => $this->description, + 'description' => first_paragraph(markdown_plain($this->description)), 'image' => $this->cover_url, 'title' => $this->name, ]; From e96033d168728fafdc0bcb04e6e60ce2a28f01ea Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 18:05:54 +0900 Subject: [PATCH 07/39] removing toMetaDescription --- app/Http/Controllers/UsersController.php | 2 +- app/Models/Forum/Forum.php | 12 ++--- app/Models/Forum/Topic.php | 12 ++--- app/Models/User.php | 68 ++++++++++++------------ 4 files changed, 43 insertions(+), 51 deletions(-) diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 6a7ac38d7f5..43962a10a9c 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -631,7 +631,7 @@ public function show($id, $mode = null) abort(404); } - // preload and set relation for toMetaDescription and transformer sharing data + // preload and set relation for opengraph header and transformer sharing data $user->statistics($currentMode)?->setRelation('user', $user); $userArray = $this->fillDeprecatedDuplicateFields(json_item( diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index e708756552c..43fde9807eb 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -375,7 +375,7 @@ public function markAsRead(User $user, bool $recursive = false) }); } - public function toMetaDescription() + public function toOpengraph(?array $options = []): array { $stack = [osu_trans('forum.title')]; foreach ($this->forum_parents as $forumId => $forumData) { @@ -384,13 +384,9 @@ public function toMetaDescription() $stack[] = $this->forum_name; - return implode(' » ', $stack); - } + $description = implode(' » ', $stack); - public function toOpengraph(?array $options = []): array - { - return [ - 'description' => $this->toMetaDescription(), - ]; + // Reminder to update Topic::toOpengraph() if these values change. + return compact('description'); } } diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index 0bf16b0d039..7ec31d451b4 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -825,16 +825,12 @@ public function hasIssueTag($tag) return strpos($this->topic_title, "[{$tag}]") !== false; } - public function toMetaDescription() - { - return "{$this->forum->toMetaDescription()} » {$this->topic_title}"; - } - public function toOpengraph(?array $options = []): array { - return [ - 'description' => $this->toMetaDescription(), - ]; + $opengraph = $this->forum->toOpengraph(); + $opengraph['description'] = "{$opengraph['description']} » {$this->topic_title}"; + + return $opengraph; } public function afterCommit() diff --git a/app/Models/User.php b/app/Models/User.php index 335a14b2c36..4db45fe1f06 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -1815,43 +1815,10 @@ public function titleUrl(): ?string return $this->rank?->url; } - public function toMetaDescription(array $options = []): string - { - static $rankTypes = ['country', 'global']; - - $ruleset = $options['ruleset'] ?? $this->playmode; - $stats = $this->statistics($ruleset); - - $replacements['ruleset'] = $ruleset; - - foreach ($rankTypes as $type) { - $method = "{$type}Rank"; - $replacements[$type] = osu_trans("users.ogp.description.{$type}", [ - 'rank' => format_rank($stats?->$method()), - ]); - - $variants = Beatmap::VARIANTS[$ruleset] ?? []; - - $variantsTexts = null; - foreach ($variants as $variant) { - $variantRank = $this->statistics($ruleset, false, $variant)?->$method(); - if ($variantRank !== null) { - $variantsTexts[] = $variant.' '.format_rank($variantRank); - } - } - - if (!empty($variantsTexts)) { - $replacements[$type] .= ' ('.implode(', ', $variantsTexts).')'; - } - } - - return osu_trans('users.ogp.description._', $replacements); - } - public function toOpengraph(?array $options = []): array { return [ - 'description' => blade_safe($this->toMetaDescription($options)), + 'description' => blade_safe($this->getOpengraphDescription($options)), 'image' => $this->user_avatar, 'title' => blade_safe(osu_trans('users.show.title', ['username' => $this->username])), ]; @@ -2434,6 +2401,39 @@ private function getDisplayedLastVisit() return $this->hide_presence ? null : $this->user_lastvisit; } + private function getOpengraphDescription(array $options = []): string + { + static $rankTypes = ['country', 'global']; + + $ruleset = $options['ruleset'] ?? $this->playmode; + $stats = $this->statistics($ruleset); + + $replacements['ruleset'] = $ruleset; + + foreach ($rankTypes as $type) { + $method = "{$type}Rank"; + $replacements[$type] = osu_trans("users.ogp.description.{$type}", [ + 'rank' => format_rank($stats?->$method()), + ]); + + $variants = Beatmap::VARIANTS[$ruleset] ?? []; + + $variantsTexts = null; + foreach ($variants as $variant) { + $variantRank = $this->statistics($ruleset, false, $variant)?->$method(); + if ($variantRank !== null) { + $variantsTexts[] = $variant.' '.format_rank($variantRank); + } + } + + if (!empty($variantsTexts)) { + $replacements[$type] .= ' ('.implode(', ', $variantsTexts).')'; + } + } + + return osu_trans('users.ogp.description._', $replacements); + } + private function getOsuPlaystyle() { $value = $this->getRawAttribute('osu_playstyle'); From c24c508484d7658387687f5b12ae0aaaaef0c814 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 18:09:49 +0900 Subject: [PATCH 08/39] add missing opengraph for beatmapset --- app/Http/Controllers/BeatmapsetsController.php | 2 ++ app/Models/Beatmapset.php | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/BeatmapsetsController.php b/app/Http/Controllers/BeatmapsetsController.php index b4fe5c93b10..5c0ed5f0aa7 100644 --- a/app/Http/Controllers/BeatmapsetsController.php +++ b/app/Http/Controllers/BeatmapsetsController.php @@ -98,6 +98,7 @@ public function show($id) } $noindex = !$beatmapset->esShouldIndex(); + $opengraph = $beatmapset->toOpengraph(); return ext_view('beatmapsets.show', compact( 'beatmapset', @@ -105,6 +106,7 @@ public function show($id) 'genres', 'languages', 'noindex', + 'opengraph', 'set' )); } diff --git a/app/Models/Beatmapset.php b/app/Models/Beatmapset.php index b927788c69d..cc34a1657fa 100644 --- a/app/Models/Beatmapset.php +++ b/app/Models/Beatmapset.php @@ -25,6 +25,7 @@ use App\Libraries\Elasticsearch\Indexable; use App\Libraries\ImageProcessorService; use App\Libraries\Transactions\AfterCommit; +use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use App\Traits\Validatable; use Cache; @@ -104,7 +105,7 @@ * @property bool $video * @property \Illuminate\Database\Eloquent\Collection $watches BeatmapsetWatch */ -class Beatmapset extends Model implements AfterCommit, Commentable, Indexable, Traits\ReportableInterface +class Beatmapset extends Model implements AfterCommit, Commentable, HasOpengraph, Indexable, Traits\ReportableInterface { use Memoizes, SoftDeletes, Traits\CommentableDefaults, Traits\Es\BeatmapsetSearch, Traits\Reportable, Validatable; @@ -1421,11 +1422,13 @@ public function updateDescription($bbcode, $user) ]); } - public function toMetaDescription() + public function toOpengraph(?array $options = []): array { $section = osu_trans('layout.menu.beatmaps._'); - return "osu! » {$section} » {$this->artist} - {$this->title}"; + return [ + 'description' => "osu! » {$section} » {$this->artist} - {$this->title}", + ]; } private function extractDescription($post) From 83d9ac45f87ba4a2a7e05e69e7c8f4ecc0e361ad Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 23:04:35 +0900 Subject: [PATCH 09/39] add more cover images to opengraph headers --- app/Http/Controllers/Forum/TopicsController.php | 2 +- app/Models/Beatmapset.php | 1 + app/Models/Forum/Forum.php | 5 ++++- app/Models/Forum/Topic.php | 8 +++++--- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Forum/TopicsController.php b/app/Http/Controllers/Forum/TopicsController.php index b1ef0dcedc4..1f2bfba29bd 100644 --- a/app/Http/Controllers/Forum/TopicsController.php +++ b/app/Http/Controllers/Forum/TopicsController.php @@ -429,7 +429,7 @@ public function show($id) $posts->last()->markRead($currentUser); - $coverModel = $topic->cover()->firstOrNew([]); + $coverModel = $topic->cover ?? new TopicCover(); $coverModel->setRelation('topic', $topic); $cover = json_item($coverModel, new TopicCoverTransformer()); diff --git a/app/Models/Beatmapset.php b/app/Models/Beatmapset.php index cc34a1657fa..26f9c1f6564 100644 --- a/app/Models/Beatmapset.php +++ b/app/Models/Beatmapset.php @@ -1428,6 +1428,7 @@ public function toOpengraph(?array $options = []): array return [ 'description' => "osu! » {$section} » {$this->artist} - {$this->title}", + 'image' => $this->coverURL('card'), ]; } diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index 43fde9807eb..cc0cf9f8aad 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -387,6 +387,9 @@ public function toOpengraph(?array $options = []): array $description = implode(' » ', $stack); // Reminder to update Topic::toOpengraph() if these values change. - return compact('description'); + return [ + 'description' => $description, + 'image' => $this->cover?->fileUrl(), + ]; } } diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index 7ec31d451b4..aca3a0ec8da 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -827,10 +827,12 @@ public function hasIssueTag($tag) public function toOpengraph(?array $options = []): array { - $opengraph = $this->forum->toOpengraph(); - $opengraph['description'] = "{$opengraph['description']} » {$this->topic_title}"; + $forumOpengraph = $this->forum->toOpengraph(); - return $opengraph; + return [ + 'description' => "{$forumOpengraph['description']} » {$this->topic_title}", + 'image' => $this->cover?->fileUrl() ?? $this->forum->cover?->defaultTopicCover->fileUrl(), + ]; } public function afterCommit() From 72fd995bcb4ae25630f582af4eb5a0d2ba1900b1 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 16 Oct 2023 23:11:42 +0900 Subject: [PATCH 10/39] add more titles --- app/Models/Beatmapset.php | 4 +++- app/Models/Forum/Forum.php | 1 + app/Models/Forum/Topic.php | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Models/Beatmapset.php b/app/Models/Beatmapset.php index 26f9c1f6564..e70f7e4f604 100644 --- a/app/Models/Beatmapset.php +++ b/app/Models/Beatmapset.php @@ -1425,10 +1425,12 @@ public function updateDescription($bbcode, $user) public function toOpengraph(?array $options = []): array { $section = osu_trans('layout.menu.beatmaps._'); + $title = "{$this->artist} - {$this->title}"; // opengrah header always intended for guest. return [ - 'description' => "osu! » {$section} » {$this->artist} - {$this->title}", + 'description' => "osu! » {$section} » {$title}", 'image' => $this->coverURL('card'), + 'title' => $title, ]; } diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index cc0cf9f8aad..85aea07e8c7 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -389,6 +389,7 @@ public function toOpengraph(?array $options = []): array // Reminder to update Topic::toOpengraph() if these values change. return [ 'description' => $description, + 'title' => $this->forum_name, 'image' => $this->cover?->fileUrl(), ]; } diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index aca3a0ec8da..29707db5436 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -832,6 +832,7 @@ public function toOpengraph(?array $options = []): array return [ 'description' => "{$forumOpengraph['description']} » {$this->topic_title}", 'image' => $this->cover?->fileUrl() ?? $this->forum->cover?->defaultTopicCover->fileUrl(), + 'title' => $this->topic_title, ]; } From 6c500261574d3474f7e6cbb2080ee95156433cea Mon Sep 17 00:00:00 2001 From: bakaneko Date: Thu, 19 Oct 2023 17:16:39 +0900 Subject: [PATCH 11/39] move opengraph stuff to separate classes; skip passing variable to view factory since only metadata view needs it. --- app/Http/Controllers/ArtistsController.php | 3 +- .../Controllers/BeatmapsetsController.php | 4 +- app/Http/Controllers/ContestsController.php | 4 +- .../Controllers/Forum/ForumsController.php | 4 +- .../Controllers/Forum/TopicsController.php | 4 +- app/Http/Controllers/NewsController.php | 3 +- app/Http/Controllers/UsersController.php | 5 +- app/Libraries/Opengraph/ArtistOpengraph.php | 26 ++++++++ .../Opengraph/BeatmapsetOpengraph.php | 29 +++++++++ app/Libraries/Opengraph/ContestOpengraph.php | 26 ++++++++ app/Libraries/Opengraph/ForumOpengraph.php | 40 +++++++++++++ .../Opengraph}/HasOpengraph.php | 3 +- app/Libraries/Opengraph/NewsPostOpengraph.php | 26 ++++++++ app/Libraries/Opengraph/TopicOpengraph.php | 28 +++++++++ app/Libraries/Opengraph/UserOpengraph.php | 60 +++++++++++++++++++ app/Models/Artist.php | 11 +--- app/Models/Beatmapset.php | 14 +---- app/Models/Contest.php | 11 +--- app/Models/Forum/Forum.php | 21 +------ app/Models/Forum/Topic.php | 13 +--- app/Models/NewsPost.php | 11 +--- app/Models/User.php | 44 +------------- app/helpers.php | 10 ++++ 23 files changed, 266 insertions(+), 134 deletions(-) create mode 100644 app/Libraries/Opengraph/ArtistOpengraph.php create mode 100644 app/Libraries/Opengraph/BeatmapsetOpengraph.php create mode 100644 app/Libraries/Opengraph/ContestOpengraph.php create mode 100644 app/Libraries/Opengraph/ForumOpengraph.php rename app/{Models/Traits => Libraries/Opengraph}/HasOpengraph.php (73%) create mode 100644 app/Libraries/Opengraph/NewsPostOpengraph.php create mode 100644 app/Libraries/Opengraph/TopicOpengraph.php create mode 100644 app/Libraries/Opengraph/UserOpengraph.php diff --git a/app/Http/Controllers/ArtistsController.php b/app/Http/Controllers/ArtistsController.php index 407dfe927d0..262a7743c24 100644 --- a/app/Http/Controllers/ArtistsController.php +++ b/app/Http/Controllers/ArtistsController.php @@ -111,6 +111,8 @@ public function show($id) } } + set_opengraph($artist); + return ext_view('artists.show', [ 'artist' => $artist, 'images' => $images, @@ -120,7 +122,6 @@ public function show($id) 'tracks' => json_collection($tracks, new ArtistTrackTransformer()), ], 'links' => $links, - 'opengraph' => $artist->toOpengraph(), ]); } } diff --git a/app/Http/Controllers/BeatmapsetsController.php b/app/Http/Controllers/BeatmapsetsController.php index 5c0ed5f0aa7..5eff49e2b3c 100644 --- a/app/Http/Controllers/BeatmapsetsController.php +++ b/app/Http/Controllers/BeatmapsetsController.php @@ -98,7 +98,8 @@ public function show($id) } $noindex = !$beatmapset->esShouldIndex(); - $opengraph = $beatmapset->toOpengraph(); + + set_opengraph($beatmapset); return ext_view('beatmapsets.show', compact( 'beatmapset', @@ -106,7 +107,6 @@ public function show($id) 'genres', 'languages', 'noindex', - 'opengraph', 'set' )); } diff --git a/app/Http/Controllers/ContestsController.php b/app/Http/Controllers/ContestsController.php index d28a7119915..e3c8caf0327 100644 --- a/app/Http/Controllers/ContestsController.php +++ b/app/Http/Controllers/ContestsController.php @@ -42,7 +42,7 @@ public function show($id) $contests = collect([$contest]); } - $opengraph = $contest->toOpengraph(); + set_opengraph($contest); if ($contest->isVotingStarted()) { if ($contest->isVotingOpen()) { @@ -58,13 +58,11 @@ public function show($id) 'contestMeta' => $contest, 'contests' => $contests, 'noVoteReason' => $noVoteReason ?? null, - 'opengraph' => $opengraph, ]); } else { return ext_view('contests.enter', [ 'contestMeta' => $contest, 'contest' => $contests->first(), - 'opengraph' => $opengraph, ]); } } diff --git a/app/Http/Controllers/Forum/ForumsController.php b/app/Http/Controllers/Forum/ForumsController.php index b68c9925cbf..61050780d07 100644 --- a/app/Http/Controllers/Forum/ForumsController.php +++ b/app/Http/Controllers/Forum/ForumsController.php @@ -104,14 +104,14 @@ public function show($id) ->keyBy('topic_id'); $noindex = !$forum->enable_indexing; - $opengraph = $forum->toOpengraph(); + + set_opengraph($forum); return ext_view('forum.forums.show', compact( 'cover', 'forum', 'lastTopics', 'noindex', - 'opengraph', 'pinnedTopics', 'sort', 'topicReadStatus', diff --git a/app/Http/Controllers/Forum/TopicsController.php b/app/Http/Controllers/Forum/TopicsController.php index 1f2bfba29bd..fe1f7bfcff1 100644 --- a/app/Http/Controllers/Forum/TopicsController.php +++ b/app/Http/Controllers/Forum/TopicsController.php @@ -437,7 +437,8 @@ public function show($id) $featureVotes = $this->groupFeatureVotes($topic); $noindex = !$topic->forum->enable_indexing; - $opengraph = $topic->toOpengraph(); + + set_opengraph($topic); return ext_view('forum.topics.show', compact( 'canEditPoll', @@ -450,7 +451,6 @@ public function show($id) 'firstPostPosition', 'navUrls', 'noindex', - 'opengraph', 'topic', 'userCanModerate', 'showDeleted' diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index 7d1d0b51684..989245ef71b 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -194,9 +194,10 @@ public function show($slug) return $postJson; } + set_opengraph($post); + return ext_view('news.show', [ 'commentBundle' => CommentBundle::forEmbed($post), - 'opengraph' => $post->toOpengraph(), 'post' => $post, 'postJson' => $postJson, 'sidebarMeta' => $this->sidebarMeta($post), diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 43962a10a9c..6ab529fe1f7 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -654,10 +654,9 @@ public function show($id, $mode = null) 'user' => $userArray, ]; + set_opengraph($user, $currentMode); - $opengraph = $user->toOpengraph(['ruleset' => $currentMode]); - - return ext_view('users.show', compact('initialData', 'mode', 'opengraph', 'user')); + return ext_view('users.show', compact('initialData', 'mode', 'user')); } } diff --git a/app/Libraries/Opengraph/ArtistOpengraph.php b/app/Libraries/Opengraph/ArtistOpengraph.php new file mode 100644 index 00000000000..2db40300e3b --- /dev/null +++ b/app/Libraries/Opengraph/ArtistOpengraph.php @@ -0,0 +1,26 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Artist; + +class ArtistOpengraph +{ + public function __construct(private Artist $artist) + { + } + + public function get(): array + { + return [ + 'description' => first_paragraph(markdown_plain($this->artist->description)), + 'image' => $this->artist->cover_url, + 'title' => $this->artist->name, + ]; + } +} diff --git a/app/Libraries/Opengraph/BeatmapsetOpengraph.php b/app/Libraries/Opengraph/BeatmapsetOpengraph.php new file mode 100644 index 00000000000..788c9ebf959 --- /dev/null +++ b/app/Libraries/Opengraph/BeatmapsetOpengraph.php @@ -0,0 +1,29 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Beatmapset; + +class BeatmapsetOpengraph +{ + public function __construct(private Beatmapset $beatmapset) + { + } + + public function get(): array + { + $section = osu_trans('layout.menu.beatmaps._'); + $title = "{$this->beatmapset->artist} - {$this->beatmapset->title}"; // opengrah header always intended for guest. + + return [ + 'description' => "osu! » {$section} » {$title}", + 'image' => $this->beatmapset->coverURL('card'), + 'title' => $title, + ]; + } +} diff --git a/app/Libraries/Opengraph/ContestOpengraph.php b/app/Libraries/Opengraph/ContestOpengraph.php new file mode 100644 index 00000000000..253b8a5c880 --- /dev/null +++ b/app/Libraries/Opengraph/ContestOpengraph.php @@ -0,0 +1,26 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Contest; + +class ContestOpengraph +{ + public function __construct(private Contest $contest) + { + } + + public function get(): array + { + return [ + 'description' => strip_tags(markdown($this->contest->currentDescription())), + 'image' => $this->contest->header_url, + 'title' => $this->contest->name, + ]; + } +} diff --git a/app/Libraries/Opengraph/ForumOpengraph.php b/app/Libraries/Opengraph/ForumOpengraph.php new file mode 100644 index 00000000000..ddee5c2c0f1 --- /dev/null +++ b/app/Libraries/Opengraph/ForumOpengraph.php @@ -0,0 +1,40 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Forum\Forum; + +class ForumOpengraph +{ + public function __construct(private Forum $forum) + { + } + + // Reminder to update Topic::toOpengraph() as necessary if this value changes. + public function description(): string + { + $stack = [osu_trans('forum.title')]; + foreach ($this->forum->forum_parents as $forumId => $forumData) { + $stack[] = $forumData[0]; + } + + $stack[] = $this->forum->forum_name; + + return implode(' » ', $stack); + } + + public function get(): array + { + + return [ + 'description' => $this->description(), + 'title' => $this->forum->forum_name, + 'image' => $this->forum->cover?->fileUrl(), + ]; + } +} diff --git a/app/Models/Traits/HasOpengraph.php b/app/Libraries/Opengraph/HasOpengraph.php similarity index 73% rename from app/Models/Traits/HasOpengraph.php rename to app/Libraries/Opengraph/HasOpengraph.php index ac8df20fdcf..a227fab94d0 100644 --- a/app/Models/Traits/HasOpengraph.php +++ b/app/Libraries/Opengraph/HasOpengraph.php @@ -5,9 +5,8 @@ declare(strict_types=1); -namespace App\Models\Traits; +namespace App\Libraries\Opengraph; interface HasOpengraph { - public function toOpengraph(?array $options): array; } diff --git a/app/Libraries/Opengraph/NewsPostOpengraph.php b/app/Libraries/Opengraph/NewsPostOpengraph.php new file mode 100644 index 00000000000..fb923275060 --- /dev/null +++ b/app/Libraries/Opengraph/NewsPostOpengraph.php @@ -0,0 +1,26 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\NewsPost; + +class NewsPostOpengraph +{ + public function __construct(private NewsPost $post) + { + } + + public function get(): array + { + return [ + 'description' => $this->post->previewText(), + 'image' => $this->post->firstImage(true), + 'title' => $this->post->title(), + ]; + } +} diff --git a/app/Libraries/Opengraph/TopicOpengraph.php b/app/Libraries/Opengraph/TopicOpengraph.php new file mode 100644 index 00000000000..2714b404b04 --- /dev/null +++ b/app/Libraries/Opengraph/TopicOpengraph.php @@ -0,0 +1,28 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Forum\Topic; + +class TopicOpengraph +{ + public function __construct(private Topic $topic) + { + } + + public function get(): array + { + $forumDescription = (new ForumOpengraph($this->topic->forum))->description(); + + return [ + 'description' => "{$forumDescription} » {$this->topic->topic_title}", + 'image' => $this->topic->cover?->fileUrl() ?? $this->topic->forum->cover?->defaultTopicCover->fileUrl(), + 'title' => $this->topic->topic_title, + ]; + } +} diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php new file mode 100644 index 00000000000..671052f3169 --- /dev/null +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -0,0 +1,60 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Beatmap; +use App\Models\User; + +class UserOpengraph +{ + public function __construct(private User $user, private string $ruleset) + { + } + + public function get() + { + return [ + 'description' => $this->description(), + 'image' => $this->user->user_avatar, + 'title' => blade_safe(osu_trans('users.show.title', ['username' => $this->user->username])), + ]; + } + + private function description(): string + { + static $rankTypes = ['country', 'global']; + + $ruleset = $this->ruleset ?? $this->user->playmode; + $stats = $this->user->statistics($ruleset); + + $replacements['ruleset'] = $ruleset; + + foreach ($rankTypes as $type) { + $method = "{$type}Rank"; + $replacements[$type] = osu_trans("users.ogp.description.{$type}", [ + 'rank' => format_rank($stats?->$method()), + ]); + + $variants = Beatmap::VARIANTS[$ruleset] ?? []; + + $variantsTexts = null; + foreach ($variants as $variant) { + $variantRank = $this->user->statistics($ruleset, false, $variant)?->$method(); + if ($variantRank !== null) { + $variantsTexts[] = $variant.' '.format_rank($variantRank); + } + } + + if (!empty($variantsTexts)) { + $replacements[$type] .= ' ('.implode(', ', $variantsTexts).')'; + } + } + + return osu_trans('users.ogp.description._', $replacements); + } +} diff --git a/app/Models/Artist.php b/app/Models/Artist.php index 47252bb72c4..245dc1a1e07 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -5,7 +5,7 @@ namespace App\Models; -use App\Models\Traits\HasOpengraph; +use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Memoizes; /** @@ -70,15 +70,6 @@ public function hasNewTracks() return $date !== null && $date->addMonths(1)->isFuture(); } - public function toOpengraph(?array $options = []): array - { - return [ - 'description' => first_paragraph(markdown_plain($this->description)), - 'image' => $this->cover_url, - 'title' => $this->name, - ]; - } - public function url() { return route('artists.show', $this); diff --git a/app/Models/Beatmapset.php b/app/Models/Beatmapset.php index e70f7e4f604..8005bd772bb 100644 --- a/app/Models/Beatmapset.php +++ b/app/Models/Beatmapset.php @@ -24,8 +24,8 @@ use App\Libraries\Commentable; use App\Libraries\Elasticsearch\Indexable; use App\Libraries\ImageProcessorService; +use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\Transactions\AfterCommit; -use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use App\Traits\Validatable; use Cache; @@ -1422,18 +1422,6 @@ public function updateDescription($bbcode, $user) ]); } - public function toOpengraph(?array $options = []): array - { - $section = osu_trans('layout.menu.beatmaps._'); - $title = "{$this->artist} - {$this->title}"; // opengrah header always intended for guest. - - return [ - 'description' => "osu! » {$section} » {$title}", - 'image' => $this->coverURL('card'), - 'title' => $title, - ]; - } - private function extractDescription($post) { // Any description (after the first match) that matches diff --git a/app/Models/Contest.php b/app/Models/Contest.php index 5c312f60368..57bca77e8b2 100644 --- a/app/Models/Contest.php +++ b/app/Models/Contest.php @@ -6,7 +6,7 @@ namespace App\Models; use App\Exceptions\InvariantException; -use App\Models\Traits\HasOpengraph; +use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Memoizes; use App\Transformers\ContestEntryTransformer; use App\Transformers\ContestTransformer; @@ -331,15 +331,6 @@ public function defaultJson($user = null) ]); } - public function toOpengraph(?array $options = []): array - { - return [ - 'description' => strip_tags(markdown($this->currentDescription())), - 'image' => $this->header_url, - 'title' => $this->name, - ]; - } - public function votesForUser($user = null) { if ($user === null) { diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index 85aea07e8c7..5941fe5d68c 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -5,7 +5,7 @@ namespace App\Models\Forum; -use App\Models\Traits\HasOpengraph; +use App\Libraries\Opengraph\HasOpengraph; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; @@ -374,23 +374,4 @@ public function markAsRead(User $user, bool $recursive = false) TopicTrack::where('user_id', $user->getKey())->whereIn('forum_id', $forumIds)->delete(); }); } - - public function toOpengraph(?array $options = []): array - { - $stack = [osu_trans('forum.title')]; - foreach ($this->forum_parents as $forumId => $forumData) { - $stack[] = $forumData[0]; - } - - $stack[] = $this->forum_name; - - $description = implode(' » ', $stack); - - // Reminder to update Topic::toOpengraph() if these values change. - return [ - 'description' => $description, - 'title' => $this->forum_name, - 'image' => $this->cover?->fileUrl(), - ]; - } } diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index 29707db5436..3973dca9c2f 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -9,11 +9,11 @@ use App\Jobs\UpdateUserForumCache; use App\Jobs\UpdateUserForumTopicFollows; use App\Libraries\BBCodeForDB; +use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\Transactions\AfterCommit; use App\Models\Beatmapset; use App\Models\Log; use App\Models\Notification; -use App\Models\Traits\HasOpengraph; use App\Models\User; use App\Traits\Memoizes; use App\Traits\Validatable; @@ -825,17 +825,6 @@ public function hasIssueTag($tag) return strpos($this->topic_title, "[{$tag}]") !== false; } - public function toOpengraph(?array $options = []): array - { - $forumOpengraph = $this->forum->toOpengraph(); - - return [ - 'description' => "{$forumOpengraph['description']} » {$this->topic_title}", - 'image' => $this->cover?->fileUrl() ?? $this->forum->cover?->defaultTopicCover->fileUrl(), - 'title' => $this->topic_title, - ]; - } - public function afterCommit() { if ($this->exists && $this->firstPost !== null) { diff --git a/app/Models/NewsPost.php b/app/Models/NewsPost.php index 2160a89abca..78cf390851c 100644 --- a/app/Models/NewsPost.php +++ b/app/Models/NewsPost.php @@ -8,8 +8,8 @@ use App\Exceptions\GitHubNotFoundException; use App\Libraries\Commentable; use App\Libraries\Markdown\OsuMarkdown; +use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\OsuWiki; -use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use Carbon\Carbon; use Exception; @@ -330,15 +330,6 @@ public function sync($force = false) return $this; } - public function toOpengraph(?array $options = []): array - { - return [ - 'description' => blade_safe($this->previewText()), - 'image' => $this->firstImage(true), - 'title' => $this->title(), - ]; - } - public function pagePublishedAt() { $date = $this->page['header']['date'] ?? null; diff --git a/app/Models/User.php b/app/Models/User.php index 4db45fe1f06..c3665f80e5e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,6 +12,7 @@ use App\Libraries\BBCodeForDB; use App\Libraries\ChangeUsername; use App\Libraries\Elasticsearch\Indexable; +use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\Session\Store as SessionStore; use App\Libraries\Transactions\AfterCommit; use App\Libraries\User\DatadogLoginAttempt; @@ -20,7 +21,6 @@ use App\Libraries\UsernameValidation; use App\Models\Forum\TopicWatch; use App\Models\OAuth\Client; -use App\Models\Traits\HasOpengraph; use App\Traits\Memoizes; use App\Traits\Validatable; use Cache; @@ -1815,15 +1815,6 @@ public function titleUrl(): ?string return $this->rank?->url; } - public function toOpengraph(?array $options = []): array - { - return [ - 'description' => blade_safe($this->getOpengraphDescription($options)), - 'image' => $this->user_avatar, - 'title' => blade_safe(osu_trans('users.show.title', ['username' => $this->username])), - ]; - } - public function hasProfile() { return $this->getKey() !== null @@ -2401,39 +2392,6 @@ private function getDisplayedLastVisit() return $this->hide_presence ? null : $this->user_lastvisit; } - private function getOpengraphDescription(array $options = []): string - { - static $rankTypes = ['country', 'global']; - - $ruleset = $options['ruleset'] ?? $this->playmode; - $stats = $this->statistics($ruleset); - - $replacements['ruleset'] = $ruleset; - - foreach ($rankTypes as $type) { - $method = "{$type}Rank"; - $replacements[$type] = osu_trans("users.ogp.description.{$type}", [ - 'rank' => format_rank($stats?->$method()), - ]); - - $variants = Beatmap::VARIANTS[$ruleset] ?? []; - - $variantsTexts = null; - foreach ($variants as $variant) { - $variantRank = $this->statistics($ruleset, false, $variant)?->$method(); - if ($variantRank !== null) { - $variantsTexts[] = $variant.' '.format_rank($variantRank); - } - } - - if (!empty($variantsTexts)) { - $replacements[$type] .= ' ('.implode(', ', $variantsTexts).')'; - } - } - - return osu_trans('users.ogp.description._', $replacements); - } - private function getOsuPlaystyle() { $value = $this->getRawAttribute('osu_playstyle'); diff --git a/app/helpers.php b/app/helpers.php index 660fbf03877..22d6fb24aa4 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -4,6 +4,7 @@ // See the LICENCE file in the repository root for full licence text. use App\Libraries\LocaleMeta; +use App\Libraries\Opengraph\HasOpengraph; use App\Models\LoginAttempt; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\NoRFCWarningsValidation; @@ -1690,6 +1691,15 @@ function seeded_shuffle(array &$items, int $seed = 0) mt_srand(); } +function set_opengraph(HasOpengraph $model, ...$options) +{ + $className = 'App\\Libraries\\Opengraph\\'.get_class_basename(get_class($model)).'Opengraph'; + + View::share([ + 'opengraph' => (new $className($model, ...$options))->get(), + ]); +} + function first_paragraph($html, $split_on = "\n") { $text = strip_tags($html); From 92cb2fb75080d80d33e744c5e99006426c07de6d Mon Sep 17 00:00:00 2001 From: bakaneko Date: Fri, 20 Oct 2023 18:53:40 +0900 Subject: [PATCH 12/39] users.show.profile version of title not actually used on current site --- app/Libraries/Opengraph/UserOpengraph.php | 2 +- resources/lang/en/users.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 671052f3169..ec595baadc6 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -21,7 +21,7 @@ public function get() return [ 'description' => $this->description(), 'image' => $this->user->user_avatar, - 'title' => blade_safe(osu_trans('users.show.title', ['username' => $this->user->username])), + 'title' => blade_safe(str_replace(' ', ' ', e($this->user->username))).' · '.page_title(), ]; } diff --git a/resources/lang/en/users.php b/resources/lang/en/users.php index 23c2adf8712..04ae8eb3896 100644 --- a/resources/lang/en/users.php +++ b/resources/lang/en/users.php @@ -183,7 +183,6 @@ 'origin_country' => 'From :country', 'previous_usernames' => 'formerly known as', 'plays_with' => 'Plays with :devices', - 'title' => ":username's profile", 'comments_count' => [ '_' => 'Posted :link', From 23f63d01f3655951ba3810c6d887c7fbedf91a61 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 23 Oct 2023 18:55:24 +0900 Subject: [PATCH 13/39] add opengraph for comment --- app/Http/Controllers/CommentsController.php | 2 ++ app/Libraries/Opengraph/CommentOpengraph.php | 29 ++++++++++++++++++++ app/Models/Comment.php | 9 +++++- app/Transformers/CommentTransformer.php | 2 +- resources/lang/en/comments.php | 4 +++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 app/Libraries/Opengraph/CommentOpengraph.php diff --git a/app/Http/Controllers/CommentsController.php b/app/Http/Controllers/CommentsController.php index b89ce074b41..a85024632c9 100644 --- a/app/Http/Controllers/CommentsController.php +++ b/app/Http/Controllers/CommentsController.php @@ -161,6 +161,8 @@ public function show($id) return $commentBundle->toArray(); } + set_opengraph($comment); + return ext_view('comments.show', compact('commentBundle')); } diff --git a/app/Libraries/Opengraph/CommentOpengraph.php b/app/Libraries/Opengraph/CommentOpengraph.php new file mode 100644 index 00000000000..da8494eed60 --- /dev/null +++ b/app/Libraries/Opengraph/CommentOpengraph.php @@ -0,0 +1,29 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Comment; + +class CommentOpengraph +{ + public function __construct(private Comment $comment) + { + } + + public function get(): array + { + $user = $this->comment->user; + + return priv_check('CommentShow', $this->comment)->can() + ? [ + 'description' => html_excerpt($this->comment->message_html, 100), + 'image' => $user->user_avatar, + 'title' => osu_trans('comments.ogp.title', ['user' => $user->username]), + ] : []; + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 67a039cea05..90b14dbab96 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -6,6 +6,7 @@ namespace App\Models; use App\Libraries\MorphMap; +use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Validatable; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; @@ -37,7 +38,7 @@ * @property \Illuminate\Database\Eloquent\Collection $votes CommentVote * @property int $votes_count_cache */ -class Comment extends Model implements Traits\ReportableInterface +class Comment extends Model implements HasOpengraph, Traits\ReportableInterface { use Traits\Reportable, Traits\WithDbCursorHelper, Validatable; @@ -169,6 +170,7 @@ public function getAttribute($key) 'pinned' => (bool) $this->getRawAttribute($key), 'disqus_user_data' => $this->getDisqusUserData(), + 'message_html' => $this->getMessageHtml(), 'commentable', 'editor', @@ -308,4 +310,9 @@ private function getDisqusUserData(): ?array return $value === null ? null : json_decode($value, true); } + + private function getMessageHtml(): ?string + { + return markdown($this->message, 'comment'); + } } diff --git a/app/Transformers/CommentTransformer.php b/app/Transformers/CommentTransformer.php index 86237f177f1..64dfe893cb8 100644 --- a/app/Transformers/CommentTransformer.php +++ b/app/Transformers/CommentTransformer.php @@ -62,7 +62,7 @@ public function includeMessage(Comment $comment) public function includeMessageHtml(Comment $comment) { - return $this->primitive(markdown($comment->message, 'comment')); + return $this->primitive($comment->message_html); } public function includeUser(Comment $comment) diff --git a/resources/lang/en/comments.php b/resources/lang/en/comments.php index 0aac70ce8dd..9c2939f22e1 100644 --- a/resources/lang/en/comments.php +++ b/resources/lang/en/comments.php @@ -43,6 +43,10 @@ 'no_comments' => 'no comments found...', ], + 'ogp' => [ + 'title' => 'comment by :user', + ], + 'placeholder' => [ 'edit' => 'Edit the comment here', 'new' => 'Type new comment here', From efe5cda2caca471dce112e290aa6ab74c069bd23 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 23 Oct 2023 18:58:37 +0900 Subject: [PATCH 14/39] memoize message_html --- app/Models/Comment.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 90b14dbab96..35cd5fb7dc7 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -7,6 +7,7 @@ use App\Libraries\MorphMap; use App\Libraries\Opengraph\HasOpengraph; +use App\Traits\Memoizes; use App\Traits\Validatable; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; @@ -40,7 +41,7 @@ */ class Comment extends Model implements HasOpengraph, Traits\ReportableInterface { - use Traits\Reportable, Traits\WithDbCursorHelper, Validatable; + use Memoizes, Traits\Reportable, Traits\WithDbCursorHelper, Validatable; const COMMENTABLES = [ MorphMap::MAP[Beatmapset::class], @@ -123,6 +124,8 @@ public function replies() public function setMessageAttribute($value) { + $this->resetMemoized(); + return $this->attributes['message'] = trim(unzalgo($value)); } @@ -313,6 +316,8 @@ private function getDisqusUserData(): ?array private function getMessageHtml(): ?string { - return markdown($this->message, 'comment'); + return $this->memoize(__FUNCTION__, function () { + return markdown($this->message, 'comment'); + }); } } From 7dbb641918b51ee472fe9c33c26a25ed38c7d8b2 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 23 Oct 2023 21:10:31 +0900 Subject: [PATCH 15/39] wiki page opengraph --- app/Http/Controllers/WikiController.php | 2 ++ app/Libraries/Opengraph/PageOpengraph.php | 28 +++++++++++++++++++++++ app/Models/Wiki/Page.php | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/Libraries/Opengraph/PageOpengraph.php diff --git a/app/Http/Controllers/WikiController.php b/app/Http/Controllers/WikiController.php index b735db094e2..78368135ae4 100644 --- a/app/Http/Controllers/WikiController.php +++ b/app/Http/Controllers/WikiController.php @@ -107,6 +107,8 @@ public function show($locale = null, $path = null) return json_item($page, 'WikiPage'); } + set_opengraph($page); + return ext_view( $page->template(), ['contentLocale' => $page->locale, 'page' => $page], diff --git a/app/Libraries/Opengraph/PageOpengraph.php b/app/Libraries/Opengraph/PageOpengraph.php new file mode 100644 index 00000000000..f78d66220ba --- /dev/null +++ b/app/Libraries/Opengraph/PageOpengraph.php @@ -0,0 +1,28 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +use App\Models\Wiki\Page; + +class PageOpengraph +{ + public function __construct(private Page $page) + { + } + + public function get(): array + { + $title = $this->page->title(); + + return [ + // TODO: need a way to mark which wiki text to use as excerpt; first_paragraph just returns the title on wiki. + 'description' => html_excerpt($this->page->get()['output']), + 'title' => $title, + ]; + } +} diff --git a/app/Models/Wiki/Page.php b/app/Models/Wiki/Page.php index a055c574f5f..4aff532a18d 100644 --- a/app/Models/Wiki/Page.php +++ b/app/Models/Wiki/Page.php @@ -12,6 +12,7 @@ use App\Libraries\Elasticsearch\Sort; use App\Libraries\LocaleMeta; use App\Libraries\Markdown\OsuMarkdown; +use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\OsuWiki; use App\Libraries\Search\BasicSearch; use App\Libraries\Wiki\MainPageRenderer; @@ -23,7 +24,7 @@ use Exception; use Log; -class Page implements WikiObject +class Page implements HasOpengraph, WikiObject { use Memoizes, Traits\Es\WikiPageSearch; From 59274279c5a476a0b7f90d4a261e1af8bbc58fbd Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 18:04:27 +0900 Subject: [PATCH 16/39] record excerpt from first text paragraph --- app/Libraries/Markdown/Osu/DocumentProcessor.php | 15 +++++++++++++++ app/Libraries/Markdown/Osu/Extension.php | 1 + app/Libraries/Markdown/OsuMarkdown.php | 5 +++++ app/Libraries/Opengraph/PageOpengraph.php | 7 +++---- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Libraries/Markdown/Osu/DocumentProcessor.php b/app/Libraries/Markdown/Osu/DocumentProcessor.php index de290acc677..6db82b5dda8 100644 --- a/app/Libraries/Markdown/Osu/DocumentProcessor.php +++ b/app/Libraries/Markdown/Osu/DocumentProcessor.php @@ -20,6 +20,7 @@ class DocumentProcessor { + public ?string $excerpt = null; public ?string $firstImage; public ?string $title; public ?array $toc; @@ -53,6 +54,7 @@ public function __invoke(DocumentParsedEvent $event): void $this->relativeUrlRoot = $relativeUrlRoot === null ? null : urldecode($relativeUrlRoot); $fixWikiUrl = $this->config->get('osu_extension/fix_wiki_url'); $generateToc = $this->config->get('osu_extension/generate_toc'); + $recordExcerpt = $this->config->get('osu_extension/record_excerpt'); $recordFirstImage = $this->config->get('osu_extension/record_first_image'); $titleFromDocument = $this->config->get('osu_extension/title_from_document'); $withGallery = $this->config->get('osu_extension/with_gallery'); @@ -81,6 +83,10 @@ public function __invoke(DocumentParsedEvent $event): void $this->recordFirstImage(); } + if ($recordExcerpt) { + $this->recordExcerpt(); + } + if ($titleFromDocument) { $this->setTitle(); } @@ -263,6 +269,15 @@ private function proxyImage() } } + private function recordExcerpt(): void + { + if ($this->excerpt !== null || !$this->node instanceof Paragraph || !$this->event->isEntering()) { + return; + } + + $this->excerpt = presence($this->getText($this->node)); + } + private function recordFirstImage() { if ($this->firstImage !== null || !$this->node instanceof Inline\Image || !$this->event->isEntering()) { diff --git a/app/Libraries/Markdown/Osu/Extension.php b/app/Libraries/Markdown/Osu/Extension.php index 0046ec2ac20..ec4d950b041 100644 --- a/app/Libraries/Markdown/Osu/Extension.php +++ b/app/Libraries/Markdown/Osu/Extension.php @@ -30,6 +30,7 @@ public function configureSchema(ConfigurationBuilderInterface $builder): void 'custom_container_inline' => Expect::bool(), 'fix_wiki_url' => Expect::bool(), 'generate_toc' => Expect::bool(), + 'record_excerpt' => Expect::bool(), 'record_first_image' => Expect::bool(), 'relative_url_root' => Expect::string()->nullable(), 'style_block_allowed_classes' => Expect::array()->nullable(), diff --git a/app/Libraries/Markdown/OsuMarkdown.php b/app/Libraries/Markdown/OsuMarkdown.php index 05c2ec64d4c..ac1ef33bf5b 100644 --- a/app/Libraries/Markdown/OsuMarkdown.php +++ b/app/Libraries/Markdown/OsuMarkdown.php @@ -44,6 +44,7 @@ class OsuMarkdown 'custom_container_inline' => false, 'fix_wiki_url' => false, 'generate_toc' => false, + 'record_excerpt' => false, 'record_first_image' => false, 'relative_url_root' => null, 'style_block_allowed_classes' => null, @@ -125,6 +126,7 @@ class OsuMarkdown 'custom_container_inline' => true, 'fix_wiki_url' => true, 'generate_toc' => true, + 'record_excerpt' => true, 'style_block_allowed_classes' => ['infobox'], 'title_from_document' => true, 'with_gallery' => true, @@ -141,6 +143,7 @@ class OsuMarkdown private array $osuMarkdownConfig; private $document = ''; + private $excerpt; private $firstImage; private $header; private $toc; @@ -215,6 +218,7 @@ public function html(): string $this->header['title'] = $processor->title; } + $this->excerpt = $processor->excerpt; $this->firstImage = $processor->firstImage; $this->toc = $processor->toc; @@ -247,6 +251,7 @@ public function toArray() $html = $this->html(); return [ + 'excerpt' => $this->excerpt, 'firstImage' => $this->firstImage, 'header' => $this->header, 'output' => $html, diff --git a/app/Libraries/Opengraph/PageOpengraph.php b/app/Libraries/Opengraph/PageOpengraph.php index f78d66220ba..c757f896d36 100644 --- a/app/Libraries/Opengraph/PageOpengraph.php +++ b/app/Libraries/Opengraph/PageOpengraph.php @@ -17,12 +17,11 @@ public function __construct(private Page $page) public function get(): array { - $title = $this->page->title(); + $pageData = $this->page->get(); return [ - // TODO: need a way to mark which wiki text to use as excerpt; first_paragraph just returns the title on wiki. - 'description' => html_excerpt($this->page->get()['output']), - 'title' => $title, + 'description' => $pageData['excerpt'] ?? html_excerpt($pageData['output']), // html_excerpt fallback + 'title' => $this->page->title(), ]; } } From cfdf270a74f252a8590905388c7be669da04c39c Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 18:14:47 +0900 Subject: [PATCH 17/39] no extra data for missing page --- app/Libraries/Opengraph/PageOpengraph.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Libraries/Opengraph/PageOpengraph.php b/app/Libraries/Opengraph/PageOpengraph.php index c757f896d36..5ff14b8f01d 100644 --- a/app/Libraries/Opengraph/PageOpengraph.php +++ b/app/Libraries/Opengraph/PageOpengraph.php @@ -17,6 +17,10 @@ public function __construct(private Page $page) public function get(): array { + if (!$this->page->isVisible()) { + return []; + } + $pageData = $this->page->get(); return [ From 32e692f0b681e8b86168ab5028c65e2adf527076 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 18:47:56 +0900 Subject: [PATCH 18/39] single line --- app/Models/Comment.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 35cd5fb7dc7..dd5b398433b 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -316,8 +316,6 @@ private function getDisqusUserData(): ?array private function getMessageHtml(): ?string { - return $this->memoize(__FUNCTION__, function () { - return markdown($this->message, 'comment'); - }); + return $this->memoize(__FUNCTION__, fn () => markdown($this->message, 'comment')); } } From 991b6e27792c37fddf4c120c564e43b8c0ca6257 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 18:48:32 +0900 Subject: [PATCH 19/39] don't coalesce into self --- resources/views/layout/metadata.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index c84f173f6bc..6abdc4fbd7a 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -7,7 +7,7 @@ $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); - $opengraph['description'] ??= $opengraph['description'] ?? $pageDescription ?? null; + $opengraph['description'] ??= $pageDescription ?? null; @endphp From c2b393f644ffad7516c913b648e75458f2adecff Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 20:24:31 +0900 Subject: [PATCH 20/39] non-model namespace can be problem for future (if at all) --- app/Libraries/Opengraph/{ => Forum}/ForumOpengraph.php | 2 +- app/Libraries/Opengraph/{ => Forum}/TopicOpengraph.php | 2 +- app/Libraries/Opengraph/{ => Wiki}/PageOpengraph.php | 2 +- app/helpers.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename app/Libraries/Opengraph/{ => Forum}/ForumOpengraph.php (95%) rename app/Libraries/Opengraph/{ => Forum}/TopicOpengraph.php (94%) rename app/Libraries/Opengraph/{ => Wiki}/PageOpengraph.php (94%) diff --git a/app/Libraries/Opengraph/ForumOpengraph.php b/app/Libraries/Opengraph/Forum/ForumOpengraph.php similarity index 95% rename from app/Libraries/Opengraph/ForumOpengraph.php rename to app/Libraries/Opengraph/Forum/ForumOpengraph.php index ddee5c2c0f1..ea5ed1733d4 100644 --- a/app/Libraries/Opengraph/ForumOpengraph.php +++ b/app/Libraries/Opengraph/Forum/ForumOpengraph.php @@ -5,7 +5,7 @@ declare(strict_types=1); -namespace App\Libraries\Opengraph; +namespace App\Libraries\Opengraph\Forum; use App\Models\Forum\Forum; diff --git a/app/Libraries/Opengraph/TopicOpengraph.php b/app/Libraries/Opengraph/Forum/TopicOpengraph.php similarity index 94% rename from app/Libraries/Opengraph/TopicOpengraph.php rename to app/Libraries/Opengraph/Forum/TopicOpengraph.php index 2714b404b04..44088789bc3 100644 --- a/app/Libraries/Opengraph/TopicOpengraph.php +++ b/app/Libraries/Opengraph/Forum/TopicOpengraph.php @@ -5,7 +5,7 @@ declare(strict_types=1); -namespace App\Libraries\Opengraph; +namespace App\Libraries\Opengraph\Forum; use App\Models\Forum\Topic; diff --git a/app/Libraries/Opengraph/PageOpengraph.php b/app/Libraries/Opengraph/Wiki/PageOpengraph.php similarity index 94% rename from app/Libraries/Opengraph/PageOpengraph.php rename to app/Libraries/Opengraph/Wiki/PageOpengraph.php index 5ff14b8f01d..e2c8106a318 100644 --- a/app/Libraries/Opengraph/PageOpengraph.php +++ b/app/Libraries/Opengraph/Wiki/PageOpengraph.php @@ -5,7 +5,7 @@ declare(strict_types=1); -namespace App\Libraries\Opengraph; +namespace App\Libraries\Opengraph\Wiki; use App\Models\Wiki\Page; diff --git a/app/helpers.php b/app/helpers.php index 22d6fb24aa4..8e269f175bf 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -1693,7 +1693,7 @@ function seeded_shuffle(array &$items, int $seed = 0) function set_opengraph(HasOpengraph $model, ...$options) { - $className = 'App\\Libraries\\Opengraph\\'.get_class_basename(get_class($model)).'Opengraph'; + $className = str_replace('App\Models', 'App\Libraries\Opengraph', $model::class).'Opengraph'; View::share([ 'opengraph' => (new $className($model, ...$options))->get(), From bb950355130fee3796f990723167b2b45036599b Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 20:45:32 +0900 Subject: [PATCH 21/39] set only on current request --- app/helpers.php | 4 +--- resources/views/layout/metadata.blade.php | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index 8e269f175bf..9df3818d4eb 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -1695,9 +1695,7 @@ function set_opengraph(HasOpengraph $model, ...$options) { $className = str_replace('App\Models', 'App\Libraries\Opengraph', $model::class).'Opengraph'; - View::share([ - 'opengraph' => (new $className($model, ...$options))->get(), - ]); + Request::instance()->attributes->set('opengraph', (new $className($model, ...$options))->get()); } function first_paragraph($html, $split_on = "\n") diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 6abdc4fbd7a..8dcd391aaa6 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -6,6 +6,7 @@ $appUrl = config('app.url'); $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); + $opengraph = Request::instance()->attributes->get('opengraph'); $opengraph['description'] ??= $pageDescription ?? null; @endphp From 911d0f44e380ffba9c1abfd3d7d8637a56d5c9a1 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 21:31:45 +0900 Subject: [PATCH 22/39] modding opengraph descriptions --- .../Users/ModdingHistoryController.php | 2 ++ .../Users/MultiplayerController.php | 2 ++ app/Http/Controllers/UsersController.php | 2 +- app/Libraries/Opengraph/UserOpengraph.php | 27 ++++++++++++++++--- resources/lang/en/users.php | 3 +++ .../users/beatmapset_activities.blade.php | 1 - .../views/users/multiplayer/index.blade.php | 1 - 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Users/ModdingHistoryController.php b/app/Http/Controllers/Users/ModdingHistoryController.php index 24b335984c1..86f1670faf9 100644 --- a/app/Http/Controllers/Users/ModdingHistoryController.php +++ b/app/Http/Controllers/Users/ModdingHistoryController.php @@ -47,6 +47,8 @@ public function index() $jsonChunks = ModdingHistoryEventsBundle::forProfile($user, $this->searchParams)->toArray(); + set_opengraph($this->user, 'modding'); + return ext_view('users.beatmapset_activities', compact( 'jsonChunks', 'user' diff --git a/app/Http/Controllers/Users/MultiplayerController.php b/app/Http/Controllers/Users/MultiplayerController.php index a81a4a371e0..f5d3abbca4c 100644 --- a/app/Http/Controllers/Users/MultiplayerController.php +++ b/app/Http/Controllers/Users/MultiplayerController.php @@ -36,6 +36,8 @@ public function index($userId, $typeGroup) return $json; } + set_opengraph($user, 'multiplayer'); + $jsonUser = json_item( $user, (new UserTransformer())->setMode($user->playmode), diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 6c6dae32465..d772a59285e 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -654,7 +654,7 @@ public function show($id, $mode = null) 'user' => $userArray, ]; - set_opengraph($user, $currentMode); + set_opengraph($user, 'show', $currentMode); return ext_view('users.show', compact('initialData', 'mode', 'user')); } diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index ec595baadc6..d26a142e7ff 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -12,20 +12,41 @@ class UserOpengraph { - public function __construct(private User $user, private string $ruleset) + public function __construct(private User $user, private string $page, private ?string $ruleset = null) { } public function get() { return [ - 'description' => $this->description(), + // none for multiplayer, playlist counts seems...not useful? + 'description' => $this->page === 'modding' ? $this->moddingDescription() : $this->showDescription(), 'image' => $this->user->user_avatar, 'title' => blade_safe(str_replace(' ', ' ', e($this->user->username))).' · '.page_title(), ]; } - private function description(): string + private function moddingDescription(): string + { + static $statuses = ['ranked', 'loved', 'pending', 'graveyard']; + + $countsText = []; + foreach ($statuses as $status) { + $count = $this->user->profileBeatmapsetCountByGroupedStatus($status); + if ($count > 0) { + $countsText[] = osu_trans("beatmapsets.show.status.{$status}").' '.number_format($count); + } + } + + return empty($countsText) + ? osu_trans("users.ogp.modding_description_empty", ['user' => $this->user->username]) + : osu_trans("users.ogp.modding_description", [ + 'counts' => implode(' | ', $countsText), + 'user' => $this->user->username, + ]); + } + + private function showDescription(): string { static $rankTypes = ['country', 'global']; diff --git a/resources/lang/en/users.php b/resources/lang/en/users.php index 04ae8eb3896..a9651a1dff5 100644 --- a/resources/lang/en/users.php +++ b/resources/lang/en/users.php @@ -124,6 +124,9 @@ ], 'ogp' => [ + 'modding_description' => ':user\'s beatmaps: :counts', + 'modding_description_empty' => ':user doesn\'t have nay beatmaps...', + 'description' => [ '_' => 'Rank (:ruleset): :global | :country', 'country' => 'Country :rank', diff --git a/resources/views/users/beatmapset_activities.blade.php b/resources/views/users/beatmapset_activities.blade.php index b7056ee9c8e..72d1ca9eb2e 100644 --- a/resources/views/users/beatmapset_activities.blade.php +++ b/resources/views/users/beatmapset_activities.blade.php @@ -3,7 +3,6 @@ See the LICENCE file in the repository root for full licence text. --}} @extends('master', [ - 'titlePrepend' => blade_safe(str_replace(' ', ' ', e($user->username))), 'pageDescription' => page_description($user->username), ]) diff --git a/resources/views/users/multiplayer/index.blade.php b/resources/views/users/multiplayer/index.blade.php index dd45d59b228..69f116be15d 100644 --- a/resources/views/users/multiplayer/index.blade.php +++ b/resources/views/users/multiplayer/index.blade.php @@ -3,7 +3,6 @@ See the LICENCE file in the repository root for full licence text. --}} @extends('master', [ - 'titlePrepend' => blade_safe(str_replace(' ', ' ', e($user->username))), 'pageDescription' => page_description($user->username), ]) From bde5e301e2b1e68728f5e6d8bfcbc3ddd652ac7e Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 25 Oct 2023 21:38:50 +0900 Subject: [PATCH 23/39] single quotes --- app/Libraries/Opengraph/UserOpengraph.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index d26a142e7ff..2ad4e8d9999 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -39,8 +39,8 @@ private function moddingDescription(): string } return empty($countsText) - ? osu_trans("users.ogp.modding_description_empty", ['user' => $this->user->username]) - : osu_trans("users.ogp.modding_description", [ + ? osu_trans('users.ogp.modding_description_empty', ['user' => $this->user->username]) + : osu_trans('users.ogp.modding_description', [ 'counts' => implode(' | ', $countsText), 'user' => $this->user->username, ]); From 0bf535b88a55cbb287e2db002cf447c05658a121 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 16:46:40 +0900 Subject: [PATCH 24/39] typo --- resources/lang/en/users.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lang/en/users.php b/resources/lang/en/users.php index a9651a1dff5..d5cd6435130 100644 --- a/resources/lang/en/users.php +++ b/resources/lang/en/users.php @@ -125,7 +125,7 @@ 'ogp' => [ 'modding_description' => ':user\'s beatmaps: :counts', - 'modding_description_empty' => ':user doesn\'t have nay beatmaps...', + 'modding_description_empty' => ':user doesn\'t have any beatmaps...', 'description' => [ '_' => 'Rank (:ruleset): :global | :country', From 078e18fcb1974624e029367d96ca144669eb514d Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 16:48:17 +0900 Subject: [PATCH 25/39] already escaped --- app/Libraries/Opengraph/CommentOpengraph.php | 2 +- app/Libraries/Opengraph/Wiki/PageOpengraph.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Libraries/Opengraph/CommentOpengraph.php b/app/Libraries/Opengraph/CommentOpengraph.php index da8494eed60..ab238e63969 100644 --- a/app/Libraries/Opengraph/CommentOpengraph.php +++ b/app/Libraries/Opengraph/CommentOpengraph.php @@ -21,7 +21,7 @@ public function get(): array return priv_check('CommentShow', $this->comment)->can() ? [ - 'description' => html_excerpt($this->comment->message_html, 100), + 'description' => blade_safe(html_excerpt($this->comment->message_html, 100)), 'image' => $user->user_avatar, 'title' => osu_trans('comments.ogp.title', ['user' => $user->username]), ] : []; diff --git a/app/Libraries/Opengraph/Wiki/PageOpengraph.php b/app/Libraries/Opengraph/Wiki/PageOpengraph.php index e2c8106a318..269dd97ac00 100644 --- a/app/Libraries/Opengraph/Wiki/PageOpengraph.php +++ b/app/Libraries/Opengraph/Wiki/PageOpengraph.php @@ -24,7 +24,7 @@ public function get(): array $pageData = $this->page->get(); return [ - 'description' => $pageData['excerpt'] ?? html_excerpt($pageData['output']), // html_excerpt fallback + 'description' => $pageData['excerpt'] ?? blade_safe(html_excerpt($pageData['output'])), // html_excerpt fallback 'title' => $this->page->title(), ]; } From 6b5cef0db6c0df93c1313c413018173c12bf1842 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 17:21:05 +0900 Subject: [PATCH 26/39] Revert "record excerpt from first text paragraph" This reverts commit 59274279c5a476a0b7f90d4a261e1af8bbc58fbd. --- app/Libraries/Markdown/Osu/DocumentProcessor.php | 15 --------------- app/Libraries/Markdown/Osu/Extension.php | 1 - app/Libraries/Markdown/OsuMarkdown.php | 5 ----- app/Libraries/Opengraph/Wiki/PageOpengraph.php | 4 +--- 4 files changed, 1 insertion(+), 24 deletions(-) diff --git a/app/Libraries/Markdown/Osu/DocumentProcessor.php b/app/Libraries/Markdown/Osu/DocumentProcessor.php index 6db82b5dda8..de290acc677 100644 --- a/app/Libraries/Markdown/Osu/DocumentProcessor.php +++ b/app/Libraries/Markdown/Osu/DocumentProcessor.php @@ -20,7 +20,6 @@ class DocumentProcessor { - public ?string $excerpt = null; public ?string $firstImage; public ?string $title; public ?array $toc; @@ -54,7 +53,6 @@ public function __invoke(DocumentParsedEvent $event): void $this->relativeUrlRoot = $relativeUrlRoot === null ? null : urldecode($relativeUrlRoot); $fixWikiUrl = $this->config->get('osu_extension/fix_wiki_url'); $generateToc = $this->config->get('osu_extension/generate_toc'); - $recordExcerpt = $this->config->get('osu_extension/record_excerpt'); $recordFirstImage = $this->config->get('osu_extension/record_first_image'); $titleFromDocument = $this->config->get('osu_extension/title_from_document'); $withGallery = $this->config->get('osu_extension/with_gallery'); @@ -83,10 +81,6 @@ public function __invoke(DocumentParsedEvent $event): void $this->recordFirstImage(); } - if ($recordExcerpt) { - $this->recordExcerpt(); - } - if ($titleFromDocument) { $this->setTitle(); } @@ -269,15 +263,6 @@ private function proxyImage() } } - private function recordExcerpt(): void - { - if ($this->excerpt !== null || !$this->node instanceof Paragraph || !$this->event->isEntering()) { - return; - } - - $this->excerpt = presence($this->getText($this->node)); - } - private function recordFirstImage() { if ($this->firstImage !== null || !$this->node instanceof Inline\Image || !$this->event->isEntering()) { diff --git a/app/Libraries/Markdown/Osu/Extension.php b/app/Libraries/Markdown/Osu/Extension.php index ec4d950b041..0046ec2ac20 100644 --- a/app/Libraries/Markdown/Osu/Extension.php +++ b/app/Libraries/Markdown/Osu/Extension.php @@ -30,7 +30,6 @@ public function configureSchema(ConfigurationBuilderInterface $builder): void 'custom_container_inline' => Expect::bool(), 'fix_wiki_url' => Expect::bool(), 'generate_toc' => Expect::bool(), - 'record_excerpt' => Expect::bool(), 'record_first_image' => Expect::bool(), 'relative_url_root' => Expect::string()->nullable(), 'style_block_allowed_classes' => Expect::array()->nullable(), diff --git a/app/Libraries/Markdown/OsuMarkdown.php b/app/Libraries/Markdown/OsuMarkdown.php index ac1ef33bf5b..05c2ec64d4c 100644 --- a/app/Libraries/Markdown/OsuMarkdown.php +++ b/app/Libraries/Markdown/OsuMarkdown.php @@ -44,7 +44,6 @@ class OsuMarkdown 'custom_container_inline' => false, 'fix_wiki_url' => false, 'generate_toc' => false, - 'record_excerpt' => false, 'record_first_image' => false, 'relative_url_root' => null, 'style_block_allowed_classes' => null, @@ -126,7 +125,6 @@ class OsuMarkdown 'custom_container_inline' => true, 'fix_wiki_url' => true, 'generate_toc' => true, - 'record_excerpt' => true, 'style_block_allowed_classes' => ['infobox'], 'title_from_document' => true, 'with_gallery' => true, @@ -143,7 +141,6 @@ class OsuMarkdown private array $osuMarkdownConfig; private $document = ''; - private $excerpt; private $firstImage; private $header; private $toc; @@ -218,7 +215,6 @@ public function html(): string $this->header['title'] = $processor->title; } - $this->excerpt = $processor->excerpt; $this->firstImage = $processor->firstImage; $this->toc = $processor->toc; @@ -251,7 +247,6 @@ public function toArray() $html = $this->html(); return [ - 'excerpt' => $this->excerpt, 'firstImage' => $this->firstImage, 'header' => $this->header, 'output' => $html, diff --git a/app/Libraries/Opengraph/Wiki/PageOpengraph.php b/app/Libraries/Opengraph/Wiki/PageOpengraph.php index 269dd97ac00..80cc21d17fb 100644 --- a/app/Libraries/Opengraph/Wiki/PageOpengraph.php +++ b/app/Libraries/Opengraph/Wiki/PageOpengraph.php @@ -21,10 +21,8 @@ public function get(): array return []; } - $pageData = $this->page->get(); - return [ - 'description' => $pageData['excerpt'] ?? blade_safe(html_excerpt($pageData['output'])), // html_excerpt fallback + 'description' => html_excerpt($this->page->get()['output']), 'title' => $this->page->title(), ]; } From 6188225c5b5b75ebdd108f6906fd4011b89dd6db Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 17:21:33 +0900 Subject: [PATCH 27/39] use the text-only searchable version for opengraph previews. The downside is it makes elasticsearch return more text that we don't need --- app/Libraries/Opengraph/Wiki/PageOpengraph.php | 2 +- app/Models/Wiki/Page.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Libraries/Opengraph/Wiki/PageOpengraph.php b/app/Libraries/Opengraph/Wiki/PageOpengraph.php index 80cc21d17fb..804484987e6 100644 --- a/app/Libraries/Opengraph/Wiki/PageOpengraph.php +++ b/app/Libraries/Opengraph/Wiki/PageOpengraph.php @@ -22,7 +22,7 @@ public function get(): array } return [ - 'description' => html_excerpt($this->page->get()['output']), + 'description' => $this->page->getTextPreview(), 'title' => $this->page->title(), ]; } diff --git a/app/Models/Wiki/Page.php b/app/Models/Wiki/Page.php index 4aff532a18d..03a810c5977 100644 --- a/app/Models/Wiki/Page.php +++ b/app/Models/Wiki/Page.php @@ -229,7 +229,7 @@ public function esIndexDocument(array $options = []) public function esFetch() { $response = (new BasicSearch(static::esIndexName(), 'wiki_page_lookup')) - ->source(['markdown', 'page', 'indexed_at', 'version']) + ->source(['markdown', 'page', 'page_text', 'indexed_at', 'version']) ->query([ 'term' => [ '_id' => $this->pagePath(), @@ -252,6 +252,11 @@ public function getMarkdown() return $this->source['markdown'] ?? null; } + public function getTextPreview() + { + return html_excerpt($this->source['page_text']); + } + public function hasParent() { return $this->parent() !== null; From ada79a7b4d8cd984f6d3c9d582f9b8f79a7f75a7 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 21:02:38 +0900 Subject: [PATCH 28/39] assume always guest for opengraph headers --- app/Libraries/Opengraph/CommentOpengraph.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Libraries/Opengraph/CommentOpengraph.php b/app/Libraries/Opengraph/CommentOpengraph.php index ab238e63969..7d4d0fa284a 100644 --- a/app/Libraries/Opengraph/CommentOpengraph.php +++ b/app/Libraries/Opengraph/CommentOpengraph.php @@ -19,7 +19,7 @@ public function get(): array { $user = $this->comment->user; - return priv_check('CommentShow', $this->comment)->can() + return priv_check_user(null, 'CommentShow', $this->comment)->can() ? [ 'description' => blade_safe(html_excerpt($this->comment->message_html, 100)), 'image' => $user->user_avatar, From 6560b148074a69c463fb150515a17f466dd43191 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 30 Oct 2023 22:33:07 +0900 Subject: [PATCH 29/39] opengraph title doesn't override titlePrepend --- app/Libraries/Opengraph/UserOpengraph.php | 2 +- app/helpers.php | 5 +++++ resources/views/users/beatmapset_activities.blade.php | 1 + resources/views/users/multiplayer/index.blade.php | 1 + resources/views/users/show.blade.php | 2 +- 5 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 2ad4e8d9999..8f4e666c609 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -22,7 +22,7 @@ public function get() // none for multiplayer, playlist counts seems...not useful? 'description' => $this->page === 'modding' ? $this->moddingDescription() : $this->showDescription(), 'image' => $this->user->user_avatar, - 'title' => blade_safe(str_replace(' ', ' ', e($this->user->username))).' · '.page_title(), + 'title' => blade_safe(escape_username($this->user->username)).' · '.page_title(), ]; } diff --git a/app/helpers.php b/app/helpers.php index 9df3818d4eb..980bbf401a1 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -378,6 +378,11 @@ function default_mode() return optional(auth()->user())->playmode ?? 'osu'; } +function escape_username(string $username) +{ + return str_replace(' ', ' ', e($username)); +} + function flag_url($countryCode) { $chars = str_split($countryCode); diff --git a/resources/views/users/beatmapset_activities.blade.php b/resources/views/users/beatmapset_activities.blade.php index 72d1ca9eb2e..4fdca750bd4 100644 --- a/resources/views/users/beatmapset_activities.blade.php +++ b/resources/views/users/beatmapset_activities.blade.php @@ -4,6 +4,7 @@ --}} @extends('master', [ 'pageDescription' => page_description($user->username), + 'titlePrepend' => blade_safe(escape_username($user->username)), ]) @section('content') diff --git a/resources/views/users/multiplayer/index.blade.php b/resources/views/users/multiplayer/index.blade.php index 69f116be15d..c998b00c7b6 100644 --- a/resources/views/users/multiplayer/index.blade.php +++ b/resources/views/users/multiplayer/index.blade.php @@ -3,6 +3,7 @@ See the LICENCE file in the repository root for full licence text. --}} @extends('master', [ + 'titlePrepend' => blade_safe(escape_username($user->username)), 'pageDescription' => page_description($user->username), ]) diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index 8fd3a063eef..fa97731a659 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -5,7 +5,7 @@ @extends('master', [ 'canonicalUrl' => $user->url($mode), - 'titlePrepend' => blade_safe(str_replace(' ', ' ', e($user->username))), + 'titlePrepend' => blade_safe(escape_username($user->username)), ]) @section('content') From 9a9fc1d9bd6b2f18b84c584634bdf163bbad2e6e Mon Sep 17 00:00:00 2001 From: bakaneko Date: Thu, 2 Nov 2023 15:53:22 +0900 Subject: [PATCH 30/39] remove username from description (standardized with profile info) --- app/Libraries/Opengraph/UserOpengraph.php | 3 +-- resources/lang/en/users.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 8f4e666c609..70cd6106bb9 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -39,10 +39,9 @@ private function moddingDescription(): string } return empty($countsText) - ? osu_trans('users.ogp.modding_description_empty', ['user' => $this->user->username]) + ? osu_trans('users.ogp.modding_description_empty') : osu_trans('users.ogp.modding_description', [ 'counts' => implode(' | ', $countsText), - 'user' => $this->user->username, ]); } diff --git a/resources/lang/en/users.php b/resources/lang/en/users.php index d5cd6435130..97eadd7277a 100644 --- a/resources/lang/en/users.php +++ b/resources/lang/en/users.php @@ -124,8 +124,8 @@ ], 'ogp' => [ - 'modding_description' => ':user\'s beatmaps: :counts', - 'modding_description_empty' => ':user doesn\'t have any beatmaps...', + 'modding_description' => 'Beatmaps: :counts', + 'modding_description_empty' => 'User doesn\'t have any beatmaps...', 'description' => [ '_' => 'Rank (:ruleset): :global | :country', From fb6f795fdec404efb8b5a40c10996f24796af6fc Mon Sep 17 00:00:00 2001 From: bakaneko Date: Thu, 2 Nov 2023 16:06:51 +0900 Subject: [PATCH 31/39] missing typehint --- app/Libraries/Opengraph/UserOpengraph.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 70cd6106bb9..6f3ea11bbdd 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -16,7 +16,7 @@ public function __construct(private User $user, private string $page, private ?s { } - public function get() + public function get(): array { return [ // none for multiplayer, playlist counts seems...not useful? From 0bb17e0d120416a58c2076f58fdd405650a67e44 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Thu, 2 Nov 2023 16:15:31 +0900 Subject: [PATCH 32/39] add page_title to opengraph title by default; site_name isn't displayed on most cards, also apparently should just be "osu!"? --- app/Libraries/Opengraph/UserOpengraph.php | 2 +- resources/views/layout/metadata.blade.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 6f3ea11bbdd..2169ceae017 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -22,7 +22,7 @@ public function get(): array // none for multiplayer, playlist counts seems...not useful? 'description' => $this->page === 'modding' ? $this->moddingDescription() : $this->showDescription(), 'image' => $this->user->user_avatar, - 'title' => blade_safe(escape_username($this->user->username)).' · '.page_title(), + 'title' => blade_safe(escape_username($this->user->username)), ]; } diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 8dcd391aaa6..e392d95d605 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -34,6 +34,11 @@ @foreach ($opengraph as $key => $value) @if (present($value)) + @php + if ($key === 'title') { + $value .= ' · '.page_title(); + } + @endphp @endif @endforeach From 68451af973ea8b74afeafa19fb2b924ee7a33d5b Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 6 Nov 2023 14:38:31 +0900 Subject: [PATCH 33/39] rewrap in HtmlString after appending page_title --- app/Libraries/Opengraph/UserOpengraph.php | 8 +++++++- app/helpers.php | 5 ----- resources/views/layout/metadata.blade.php | 6 ++++++ resources/views/users/multiplayer/index.blade.php | 2 +- resources/views/users/show.blade.php | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 2169ceae017..f6b758eb875 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -9,9 +9,15 @@ use App\Models\Beatmap; use App\Models\User; +use Illuminate\Support\HtmlString; class UserOpengraph { + public static function escapeForTitle(string $username) + { + return blade_safe(str_replace(' ', ' ', e($username))); + } + public function __construct(private User $user, private string $page, private ?string $ruleset = null) { } @@ -22,7 +28,7 @@ public function get(): array // none for multiplayer, playlist counts seems...not useful? 'description' => $this->page === 'modding' ? $this->moddingDescription() : $this->showDescription(), 'image' => $this->user->user_avatar, - 'title' => blade_safe(escape_username($this->user->username)), + 'title' => static::escapeForTitle($this->user->username), ]; } diff --git a/app/helpers.php b/app/helpers.php index 980bbf401a1..9df3818d4eb 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -378,11 +378,6 @@ function default_mode() return optional(auth()->user())->playmode ?? 'osu'; } -function escape_username(string $username) -{ - return str_replace(' ', ' ', e($username)); -} - function flag_url($countryCode) { $chars = str_split($countryCode); diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index e392d95d605..77c5fb8656e 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -3,6 +3,8 @@ See the LICENCE file in the repository root for full licence text. --}} @php + use Illuminate\Support\HtmlString; + $appUrl = config('app.url'); $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); @@ -36,7 +38,11 @@ @if (present($value)) @php if ($key === 'title') { + $isHtmlString = $value instanceof HtmlString; $value .= ' · '.page_title(); + if ($isHtmlString) { + $value = new HtmlString($value); + } } @endphp diff --git a/resources/views/users/multiplayer/index.blade.php b/resources/views/users/multiplayer/index.blade.php index c998b00c7b6..7068061c619 100644 --- a/resources/views/users/multiplayer/index.blade.php +++ b/resources/views/users/multiplayer/index.blade.php @@ -3,7 +3,7 @@ See the LICENCE file in the repository root for full licence text. --}} @extends('master', [ - 'titlePrepend' => blade_safe(escape_username($user->username)), + 'titlePrepend' => \App\Libraries\Opengraph::escapeForTitle($user->username), 'pageDescription' => page_description($user->username), ]) diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index fa97731a659..146a602893c 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -5,7 +5,7 @@ @extends('master', [ 'canonicalUrl' => $user->url($mode), - 'titlePrepend' => blade_safe(escape_username($user->username)), + 'titlePrepend' => \App\Libraries\Opengraph::escapeForTitle($user->username), ]) @section('content') From d2b1930007e75bcbd0f2d0e68af359533c080234 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 6 Nov 2023 15:38:08 +0900 Subject: [PATCH 34/39] whoops --- app/Libraries/Opengraph/UserOpengraph.php | 1 - resources/views/users/beatmapset_activities.blade.php | 2 +- resources/views/users/multiplayer/index.blade.php | 2 +- resources/views/users/show.blade.php | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index f6b758eb875..21ea8608eff 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -9,7 +9,6 @@ use App\Models\Beatmap; use App\Models\User; -use Illuminate\Support\HtmlString; class UserOpengraph { diff --git a/resources/views/users/beatmapset_activities.blade.php b/resources/views/users/beatmapset_activities.blade.php index 4fdca750bd4..3e11a8a6990 100644 --- a/resources/views/users/beatmapset_activities.blade.php +++ b/resources/views/users/beatmapset_activities.blade.php @@ -4,7 +4,7 @@ --}} @extends('master', [ 'pageDescription' => page_description($user->username), - 'titlePrepend' => blade_safe(escape_username($user->username)), + 'titlePrepend' => App\Libraries\Opengraph\UserOpengraph::escapeForTitle($user->username), ]) @section('content') diff --git a/resources/views/users/multiplayer/index.blade.php b/resources/views/users/multiplayer/index.blade.php index 7068061c619..a8314be0b0c 100644 --- a/resources/views/users/multiplayer/index.blade.php +++ b/resources/views/users/multiplayer/index.blade.php @@ -3,7 +3,7 @@ See the LICENCE file in the repository root for full licence text. --}} @extends('master', [ - 'titlePrepend' => \App\Libraries\Opengraph::escapeForTitle($user->username), + 'titlePrepend' => App\Libraries\Opengraph\UserOpengraph::escapeForTitle($user->username), 'pageDescription' => page_description($user->username), ]) diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index 146a602893c..db1f7bdadfc 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -5,7 +5,7 @@ @extends('master', [ 'canonicalUrl' => $user->url($mode), - 'titlePrepend' => \App\Libraries\Opengraph::escapeForTitle($user->username), + 'titlePrepend' => App\Libraries\Opengraph\UserOpengraph::escapeForTitle($user->username), ]) @section('content') From 25afc435e3c111de5e7912529d5dc99f25669bb2 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Mon, 6 Nov 2023 16:09:24 +0900 Subject: [PATCH 35/39] shorter --- resources/views/layout/metadata.blade.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 77c5fb8656e..25e0b68b176 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -36,16 +36,11 @@ @foreach ($opengraph as $key => $value) @if (present($value)) - @php - if ($key === 'title') { - $isHtmlString = $value instanceof HtmlString; - $value .= ' · '.page_title(); - if ($isHtmlString) { - $value = new HtmlString($value); - } - } - @endphp - + @if ($key === 'title') + + @else + + @endif @endif @endforeach From 7a7967ff75056dc1eca69a0937b660ab83549ff8 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Tue, 7 Nov 2023 14:35:41 +0900 Subject: [PATCH 36/39] unused --- resources/views/layout/metadata.blade.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index 25e0b68b176..adfc735af09 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -3,8 +3,6 @@ See the LICENCE file in the repository root for full licence text. --}} @php - use Illuminate\Support\HtmlString; - $appUrl = config('app.url'); $currentLocale = App::getLocale(); $fallbackLocale = config('app.fallback_locale'); From 6f52526359cedcd7c3b9bcb8cc113f052ef75ec6 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Tue, 7 Nov 2023 14:35:52 +0900 Subject: [PATCH 37/39] remove page_title from og:site_name (which apparently is supposed to be constant, anyway) --- resources/views/layout/metadata.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/layout/metadata.blade.php b/resources/views/layout/metadata.blade.php index adfc735af09..dee130b8d27 100644 --- a/resources/views/layout/metadata.blade.php +++ b/resources/views/layout/metadata.blade.php @@ -25,7 +25,7 @@ - + @if (isset($canonicalUrl)) From 5c466b72c77f4dd5ab8cbab4f0d6dfa4050fff15 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Tue, 7 Nov 2023 14:50:38 +0900 Subject: [PATCH 38/39] implemnent interface --- app/Libraries/Opengraph/ArtistOpengraph.php | 2 +- app/Libraries/Opengraph/BeatmapsetOpengraph.php | 4 ++-- app/Libraries/Opengraph/CommentOpengraph.php | 2 +- app/Libraries/Opengraph/ContestOpengraph.php | 2 +- app/Libraries/Opengraph/Forum/ForumOpengraph.php | 3 ++- app/Libraries/Opengraph/Forum/TopicOpengraph.php | 3 ++- app/Libraries/Opengraph/NewsPostOpengraph.php | 2 +- app/Libraries/Opengraph/OpengraphInterface.php | 13 +++++++++++++ app/Libraries/Opengraph/UserOpengraph.php | 2 +- app/Libraries/Opengraph/Wiki/PageOpengraph.php | 3 ++- 10 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 app/Libraries/Opengraph/OpengraphInterface.php diff --git a/app/Libraries/Opengraph/ArtistOpengraph.php b/app/Libraries/Opengraph/ArtistOpengraph.php index 2db40300e3b..98b274f9107 100644 --- a/app/Libraries/Opengraph/ArtistOpengraph.php +++ b/app/Libraries/Opengraph/ArtistOpengraph.php @@ -9,7 +9,7 @@ use App\Models\Artist; -class ArtistOpengraph +class ArtistOpengraph implements OpengraphInterface { public function __construct(private Artist $artist) { diff --git a/app/Libraries/Opengraph/BeatmapsetOpengraph.php b/app/Libraries/Opengraph/BeatmapsetOpengraph.php index 788c9ebf959..ae6cec6099f 100644 --- a/app/Libraries/Opengraph/BeatmapsetOpengraph.php +++ b/app/Libraries/Opengraph/BeatmapsetOpengraph.php @@ -9,7 +9,7 @@ use App\Models\Beatmapset; -class BeatmapsetOpengraph +class BeatmapsetOpengraph implements OpengraphInterface { public function __construct(private Beatmapset $beatmapset) { @@ -18,7 +18,7 @@ public function __construct(private Beatmapset $beatmapset) public function get(): array { $section = osu_trans('layout.menu.beatmaps._'); - $title = "{$this->beatmapset->artist} - {$this->beatmapset->title}"; // opengrah header always intended for guest. + $title = "{$this->beatmapset->artist} - {$this->beatmapset->title}"; // opengraph header always intended for guest. return [ 'description' => "osu! » {$section} » {$title}", diff --git a/app/Libraries/Opengraph/CommentOpengraph.php b/app/Libraries/Opengraph/CommentOpengraph.php index 7d4d0fa284a..8aa4cdb9612 100644 --- a/app/Libraries/Opengraph/CommentOpengraph.php +++ b/app/Libraries/Opengraph/CommentOpengraph.php @@ -9,7 +9,7 @@ use App\Models\Comment; -class CommentOpengraph +class CommentOpengraph implements OpengraphInterface { public function __construct(private Comment $comment) { diff --git a/app/Libraries/Opengraph/ContestOpengraph.php b/app/Libraries/Opengraph/ContestOpengraph.php index 253b8a5c880..5bf4a188e27 100644 --- a/app/Libraries/Opengraph/ContestOpengraph.php +++ b/app/Libraries/Opengraph/ContestOpengraph.php @@ -9,7 +9,7 @@ use App\Models\Contest; -class ContestOpengraph +class ContestOpengraph implements OpengraphInterface { public function __construct(private Contest $contest) { diff --git a/app/Libraries/Opengraph/Forum/ForumOpengraph.php b/app/Libraries/Opengraph/Forum/ForumOpengraph.php index ea5ed1733d4..6d730161cc4 100644 --- a/app/Libraries/Opengraph/Forum/ForumOpengraph.php +++ b/app/Libraries/Opengraph/Forum/ForumOpengraph.php @@ -7,9 +7,10 @@ namespace App\Libraries\Opengraph\Forum; +use App\Libraries\Opengraph\OpengraphInterface; use App\Models\Forum\Forum; -class ForumOpengraph +class ForumOpengraph implements OpengraphInterface { public function __construct(private Forum $forum) { diff --git a/app/Libraries/Opengraph/Forum/TopicOpengraph.php b/app/Libraries/Opengraph/Forum/TopicOpengraph.php index 44088789bc3..1db89abe3d3 100644 --- a/app/Libraries/Opengraph/Forum/TopicOpengraph.php +++ b/app/Libraries/Opengraph/Forum/TopicOpengraph.php @@ -7,9 +7,10 @@ namespace App\Libraries\Opengraph\Forum; +use App\Libraries\Opengraph\OpengraphInterface; use App\Models\Forum\Topic; -class TopicOpengraph +class TopicOpengraph implements OpengraphInterface { public function __construct(private Topic $topic) { diff --git a/app/Libraries/Opengraph/NewsPostOpengraph.php b/app/Libraries/Opengraph/NewsPostOpengraph.php index fb923275060..5939cd8e8a9 100644 --- a/app/Libraries/Opengraph/NewsPostOpengraph.php +++ b/app/Libraries/Opengraph/NewsPostOpengraph.php @@ -9,7 +9,7 @@ use App\Models\NewsPost; -class NewsPostOpengraph +class NewsPostOpengraph implements OpengraphInterface { public function __construct(private NewsPost $post) { diff --git a/app/Libraries/Opengraph/OpengraphInterface.php b/app/Libraries/Opengraph/OpengraphInterface.php new file mode 100644 index 00000000000..cec159c4673 --- /dev/null +++ b/app/Libraries/Opengraph/OpengraphInterface.php @@ -0,0 +1,13 @@ +. Licensed under the GNU Affero General Public License v3.0. +// See the LICENCE file in the repository root for full licence text. + +declare(strict_types=1); + +namespace App\Libraries\Opengraph; + +interface OpengraphInterface +{ + public function get(): array; +} diff --git a/app/Libraries/Opengraph/UserOpengraph.php b/app/Libraries/Opengraph/UserOpengraph.php index 21ea8608eff..f4278d6715b 100644 --- a/app/Libraries/Opengraph/UserOpengraph.php +++ b/app/Libraries/Opengraph/UserOpengraph.php @@ -10,7 +10,7 @@ use App\Models\Beatmap; use App\Models\User; -class UserOpengraph +class UserOpengraph implements OpengraphInterface { public static function escapeForTitle(string $username) { diff --git a/app/Libraries/Opengraph/Wiki/PageOpengraph.php b/app/Libraries/Opengraph/Wiki/PageOpengraph.php index 804484987e6..697f1799bc7 100644 --- a/app/Libraries/Opengraph/Wiki/PageOpengraph.php +++ b/app/Libraries/Opengraph/Wiki/PageOpengraph.php @@ -7,9 +7,10 @@ namespace App\Libraries\Opengraph\Wiki; +use App\Libraries\Opengraph\OpengraphInterface; use App\Models\Wiki\Page; -class PageOpengraph +class PageOpengraph implements OpengraphInterface { public function __construct(private Page $page) { From b2f67277dbc7a592434a7c9b2547e4b345d42ee3 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Tue, 7 Nov 2023 14:53:36 +0900 Subject: [PATCH 39/39] remove kind of useless interface; doesn't actually implement anything, not really used for typecheck --- app/Libraries/Opengraph/HasOpengraph.php | 12 ------------ app/Models/Artist.php | 3 +-- app/Models/Beatmapset.php | 3 +-- app/Models/Comment.php | 3 +-- app/Models/Contest.php | 3 +-- app/Models/Forum/Forum.php | 3 +-- app/Models/Forum/Topic.php | 3 +-- app/Models/NewsPost.php | 3 +-- app/Models/User.php | 3 +-- app/Models/Wiki/Page.php | 3 +-- app/helpers.php | 3 +-- 11 files changed, 10 insertions(+), 32 deletions(-) delete mode 100644 app/Libraries/Opengraph/HasOpengraph.php diff --git a/app/Libraries/Opengraph/HasOpengraph.php b/app/Libraries/Opengraph/HasOpengraph.php deleted file mode 100644 index a227fab94d0..00000000000 --- a/app/Libraries/Opengraph/HasOpengraph.php +++ /dev/null @@ -1,12 +0,0 @@ -. Licensed under the GNU Affero General Public License v3.0. -// See the LICENCE file in the repository root for full licence text. - -declare(strict_types=1); - -namespace App\Libraries\Opengraph; - -interface HasOpengraph -{ -} diff --git a/app/Models/Artist.php b/app/Models/Artist.php index 245dc1a1e07..2505f299d84 100644 --- a/app/Models/Artist.php +++ b/app/Models/Artist.php @@ -5,7 +5,6 @@ namespace App\Models; -use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Memoizes; /** @@ -32,7 +31,7 @@ * @property string|null $website * @property string|null $youtube */ -class Artist extends Model implements HasOpengraph +class Artist extends Model { use Memoizes; diff --git a/app/Models/Beatmapset.php b/app/Models/Beatmapset.php index 9b92ec57c50..520271a54f0 100644 --- a/app/Models/Beatmapset.php +++ b/app/Models/Beatmapset.php @@ -24,7 +24,6 @@ use App\Libraries\Commentable; use App\Libraries\Elasticsearch\Indexable; use App\Libraries\ImageProcessorService; -use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\StorageWithUrl; use App\Libraries\Transactions\AfterCommit; use App\Traits\Memoizes; @@ -105,7 +104,7 @@ * @property bool $video * @property \Illuminate\Database\Eloquent\Collection $watches BeatmapsetWatch */ -class Beatmapset extends Model implements AfterCommit, Commentable, HasOpengraph, Indexable, Traits\ReportableInterface +class Beatmapset extends Model implements AfterCommit, Commentable, Indexable, Traits\ReportableInterface { use Memoizes, SoftDeletes, Traits\CommentableDefaults, Traits\Es\BeatmapsetSearch, Traits\Reportable, Validatable; diff --git a/app/Models/Comment.php b/app/Models/Comment.php index dd5b398433b..b249fcaa4d5 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -6,7 +6,6 @@ namespace App\Models; use App\Libraries\MorphMap; -use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Memoizes; use App\Traits\Validatable; use Carbon\Carbon; @@ -39,7 +38,7 @@ * @property \Illuminate\Database\Eloquent\Collection $votes CommentVote * @property int $votes_count_cache */ -class Comment extends Model implements HasOpengraph, Traits\ReportableInterface +class Comment extends Model implements Traits\ReportableInterface { use Memoizes, Traits\Reportable, Traits\WithDbCursorHelper, Validatable; diff --git a/app/Models/Contest.php b/app/Models/Contest.php index f3fcd188908..fd495ba30be 100644 --- a/app/Models/Contest.php +++ b/app/Models/Contest.php @@ -6,7 +6,6 @@ namespace App\Models; use App\Exceptions\InvariantException; -use App\Libraries\Opengraph\HasOpengraph; use App\Traits\Memoizes; use App\Transformers\ContestEntryTransformer; use App\Transformers\ContestTransformer; @@ -39,7 +38,7 @@ * @property \Carbon\Carbon|null $voting_ends_at * @property \Carbon\Carbon|null $voting_starts_at */ -class Contest extends Model implements HasOpengraph +class Contest extends Model { use Memoizes; diff --git a/app/Models/Forum/Forum.php b/app/Models/Forum/Forum.php index 5941fe5d68c..82a24493270 100644 --- a/app/Models/Forum/Forum.php +++ b/app/Models/Forum/Forum.php @@ -5,7 +5,6 @@ namespace App\Models\Forum; -use App\Libraries\Opengraph\HasOpengraph; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; @@ -60,7 +59,7 @@ * @property \Illuminate\Database\Eloquent\Collection $subforums static * @property \Illuminate\Database\Eloquent\Collection $topics Topic */ -class Forum extends Model implements HasOpengraph +class Forum extends Model { public $timestamps = false; diff --git a/app/Models/Forum/Topic.php b/app/Models/Forum/Topic.php index 3973dca9c2f..ea0d9c426f9 100644 --- a/app/Models/Forum/Topic.php +++ b/app/Models/Forum/Topic.php @@ -9,7 +9,6 @@ use App\Jobs\UpdateUserForumCache; use App\Jobs\UpdateUserForumTopicFollows; use App\Libraries\BBCodeForDB; -use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\Transactions\AfterCommit; use App\Models\Beatmapset; use App\Models\Log; @@ -73,7 +72,7 @@ * @property \Illuminate\Database\Eloquent\Collection $userTracks TopicTrack * @property \Illuminate\Database\Eloquent\Collection $watches TopicWatch */ -class Topic extends Model implements AfterCommit, HasOpengraph +class Topic extends Model implements AfterCommit { use Memoizes, Validatable; use SoftDeletes { diff --git a/app/Models/NewsPost.php b/app/Models/NewsPost.php index 78cf390851c..66d83085ac4 100644 --- a/app/Models/NewsPost.php +++ b/app/Models/NewsPost.php @@ -8,7 +8,6 @@ use App\Exceptions\GitHubNotFoundException; use App\Libraries\Commentable; use App\Libraries\Markdown\OsuMarkdown; -use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\OsuWiki; use App\Traits\Memoizes; use Carbon\Carbon; @@ -27,7 +26,7 @@ * @property \Carbon\Carbon|null $updated_at * @property string|null $version */ -class NewsPost extends Model implements Commentable, HasOpengraph, Wiki\WikiObject +class NewsPost extends Model implements Commentable, Wiki\WikiObject { use Memoizes, Traits\CommentableDefaults, Traits\WithDbCursorHelper; diff --git a/app/Models/User.php b/app/Models/User.php index c3665f80e5e..013951fe5a7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,7 +12,6 @@ use App\Libraries\BBCodeForDB; use App\Libraries\ChangeUsername; use App\Libraries\Elasticsearch\Indexable; -use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\Session\Store as SessionStore; use App\Libraries\Transactions\AfterCommit; use App\Libraries\User\DatadogLoginAttempt; @@ -211,7 +210,7 @@ * @method static Builder eagerloadForListing() * @method static Builder online() */ -class User extends Model implements AfterCommit, AuthenticatableContract, HasLocalePreference, HasOpengraph, Indexable, Traits\ReportableInterface +class User extends Model implements AfterCommit, AuthenticatableContract, HasLocalePreference, Indexable, Traits\ReportableInterface { use Authenticatable, HasApiTokens, Memoizes, Traits\Es\UserSearch, Traits\Reportable, Traits\UserAvatar, Traits\UserScoreable, Traits\UserStore, Validatable; diff --git a/app/Models/Wiki/Page.php b/app/Models/Wiki/Page.php index 03a810c5977..d0b7bd29129 100644 --- a/app/Models/Wiki/Page.php +++ b/app/Models/Wiki/Page.php @@ -12,7 +12,6 @@ use App\Libraries\Elasticsearch\Sort; use App\Libraries\LocaleMeta; use App\Libraries\Markdown\OsuMarkdown; -use App\Libraries\Opengraph\HasOpengraph; use App\Libraries\OsuWiki; use App\Libraries\Search\BasicSearch; use App\Libraries\Wiki\MainPageRenderer; @@ -24,7 +23,7 @@ use Exception; use Log; -class Page implements HasOpengraph, WikiObject +class Page implements WikiObject { use Memoizes, Traits\Es\WikiPageSearch; diff --git a/app/helpers.php b/app/helpers.php index 9df3818d4eb..8e4e3677ba9 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -4,7 +4,6 @@ // See the LICENCE file in the repository root for full licence text. use App\Libraries\LocaleMeta; -use App\Libraries\Opengraph\HasOpengraph; use App\Models\LoginAttempt; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\NoRFCWarningsValidation; @@ -1691,7 +1690,7 @@ function seeded_shuffle(array &$items, int $seed = 0) mt_srand(); } -function set_opengraph(HasOpengraph $model, ...$options) +function set_opengraph($model, ...$options) { $className = str_replace('App\Models', 'App\Libraries\Opengraph', $model::class).'Opengraph';