From 42c82d544ecfe138da5b0b5a336be2109179cf26 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Mon, 27 May 2019 17:28:41 +0300 Subject: [PATCH] FEATURE: Send notification when member was accepted to group. (#7503) --- .../discourse-common/lib/icon-library.js.es6 | 3 ++- .../concerns/notification-types.js.es6.erb | 3 +++ .../widgets/notification-item.js.es6 | 25 +++++++++++++------ app/controllers/groups_controller.rb | 2 +- app/models/group.rb | 13 +++++++++- app/models/notification.rb | 3 ++- config/locales/client.en.yml | 1 + .../fixtures/notification_fixtures.js.es6 | 2 +- .../javascripts/fixtures/site-fixtures.js.es6 | 2 +- 9 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 app/assets/javascripts/discourse/components/concerns/notification-types.js.es6.erb diff --git a/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 b/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 index a8751806a47..e945fc25b2c 100644 --- a/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 +++ b/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 @@ -35,7 +35,8 @@ const REPLACEMENTS = { "notification.topic_reminder": "far-clock", "notification.watching_first_post": "far-dot-circle", "notification.group_message_summary": "group", - "notification.post_approved": "check" + "notification.post_approved": "check", + "notification.group_invite": "user-plus" }; // TODO: use lib/svg_sprite/fa4-renames.json here diff --git a/app/assets/javascripts/discourse/components/concerns/notification-types.js.es6.erb b/app/assets/javascripts/discourse/components/concerns/notification-types.js.es6.erb new file mode 100644 index 00000000000..476ea707de1 --- /dev/null +++ b/app/assets/javascripts/discourse/components/concerns/notification-types.js.es6.erb @@ -0,0 +1,3 @@ +<% Notification.types.each do |key, value| %> +export const <%= key.upcase %>_TYPE = <%= value %>; +<% end %> diff --git a/app/assets/javascripts/discourse/widgets/notification-item.js.es6 b/app/assets/javascripts/discourse/widgets/notification-item.js.es6 index a298e1ab938..e72765946ff 100644 --- a/app/assets/javascripts/discourse/widgets/notification-item.js.es6 +++ b/app/assets/javascripts/discourse/widgets/notification-item.js.es6 @@ -10,13 +10,15 @@ import { formatUsername } from "discourse/lib/utilities"; import { setTransientHeader } from "discourse/lib/ajax"; -import { userPath } from "discourse/lib/url"; +import { groupPath, userPath } from "discourse/lib/url"; import { iconNode } from "discourse-common/lib/icon-library"; - -const LIKED_TYPE = 5; -const INVITED_TYPE = 8; -const GROUP_SUMMARY_TYPE = 16; -export const LIKED_CONSOLIDATED_TYPE = 19; +import { + LIKED_TYPE, + INVITED_TYPE, + GROUP_SUMMARY_TYPE, + LIKED_CONSOLIDATED_TYPE, + GROUP_INVITE_TYPE +} from "discourse/components/concerns/notification-types"; createWidget("notification-item", { tagName: "li", @@ -72,6 +74,10 @@ createWidget("notification-item", { ); } + if (attrs.notification_type === GROUP_INVITE_TYPE) { + return groupPath(data.group_name); + } + if (data.group_id) { return userPath(data.username + "/messages/group/" + data.group_name); } @@ -114,8 +120,11 @@ createWidget("notification-item", { if (notificationType === GROUP_SUMMARY_TYPE) { const count = data.inbox_count; - const group_name = data.group_name; - return I18n.t(scope, { count, group_name }); + const groupName = data.group_name; + return I18n.t(scope, { count, group_name: groupName }); + } else if (notificationType === GROUP_INVITE_TYPE) { + const groupName = data.group_name; + return I18n.t(scope, { group_name: groupName }); } const username = formatUsername(data.display_username); diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 7a632eb8f6a..5cdfb6d578e 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -341,7 +341,7 @@ class GroupsController < ApplicationController raise Discourse::InvalidParameters.new(:user_id) if user.blank? if params[:accept] - group.add(user) + group.add(user, notify: true) GroupActionLogger.new(current_user, group).log_add_user_to_group(user) end diff --git a/app/models/group.rb b/app/models/group.rb index bb0adf78e43..04327c93e2a 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -506,9 +506,20 @@ class Group < ActiveRecord::Base PUBLISH_CATEGORIES_LIMIT = 10 - def add(user) + def add(user, notify: false) self.users.push(user) unless self.users.include?(user) + if notify + Notification.create!( + notification_type: Notification.types[:group_invite], + user_id: user.id, + data: { + group_id: id, + group_name: name + }.to_json + ) + end + if self.categories.count < PUBLISH_CATEGORIES_LIMIT MessageBus.publish('/categories', { categories: ActiveModel::ArraySerializer.new(self.categories).as_json diff --git a/app/models/notification.rb b/app/models/notification.rb index c54d4495f8c..c0be484b0d8 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -63,7 +63,8 @@ class Notification < ActiveRecord::Base watching_first_post: 17, topic_reminder: 18, liked_consolidated: 19, - post_approved: 20 + post_approved: 20, + group_invite: 21 ) end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 1d16b9bb17f..dbbf6dda3e7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1689,6 +1689,7 @@ en: granted_badge: "Earned '{{description}}'" topic_reminder: "{{username}} {{description}}" watching_first_post: "New Topic {{description}}" + group_invite: "Invited to '{{group_name}}'" group_message_summary: one: "{{count}} message in your {{group_name}} inbox" diff --git a/test/javascripts/fixtures/notification_fixtures.js.es6 b/test/javascripts/fixtures/notification_fixtures.js.es6 index 0ebf8bd6784..5e3c1bd1c4b 100644 --- a/test/javascripts/fixtures/notification_fixtures.js.es6 +++ b/test/javascripts/fixtures/notification_fixtures.js.es6 @@ -1,5 +1,5 @@ /*jshint maxlen:10000000 */ -import { LIKED_CONSOLIDATED_TYPE } from "discourse/widgets/notification-item"; +import { LIKED_CONSOLIDATED_TYPE } from "discourse/components/concerns/notification-types"; export default { "/notifications": { diff --git a/test/javascripts/fixtures/site-fixtures.js.es6 b/test/javascripts/fixtures/site-fixtures.js.es6 index 25d1dc32c01..a567dcd0acb 100644 --- a/test/javascripts/fixtures/site-fixtures.js.es6 +++ b/test/javascripts/fixtures/site-fixtures.js.es6 @@ -1,4 +1,4 @@ -import { LIKED_CONSOLIDATED_TYPE } from "discourse/widgets/notification-item"; +import { LIKED_CONSOLIDATED_TYPE } from "discourse/components/concerns/notification-types"; export default { "site.json": {