From 3406a49e219c56c985c17d6d9a98e781b0badd87 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Thu, 26 Aug 2021 10:43:56 +0300 Subject: [PATCH] FEATURE: Create notification for redeemed invite (#14146) Users can invite people to topic and they will be automatically redirected to the topic when logging in after signing up. This commit ensures a "invited_to_topic" notification is created when the invite is redeemed. The same notification is used for the "Notify" sharing method that is found in share topic modal. --- app/controllers/invites_controller.rb | 23 +++++++++++++++++++++++ spec/requests/invites_controller_spec.rb | 9 ++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) 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