1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 20:04:24 +02:00

Merge branch 'master' into next-back

# Conflicts:
#	src/Listener/AddPostMentionedByRelationship.php
#	src/Listener/FormatPostMentions.php
#	src/Listener/FormatUserMentions.php
#	src/Listener/UpdatePostMentionsMetadata.php
#	src/Listener/UpdateUserMentionsMetadata.php
This commit is contained in:
Toby Zerner
2017-12-26 20:20:31 +10:30
13 changed files with 204 additions and 80 deletions

View File

@@ -24,6 +24,7 @@ use Flarum\Post\Post;
use Flarum\Post\PostRepository;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Collection;
class AddPostMentionedByRelationship
{
@@ -94,7 +95,7 @@ class AddPostMentionedByRelationship
*/
public function includeRelationships(WillGetData $event)
{
if ($event->isController(ShowDiscussionController::class)) {
if ($event->isController(Controller\ShowDiscussionController::class)) {
$event->addInclude([
'posts.mentionedBy',
'posts.mentionedBy.user',
@@ -102,8 +103,8 @@ class AddPostMentionedByRelationship
]);
}
if ($event->isController(ShowPostController::class)
|| $event->isController(ListPostsController::class)) {
if ($event->isController(Controller\ShowPostController::class)
|| $event->isController(Controller\ListPostsController::class)) {
$event->addInclude([
'mentionedBy',
'mentionedBy.user',
@@ -111,7 +112,7 @@ class AddPostMentionedByRelationship
]);
}
if ($event->isController(CreatePostController::class)) {
if ($event->isController(Controller\CreatePostController::class)) {
$event->addInclude([
'mentionsPosts',
'mentionsPosts.mentionedBy'
@@ -133,22 +134,33 @@ class AddPostMentionedByRelationship
{
// Firstly we gather a list of posts contained within the API document.
// This will vary according to the API endpoint that is being accessed.
if ($event->isController(ShowDiscussionController::class)) {
if ($event->isController(Controller\ShowDiscussionController::class)) {
$posts = $event->data->posts;
} elseif ($event->isController(ShowPostController::class)) {
} elseif ($event->isController(Controller\ShowPostController::class)
|| $event->isController(Controller\CreatePostController::class)
|| $event->isController(Controller\UpdatePostController::class)) {
$posts = [$event->data];
} elseif ($event->isController(ListPostsController::class)) {
} elseif ($event->isController(Controller\ListPostsController::class)) {
$posts = $event->data;
}
if (isset($posts)) {
$posts = array_filter((array) $posts, 'is_object');
$posts = new Collection($posts);
$posts = $posts->filter(function ($post) {
return $post instanceof CommentPost;
});
// Load all of the users that these posts mention. This way the data
// will be ready to go when we need to sub in current usernames
// during the rendering process.
$posts->load(['mentionsUsers', 'mentionsPosts.user']);
// Construct a list of the IDs of all of the posts that these posts
// have been mentioned in. We can then filter this list of IDs to
// weed out all of the ones which the user is not meant to see.
$ids = [];
// Once we have the posts, construct a list of the IDs of all of
// the posts that they have been mentioned in. We can then filter
// this list of IDs to weed out all of the ones which the user is
// not meant to see.
foreach ($posts as $post) {
$ids = array_merge($ids, $post->mentionedBy->pluck('id')->all());
}

View File

@@ -16,6 +16,7 @@ use Flarum\Formatter\Event\Rendering;
use Flarum\Http\UrlGenerator;
use Flarum\Post\CommentPost;
use Illuminate\Contracts\Events\Dispatcher;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
@@ -48,22 +49,23 @@ class FormatPostMentions
{
$configurator = $event->configurator;
$configurator->rendering->parameters['DISCUSSION_URL'] = $this->url->to('forum')->route('discussion', ['id' => '']);
$tagName = 'POSTMENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('number')->filterChain->append('#uint');
$tag->attributes->add('discussionid')->filterChain->append('#uint');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->attributes['number']->required = false;
$tag->attributes['discussionid']->required = false;
$tag->template = '<a href="{$DISCUSSION_URL}{@discussionid}/{@number}" class="PostMention" data-id="{@id}"><xsl:value-of select="@username"/></a>';
$tag->template = '<a href="{$DISCUSSION_URL}{@discussionid}/{@number}" class="PostMention" data-id="{@id}"><xsl:value-of select="@displayname"/></a>';
$tag->filterChain
->prepend([static::class, 'addId'])
->setJS('function() { return true; }');
->setJS('function(tag) { return System.get("flarum/mentions/utils/textFormatter").filterPostMentions(tag); }');
$configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)#(?<id>\d+)/i', $tagName);
}
@@ -73,7 +75,16 @@ class FormatPostMentions
*/
public function render(Rendering $event)
{
$event->renderer->setParameter('DISCUSSION_URL', $this->url->to('forum')->route('discussion', ['id' => '']));
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
return $attributes;
});
}
/**
@@ -87,6 +98,7 @@ class FormatPostMentions
if ($post) {
$tag->setAttribute('discussionid', (int) $post->discussion_id);
$tag->setAttribute('number', (int) $post->number);
$tag->setAttribute('displayname', $post->user->display_name);
return true;
}

View File

@@ -15,28 +15,22 @@ use Flarum\Formatter\Event\Configuring;
use Flarum\Formatter\Event\Parsing;
use Flarum\Formatter\Event\Rendering;
use Flarum\Http\UrlGenerator;
use Flarum\User\UserRepository;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
/**
* @var UserRepository
*/
protected $users;
/**
* @var UrlGenerator
*/
protected $url;
/**
* @param UserRepository $users
* @param UrlGenerator $url
*/
public function __construct(UserRepository $users, UrlGenerator $url)
public function __construct(UrlGenerator $url)
{
$this->users = $users;
$this->url = $url;
}
@@ -46,7 +40,6 @@ class FormatUserMentions
public function subscribe(Dispatcher $events)
{
$events->listen(Configuring::class, [$this, 'configure']);
$events->listen(Parsing::class, [$this, 'parse']);
$events->listen(Rendering::class, [$this, 'render']);
}
@@ -57,46 +50,50 @@ class FormatUserMentions
{
$configurator = $event->configurator;
$configurator->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']);
$tagName = 'USERMENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->attributes['id']->required = false;
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@username"/></a>';
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@displayname"/></a>';
$tag->filterChain->prepend([static::class, 'addId'])
->addParameterByName('userRepository')
->setJS('function() { return true; }');
->setJS('function(tag) { return System.get("flarum/mentions/utils/textFormatter").filterUserMentions(tag); }');
$configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)(?!#)/i', $tagName);
}
/**
* @param Parsing $event
*/
public function parse(Parsing $event)
{
$event->parser->registeredVars['userRepository'] = $this->users;
}
/**
* @param Rendering $event
*/
public function render(Rendering $event)
{
$event->renderer->setParameter('PROFILE_URL', $this->url->to('forum')->route('user', ['username' => '']));
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'USERMENTION', function ($attributes) use ($post) {
$user = $post->mentionsUsers->find($attributes['id']);
if ($user) {
$attributes['displayname'] = $user->display_name;
}
return $attributes;
});
}
/**
* @param $tag
* @param UserRepository $users
*
* @return bool
*/
public static function addId($tag, UserRepository $users)
public static function addId($tag)
{
if ($id = $users->getIdForUsername($tag->getAttribute('username'))) {
$tag->setAttribute('id', $id);
if ($user = User::where('username', 'like', $tag->getAttribute('username'))->first()) {
$tag->setAttribute('id', $user->id);
$tag->setAttribute('displayname', $user->display_name);
return true;
}

View File

@@ -11,7 +11,7 @@
namespace Flarum\Mentions\Listener;
use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Mentions\Notification\PostMentionedBlueprint;
use Flarum\Notification\NotificationSyncer;
@@ -57,7 +57,7 @@ class UpdatePostMentionsMetadata
*/
public function addNotificationType(ConfigureNotificationTypes $event)
{
$event->add(PostMentionedBlueprint::class, BasicPostSerializer::class, ['alert']);
$event->add(PostMentionedBlueprint::class, PostSerializer::class, ['alert']);
}
/**

View File

@@ -11,7 +11,7 @@
namespace Flarum\Mentions\Listener;
use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Mentions\Notification\UserMentionedBlueprint;
use Flarum\Notification\NotificationSyncer;
@@ -58,7 +58,7 @@ class UpdateUserMentionsMetadata
*/
public function addNotificationType(ConfigureNotificationTypes $event)
{
$event->add(UserMentionedBlueprint::class, BasicPostSerializer::class, ['alert']);
$event->add(UserMentionedBlueprint::class, PostSerializer::class, ['alert']);
}
/**