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

Notification on merge #32

Merged
merged 5 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
}
],
"require": {
"flarum/core": "^1.0.0"
"flarum/core": "^1.1.0"
},
"authors": [
{
"name": "David Sevilla Martín",
"email": "me+fof@datitisev.me",
"role": "Developer"
},
{
"name": "IanM",
"email": "ian@blomstra.net",
"role": "Developer"
}
],
"autoload": {
Expand Down
9 changes: 8 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
->type(DiscussionMergePost::class),

(new Extend\Event())
->listen(DiscussionWasMerged::class, Listeners\CreatePostWhenMerged::class),
->listen(DiscussionWasMerged::class, Listeners\CreatePostWhenMerged::class)
->listen(DiscussionWasMerged::class, Listeners\NotifyParticipantsWhenMerged::class),

(new Extend\ApiSerializer(DiscussionSerializer::class))
->attribute('canMerge', function (DiscussionSerializer $serializer, AbstractModel $discussion) {
Expand All @@ -43,4 +44,10 @@

(new Extend\Settings())
->serializeToForum('fof-merge-discussions.search_limit', 'fof-merge-discussions.search_limit', 'intVal', 4),

(new Extend\View())
->namespace('fof-merge-discussions', __DIR__.'/resources/views'),

(new Extend\Notification())
->type(Notification\DiscussionMergedBlueprint::class, DiscussionSerializer::class, ['alert', 'email']),
];
8 changes: 8 additions & 0 deletions js/src/forum/addNotifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import app from 'flarum/forum/app';
import DiscussionMergedNotification from './components/DiscussionMergedNotification';
import extendNotifications from "./extend/extendNotifications";

export default function() {
app.notificationComponents.discussionMerged = DiscussionMergedNotification;
extendNotifications();
}
27 changes: 27 additions & 0 deletions js/src/forum/components/DiscussionMergedNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import app from 'flarum/forum/app';
import Notification from 'flarum/forum/components/Notification';

export default class DiscussionMergedNotification extends Notification {
icon() {
return 'fas fa-code-branch fa-flip-vertical';
}

href() {
const notification = this.attrs.notification;
const discussion = notification.subject();

return app.route.discussion(discussion);
}

content() {
const notification = this.attrs.notification;
const user = notification.fromUser();
const oldDiscussion = notification.content();
const oldTitle = oldDiscussion.merged_title;

return app.translator.trans('fof-merge-discussions.forum.notification.discussion_merged', {
user,
oldTitle
});
}
}
14 changes: 14 additions & 0 deletions js/src/forum/extend/extendNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import ItemList from 'flarum/common/utils/ItemList';
import NotificationGrid from 'flarum/forum/components/NotificationGrid';

export default function () {
extend(NotificationGrid.prototype, 'notificationTypes', function (items: ItemList) {
items.add('discussionMerged', {
name: 'discussionMerged',
icon: 'fas fa-code-branch fa-flip-vertical',
label: app.translator.trans('fof-merge-discussions.forum.notification.preferences.discussion_merged'),
});
});
}
3 changes: 3 additions & 0 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DiscussionControls from 'flarum/forum/utils/DiscussionControls';

import DiscussionMergeModal from './components/DiscussionMergeModal';
import DiscussionMergePost from './components/DiscussionMergePost';
import addNotifications from './addNotifications';

app.initializers.add('fof/merge-discussions', () => {
app.store.models.discussions.prototype.canMerge = Model.attribute('canMerge');
Expand All @@ -26,4 +27,6 @@ app.initializers.add('fof/merge-discussions', () => {
)
);
});

addNotifications();
});
15 changes: 15 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ fof-merge-discussions:
load_preview_button: Preview
submit_button: => fof-merge-discussions.ref.merge

notification:
discussion_merged: Your discussion <b>{oldTitle}</b> was merged into this discussion by {username}.
preferences:
discussion_merged: A moderator merges one of my discussions with another
imorland marked this conversation as resolved.
Show resolved Hide resolved

discussion:
merge: => fof-merge-discussions.ref.merge

Expand All @@ -34,3 +39,13 @@ fof-merge-discussions:

ref:
merge: Merge

email:
merged:
body: |
Hey {recipient_display_name},

Your discussion, {merged_discussion_title}, was merged into {discussion_title} by {actor_display_name}
imorland marked this conversation as resolved.
Show resolved Hide resolved

View it here {discussion_url}
subject: Your discussion "{merged_discussion_title}" was merged
7 changes: 7 additions & 0 deletions resources/views/emails/discussionMerged.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{!! $translator->trans('fof-merge-discussions.email.merged.body', [
'{recipient_display_name}' => $user->display_name,
'{actor_display_name}' => $blueprint->actor->display_name,
'{merged_discussion_title}' => $blueprint->mergedDiscussion->title,
'{discussion_title}' => $blueprint->discussion->title,
'{discussion_url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->discussion->id]),
]) !!}
60 changes: 60 additions & 0 deletions src/Jobs/SendNotificationWhenDiscussionIsMerged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of fof/merge-discussions.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\MergeDiscussions\Jobs;

use Flarum\Discussion\Discussion;
use Flarum\Notification\NotificationSyncer;
use Flarum\User\User;
use FoF\MergeDiscussions\Notification\DiscussionMergedBlueprint;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;

class SendNotificationWhenDiscussionIsMerged implements ShouldQueue
{
use Queueable;
use SerializesModels;

/**
* @var Discussion
*/
protected $discussion;

/**
* @var User
*/
protected $actor;

/**
* @var Discussion[]
*/
protected $mergedDiscussions;

public function __construct(Discussion $discussion, array $mergedDiscussions, User $actor)
{
$this->discussion = $discussion;
$this->mergedDiscussions = $mergedDiscussions;
$this->actor = $actor;
}

public function handle(NotificationSyncer $notifications): void
{
foreach ($this->mergedDiscussions as $mergedDiscussion) {
/** @var Discussion $mergedDiscussion */
$user = User::find($mergedDiscussion->user_id);

if ($user) {
$notifications->sync(new DiscussionMergedBlueprint($this->discussion, $this->actor, $mergedDiscussion), [$user]);
}
}
}
}
25 changes: 25 additions & 0 deletions src/Listeners/NotifyParticipantsWhenMerged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of fof/merge-discussions.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\MergeDiscussions\Listeners;

use FoF\MergeDiscussions\Events\DiscussionWasMerged;
use FoF\MergeDiscussions\Jobs;

class NotifyParticipantsWhenMerged
{
public function handle(DiscussionWasMerged $event): void
{
resolve('flarum.queue.connection')->push(
new Jobs\SendNotificationWhenDiscussionIsMerged($event->discussion, $event->mergedDiscussions, $event->actor)
);
}
}
114 changes: 114 additions & 0 deletions src/Notification/DiscussionMergedBlueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/*
* This file is part of fof/merge-discussions.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\MergeDiscussions\Notification;

use Flarum\Discussion\Discussion;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\MailableInterface;
use Flarum\User\User;
use Symfony\Contracts\Translation\TranslatorInterface;

class DiscussionMergedBlueprint implements BlueprintInterface, MailableInterface
{
/**
* @var Discussion
*/
public $discussion;

/**
* @var User
*/
public $actor;

/**
* @var Discussion
*/
public $mergedDiscussion;

public function __construct(Discussion $discussion, User $actor, Discussion $mergedDiscussion)
{
$this->discussion = $discussion;
$this->actor = $actor;
$this->mergedDiscussion = $mergedDiscussion;
}

/**
* Get the user that sent the notification.
*/
public function getFromUser()
{
return $this->actor;
}

/**
* Get the model that is the subject of this activity.
*/
public function getSubject()
{
return $this->discussion;
}

/**
* Get the data to be stored in the notification.
*/
public function getData()
{
return [
'merged_title' => $this->mergedDiscussion->title,
'merged_id' => $this->mergedDiscussion->id,
];
}

/**
* Get the serialized type of this activity.
*
* @return string
*/
public static function getType()
{
return 'discussionMerged';
}

/**
* Get the name of the model class for the subject of this activity.
*
* @return string
*/
public static function getSubjectModel()
{
return Discussion::class;
}

/**
* Get the name of the view to construct a notification email with.
*
* @return string
*/
public function getEmailView()
{
return ['text' => 'fof-merge-discussions::emails.discussionMerged'];
}

/**
* Get the subject line for the notification email.
*
* @return string
*/
public function getEmailSubject(TranslatorInterface $translator)
{
return $translator->trans('fof-merge-discussions.email.merged.subject', [
'{display_name}' => $this->actor->display_name,
'{discussion_title}' => $this->discussion->title,
'{merged_discussion_title}' => $this->mergedDiscussion->title,
]);
}
}