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

Bug fixes & default images #67

Merged
merged 5 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
},
"require": {
"flarum/core": "^0.1.0-beta.15",
"flarum/tags": "^0.1.0-beta.15",
"ext-json": "*"
},
"suggest": {
"flarum/tags": "The Flarum Tags extension",
"v17development/flarum-seo": "Adds Flarum SEO and Social media tags"
},
"extra": {
Expand Down
6 changes: 5 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use V17Development\FlarumBlog\Api\AttachForumSerializerAttributes;
use V17Development\FlarumBlog\Api\Controller\CreateBlogMetaController;
use V17Development\FlarumBlog\Api\Controller\UpdateBlogMetaController;
use V17Development\FlarumBlog\Api\Controller\UploadDefaultBlogImageController;
use V17Development\FlarumBlog\Api\Controller\DeleteDefaultBlogImageController;
use V17Development\FlarumBlog\Api\Serializer\BlogMetaSerializer;
// Listeners
use V17Development\FlarumBlog\Listeners\FilterBlogArticles;
Expand Down Expand Up @@ -53,7 +55,9 @@

(new Extend\Routes('api'))
->post('/blogMeta', 'blog.meta', CreateBlogMetaController::class)
->patch('/blogMeta/{id}', 'blog.meta.edit', UpdateBlogMetaController::class),
->patch('/blogMeta/{id}', 'blog.meta.edit', UpdateBlogMetaController::class)
->post('/blog_default_image', 'pages.index', UploadDefaultBlogImageController::class)
->delete('/blog_default_image', 'pages.index', DeleteDefaultBlogImageController::class),

new Extend\Locales(__DIR__ . '/locale'),

Expand Down
2 changes: 1 addition & 1 deletion js/dist/admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions js/src/admin/pages/BlogSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Button from "flarum/components/Button";
import FieldSet from "flarum/components/FieldSet";
import Switch from "flarum/components/Switch";
import SelectCategoriesModal from "../components/Modals/SelectCategoriesModal";
import UploadImageButton from "flarum/components/UploadImageButton";

export default class BlogSettings extends ExtensionPage {
oninit(attrs) {
Expand Down Expand Up @@ -39,6 +40,11 @@ export default class BlogSettings extends ExtensionPage {
this.addSidebarNav = app.data.settings.blog_add_sidebar_nav
? app.data.settings.blog_add_sidebar_nav
: true;

app.forum.data.attributes.blog_default_imageUrl =
app.forum.attribute("baseUrl") +
"/assets/" +
app.data.settings.blog_default_image_path;
}

content() {
Expand Down Expand Up @@ -296,6 +302,24 @@ export default class BlogSettings extends ExtensionPage {
]
)}

{FieldSet.component(
{
label: app.translator.trans(
"v17development-flarum-blog.admin.settings.default_article_image_label"
),
},
[
<div className="helpText">
{app.translator.trans(
"v17development-flarum-blog.admin.settings.default_article_image_text"
)}
</div>,
UploadImageButton.component({
name: "blog_default_image",
}),
]
)}

<Button
loading={this.isSaving}
className={"Button Button--primary"}
Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ app.initializers.add("v17development-flarum-blog", (app) => {

app.routes.blogArticle = { path: "/blog/:id", component: BlogItem };

app.routes["blogArticle.near"] = {
path: "/blog/:id/:near",
component: BlogItem,
};

app.store.models.blogMeta = BlogMeta;

Discussion.prototype.blogMeta = Model.hasOne("blogMeta");
Expand Down
18 changes: 16 additions & 2 deletions js/src/forum/pages/BlogItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default class BlogItem extends Page {

this.bodyClass = "BlogItemPage";

this.near = m.route.param("near") || 0;

this.loading = true;
this.found = false;
this.article = null;
Expand Down Expand Up @@ -80,16 +82,28 @@ export default class BlogItem extends Page {

this.stream = new PostStreamState(article, includedPosts);

// Scroll to specific post
if (this.near) {
this.stream.goToNumber(this.near, true);
}

m.redraw();
}

view() {
const defaultImage = app.forum.attribute("blogDefaultImage")
? `url(${
app.forum.attribute("baseUrl") +
"/assets/" +
app.forum.attribute("blogDefaultImage")
})`
: null;
const blogImage =
this.article &&
this.article.blogMeta() &&
this.article.blogMeta().featuredImage()
? `url(${this.article.blogMeta().featuredImage()})`
: null;
: defaultImage;
let articlePost = null;

if (!this.loading && this.article) {
Expand Down Expand Up @@ -255,7 +269,7 @@ export default class BlogItem extends Page {
PostStream.component({
discussion: this.article,
stream: this.stream,
onPositionChange: () => {},
onPositionChange: this.positionChanged.bind(this),
})}
</div>
</div>
Expand Down
12 changes: 10 additions & 2 deletions js/src/forum/pages/BlogOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ export default class BlogOverview extends Page {
}

view() {
const defaultImage = app.forum.attribute("blogDefaultImage")
? `url(${
app.forum.attribute("baseUrl") +
"/assets/" +
app.forum.attribute("blogDefaultImage")
})`
: null;

return (
<div className={"FlarumBlogOverview"}>
<div className={"container"}>
Expand Down Expand Up @@ -213,7 +221,7 @@ export default class BlogOverview extends Page {
const blogImage =
article.blogMeta() && article.blogMeta().featuredImage()
? `url(${article.blogMeta().featuredImage()})`
: null;
: defaultImage;
const blogTag = article.tags()
? article.tags().filter((tag) => tag.isChild())
: [];
Expand Down Expand Up @@ -320,7 +328,7 @@ export default class BlogOverview extends Page {
const blogImage =
article.blogMeta() && article.blogMeta().featuredImage()
? `url(${article.blogMeta().featuredImage()})`
: null;
: defaultImage;
const isSized =
article.blogMeta() && article.blogMeta().isSized();
const summary =
Expand Down
11 changes: 8 additions & 3 deletions js/src/forum/utils/discussionRouting.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ export default function () {
}
}
if (shouldRedirect) {
return app.route("blogArticle", {
id: discussion.slug(),
});
return discussion.lastReadPostNumber() > 1
? app.route("blogArticle.near", {
id: discussion.slug(),
near: discussion.lastReadPostNumber(),
})
: app.route("blogArticle", {
id: discussion.slug(),
});
} else {
return original_discussion_route(discussion, near);
}
Expand Down
2 changes: 1 addition & 1 deletion less/Forum/Item.less
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@
text-align: center;
padding: 0 10px;
margin: 15px 0;
word-break: break-word;
}

}
}
}
Expand Down
6 changes: 3 additions & 3 deletions less/Forum/Overview.less
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@

&-details {
padding: 80px 20px 20px 20px;
color: @body-bg;
color: #FFFFFF;
flex-grow: 1;
position: relative;
text-shadow: 0px 0px 4px fade(@text-color, 28%);
text-shadow: 0px 0px 4px fade(black, 28%);

h4 {
font-size: 22px;
Expand All @@ -100,7 +100,7 @@
}

.data {
color: @body-bg;
color: #FFFFFF;
position: relative;
z-index: 2;

Expand Down
3 changes: 2 additions & 1 deletion locale/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ v17development-flarum-blog:
redirect_articles_text: "When enabled, this extension will redirect original discussion URLs to their blog URL."
redirect_tags_label: "Redirect blog tags"
redirect_tags_text: "When enabled, this extension redirects blog tag URLs to the blog category URL."

default_article_image_label: Default article image
default_article_image_text: Select a default article image to display when no image is set.

forum:
blog: "Blog"
Expand Down
1 change: 1 addition & 0 deletions src/Api/AttachForumSerializerAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __invoke(ForumSerializer $serializer, $model, $attributes)
$attributes['blogRedirectsEnabled'] = $this->settings->get('blog_redirects_enabled', 'both');
$attributes['blogCommentsEnabled'] = $this->settings->get('blog_allow_comments', true);
$attributes['blogHideTags'] = $this->settings->get('blog_hide_tags', true);
$attributes['blogDefaultImage'] = $this->settings->get('blog_default_image_path', null);
$attributes['blogCategoryHierarchy'] = $this->settings->get('blog_category_hierarchy', true);
$attributes['blogAddSidebarNav'] = $this->settings->get('blog_add_sidebar_nav', true);
$attributes['canApproveBlogPosts'] = $serializer->getActor()->can('blog.canApprovePosts');
Expand Down
47 changes: 47 additions & 0 deletions src/Api/Controller/DeleteDefaultBlogImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace V17Development\FlarumBlog\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ServerRequestInterface;
use Laminas\Diactoros\Response\EmptyResponse;
use Flarum\Api\Controller\AbstractDeleteController;
use Flarum\Foundation\Paths;

class DeleteDefaultBlogImageController extends AbstractDeleteController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

/**
* @var Paths
*/
protected $paths;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths)
{
$this->settings = $settings;
$this->paths = $paths;
}
/**
* {@inheritdoc}
*/
protected function delete(ServerRequestInterface $request)
{
$request->getAttribute('actor')->assertAdmin();

$path = $this->settings->get('blog_default_image_path');
$this->settings->set('blog_default_image_path', null);
$uploadDir = new Filesystem(new Local($this->paths->public.'/assets'));
if ($uploadDir->has($path)) {
$uploadDir->delete($path);
}
return new EmptyResponse(204);
}
}
61 changes: 61 additions & 0 deletions src/Api/Controller/UploadDefaultBlogImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace V17Development\FlarumBlog\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Foundation\Paths;

class UploadDefaultBlogImageController extends ShowForumController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

/**
* @var Paths
*/
protected $paths;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths)
{
$this->settings = $settings;
$this->paths = $paths;
}
/**
* {@inheritdoc}
*/
public function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();

$file = Arr::get($request->getUploadedFiles(), 'blog_default_image');
$tmpFile = tempnam($this->paths->storage.'/tmp', 'blog-default');
$file->moveTo($tmpFile);

$mount = new MountManager([
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
'target' => new Filesystem(new Local($this->paths->public.'/assets')),
]);

if (($path = $this->settings->get('blog_default_image_path')) && $mount->has($file = "target://$path")) {
$mount->delete($file);
}

$uploadName = 'blog-default-'.Str::lower(Str::random(8)).'.png';
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
$this->settings->set('blog_default_image_path', $uploadName);
return parent::data($request, $document);
}
}
6 changes: 3 additions & 3 deletions src/Api/Serializer/BlogMetaSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ protected function getDefaultAttributes($meta)
return [
'featuredImage' => $meta->featured_image,
'summary' => $meta->summary,
'isFeatured' => $meta->is_featured,
'isSized' => $meta->is_sized,
'isPendingReview' => $meta->is_pending_review
'isFeatured' => (bool) $meta->is_featured,
'isSized' => (bool) $meta->is_sized,
'isPendingReview' => (bool) $meta->is_pending_review
];
}

Expand Down