From 001e03ea1ba20ec995ced35167264ac6a36387cb Mon Sep 17 00:00:00 2001 From: kimardenmiller Date: Fri, 8 Feb 2019 15:50:44 -0800 Subject: [PATCH 1/2] - extended notifications module - created example script - Prepared for pull request --- ...s_groups_notification_update_to_default.rb | 65 +++++++++++++++++++ lib/discourse_api/api/notifications.rb | 5 ++ 2 files changed, 70 insertions(+) create mode 100644 examples/users_groups_notification_update_to_default.rb diff --git a/examples/users_groups_notification_update_to_default.rb b/examples/users_groups_notification_update_to_default.rb new file mode 100644 index 0000000..579538a --- /dev/null +++ b/examples/users_groups_notification_update_to_default.rb @@ -0,0 +1,65 @@ +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) +require File.expand_path('../../lib/discourse_api', __FILE__) + +client = DiscourseApi::Client.new('http://localhost:3000') +client.api_key = ENV['LOCAL_DISCOURSE_API'] +client.api_username = 'KM_Admin' + +# testing variables +# @target_username = 'some_test_username' +@@target_username = nil # comment this and uncomment the above to test on a single user + + +def member_notifications(client, user) + puts "#{user['id']} #{user['name']}" + @users_username = user['username'] + @users_groups = client.user(@users_username)['groups'] + + @users_group_users = client.user(@users_username)['group_users'] + @users_group_users.each do |users_group| + @user_group_id = users_group['group_id'] + @notification_level = users_group['notification_level'] + @users_groups.each do |group| + if group['id'] == @user_group_id + @group_name = group['name'] + @default_level = group['default_notification_level'] + end + end + if @notification_level != @default_level + puts "Group ID:#{@user_group_id} #{@group_name} Notification Level: #{@notification_level} Default: #{@default_level}" + response = client.set_group_user_note_lev(@group_name, user['id'], @default_level) + puts response + @users_group_users_after_update = client.user(@users_username)['group_users'] + + # uncomment to print each update just made to terminal + # @users_group_users_after_update.each do |users_group_second_pass| + # if users_group_second_pass['group_id'] == @user_group_id + # puts "Updated ID:#{@user_group_id} #{@group_name} Notification Level: #{users_group_second_pass['notification_level']} Default: #{@default_level}" + # end + # end + + end + end +end + + +retrieve_next = 1 +while retrieve_next > 0 + @users = client.list_users('active', page: retrieve_next) + if @users.empty? + retrieve_next = 0 + else + # puts "Page .................. #{retrieve_next}" + @users.each do |user| + if @target_username + if user['username'] == @target_username + member_notifications(client, user) + end + else + member_notifications(client, user) + sleep(10) # needed to avoid the DiscourseApi::TooManyRequests limit + end + end + retrieve_next += 1 + end +end diff --git a/lib/discourse_api/api/notifications.rb b/lib/discourse_api/api/notifications.rb index 74d1633..4ed366d 100644 --- a/lib/discourse_api/api/notifications.rb +++ b/lib/discourse_api/api/notifications.rb @@ -5,6 +5,11 @@ def notifications response = get('/notifications.json') response[:body] end + + def set_group_user_note_lev(group, user_id, notification_level) + response = post("/groups/#{group}/notifications?user_id=#{user_id}¬ification_level=#{notification_level}") + response + end end end end \ No newline at end of file From b61dff7f966a9774ac7340cefdf477860630c27c Mon Sep 17 00:00:00 2001 From: kimardenmiller Date: Sat, 9 Feb 2019 15:36:07 -0800 Subject: [PATCH 2/2] - Reset Client credentials to same look as other examples --- examples/users_groups_notification_update_to_default.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/users_groups_notification_update_to_default.rb b/examples/users_groups_notification_update_to_default.rb index 579538a..50e0de1 100644 --- a/examples/users_groups_notification_update_to_default.rb +++ b/examples/users_groups_notification_update_to_default.rb @@ -1,9 +1,9 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require File.expand_path('../../lib/discourse_api', __FILE__) -client = DiscourseApi::Client.new('http://localhost:3000') -client.api_key = ENV['LOCAL_DISCOURSE_API'] -client.api_username = 'KM_Admin' +client = DiscourseApi::Client.new("http://localhost:3000") +client.api_key = "YOUR_API_KEY" +client.api_username = "YOUR_USERNAME" # testing variables # @target_username = 'some_test_username' @@ -43,6 +43,7 @@ def member_notifications(client, user) end +# API allows only 100 users per call(page), so this loops to get them all retrieve_next = 1 while retrieve_next > 0 @users = client.list_users('active', page: retrieve_next)