Skip to content

Commit

Permalink
feat: Reintroduce make public (#167)
Browse files Browse the repository at this point in the history
* Re-introduce option to remove recipients and make the private discussion public

* Apply fixes from StyleCI

* format

* Use transformPublic, remove debug code

* Select tag when making public

* Present tag modal in promise

* Add translations

Co-authored-by: Ian Morland <[email protected]>
  • Loading branch information
imorland and imorland authored Dec 23, 2021
1 parent 8638fea commit a1e194e
Show file tree
Hide file tree
Showing 23 changed files with 500 additions and 30 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"homepage": "http://luceos.com"
},
{
"name": "Ian Morland",
"homepage": "https://discuss.flarum.org/u/ianm",
"name": "IanM",
"homepage": "https://blomstra.net",
"role": "Developer"
}
],
Expand Down
12 changes: 8 additions & 4 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@

(new Extend\Post())
->type(Posts\RecipientLeft::class)
->type(Posts\RecipientsModified::class),
->type(Posts\RecipientsModified::class)
->type(Posts\MadePublic::class),

(new Extend\Notification())
->type(Notifications\DiscussionCreatedBlueprint::class, Serializer\DiscussionSerializer::class, ['alert', 'email'])
->type(Notifications\DiscussionRepliedBlueprint::class, Serializer\DiscussionSerializer::class, ['alert', 'email'])
->type(Notifications\DiscussionRecipientRemovedBlueprint::class, Serializer\DiscussionSerializer::class, ['alert', 'email'])
->type(Notifications\DiscussionAddedBlueprint::class, Serializer\DiscussionSerializer::class, ['alert', 'email']),
->type(Notifications\DiscussionAddedBlueprint::class, Serializer\DiscussionSerializer::class, ['alert', 'email'])
->type(Notifications\DiscussionMadePublicBlueprint::class, Serializer\DiscussionSerializer::class, ['alert']),

(new Extend\Event())
->listen(PostSaving::class, Listeners\IgnoreApprovals::class)
Expand All @@ -144,10 +146,12 @@
->addGambit(Gambits\User\AllowsPdGambit::class),

(new Extend\Settings())
->serializeToForum('byobu.icon-badge', 'fof-byobu.icon-badge', function ($value) {
// we have to use the callback here, else we risk returning empty values instead of the defaults.
// see https://github.com/flarum/core/issues/3209
->serializeToForum('byobu.icon-badge', 'fof-byobu.icon-badge', function ($value): string {
return empty($value) ? 'fas fa-map' : $value;
})
->serializeToForum('byobu.icon-postAction', 'fof-byobu.icon-postAction', function ($value) {
->serializeToForum('byobu.icon-postAction', 'fof-byobu.icon-postAction', function ($value): string {
return empty($value) ? 'far fa-map' : $value;
}),
];
18 changes: 16 additions & 2 deletions js/src/admin/addPrivateDiscussionPermission.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import app from 'flarum/admin/app';

export default function () {
app.extensionData
.for('fof-byobu')
const byobuData = app.extensionData.for('fof-byobu');

byobuData
.registerPermission(
{
icon: 'far fa-map',
Expand Down Expand Up @@ -63,4 +64,17 @@ export default function () {
'moderate',
95
);

if (app.data.settings['fof-byobu.makePublic']) {
byobuData.registerPermission(
{
icon: 'far fa-map',
label: app.translator.trans('fof-byobu.admin.permission.make_private_into_public'),
permission: 'discussion.makePublic',
tagScoped: false,
},
'reply',
95
);
}
}
37 changes: 24 additions & 13 deletions js/src/admin/components/ByobuSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,46 @@ export default class ByobuSetingsPage extends ExtensionPage {
}

content() {
const helpText = flarum.extensions['flarum-tags']
? app.translator.trans('flarum-tags.admin.edit_tag.icon_text', {
a: <a href="https://fontawesome.com/icons?m=free" tabindex="-1" />,
})
: '';

return [
<div className="container">
<div className="ByobuSettingsPage">
<div className="ByobuSettingsPage">
<div className="container">
<div className="Form">
<div className="Form-group">
{this.buildSettingComponent({
type: 'string',
setting: 'fof-byobu.icon-badge',
label: app.translator.trans('fof-byobu.admin.settings.badge-icon'),
help: <Badge icon={this.setting('fof-byobu.icon-badge').toJSON() || this.badgeDefault}></Badge>,
help: (
<div>
<Badge icon={this.setting('fof-byobu.icon-badge').toJSON() || this.badgeDefault}></Badge> {helpText}
</div>
),
placeholder: this.badgeDefault,
})}
{this.buildSettingComponent({
type: 'string',
setting: 'fof-byobu.icon-postAction',
label: app.translator.trans('fof-byobu.admin.settings.post-event-icon'),
help: <h2>{icon(this.setting('fof-byobu.icon-postAction').toJSON() || this.postActionDefault)}</h2>,
help: (
<div>
{icon(this.setting('fof-byobu.icon-postAction').toJSON() || this.postActionDefault)} {helpText}
</div>
),
placeholder: this.postActionDefault,
})}
{this.buildSettingComponent({
type: 'boolean',
setting: 'fof-byobu.makePublic',
label: app.translator.trans('fof-byobu.admin.settings.enable-make-public-option'),
help: app.translator.trans('fof-byobu.admin.settings.enable-make-public-option-help'),
})}
</div>
{flarum.extensions['flarum-tags'] && (
<div className="Form-group">
<p>
{app.translator.trans('flarum-tags.admin.edit_tag.icon_text', {
a: <a href="https://fontawesome.com/icons?m=free" tabindex="-1" />,
})}
</p>
</div>
)}
<div className="Form-group">{this.submitButton()}</div>
</div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions js/src/forum/events/MadePublic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import app from 'flarum/forum/app';
import EventPost from 'flarum/forum/components/EventPost';

export default class MadePublic extends EventPost {
static initAttrs(attrs: any) {
super.initAttrs(attrs);
}

icon() {
return app.forum.attribute('byobu.icon-postAction');
}

descriptionKey() {
return 'fof-byobu.forum.post.recipients_modified.made_public';
}
}
2 changes: 2 additions & 0 deletions js/src/forum/events/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import app from 'flarum/forum/app';
import MadePublic from './MadePublic';
import RecipientLeft from './RecipientLeft';
import RecipientsModified from './RecipientsModified';

export default () => {
app.postComponents.recipientsModified = RecipientsModified;
app.postComponents.recipientLeft = RecipientLeft;
app.postComponents.madePublic = MadePublic;
};
37 changes: 37 additions & 0 deletions js/src/forum/extend/Discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import recipientsLabel from '../pages/labels/recipientsLabels';
import DiscussionControls from 'flarum/forum/utils/DiscussionControls';
import ItemList from 'flarum/common/utils/ItemList';
import AddRecipientModal from './../modals/AddRecipientModal';
import ByobuTagDiscussionModal from '../modals/ByobuTagDiscussionModal';
import DiscussionPage from 'flarum/components/DiscussionPage';

export default () => {
attributes();
Expand Down Expand Up @@ -137,6 +139,40 @@ function controls() {
app.translator.trans('fof-byobu.forum.buttons.remove_from_discussion')
)
);

if (discussion?.isPrivateDiscussion?.() && discussion?.canMakePublic?.()) {
items.add(
'transform-public',
<Button
icon="far fa-eye"
onclick={() => {
if (discussion && confirm(app.translator.trans('fof-byobu.forum.confirm.make_public'))) {
const recipientGroups = [];
const recipientUsers = [];

if (flarum.extensions['flarum-tags']) {
new Promise((resolve, reject) => {
app.modal.show(ByobuTagDiscussionModal, { discussion, resolve, reject });
}).then((tags) => {
discussion.save({ relationships: { recipientUsers, recipientGroups }, public: discussion.id() }).then(() => {
discussion.save({ relationships: { tags } }).then(() => {
if (app.current.matches(DiscussionPage)) {
app.current.get('stream').update();
}
m.redraw();
});
});
});
} else {
discussion.save({ relationships: { recipientUsers, recipientGroups }, public: discussion.id() }).then(() => m.redraw());
}
}
}}
>
{app.translator.trans('fof-byobu.forum.buttons.make_public')}
</Button>
);
}
}
});
}
Expand All @@ -151,6 +187,7 @@ function attributes() {
Discussion.prototype.canEditUserRecipients = Model.attribute('canEditUserRecipients');
Discussion.prototype.canEditGroupRecipients = Model.attribute('canEditGroupRecipients');
Discussion.prototype.canEditGroupRecipients = Model.attribute('canEditGroupRecipients');
Discussion.prototype.canMakePublic = Model.attribute('canMakePublic');

Discussion.prototype.isPrivateDiscussion = Model.attribute('isPrivateDiscussion');
}
16 changes: 16 additions & 0 deletions js/src/forum/modals/ByobuTagDiscussionModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import app from 'flarum/forum/app';
import TagDiscussionModal from 'flarum/tags/components/TagDiscussionModal';

export default class ByobuTagDiscussionModal extends TagDiscussionModal {
static isDismissible = false;

onsubmit(e) {
e.preventDefault();

const tags = this.selected;

if (this.attrs.resolve) this.attrs.resolve(tags);

this.hide();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import app from 'flarum/forum/app';
import Notification from 'flarum/components/Notification';

export default class PrivateDiscussionMadePublicNotification extends Notification {
icon() {
return app.forum.attribute('byobu.icon-badge');
}

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

return app.route.discussion(discussion);
}

content() {
const user = this.props.notification.fromUser();
return app.translator.trans('fof-byobu.forum.notifications.pd_made_public_text', {
user: user,
});
}
}
2 changes: 2 additions & 0 deletions js/src/forum/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import PrivateDiscussionNotification from './PrivateDiscussionNotification';
import PrivateDiscussionRepliedNotification from './PrivateDiscussionReplyNotification';
import PrivateDiscussionUserLeftNotification from './PrivateDiscussionUserLeftNotification';
import PrivateDiscussionAddedNotification from './PrivateDiscussionAddedNotification';
import PrivateDiscussionMadePublicNotification from './PrivateDiscussionMadePublicNotification';

export default function () {
app.notificationComponents.byobuPrivateDiscussionCreated = PrivateDiscussionNotification;
app.notificationComponents.byobuPrivateDiscussionReplied = PrivateDiscussionRepliedNotification;
app.notificationComponents.byobuRecipientRemoved = PrivateDiscussionUserLeftNotification;
app.notificationComponents.byobuPrivateDiscussionAdded = PrivateDiscussionAddedNotification;
app.notificationComponents.byobuPrivateDiscussionMadePubic = PrivateDiscussionMadePublicNotification;

grid();
}
Expand Down
11 changes: 9 additions & 2 deletions resources/less/admin.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.ByobuSettingsPage {
margin-top: 25px;
}
margin-top: 15px;

@media @desktop-up {
.container {
max-width: 600px;
margin: 0;
}
}
}
8 changes: 8 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ fof-byobu:
edit_recipients: Edit Recipients
send_pd: "Send {username} a message"
cancel: Cancel
make_public: Make this discussion public
remove_from_discussion: Leave this private discussion

composer_private_discussion:
submit_button: Post Private Discussion
title_placeholder: Private Discussion Title

confirm:
make_public: Are you sure you want to remove the recipients and make this discussion visible to anyone who can view the assigned tag?

labels:
recipients: "{count, plural, one {{count} Recipient} other {{count} Recipients}}"

Expand Down Expand Up @@ -49,6 +53,7 @@ fof-byobu:
recipients_modified:
added_and_removed: "{username} added recipients {added} and removed {removed}."
added: "{username} added recipients {added}."
made_public: "{username} removed all recipients and made the discussion public"
removed: "{username} removed recipients {removed}."
removed_self: "{username} left the private discussion."

Expand All @@ -75,10 +80,13 @@ fof-byobu:
create_private_discussions_with_blocking_users: Create private discussions with users that block it
edit_user_recipients: Edit users partaking in private discussions
edit_group_recipients: Edit groups partaking in private discussions
make_private_into_public: Transform a private discussion into a public discussion
view_private_discussions-when-flagged: View private discussions of other users if flagged

settings:
badge-icon: Byobu Discussion Badge
enable-make-public-option: Enable the "make public" ability
enable-make-public-option-help: Adds the ability for those with permission to remove all recipients, assign a new tag and make the discussion publically visible (accoring to the visibility settings of the chosen tag).
post-event-icon: Byobu Post Events

email:
Expand Down
6 changes: 6 additions & 0 deletions resources/views/emails/byobuMadePublic.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{!! $translator->trans('fof-byobu.email.body.made_public', [
'{recipient_display_name}' => $user->display_name,
'{actor_display_name}' => $blueprint->actor->display_name,
'{discussion_title}' => $blueprint->discussion->title,
'{discussion_url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->discussion->id]),
]) !!}
12 changes: 12 additions & 0 deletions src/Access/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FoF\Byobu\Access;

use Flarum\Discussion\Discussion;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Access\AbstractPolicy;
use Flarum\User\User;
use FoF\Byobu\Database\RecipientsConstraint;
Expand Down Expand Up @@ -94,4 +95,15 @@ private function isPrivate(Discussion $discussion): bool

return $screener->isPrivate();
}

public function transformToPublic(User $actor, Discussion $discussion)
{
/** @var SettingsRepositoryInterface $settings */
$settings = resolve('flarum.settings');
if (!(bool) $settings->get('fof-byobu.makePublic')) {
return $this->deny();
}

return $actor->can('makePublic', $discussion);
}
}
Loading

0 comments on commit a1e194e

Please sign in to comment.