Skip to content

Commit

Permalink
Update for beta 15
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Dec 17, 2020
1 parent 5ef8ce1 commit 5083806
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
15 changes: 12 additions & 3 deletions composer.json
Expand Up @@ -6,10 +6,18 @@
"license": "MIT",
"support": {
"issues": "https://github.com/FriendsOfFlarum/discussion-thumbnail/issues",
"source": "https://github.com/FriendsOfFlarum/discussion-thumbnail"
"source": "https://github.com/FriendsOfFlarum/discussion-thumbnail",
"forum": "https://discuss.flarum.org/d/22231"
},
"homepage": "https://friendsofflarum.org",
"funding": [
{
"type": "website",
"url": "https://opencollective.com/fof/donate"
}
],
"require": {
"flarum/core": ">=0.1.0-beta.14 <0.1.0-beta.15"
"flarum/core": "^0.1.0-beta.15"
},
"authors": [
{
Expand All @@ -27,14 +35,15 @@
"extra": {
"flarum-extension": {
"title": "FoF Discussion Thumbnail",
"category": "discussion",
"icon": {
"name": "fas fa-image",
"backgroundColor": "#e74c3c",
"color": "#fff"
}
},
"flagrow": {
"discuss": "https://discuss.flarum.org/d/22231-friendsofflarum-discussion-thumbnail"
"discuss": "https://discuss.flarum.org/d/22231"
}
}
}
7 changes: 4 additions & 3 deletions extend.php
Expand Up @@ -11,13 +11,14 @@

namespace FoF\DiscussionThumbnail;

use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Extend;

return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less'),
(new Extend\Event())
->listen(Serializing::class, Listener\AddDiscussionThumbnail::class),

(new Extend\ApiSerializer(BasicDiscussionSerializer::class))
->mutate(Listener\AddDiscussionThumbnail::class),
];
6 changes: 3 additions & 3 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 24 additions & 24 deletions src/Listener/AddDiscussionThumbnail.php
Expand Up @@ -11,8 +11,8 @@

namespace FoF\DiscussionThumbnail\Listener;

use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Post\CommentPost;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Arr;
Expand All @@ -29,37 +29,37 @@ public function __construct(Repository $cache)
$this->cache = $cache;
}

public function handle(Serializing $event)
public function __invoke(BasicDiscussionSerializer $serializer, Discussion $discussion)
{
if ($event->isSerializer(BasicDiscussionSerializer::class)) {
$post = $event->model->firstPost;
$post = $discussion->firstPost;

if (!($post instanceof CommentPost)) {
return;
}

$key = "fof-discussion-thumbnail.discussion.{$post->id}";
$cached = $this->cache->get($key);
$thumbnail = Arr::get($cached, 'url');
if (!($post instanceof CommentPost)) {
return;
}

if (!$this->cache->has($key) || ($post->edited_at && Arr::has($cached, 'date') && $post->edited_at->isAfter($cached['date']))) {
$content = $event->model->firstPost->formatContent();
$key = "fof-discussion-thumbnail.discussion.{$post->id}";
$cached = $this->cache->get($key);
$thumbnail = Arr::get($cached, 'url');

if (!$content) {
return;
}
if (!$this->cache->has($key) || ($post->edited_at && Arr::has($cached, 'date') && $post->edited_at->isAfter($cached['date']))) {
$content = $discussion->firstPost->formatContent();

preg_match('/<img.+?src=[\"\'](.+?)[\"\'].*?>/i', $content, $match);
if (!$content) {
return;
}

$thumbnail = @$match[1];
preg_match('/<img.+?src=[\"\'](.+?)[\"\'].*?>/i', $content, $match);

$this->cache->forever($key, $match ? [
'url' => @$match[1],
'date' => $post->edited_at,
] : null);
}
$thumbnail = @$match[1];

$event->attributes['customThumbnail'] = $thumbnail;
$this->cache->forever($key, $match ? [
'url' => @$match[1],
'date' => $post->edited_at,
] : null);
}

$attributes['customThumbnail'] = $thumbnail;

return $attributes;
}
}

0 comments on commit 5083806

Please sign in to comment.