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.
This commit is contained in:
Dan Ungureanu 2021-08-26 10:43:56 +03:00 committed by GitHub
parent e43a8af3bd
commit 3406a49e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -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)

View File

@ -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