UX: only groups can be invited (#29)

UX: only groups can be invited (#29)

diff --git a/app/models/discourse_post_event/invitee.rb b/app/models/discourse_post_event/invitee.rb
index dcaf89e..8198679 100644
--- a/app/models/discourse_post_event/invitee.rb
+++ b/app/models/discourse_post_event/invitee.rb
@@ -19,12 +19,12 @@ module DiscoursePostEvent
       self.update!(params)
     end
 
-    def self.extract_uniq_usernames(user_and_groups_list)
+    def self.extract_uniq_usernames(groups)
       User.where(
         id: GroupUser.where(
-          group_id: Group.where(name: user_and_groups_list).select(:id)
+          group_id: Group.where(name: groups).select(:id)
         ).select(:user_id)
-      ).or(User.where(username: user_and_groups_list))
+      )
     end
   end
 end
diff --git a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6 b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
index f63c93f..0cc6c17 100644
--- a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
+++ b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
@@ -1,3 +1,4 @@
+import Group from "discourse/models/group";
 import ModalFunctionality from "discourse/mixins/modal-functionality";
 import Controller from "@ember/controller";
 import { action, computed } from "@ember/object";
@@ -13,6 +14,10 @@ export default Controller.extend(ModalFunctionality, {
     }
   }),
 
+  groupFinder(term) {
+    return Group.findAll({ term, ignore_automatic: true });
+  },
+
   allowsInvitees: equal("model.eventModel.status", "private"),
 
   @action
diff --git a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
index a080115..76eb189 100644
--- a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
+++ b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
@@ -59,19 +59,12 @@
     {{/event-field}}
 
     {{#event-field enabled=allowsInvitees label="discourse_post_event.builder_modal.invitees.label"}}
-      {{user-selector
-        topicId=model.topicId
-        single=false
-        onChangeCallback=(action "setRawInvitees")
+      {{group-selector
         fullWidthWrap=true
-        allowAny=false
-        includeMessageableGroups=true
-        placeholderKey="composer.users_placeholder"
-        tabindex="1"
-        usernames=model.eventModel.raw_invitees
-        hasGroups=true
-        autocomplete="discourse"
-        excludeCurrentUser=true
+        groupFinder=groupFinder
+        groupNames=model.eventModel.raw_invitees
+        onChangeCallback=(action "setRawInvitees")
+        placeholderKey="topic.invite_private.group_name"
       }}
     {{/event-field}}
   </form>
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 75130a0..4f97123 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -61,7 +61,7 @@ en:
           label: Event name
           placeholder: Optional, defaults to topic title
         invitees:
-          label: Invited users/groups
+          label: Invited groups
         status:
           label: Status
       invite_user_or_group:
diff --git a/spec/requests/post_events_controller_spec.rb b/spec/requests/post_events_controller_spec.rb
index 43d8a8d..c00e15f 100644
--- a/spec/requests/post_events_controller_spec.rb
+++ b/spec/requests/post_events_controller_spec.rb
@@ -29,6 +29,16 @@ module DiscoursePostEvent
         end
       }
 
+      fab!(:large_group) {
+        Fabricate(:group).tap do |g|
+          g.add(invitee2)
+          g.add(invitee3)
+          g.add(invitee4)
+          g.add(invitee5)
+          g.save!
+        end
+      }
+
       before do
         sign_in(user)
       end
@@ -46,8 +56,8 @@ module DiscoursePostEvent
         expect(Event).to exist(id: post1.id)
       end
 
-      it 'accepts user and group invitees' do
-        invitees = [invitee1.username, group.name]
+      it 'accepts group as invitees' do
+        invitees = [group.name]
 
         post '/discourse-post-event/events.json', params: {
           event: {
@@ -60,12 +70,12 @@ module DiscoursePostEvent
 
         expect(response.status).to eq(200)
         sample_invitees = response.parsed_body['event']['sample_invitees']
-        expect(sample_invitees.map { |i| i['user']['id'] }).to match_array([user.id, invitee1.id, group.group_users.first.user.id])
+        expect(sample_invitees.map { |i| i['user']['id'] }).to match_array([user.id] + group.group_users.map { |gu| gu.user.id })
         raw_invitees = response.parsed_body['event']['raw_invitees']
         expect(raw_invitees).to match_array(invitees)
       end
 
-      it 'accepts one user invitee' do
+      it 'doesn’t accept one user invitee' do
         post '/discourse-post-event/events.json', params: {
           event: {
             id: post1.id,
@@ -77,8 +87,7 @@ module DiscoursePostEvent
 
         expect(response.status).to eq(200)
         sample_invitees = response.parsed_body['event']['sample_invitees']
-        expect(sample_invitees[0]['user']['username']).to eq(user.username)
-        expect(sample_invitees[1]['user']['username']).to eq(invitee1.username)
+        expect(sample_invitees.map { |i| i['user']['id'] }).to match_array([user.id])
       end
 
       it 'accepts one group invitee' do
@@ -117,20 +126,15 @@ module DiscoursePostEvent
           event: {
             id: post1.id,
             status: Event.statuses[:private],
-            raw_invitees: [
-              invitee1.username,
-              invitee2.username,
-              invitee3.username,
-              invitee4.username,
-              invitee5.username,
-            ],
+            raw_invitees: [large_group.name],
             starts_at: 2.days.from_now,
           }
         }
 
         expect(response.status).to eq(200)
         sample_invitees = response.parsed_body['event']['sample_invitees']
-        expect(sample_invitees.map { |i| i['user']['username'] }).to match_array([user.username, invitee1.username, invitee2.username])
+        expect(large_group.group_users.length).to eq(4)
+        expect(sample_invitees.length).to eq(3)
       end
 
       context 'when a event exists' do
@@ -144,15 +148,15 @@ module DiscoursePostEvent
               put "/discourse-post-event/events/#{event.id}.json", params: {
                 event: {
                   status: Event.statuses[:private].to_s,
-                  raw_invitees: [invitee1.username]
+                  raw_invitees: [group.name]
                 }
               }
 
               event.reload
 
               expect(event.status).to eq(Event.statuses[:private])
-              expect(event.raw_invitees).to eq([invitee1.username])
-              expect(event.invitees.pluck(:user_id)).to match_array([invitee1.id])
+              expect(event.raw_invitees).to eq([group.name])
+              expect(event.invitees.pluck(:user_id)).to match_array(group.group_users.map { |gu| gu.user.id })
             end
           end
 
@@ -178,14 +182,14 @@ module DiscoursePostEvent
             it 'changes the status' do
               event.update!(
                 status: Event.statuses[:private],
-                raw_invitees: [invitee1.username]
+                raw_invitees: [group.name]
               )
               event.fill_invitees!
 
               event.reload
 
-              expect(event.invitees.pluck(:user_id)).to eq([invitee1.id])
-              expect(event.raw_invitees).to eq([invitee1.username])
+              expect(event.invitees.pluck(:user_id)).to match_array(group.group_users.map { |gu| gu.user.id })
+              expect(event.raw_invitees).to eq([group.name])
 
               put "/discourse-post-event/events/#{event.id}.json", params: {
                 event: {
@@ -205,14 +209,14 @@ module DiscoursePostEvent
             it 'changes the status, removes raw_invitees and keeps invitees' do

[... diff too long, it was truncated ...]

GitHub sha: 03dfa049

This commit appears in #29 which was merged by jjaffeux.