Skip to content

Commit

Permalink
Merge pull request #102 from rafaucau/fix-v2
Browse files Browse the repository at this point in the history
Fix some issues with v2
  • Loading branch information
jaspervriends authored Dec 16, 2024
2 parents da3cb93 + 8d2c934 commit e495989
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.phar
.idea
.DS_Store
Thumbs.db
node_modules
node_modules
composer.lock
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
}
},
"require": {
"php": "^8.0",
"flarum/core": "^1.0.0",
"ext-json": "*"
},
"require-dev": {
"flarum/tags": "^1.8",
"fof/pages": "^1.0",
"fof/best-answer": "^1.6",
"flarum/phpstan": "^1.0"
},
"conflict": {
"zerosonesfun/elint": "*",
"franzl/flarum-open-links-in-new-tab": "*"
Expand All @@ -45,5 +52,9 @@
"flagrow": {
"discuss": "https://discuss.flarum.org/d/18316"
}
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
}
}
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
- vendor/flarum/phpstan/extension.neon

parameters:
level: 5
paths:
- src
- extend.php
excludePaths:
- *.blade.php
checkMissingIterableValueType: false
databaseMigrationsPath: ['migrations']
32 changes: 19 additions & 13 deletions src/Page/DiscussionBestAnswerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace V17Development\FlarumSeo\Page;

use Flarum\Database\Eloquent\Collection;
use Flarum\Discussion\DiscussionRepository;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Http\UrlGenerator;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;
use Flarum\User\UserRepository;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use V17Development\FlarumSeo\Page\PageDriverInterface;
use V17Development\FlarumSeo\SeoMeta\SeoMeta;
use V17Development\FlarumSeo\SeoProperties;

Expand Down Expand Up @@ -111,7 +113,10 @@ public function handle(
// Fallback to simple discussions for not-answer tags
$enableBestAnswer = $this->extensionManager->isEnabled('fof-best-answer');

if ($enableBestAnswer && $discussion->tags()->where('is_qna', true)->count() === 0) {
/** @var Collection<Tag> $discussionTags */
$discussionTags = $discussion->tags;

if ($enableBestAnswer && $discussionTags->contains(fn(Tag $tag) => (bool)$tag->is_qna )) {
$this->discussionFallback->handle($request, $properties);
return;
}
Expand All @@ -124,7 +129,7 @@ public function handle(
// Run events in case the model was created
$this->dispatchEventsFor($seoMeta);

$firstPost = $discussion->firstPost()->first();
$firstPost = $discussion->firstPost;

// Update ld-json
$properties
Expand All @@ -150,30 +155,31 @@ public function handle(
'dateCreated' => $seoMeta->created_at,
'author' => [
"@type" => "Person",
"name" => $discussion->user() ? $discussion->user()->first()->getDisplayNameAttribute() : null
"name" => $discussion->user?->getDisplayNameAttribute()
],
'answerCount' => $discussion->comment_count - 1
];

// Generate a breadcrum if discussion has tags
if ($discussion->tags()->count() >= 1) {
$tags = $discussion->tags()->get()->all();
$properties->generateSchemaBreadcrumb(array_map(function ($tag) {
return [
// Generate a breadcrumb if discussion has tags
if ($discussionTags->count() >= 1) {
$properties->generateSchemaBreadcrumb(
$discussionTags->map(fn(Tag $tag) => [
'name' => $tag->name,
'url' => $this->urlGenerator->to('forum')->route('tag', ['slug' => $tag->slug])
];
}, $tags));
])->toArray()
);
}

// Only add suggested answers property if there are posts
$mainEntity['suggestedAnswer'] = [];

// Get all public comments for this discussion
/** @var Collection<Post> $posts */
$posts = $discussion->posts()
->where('number', '>', '1')->get();

foreach ($posts as $post) {
/** @var Post $post */
if ($post->is_private || $post->type !== 'comment') {
continue;
}
Expand All @@ -182,7 +188,7 @@ public function handle(
$generatedPost = [
'@type' => 'Answer',
'text' => strip_tags($post->content),
'dateCreated' => (new \DateTime($post->created_at))->format("c"),
'dateCreated' => $post->created_at->toIso8601String(),
'url' => $this->urlGenerator->to('forum')->route('discussion', ['id' => $discussion->id . '-' . $discussion->slug, 'near' => $post->number]),
'author' => [
"@type" => "Person",
Expand Down
23 changes: 13 additions & 10 deletions src/Page/DiscussionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace V17Development\FlarumSeo\Page;

use Flarum\Database\Eloquent\Collection;
use Flarum\Discussion\DiscussionRepository;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;
use Flarum\User\UserRepository;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use V17Development\FlarumSeo\Page\PageDriverInterface;
use V17Development\FlarumSeo\SeoMeta\SeoMeta;
use V17Development\FlarumSeo\SeoProperties;

Expand Down Expand Up @@ -98,10 +99,13 @@ public function handle(
$tagsEnabled = $this->extensionManager->isEnabled('flarum-tags');
$enableBestAnswer = $this->extensionManager->isEnabled('fof-best-answer');

/** @var Collection<Tag> $discussionTags */
$discussionTags = $discussion->tags;

// Do not continue discussion matches a FriendsOfFlarum BestAnswer discussion (if enabled)
if (
$this->settingsRepositoryInterface->get('seo_post_crawler', 0) == 1 &&
$tagsEnabled && (!$enableBestAnswer || ($enableBestAnswer && $discussion->tags()->where('is_qna', true)->count() >= 1))
$tagsEnabled && (!$enableBestAnswer || ($enableBestAnswer && $discussionTags->contains(fn(Tag $tag) => (bool)$tag->is_qna )))
) {
return;
}
Expand Down Expand Up @@ -129,7 +133,7 @@ public function handle(

try {
// Add author to the page meta data
$user = $discussion->user()->first();
$user = $discussion->user;

// Set author data if found
if ($user !== null) {
Expand All @@ -145,14 +149,13 @@ public function handle(
}

// Generate a breadcrum if discussion has tags
if ($tagsEnabled && $discussion->tags()->count() >= 1) {
$tags = $discussion->tags()->get()->all();
$properties->generateSchemaBreadcrumb(array_map(function ($tag) {
return [
if ($tagsEnabled && $discussionTags->count() >= 1) {
$properties->generateSchemaBreadcrumb(
$discussionTags->map(fn(Tag $tag) => [
'name' => $tag->name,
'url' => $this->urlGenerator->to('forum')->route('tag', ['slug' => $tag->slug])
];
}, $tags));
])->toArray()
);
}
}
}
6 changes: 1 addition & 5 deletions src/Page/ProfilePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace V17Development\FlarumSeo\Page;

use Flarum\Tags\TagRepository;
use Flarum\User\UserRepository;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use V17Development\FlarumSeo\Page\PageDriverInterface;
use V17Development\FlarumSeo\SeoProperties;

class ProfilePage implements PageDriverInterface
Expand Down Expand Up @@ -61,8 +59,6 @@ public function handle(
return;
}

$joinedAt = (new \DateTime($user->getAttribute('joined_at')))->format("c");

// Profile title
$profileTitle = $this->translator->trans("v17development-flarum-seo.forum.profile_title", [
'username' => $user->getAttribute('display_name'),
Expand Down Expand Up @@ -90,7 +86,7 @@ public function handle(
->setSchemaJson('@type', 'ProfilePage')
->setSchemaJson('mainEntity', $mainEntity)
->setSchemaJson('name', $user->getAttribute('display_name'))
->setSchemaJson('dateCreated', $joinedAt);
->setSchemaJson('dateCreated', $user->joined_at->toIso8601String());

// Add avatar
if ($user->getAttribute('avatar_url') !== null) {
Expand Down
3 changes: 1 addition & 2 deletions src/Page/TagPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Tags\TagRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Contracts\Translation\TranslatorInterface;
use V17Development\FlarumSeo\Page\PageDriverInterface;
use V17Development\FlarumSeo\SeoMeta\SeoMeta;
use V17Development\FlarumSeo\SeoProperties;

Expand Down
32 changes: 32 additions & 0 deletions src/SeoMeta/SeoMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@
use Illuminate\Database\Eloquent\Model;
use V17Development\FlarumSeo\SeoMeta\Event\Created;

/**
* @property int $id
* @property int $object_id
* @property string $object_type
*
* @property bool $auto_update_data
*
* @property ?string $title
* @property ?string $description
* @property ?string $keywords
*
* @property bool $robots_noindex
* @property bool $robots_nofollow
* @property bool $robots_noarchive
* @property bool $robots_noimageindex
* @property bool $robots_nosnippet
*
* @property ?string $twitter_title
* @property ?string $twitter_description
* @property ?string $twitter_image
* @property ?string $twitter_image_source
*
* @property ?string $open_graph_title
* @property ?string $open_graph_description
* @property ?string $open_graph_image
* @property ?string $open_graph_image_source
*
* @property ?int $estimated_reading_time
*
* @property Carbon $created_at
* @property Carbon|null $updated_at
*/
class SeoMeta extends AbstractModel
{
use EventGeneratorTrait;
Expand Down

0 comments on commit e495989

Please sign in to comment.