Skip to content

Commit 1a5f628

Browse files
committed
Subscriptions now have a token, which is used during the unsubscription process
1 parent 7acc830 commit 1a5f628

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

app/controllers/forem/topics_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def subscribe
5151

5252
def unsubscribe
5353
if find_topic
54+
subscription = @topic.subscriptions.find_by(subscriber_id: forem_user.id, token: params[:token])
55+
unsubscribe_unsuccessful and return unless subscription
56+
5457
@topic.unsubscribe_user(forem_user.id)
5558
unsubscribe_successful
5659
end
@@ -93,6 +96,11 @@ def unsubscribe_successful
9396
redirect_to forum_topic_url(@topic.forum, @topic)
9497
end
9598

99+
def unsubscribe_unsuccessful
100+
flash.alert = t("forem.topic.unsubscription_failed")
101+
redirect_to forum_topic_url(@topic.forum, @topic)
102+
end
103+
96104
private
97105
def find_forum
98106
@forum = Forem::Forum.friendly.find(params[:forum_id])

app/models/forem/subscription.rb

+8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ class Subscription < ActiveRecord::Base
55

66
validates :subscriber_id, :presence => true
77

8+
before_create :set_token
9+
810
def send_notification(post_id)
911
# If a user cannot be found, then no-op
1012
# This will happen if the user record has been deleted.
1113
if subscriber.present?
1214
SubscriptionMailer.topic_reply(post_id, subscriber.id).deliver
1315
end
1416
end
17+
18+
private
19+
20+
def set_token
21+
self.token = SecureRandom.hex(24)
22+
end
1523
end
1624
end

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ en:
122122
not_updated: This topic could not be updated.
123123
subscribed: You have subscribed to this topic.
124124
unsubscribed: You have unsubscribed from this topic.
125+
unsubscription_failed: Failed to unsubscribe you from this topic.
125126
none: There are no topics in this forum currently.
126127
links:
127128
new: New topic

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
resources :posts, :except => :index
4545
member do
4646
post :subscribe
47-
post :unsubscribe
47+
get :unsubscribe
4848
end
4949
end
5050
end

spec/controllers/topics_controller_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,34 @@
6868
flash.alert.should == "You must sign in first."
6969
end
7070
end
71+
72+
context "subscribing & unsubscribing" do
73+
let!(:forum) { create(:forum) }
74+
let!(:user) { create(:user) }
75+
let!(:topic) { create(:approved_topic, forum: forum, forem_user: user) }
76+
let(:subscription) { topic.subscriptions.first }
77+
78+
before do
79+
sign_in(user)
80+
controller.current_user.stub :can_read_topic? => true
81+
end
82+
83+
it "can subscribe to a topic" do
84+
post :subscribe, forum_id: forum.to_param, id: topic.to_param
85+
expect(flash[:notice]).to eq(I18n.t("forem.topic.subscribed"))
86+
expect(response).to redirect_to(forum_topic_path(forum, topic))
87+
end
88+
89+
it "can unsubscribe with a valid token" do
90+
get :unsubscribe, forum_id: forum.to_param, id: topic.to_param, token: subscription.token
91+
expect(flash[:notice]).to eq(I18n.t("forem.topic.unsubscribed"))
92+
expect(response).to redirect_to(forum_topic_path(forum, topic))
93+
end
94+
95+
it "cannot unsubscribe without a token" do
96+
get :unsubscribe, forum_id: forum.to_param, id: topic.to_param, token: "fake"
97+
expect(flash[:alert]).to eq(I18n.t("forem.topic.unsubscription_failed"))
98+
expect(response).to redirect_to(forum_topic_path(forum, topic))
99+
end
100+
end
71101
end

spec/models/subscription_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
FactoryGirl.build(:subscription).should be_valid
77
end
88

9+
context "set_token" do
10+
it "sets a unique token for the subscription" do
11+
subscription = Forem::Subscription.create!(subscriber_id: 1)
12+
expect(subscription.token).to be_present
13+
end
14+
end
15+
916
describe "topic subscriptions" do
1017
before(:each) do
1118
Forem::Topic.any_instance.stub(:set_first_post_user)

0 commit comments

Comments
 (0)