From 54f731942e453619e030725b0b3e1bd4ee292b1c Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 6 May 2015 11:25:19 +0930 Subject: [PATCH] Add user group badges --- js/forum/src/components/post-header-user.js | 2 +- js/lib/models/group.js | 5 +- js/lib/models/user.js | 19 ++++++- less/forum/discussion.less | 6 +-- less/lib/badges.less | 6 +-- .../2015_02_24_000000_create_groups_table.php | 51 ++++++++++--------- src/Api/Actions/Posts/ShowAction.php | 1 + src/Api/Serializers/GroupSerializer.php | 7 ++- src/Api/Serializers/UserBasicSerializer.php | 5 ++ src/Api/Serializers/UserSerializer.php | 5 -- src/Core/Seeders/GroupsTableSeeder.php | 14 ++++- 11 files changed, 78 insertions(+), 43 deletions(-) diff --git a/js/forum/src/components/post-header-user.js b/js/forum/src/components/post-header-user.js index 00d492333..6627dc746 100644 --- a/js/forum/src/components/post-header-user.js +++ b/js/forum/src/components/post-header-user.js @@ -25,7 +25,7 @@ export default class PostHeaderUser extends Component { avatar(user), username(user) ]), - m('ul.badges', listItems(user.badges().toArray())) + m('ul.badges', listItems(user.badges().toArray().reverse())) ] : [ avatar(), username() diff --git a/js/lib/models/group.js b/js/lib/models/group.js index 8b8623d61..3379fa383 100644 --- a/js/lib/models/group.js +++ b/js/lib/models/group.js @@ -3,6 +3,9 @@ import Model from 'flarum/model'; class Group extends Model {} Group.prototype.id = Model.prop('id'); -Group.prototype.name = Model.prop('name'); +Group.prototype.nameSingular = Model.prop('nameSingular'); +Group.prototype.namePlural = Model.prop('namePlural'); +Group.prototype.color = Model.prop('color'); +Group.prototype.icon = Model.prop('icon'); export default Group; diff --git a/js/lib/models/user.js b/js/lib/models/user.js index c083065db..87d62fad8 100644 --- a/js/lib/models/user.js +++ b/js/lib/models/user.js @@ -2,6 +2,7 @@ import Model from 'flarum/model' import stringToColor from 'flarum/utils/string-to-color'; import ItemList from 'flarum/utils/item-list'; import computed from 'flarum/utils/computed'; +import Badge from 'flarum/components/badge'; class User extends Model {} @@ -48,6 +49,22 @@ User.prototype.color = computed('username', 'avatarUrl', 'avatarColor', function } }); -User.prototype.badges = () => new ItemList(); +User.prototype.badges = function() { + var items = new ItemList(); + + this.groups().forEach(group => { + if (group.id() != 3) { + items.add('group'+group.id(), + Badge.component({ + label: group.nameSingular(), + icon: group.icon(), + style: {backgroundColor: group.color()} + }) + ); + } + }); + + return items; +} export default User; diff --git a/less/forum/discussion.less b/less/forum/discussion.less index 2ce2ec142..ef501d6ad 100644 --- a/less/forum/discussion.less +++ b/less/forum/discussion.less @@ -191,7 +191,7 @@ font-size: 15px; } - & .badges { + & h3 .badges { text-align: right; white-space: nowrap; @@ -337,7 +337,7 @@ .post-user { position: relative; - & .badges { + & h3 .badges { position: absolute; top: -7px; left: 5px; @@ -381,7 +381,7 @@ } } .post-user { - & .badges { + & h3 .badges { float: left; margin-left: -85px; margin-top: -3px; diff --git a/less/lib/badges.less b/less/lib/badges.less index bf4b8fad9..a63696f03 100644 --- a/less/lib/badges.less +++ b/less/lib/badges.less @@ -12,13 +12,13 @@ width: @size; height: @size; border-radius: @size / 2; - line-height: @size - 4px; + line-height: @size - 5px; &, & .fa { - font-size: 0.6 * @size; + font-size: 0.56 * @size; } } .badge { - .badge-size(23px); + .badge-size(25px); border: 2px solid @fl-body-bg; background: @fl-body-muted-color; color: #fff; diff --git a/migrations/2015_02_24_000000_create_groups_table.php b/migrations/2015_02_24_000000_create_groups_table.php index 34e5b354f..44ec0710d 100644 --- a/migrations/2015_02_24_000000_create_groups_table.php +++ b/migrations/2015_02_24_000000_create_groups_table.php @@ -3,30 +3,31 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateGroupsTable extends Migration { - - /** - * Run the migrations. - * - * @return void - */ - public function up() - { - Schema::create('groups', function(Blueprint $table) - { - $table->increments('id'); - $table->string('name'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::drop('groups'); - } +class CreateGroupsTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('groups', function (Blueprint $table) { + $table->increments('id'); + $table->string('name_singular'); + $table->string('name_plural'); + $table->string('color')->nullable(); + $table->string('icon')->nullable(); + }); + } + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('groups'); + } } diff --git a/src/Api/Actions/Posts/ShowAction.php b/src/Api/Actions/Posts/ShowAction.php index 18f96de34..77344e1f1 100644 --- a/src/Api/Actions/Posts/ShowAction.php +++ b/src/Api/Actions/Posts/ShowAction.php @@ -28,6 +28,7 @@ class ShowAction extends SerializeResourceAction */ public static $include = [ 'user' => true, + 'user.groups' => true, 'editUser' => true, 'hideUser' => true, 'discussion' => false diff --git a/src/Api/Serializers/GroupSerializer.php b/src/Api/Serializers/GroupSerializer.php index 352315a38..395351700 100644 --- a/src/Api/Serializers/GroupSerializer.php +++ b/src/Api/Serializers/GroupSerializer.php @@ -18,8 +18,11 @@ class GroupSerializer extends BaseSerializer protected function attributes($group) { $attributes = [ - 'id' => (int) $group->id, - 'name' => $group->name + 'id' => (int) $group->id, + 'nameSingular' => $group->name_singular, + 'namePlural' => $group->name_plural, + 'color' => $group->color, + 'icon' => $group->icon, ]; return $this->extendAttributes($group, $attributes); diff --git a/src/Api/Serializers/UserBasicSerializer.php b/src/Api/Serializers/UserBasicSerializer.php index 6f463da68..b1bfd1781 100644 --- a/src/Api/Serializers/UserBasicSerializer.php +++ b/src/Api/Serializers/UserBasicSerializer.php @@ -24,4 +24,9 @@ class UserBasicSerializer extends BaseSerializer return $this->extendAttributes($user, $attributes); } + + protected function groups() + { + return $this->hasMany('Flarum\Api\Serializers\GroupSerializer'); + } } diff --git a/src/Api/Serializers/UserSerializer.php b/src/Api/Serializers/UserSerializer.php index 0488c1c4b..1ee17da48 100644 --- a/src/Api/Serializers/UserSerializer.php +++ b/src/Api/Serializers/UserSerializer.php @@ -56,9 +56,4 @@ class UserSerializer extends UserBasicSerializer return $this->extendAttributes($user, $attributes); } - - protected function groups() - { - return $this->hasMany('Flarum\Api\Serializers\GroupSerializer'); - } } diff --git a/src/Core/Seeders/GroupsTableSeeder.php b/src/Core/Seeders/GroupsTableSeeder.php index 978ed6835..14b7823f3 100644 --- a/src/Core/Seeders/GroupsTableSeeder.php +++ b/src/Core/Seeders/GroupsTableSeeder.php @@ -15,9 +15,19 @@ class GroupsTableSeeder extends Seeder { Group::unguard(); Group::truncate(); - $groups = ['Administrator', 'Guest', 'Member', 'Moderator', 'Staff']; + $groups = [ + ['Admin', 'Admins', '#B72A2A', 'wrench'], + ['Guest', 'Guests', null, null], + ['Member', 'Members', null, null], + ['Mod', 'Mods', '#80349E', 'bolt'] + ]; foreach ($groups as $group) { - Group::create(['name' => $group]); + Group::create([ + 'name_singular' => $group[0], + 'name_plural' => $group[1], + 'color' => $group[2], + 'icon' => $group[3] + ]); } }