Skip to content
Closed
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
22 changes: 20 additions & 2 deletions js/admin/dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16913,10 +16913,10 @@ System.register('flarum/app', ['flarum/App', 'flarum/initializers/store', 'flaru
});;
'use strict';

System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert', 'flarum/components/Button', 'flarum/components/RequestErrorModal', 'flarum/components/ConfirmPasswordModal', 'flarum/Translator', 'flarum/utils/extract', 'flarum/utils/patchMithril', 'flarum/utils/RequestError', 'flarum/extend'], function (_export, _context) {
System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert', 'flarum/components/Button', 'flarum/components/RequestErrorModal', 'flarum/components/ConfirmPasswordModal', 'flarum/Translator', 'flarum/utils/extract', 'flarum/utils/patchMithril', 'flarum/utils/RequestError', 'flarum/extend', 'flarum/utils/string'], function (_export, _context) {
"use strict";

var ItemList, Alert, Button, RequestErrorModal, ConfirmPasswordModal, Translator, extract, patchMithril, RequestError, extend, App;
var ItemList, Alert, Button, RequestErrorModal, ConfirmPasswordModal, Translator, extract, patchMithril, RequestError, extend, getPlainContent, truncate, App;
return {
setters: [function (_flarumUtilsItemList) {
ItemList = _flarumUtilsItemList.default;
Expand All @@ -16938,6 +16938,9 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert
RequestError = _flarumUtilsRequestError.default;
}, function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumUtilsString) {
getPlainContent = _flarumUtilsString.getPlainContent;
truncate = _flarumUtilsString.truncate;
}],
execute: function () {
App = function () {
Expand Down Expand Up @@ -17029,6 +17032,8 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert

this.title = '';
this.titleCount = 0;

this.description = '';
}

/**
Expand Down Expand Up @@ -17080,6 +17085,18 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert
value: function updateTitle() {
document.title = (this.titleCount ? '(' + this.titleCount + ') ' : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title');
}
}, {
key: 'setDescription',
value: function setDescription(description) {
description = truncate(getPlainContent(description), 300, 0);
this.description = description;
this.updateDescription();
}
}, {
key: 'updateDescription',
value: function updateDescription() {
document.head.querySelector('meta[name=description]').content = this.description ? this.description : this.forum.attribute('description');
}
}, {
key: 'request',
value: function request(originalOptions) {
Expand Down Expand Up @@ -22375,6 +22392,7 @@ System.register('flarum/models/Discussion', ['flarum/Model', 'flarum/utils/compu
babelHelpers.extends(Discussion.prototype, {
title: Model.attribute('title'),
slug: Model.attribute('slug'),
description: Model.attribute('description'),

startTime: Model.attribute('startTime', Model.transformDate),
startUser: Model.hasOne('startUser'),
Expand Down
30 changes: 27 additions & 3 deletions js/forum/dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18246,10 +18246,10 @@ System.register('flarum/app', ['flarum/ForumApp', 'flarum/initializers/store', '
});;
'use strict';

System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert', 'flarum/components/Button', 'flarum/components/RequestErrorModal', 'flarum/components/ConfirmPasswordModal', 'flarum/Translator', 'flarum/utils/extract', 'flarum/utils/patchMithril', 'flarum/utils/RequestError', 'flarum/extend'], function (_export, _context) {
System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert', 'flarum/components/Button', 'flarum/components/RequestErrorModal', 'flarum/components/ConfirmPasswordModal', 'flarum/Translator', 'flarum/utils/extract', 'flarum/utils/patchMithril', 'flarum/utils/RequestError', 'flarum/extend', 'flarum/utils/string'], function (_export, _context) {
"use strict";

var ItemList, Alert, Button, RequestErrorModal, ConfirmPasswordModal, Translator, extract, patchMithril, RequestError, extend, App;
var ItemList, Alert, Button, RequestErrorModal, ConfirmPasswordModal, Translator, extract, patchMithril, RequestError, extend, getPlainContent, truncate, App;
return {
setters: [function (_flarumUtilsItemList) {
ItemList = _flarumUtilsItemList.default;
Expand All @@ -18271,6 +18271,9 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert
RequestError = _flarumUtilsRequestError.default;
}, function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumUtilsString) {
getPlainContent = _flarumUtilsString.getPlainContent;
truncate = _flarumUtilsString.truncate;
}],
execute: function () {
App = function () {
Expand Down Expand Up @@ -18362,6 +18365,8 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert

this.title = '';
this.titleCount = 0;

this.description = '';
}

/**
Expand Down Expand Up @@ -18413,6 +18418,18 @@ System.register('flarum/App', ['flarum/utils/ItemList', 'flarum/components/Alert
value: function updateTitle() {
document.title = (this.titleCount ? '(' + this.titleCount + ') ' : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title');
}
}, {
key: 'setDescription',
value: function setDescription(description) {
description = truncate(getPlainContent(description), 300, 0);
this.description = description;
this.updateDescription();
}
}, {
key: 'updateDescription',
value: function updateDescription() {
document.head.querySelector('meta[name=description]').content = this.description ? this.description : this.forum.attribute('description');
}
}, {
key: 'request',
value: function request(originalOptions) {
Expand Down Expand Up @@ -21161,6 +21178,9 @@ System.register('flarum/components/DiscussionPage', ['flarum/components/Page', '
app.setTitle(discussion.title());
app.setTitleCount(0);

var description = this.discussion.description() || this.discussion.startPost.content();
app.setDescription(description);

// When the API responds with a discussion, it will also include a number of
// posts. Some of these posts are included because they are on the first
// page of posts we want to display (determined by the `near` parameter) –
Expand Down Expand Up @@ -22649,6 +22669,7 @@ System.register('flarum/components/IndexPage', ['flarum/extend', 'flarum/compone

app.setTitle('');
app.setTitleCount(0);
app.setDescription('');

// Work out the difference between the height of this hero and that of the
// previous hero. Maintain the same scroll position relative to the bottom
Expand Down Expand Up @@ -24950,7 +24971,7 @@ System.register('flarum/components/PostStream', ['flarum/Component', 'flarum/uti
}, {
key: 'update',
value: function update() {
if (!this.viewingEnd) return;
if (!this.viewingEnd) return m.deferred().resolve().promise;

this.visibleEnd = this.count();

Expand Down Expand Up @@ -27164,6 +27185,7 @@ System.register('flarum/components/SettingsPage', ['flarum/components/UserPage',

this.show(app.session.user);
app.setTitle(app.translator.trans('core.forum.settings.title'));
app.setDescription('');
}
}, {
key: 'content',
Expand Down Expand Up @@ -27997,6 +28019,7 @@ System.register('flarum/components/UserPage', ['flarum/components/Page', 'flarum
this.user = user;

app.setTitle(user.displayName());
app.setDescription('');

m.redraw();
}
Expand Down Expand Up @@ -29427,6 +29450,7 @@ System.register('flarum/models/Discussion', ['flarum/Model', 'flarum/utils/compu
babelHelpers.extends(Discussion.prototype, {
title: Model.attribute('title'),
slug: Model.attribute('slug'),
description: Model.attribute('description'),

startTime: Model.attribute('startTime', Model.transformDate),
startUser: Model.hasOne('startUser'),
Expand Down
3 changes: 3 additions & 0 deletions js/forum/src/components/DiscussionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ export default class DiscussionPage extends Page {
app.setTitle(discussion.title());
app.setTitleCount(0);

let description = this.discussion.description() || this.discussion.startPost.content();
app.setDescription(description);

// When the API responds with a discussion, it will also include a number of
// posts. Some of these posts are included because they are on the first
// page of posts we want to display (determined by the `near` parameter) –
Expand Down
1 change: 1 addition & 0 deletions js/forum/src/components/IndexPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default class IndexPage extends Page {

app.setTitle('');
app.setTitleCount(0);
app.setDescription('');

// Work out the difference between the height of this hero and that of the
// previous hero. Maintain the same scroll position relative to the bottom
Expand Down
1 change: 1 addition & 0 deletions js/forum/src/components/SettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class SettingsPage extends UserPage {

this.show(app.session.user);
app.setTitle(app.translator.trans('core.forum.settings.title'));
app.setDescription('');
}

content() {
Expand Down
1 change: 1 addition & 0 deletions js/forum/src/components/UserPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default class UserPage extends Page {
this.user = user;

app.setTitle(user.displayName());
app.setDescription('');

m.redraw();
}
Expand Down
20 changes: 20 additions & 0 deletions js/lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import extract from 'flarum/utils/extract';
import patchMithril from 'flarum/utils/patchMithril';
import RequestError from 'flarum/utils/RequestError';
import { extend } from 'flarum/extend';
import { getPlainContent, truncate } from 'flarum/utils/string';

/**
* The `App` class provides a container for an application, as well as various
Expand Down Expand Up @@ -100,6 +101,8 @@ export default class App {

this.title = '';
this.titleCount = 0;

this.description = '';
}

/**
Expand Down Expand Up @@ -159,6 +162,23 @@ export default class App {
this.forum.attribute('title');
}

/**
* Set the <meta name="description"> of the page.
*
* @param {String} description
* @public
*/
setDescription(description) {
description = truncate(getPlainContent(description), 300, 0);
this.description = description;
this.updateDescription();
}

updateDescription() {
document.head.querySelector('meta[name=description]').content =
(this.description ? this.description : this.forum.attribute('description'));
}

/**
* Make an AJAX request, handling any low-level errors that may occur.
*
Expand Down
1 change: 1 addition & 0 deletions js/lib/models/Discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class Discussion extends Model {}
Object.assign(Discussion.prototype, {
title: Model.attribute('title'),
slug: Model.attribute('slug'),
description: Model.attribute('description'),

startTime: Model.attribute('startTime', Model.transformDate),
startUser: Model.hasOne('startUser'),
Expand Down
12 changes: 9 additions & 3 deletions migrations/2016_02_04_095452_add_slug_to_discussions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
* file that was distributed with this source code.
*/

use Flarum\Util\Str;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Str;

return [
'up' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$table->string('slug');
});

$app = Application::getInstance();
$settings = $app->make(SettingsRepositoryInterface::class);
$locale = $settings->get('default_locale') ?? 'en';

// Store slugs for existing discussions
$schema->getConnection()->table('discussions')->chunkById(100, function ($discussions) use ($schema) {
$schema->getConnection()->table('discussions')->chunkById(100, function ($discussions) use ($schema, $locale) {
foreach ($discussions as $discussion) {
$schema->getConnection()->table('discussions')->where('id', $discussion->id)->update([
'slug' => Str::slug($discussion->title)
'slug' => Str::slug($discussion->title, '-', $locale)
]);
}
});
Expand Down
27 changes: 27 additions & 0 deletions migrations/2018_03_18_194500_add_description_to_discussions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$table->string('description')->after('title');
});
},

'down' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$table->dropColumn('description');
});
}
];
3 changes: 2 additions & 1 deletion src/Api/Serializer/BasicDiscussionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected function getDefaultAttributes($discussion)

return [
'title' => $discussion->title,
'slug' => $discussion->slug,
'description' => $discussion->description,
'slug' => $discussion->slug
];
}

Expand Down
25 changes: 23 additions & 2 deletions src/Discussion/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
use Flarum\Discussion\Event\Restored;
use Flarum\Discussion\Event\Started;
use Flarum\Event\GetModelIsPrivate;
use Flarum\Foundation\Application;
use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Post\MergeableInterface;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Flarum\Util\Str;
use Illuminate\Support\Str;

/**
* @property int $id
* @property string $title
* @property string $slug
* @property string $description
* @property int $comments_count
* @property int $participants_count
* @property int $number_index
Expand Down Expand Up @@ -461,6 +464,24 @@ public static function setStateUser(User $user)
protected function setTitleAttribute($title)
{
$this->attributes['title'] = $title;
$this->slug = Str::slug($title);
$app = Application::getInstance();
$settings = $app->make(SettingsRepositoryInterface::class);
$locale = $settings->get('default_locale') ?? 'en';
$this->slug = Str::slug($title, '-', $locale);
}

/**
* Get the description.
*
* Returns the stored description (if any) or generates one based on startPost´s content.
*
* @param string $description
* @return string
*/
protected function getDescriptionAttribute($description)
{
$description = ($description == null) ? $this->startPost->content : $description;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is gonna be inefficient when listing discussions D:

Copy link
Contributor Author

@johannsa johannsa Apr 1, 2018

Choose a reason for hiding this comment

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

@tobscure Are you referring to $this->startPost->content, right? The other option is to always have a description (from discussion creation) on the database, not only when you (i.e.: via 3rd party extension) want to have a custom non-generated description there (that was my original intention when adding the field).

I guess we could:

  1. Store a generated (first 300 characters plus '...' if needed) description upon discussion creation and/or update.
  2. In case the forum admin is interested in custom descriptions a third party extension would have to set the description field and prevent it from being edited (via additional listeners and maybe additional field) in case the OP edits the start post.

What do you think about this approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think we need to do something like this - any better ideas @franzliedke?

Copy link
Member

Choose a reason for hiding this comment

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

The line we're talking about could also have been improved with the null coalescence operator.

I, however, agree to store a generated description, but only if none is set beforehand.


return $description;
}
}
1 change: 1 addition & 0 deletions src/Forum/Controller/DiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected function getView(Request $request)
}

$view->title = $document->data->attributes->title;
$view->description = $document->data->attributes->description;
$view->document = $document;
$view->content = app('view')->make('flarum.forum::frontend.content.discussion', compact('document', 'page', 'getResource', 'posts', 'url'));

Expand Down
Loading