Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Create notification for redeemed invite (#14146)

Dan Ungureanu3406a49e219c
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 270aed283f0..85c17e4b4de 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -37,6 +37,8 @@ class InvitesController < ApplicationController
end
end
+ create_topic_invite_notifications(invite, current_user)
+
if topic = invite.topics.first
new_guardian = Guardian.new(current_user)
return redirect_to(topic.url) if new_guardian.can_see?(topic)
@@ -281,6 +283,7 @@ class InvitesController < ApplicationController
log_on_user(user) if user.active?
user.update_timezone_if_missing(params[:timezone])
post_process_invite(user)
+ create_topic_invite_notifications(invite, user)
topic = invite.topics.first
response = {}
@@ -418,6 +421,26 @@ class InvitesController < ApplicationController
end
end
+ def create_topic_invite_notifications(invite, user)
+ invite.topics.each do |topic|
+ if user.guardian.can_see?(topic)
+ last_notification = user.notifications
+ .where(notification_type: Notification.types[:invited_to_topic])
+ .where(topic_id: topic.id)
+ .where(post_number: 1)
+ .where('created_at > ?', 1.hour.ago)
+
+ if !last_notification.exists?
+ topic.create_invite_notification!(
+ user,
+ Notification.types[:invited_to_topic],
+ invite.invited_by.username
+ )
+ end
+ end
+ end
+ end
+
def send_activation_email(user)
email_token = user.email_tokens.create!(email: user.email)
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index 4c8efebe093..852893954de 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -78,6 +78,7 @@ describe InvitesController do
get "/invites/#{invite.invite_key}"
expect(response).to redirect_to(topic.url)
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
end
it 'adds logged in user to group and redirects them to invite topic' do
@@ -95,6 +96,7 @@ describe InvitesController do
get "/invites/#{invite.invite_key}"
expect(user.reload.groups).to include(group)
expect(response).to redirect_to(topic.url)
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
end
it 'fails for logged in users' do
@@ -486,6 +488,7 @@ describe InvitesController do
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
expect(response.status).to eq(200)
expect(response.parsed_body['redirect_to']).to eq(topic.relative_url)
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
end
it 'sets the timezone of the user in user_options' do
@@ -741,6 +744,7 @@ describe InvitesController do
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
expect(response.parsed_body['redirect_to']).to eq(topic.relative_url)
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
end
it 'sets destination_url cookie if user is not activated' do
@@ -749,13 +753,16 @@ describe InvitesController do
put "/invites/show/#{invite.invite_key}.json"
expect(cookies['destination_url']).to eq(topic.relative_url)
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
end
it 'does not redirect user if they cannot see topic' do
- TopicInvite.create!(invite: invite, topic: Fabricate(:topic, category: secured_category))
+ topic = Fabricate(:topic, category: secured_category)
+ TopicInvite.create!(invite: invite, topic: topic)
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
expect(response.parsed_body['redirect_to']).to eq("/")
+ expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(0)
end
end