Skip to content

Commit 74c423c

Browse files
authored
Allow setting topic notifications (#220)
Deprecate #category_set_user_notification and add #category_set_user_notification_level in favor of passing require args as params
1 parent 99446ad commit 74c423c

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

examples/notifications.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3+
require File.expand_path('../../lib/discourse_api', __FILE__)
4+
5+
config = DiscourseApi::ExampleHelper.load_yml
6+
7+
client = DiscourseApi::Client.new(config['host'] || 'http://localhost:3000')
8+
client.api_key = config['api_key'] || "YOUR_API_KEY"
9+
client.api_username = config['api_username'] || "YOUR_USERNAME"
10+
11+
# watch an entire category
12+
client.category_set_user_notification_level(1, notification_level: 3)
13+
14+
# mute a topic
15+
client.topic_set_user_notification_level(1, notification_level: 0)

lib/discourse_api/api/categories.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,19 @@ def category(id)
7575
response[:body]['category']
7676
end
7777

78+
# TODO: Deprecated. Remove after 20210727
7879
def category_set_user_notification(args = {})
7980
category_id = args[:id]
8081
args = API.params(args)
8182
.required(:notification_level)
8283
post("/category/#{category_id}/notifications", args)
8384
end
85+
86+
def category_set_user_notification_level(category_id, params)
87+
params = API.params(params)
88+
.required(:notification_level)
89+
post("/category/#{category_id}/notifications", params)
90+
end
8491
end
8592
end
8693
end

lib/discourse_api/api/topics.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def change_owner(topic_id, params = {})
9090

9191
post("/t/#{topic_id}/change-owner.json", params)
9292
end
93+
94+
def topic_set_user_notification_level(topic_id, params)
95+
params = API.params(params)
96+
.required(:notification_level)
97+
post("/t/#{topic_id}/notifications", params)
98+
end
9399
end
94100
end
95101
end

spec/discourse_api/api/categories_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,27 @@
8585
end
8686
end
8787

88+
describe "#category_set_user_notification" do
89+
before do
90+
stub_post("#{host}/category/1/notifications").to_return(body: fixture("notification_success.json"), headers: { content_type: "application/json" })
91+
end
92+
93+
it "makes the post request" do
94+
response = subject.category_set_user_notification(id: 1, notification_level: 3)
95+
expect(a_post("#{host}/category/1/notifications")).to have_been_made
96+
expect(response['success']).to eq('OK')
97+
end
98+
end
99+
100+
describe "#category_set_user_notification_level" do
101+
before do
102+
stub_post("#{host}/category/1/notifications").to_return(body: fixture("notification_success.json"), headers: { content_type: "application/json" })
103+
end
104+
105+
it "makes the post request" do
106+
response = subject.category_set_user_notification_level(1, notification_level: 3)
107+
expect(a_post("#{host}/category/1/notifications").with(body: "notification_level=3")).to have_been_made
108+
expect(response['success']).to eq('OK')
109+
end
110+
end
88111
end

spec/discourse_api/api/topics_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,15 @@
189189
end
190190
end
191191

192+
describe "#topic_set_user_notification_level" do
193+
before do
194+
stub_post("#{host}/t/1/notifications").to_return(body: fixture("notification_success.json"), headers: { content_type: "application/json" })
195+
end
196+
197+
it "makes the post request" do
198+
response = subject.topic_set_user_notification_level(1, notification_level: 3)
199+
expect(a_post("#{host}/t/1/notifications").with(body: "notification_level=3")).to have_been_made
200+
expect(response['success']).to eq('OK')
201+
end
202+
end
192203
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"success": "OK"
3+
}

0 commit comments

Comments
 (0)