FIX: ensures post event works when no user is logged in

FIX: ensures post event works when no user is logged in

diff --git a/app/serializers/discourse_post_event/event_serializer.rb b/app/serializers/discourse_post_event/event_serializer.rb
index 01e7709..235b54e 100644
--- a/app/serializers/discourse_post_event/event_serializer.rb
+++ b/app/serializers/discourse_post_event/event_serializer.rb
@@ -45,7 +45,7 @@ module DiscoursePostEvent
     end
 
     def can_update_attendance
-      object.can_user_update_attendance(scope.current_user)
+      scope.current_user && object.can_user_update_attendance(scope.current_user)
     end
 
     def creator
@@ -70,10 +70,12 @@ module DiscoursePostEvent
     end
 
     def watching_invitee
-      watching_invitee = Invitee.find_by(
-        user_id: scope.current_user.id,
-        post_id: object.id
-      )
+      if scope.current_user
+        watching_invitee = Invitee.find_by(
+          user_id: scope.current_user.id,
+          post_id: object.id
+        )
+      end
 
       if watching_invitee
         InviteeSerializer.new(watching_invitee, root: false)
diff --git a/assets/javascripts/discourse/controllers/discourse-post-event-upcoming-events-index.js.es6 b/assets/javascripts/discourse/controllers/discourse-post-event-upcoming-events-index.js.es6
index 7d0611c..2798327 100644
--- a/assets/javascripts/discourse/controllers/discourse-post-event-upcoming-events-index.js.es6
+++ b/assets/javascripts/discourse/controllers/discourse-post-event-upcoming-events-index.js.es6
@@ -1,9 +1,16 @@
 import Controller from "@ember/controller";
+import { inject } from "@ember/controller";
 
 export default Controller.extend({
+  exceptionController: inject("exception"),
+
   loadEvents(params) {
     this.store
       .findAll("discourse-post-event-event", params)
-      .then(events => this.set("events", events));
+      .then(events => this.set("events", events))
+      .catch(e => {
+        this.exceptionController.set("thrown", e.jqXHR);
+        this.replaceRoute("exception");
+      });
   }
 });
diff --git a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6 b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
index 4f60781..177331f 100644
--- a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
+++ b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
@@ -113,7 +113,10 @@ export default createWidget("discourse-post-event", {
       ),
       statusClass: `status ${eventModel.status}`,
       isPublicEvent: eventModel.status === "public",
-      isStandaloneEvent: eventModel.status === "standalone"
+      isStandaloneEvent: eventModel.status === "standalone",
+      canActOnEvent:
+        this.currentUser &&
+        this.state.eventModel.can_act_on_discourse_post_event
     };
   },
 
@@ -151,7 +154,7 @@ export default createWidget("discourse-post-event", {
         {{attach
           widget="more-dropdown"
           attrs=(hash
-            canActOnEvent=state.eventModel.can_act_on_discourse_post_event
+            canActOnEvent=this.transformed.canActOnEvent
             postEventId=state.eventModel.id
             isExpired=state.eventModel.is_expired
             creatorUsername=state.eventModel.creator.username
diff --git a/assets/javascripts/discourse/widgets/more-dropdown.js.es6 b/assets/javascripts/discourse/widgets/more-dropdown.js.es6
index 07f51cc..9553d7c 100644
--- a/assets/javascripts/discourse/widgets/more-dropdown.js.es6
+++ b/assets/javascripts/discourse/widgets/more-dropdown.js.es6
@@ -39,14 +39,16 @@ export default createWidget("more-dropdown", {
       });
     }
 
-    content.push({
-      id: "sendPMToCreator",
-      icon: "envelope",
-      translatedLabel: I18n.t(
-        "discourse_post_event.event_ui.send_pm_to_creator",
-        { username: attrs.creatorUsername }
-      )
-    });
+    if (this.currentUser) {
+      content.push({
+        id: "sendPMToCreator",
+        icon: "envelope",
+        translatedLabel: I18n.t(
+          "discourse_post_event.event_ui.send_pm_to_creator",
+          { username: attrs.creatorUsername }
+        )
+      });
+    }
 
     if (!attrs.isExpired && attrs.canActOnEvent && attrs.isPublicEvent) {
       content.push({
diff --git a/spec/requests/post_events_controller_spec.rb b/spec/requests/post_events_controller_spec.rb
index 936be81..bc04136 100644
--- a/spec/requests/post_events_controller_spec.rb
+++ b/spec/requests/post_events_controller_spec.rb
@@ -137,7 +137,7 @@ module DiscoursePostEvent
         expect(sample_invitees.length).to eq(3)
       end
 
-      context 'when a event exists' do
+      context 'when an event exists' do
         let(:event) { Fabricate(:event, post: post1) }
 
         context 'when we update the event' do
@@ -325,6 +325,32 @@ module DiscoursePostEvent
             expect(response.status).to eq(403)
           end
         end
+
+        context 'when watching user is not logged' do
+          before do
+            sign_out
+          end
+
+          context 'when topic is public' do
+            it 'can see the event' do
+              get "/discourse-post-event/events/#{event.id}.json"
+
+              expect(response.status).to eq(200)
+            end
+          end
+
+          context 'when topic is not public' do
+            before do
+              event.post.topic.convert_to_private_message(Discourse.system_user)
+            end
+
+            it 'can’t see the event' do
+              get "/discourse-post-event/events/#{event.id}.json"
+
+              expect(response.status).to eq(404)
+            end
+          end
+        end
       end
     end
   end

GitHub sha: 8ccc7f3d