Skip to content

Commit

Permalink
Merge pull request #10655 from notbakaneko/feature/opengraph-interface-2
Browse files Browse the repository at this point in the history
Extract opengraph methods
  • Loading branch information
nanaya authored Nov 7, 2023
2 parents 10db665 + 6911367 commit 5809146
Show file tree
Hide file tree
Showing 41 changed files with 410 additions and 103 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/ArtistsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public function show($id)
}
}

set_opengraph($artist);

return ext_view('artists.show', [
'artist' => $artist,
'images' => $images,
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/BeatmapsetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public function show($id)

$noindex = !$beatmapset->esShouldIndex();

set_opengraph($beatmapset);

return ext_view('beatmapsets.show', compact(
'beatmapset',
'commentBundle',
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public function show($id)
return $commentBundle->toArray();
}

set_opengraph($comment);

return ext_view('comments.show', compact('commentBundle'));
}

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/ContestsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function show($id)
$contests = collect([$contest]);
}

set_opengraph($contest);

if ($contest->isVotingStarted()) {
if ($contest->isVotingOpen()) {
// TODO: add support for $contests requirement instead of at parent
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Forum/ForumsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public function show($id)

$noindex = !$forum->enable_indexing;

set_opengraph($forum);

return ext_view('forum.forums.show', compact(
'cover',
'forum',
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Forum/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -438,6 +438,8 @@ public function show($id)
$featureVotes = $this->groupFeatureVotes($topic);
$noindex = !$topic->forum->enable_indexing;

set_opengraph($topic);

return ext_view('forum.topics.show', compact(
'canEditPoll',
'cover',
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public function show($slug)
return $postJson;
}

set_opengraph($post);

return ext_view('news.show', [
'commentBundle' => CommentBundle::forEmbed($post),
'post' => $post,
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Users/ModdingHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Users/MultiplayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function index($userId, $typeGroup)
return $json;
}

set_opengraph($user, 'multiplayer');

$jsonUser = json_item(
$user,
(new UserTransformer())->setMode($user->playmode),
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -654,9 +654,9 @@ public function show($id, $mode = null)
'user' => $userArray,
];

$pageDescription = blade_safe($user->toMetaDescription(['ruleset' => $currentMode]));
set_opengraph($user, 'show', $currentMode);

return ext_view('users.show', compact('initialData', 'pageDescription', 'mode', 'user'));
return ext_view('users.show', compact('initialData', 'mode', 'user'));
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
26 changes: 26 additions & 0 deletions app/Libraries/Opengraph/ArtistOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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 implements OpengraphInterface
{
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,
];
}
}
29 changes: 29 additions & 0 deletions app/Libraries/Opengraph/BeatmapsetOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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 implements OpengraphInterface
{
public function __construct(private Beatmapset $beatmapset)
{
}

public function get(): array
{
$section = osu_trans('layout.menu.beatmaps._');
$title = "{$this->beatmapset->artist} - {$this->beatmapset->title}"; // opengraph header always intended for guest.

return [
'description' => "osu! » {$section} » {$title}",
'image' => $this->beatmapset->coverURL('card'),
'title' => $title,
];
}
}
29 changes: 29 additions & 0 deletions app/Libraries/Opengraph/CommentOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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 implements OpengraphInterface
{
public function __construct(private Comment $comment)
{
}

public function get(): array
{
$user = $this->comment->user;

return priv_check_user(null, 'CommentShow', $this->comment)->can()
? [
'description' => blade_safe(html_excerpt($this->comment->message_html, 100)),
'image' => $user->user_avatar,
'title' => osu_trans('comments.ogp.title', ['user' => $user->username]),
] : [];
}
}
26 changes: 26 additions & 0 deletions app/Libraries/Opengraph/ContestOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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 implements OpengraphInterface
{
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,
];
}
}
41 changes: 41 additions & 0 deletions app/Libraries/Opengraph/Forum/ForumOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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\Forum;

use App\Libraries\Opengraph\OpengraphInterface;
use App\Models\Forum\Forum;

class ForumOpengraph implements OpengraphInterface
{
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(),
];
}
}
29 changes: 29 additions & 0 deletions app/Libraries/Opengraph/Forum/TopicOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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\Forum;

use App\Libraries\Opengraph\OpengraphInterface;
use App\Models\Forum\Topic;

class TopicOpengraph implements OpengraphInterface
{
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,
];
}
}
26 changes: 26 additions & 0 deletions app/Libraries/Opengraph/NewsPostOpengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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 implements OpengraphInterface
{
public function __construct(private NewsPost $post)
{
}

public function get(): array
{
return [
'description' => $this->post->previewText(),
'image' => $this->post->firstImage(true),
'title' => $this->post->title(),
];
}
}
13 changes: 13 additions & 0 deletions app/Libraries/Opengraph/OpengraphInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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;
}
Loading

0 comments on commit 5809146

Please sign in to comment.