Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract opengraph methods #10655

Merged
merged 41 commits into from
Nov 7, 2023
Merged

Conversation

notbakaneko
Copy link
Collaborator

@notbakaneko notbakaneko commented Oct 19, 2023

  • directly continues Add more Opengraph headers to more pages #10639
  • moves the opengraph methods to their own helper classes instead of filling up the existing model files
  • no need to pass the variable into the view factory since only the metadata blade cares about it
  • wiki page parser now has a thing that pulls out first paragraph block as the excerpt (maybe news can use it too?)

Also I did think of removing the Opengraph suffixes from the class names, except, they're usually used in places where there'd already be an existing model class import with the same name, so they'd have to be aliased to as xxOpengraph anyway 👀

Not sure what to with the user multiplayer tabs descriptions as the room/playlist counts seems...not useful 🤔

@notbakaneko notbakaneko self-assigned this Oct 19, 2023
@nanaya
Copy link
Collaborator

nanaya commented Oct 19, 2023

I'm skipping reviewing the other one

@notbakaneko
Copy link
Collaborator Author

notbakaneko commented Oct 20, 2023

Adding per beatmap opengraph on the beatmapset url isn't currently possible as the beatmap portion of the url is part of the fragment and client-side;
turns out the same is true for discussion posts, it's all client-side.

app/helpers.php Outdated
@@ -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';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use plain replace otherwise models in subdirectory will be structured wrong (like the wiki page).

also, there's $model::class

@@ -6,6 +6,8 @@
$appUrl = config('app.url');
$currentLocale = App::getLocale();
$fallbackLocale = config('app.fallback_locale');

$opengraph['description'] ??= $opengraph['description'] ?? $pageDescription ?? null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant $opengraph['description']


private function getMessageHtml(): ?string
{
return $this->memoize(__FUNCTION__, function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use shorter fn?


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']),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error on nonexistent page

app/helpers.php Outdated
Comment on lines 1698 to 1700
View::share([
'opengraph' => (new $className($model, ...$options))->get(),
]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be reset after each request because the value persists to the next requests until overwritten

@@ -124,6 +124,9 @@
],

'ogp' => [
'modding_description' => ':user\'s beatmaps: :counts',
'modding_description_empty' => ':user doesn\'t have nay beatmaps...',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any

{
$user = $this->comment->user;

return priv_check('CommentShow', $this->comment)->can()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

priv_check_user with null user?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originally, I had everything assume opengrapgh would be viewed only by null user, but if you go to the page with a deleted comment as a moderator, you see a different thing, so I'm not so sure now 👀


return priv_check('CommentShow', $this->comment)->can()
? [
'description' => html_excerpt($this->comment->message_html, 100),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blade_safe? return of html_excerpt is already escaped

although I noticed discord double unescape description by default 🤔 and twitter is even worse

$pageData = $this->page->get();

return [
'description' => $pageData['excerpt'] ?? html_excerpt($pageData['output']), // html_excerpt fallback
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another one needing blade_safe

@@ -263,6 +269,15 @@ private function proxyImage()
}
}

private function recordExcerpt(): void
{
if ($this->excerpt !== null || !$this->node instanceof Paragraph || !$this->event->isEntering()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with first two paragraphs may not even contain actual content I wonder if it's better to just show first 100 characters instead.

Comment on lines 39 to 48
@php
if ($key === 'title') {
$isHtmlString = $value instanceof HtmlString;
$value .= ' · '.page_title();
if ($isHtmlString) {
$value = new HtmlString($value);
}
}
@endphp
<meta property="og:{{ $key }}" content="{{ $value }}">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit more repetitive but a lot shorter. (also the page_title() should be escaped)

Suggested change
@php
if ($key === 'title') {
$isHtmlString = $value instanceof HtmlString;
$value .= ' · '.page_title();
if ($isHtmlString) {
$value = new HtmlString($value);
}
}
@endphp
<meta property="og:{{ $key }}" content="{{ $value }}">
@if ($key === 'title')
<meta property="og:{{ $key }}" content="{{ $value }} · {{ page_title() }}">
@else
<meta property="og:{{ $key }}" content="{{ $value }}">
@endif

@if (isset($opengraph))
<meta property="og:site_name" content="osu! » {{ page_title() }}">
<meta property="og:type" content="website">
<meta property="og:site_name" content="osu! » {{ page_title() }}">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

page title not needed anymore? Both og title and html title always have it now

@@ -3,9 +3,14 @@
See the LICENCE file in the repository root for full licence text.
--}}
@php
use Illuminate\Support\HtmlString;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used


use App\Models\Beatmapset;

class BeatmapsetOpengraph
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implements Something? The set_opengraph assumes everything here have the get method.

namespace App\Libraries\Opengraph;

interface HasOpengraph
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should require something like $opengraphClass and use it for set_opengraph. Not sure if helps anything at all though. And this currently isn't enforcing anything. One can have HasOpengraph model without corresponding class and it'll only explode on runtime. That said, specifying the class name won't help either since it's not checked and can't specify inheritance either.

eh 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of it since the type check is more of "belongs to App\Models" than anything 🤷

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, opengraph missing p

doesn't actually implement anything, not really used for typecheck
@nanaya nanaya enabled auto-merge November 7, 2023 08:37
@nanaya nanaya merged commit 5809146 into ppy:master Nov 7, 2023
3 checks passed
@notbakaneko notbakaneko deleted the feature/opengraph-interface-2 branch November 13, 2023 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants