Skip to content

Commit

Permalink
Various fixes (#96)
Browse files Browse the repository at this point in the history
* fix: Only display vote notification settings when a user can actually recieve them, based on admin settings #90

* Apply fixes from StyleCI

* fix: php8.1 error when settings key is null

* fix: error when rank badges preference is not zero #29

* feat: option to prevent users voting for themselves #28

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Oct 25, 2022
1 parent b4d70f0 commit 4fbd0c1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
9 changes: 4 additions & 5 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@
}),

(new Extend\Settings())
->default('fof-gamification.blockedUsers', '')
->default('fof-gamification.rankAmt', 2)
->default('fof-gamification.firstPostOnly', false)
->default('fof-gamification.allowSelfVotes', true)
->serializeToForum('fof-gamification.topimage1Url', 'fof-gamification.topimage1_path', function ($value) {
return $value ? "/assets/$value" : null;
})
Expand All @@ -128,11 +131,7 @@
->serializeToForum('fof-gamification-op-votes-only', 'fof-gamification.firstPostOnly', 'boolVal'),

(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attributes(function (Serializer\UserSerializer $serializer, User $user, array $attributes) {
$attributes['points'] = $user->votes;

return $attributes;
}),
->attributes(AddUserAttributes::class),

(new Extend\ApiSerializer(Serializer\BasicDiscussionSerializer::class))
->attributes(AddDiscussionData::class),
Expand Down
14 changes: 12 additions & 2 deletions js/src/admin/components/SettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class SettingsPage extends ExtensionPage {
'altPostVotingUi',
'upVotesOnly',
'firstPostOnly',
'allowSelfVotes',
];

this.ranks = app.store.all('ranks');
Expand Down Expand Up @@ -250,8 +251,9 @@ export default class SettingsPage extends ExtensionPage {
<label>{app.translator.trans('fof-gamification.admin.page.ranks.number_title')}</label>
<input
className="FormControl Ranks-default"
value={this.values.rankAmt() || ''}
placeholder="2"
value={this.values.rankAmt()}
type="number"
min="0"
oninput={withAttr('value', this.values.rankAmt)}
/>
</fieldset>,
Expand Down Expand Up @@ -386,6 +388,14 @@ export default class SettingsPage extends ExtensionPage {
20
);

items.add(
'allowSelfVotes',
<Switch state={this.values.allowSelfVotes()} onchange={this.values.allowSelfVotes} className="votes-switch">
{app.translator.trans('fof-gamification.admin.page.votes.allow_self_votes')}
</Switch>,
10
);

return items;
}

Expand Down
6 changes: 5 additions & 1 deletion js/src/forum/addNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import ItemList from 'flarum/common/utils/ItemList';
export default function addNotifications() {
app.notificationComponents.vote = VoteNotification;

extend(NotificationGrid.prototype, 'notificationTypes', function (items: ItemList) {
extend(NotificationGrid.prototype, 'notificationTypes', function (items: ItemList<{ name: string; icon: string; label: any }>) {
const user = app.session?.user;

if (!user?.canHaveVotingNotifications?.()) return;

items.add('vote', {
name: 'vote',
icon: 'fas fa-thumbs-up',
Expand Down
3 changes: 2 additions & 1 deletion js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import addNotifications from './addNotifications';
import addVotersToDiscussionPageSideBar from './addVotersToDiscussionPageSideBar';
import addUpvoteTabToUserProfile from './addUpvoteTabToUserProfile';

app.initializers.add('fof-gamification', (app) => {
app.initializers.add('fof-gamification', () => {
Discussion.prototype.votes = Model.attribute('votes');
Discussion.prototype.hasUpvoted = Model.attribute('hasUpvoted');
Discussion.prototype.hasDownvoted = Model.attribute('hasDownvoted');
Expand All @@ -31,6 +31,7 @@ app.initializers.add('fof-gamification', (app) => {

User.prototype.points = Model.attribute('points');
User.prototype.ranks = Model.hasMany('ranks');
User.prototype.canHaveVotingNotifications = Model.attribute('canHaveVotingNotifications');

Post.prototype.upvotes = Model.hasMany('upvotes');
Post.prototype.downvotes = Model.hasMany('downvotes');
Expand Down
3 changes: 2 additions & 1 deletion resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fof-gamification:
page:
rankings:
blocked:
placeholder: CDK2020, Ralkage, AngelAvila
placeholder: admin, user1, user2
title: Ignored Users
help: These users will not be shown on the ranking page. Usernames should be separated by a comma followed by a space.
title: Rankings Page
Expand All @@ -73,6 +73,7 @@ fof-gamification:
icon_help: "Input any Font-Awesome icon that is suffixed with -up and -down. Examples: arrow, thumbs, chevron"
upvotes_only: Only allow upvoting
first_post_only: Only allow up/down votes in the first post in a discussion
allow_self_votes: Users may vote on their own posts
save_settings: Save settings
convert:
button: Convert likes to upvotes
Expand Down
11 changes: 10 additions & 1 deletion src/Access/PostPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public function __construct(SettingsRepositoryInterface $settings)

private function isFirstPostOnlyMode(): bool
{
return $this->settings->get('fof-gamification.firstPostOnly', false);
return $this->settings->get('fof-gamification.firstPostOnly');
}

private function voteForSelfEnabled(): bool
{
return $this->settings->get('fof-gamification.allowSelfVotes');
}

public function vote(User $actor, Post $post)
Expand All @@ -39,6 +44,10 @@ public function vote(User $actor, Post $post)
return $this->deny();
}

if ($actor->id === $post->user_id && !$this->voteForSelfEnabled()) {
return $this->deny();
}

return $actor->can('votePosts', $post->discussion);
}

Expand Down
26 changes: 26 additions & 0 deletions src/AddUserAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

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

namespace FoF\Gamification;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;

class AddUserAttributes
{
public function __invoke(UserSerializer $serializer, User $user, array $attributes): array
{
$attributes['points'] = $user->votes;
$attributes['canHaveVotingNotifications'] = $user->hasPermission('discussion.upvote_notifications') || $user->hasPermission('discussion.downvote_notifications');

return $attributes;
}
}

0 comments on commit 4fbd0c1

Please sign in to comment.