FIX: mute new categories for all users, and add rake task to mute old categories

This commit is contained in:
Leo McArdle 2020-01-22 18:38:39 +00:00
Родитель 575369c2fc
Коммит 1d15d63e6d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 8262833620A64C3F
5 изменённых файлов: 30 добавлений и 2 удалений

Просмотреть файл

@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Discourse Mozilla GCM API
version: 0.1.3
version: 0.1.4
description: |-
API to create/manage namespaced groups and categories on a Discourse instance.

Просмотреть файл

@ -16,6 +16,13 @@ module MozillaGCM
category.save!
# shamelessly adapted from https://github.com/discourse/discourse/blob/4e4844f4dbcbab50502f9feb973dbd13c6a75246/app/controllers/admin/site_settings_controller.rb#L54-L71
User.select(:id).find_in_batches do |users|
category_users = []
users.each { |user| category_users << { category_id: category.id, user_id: user.id, notification_level: NotificationLevels.all[:muted] } }
CategoryUser.insert_all!(category_users)
end
DistributedMutex.synchronize("mozilla_gcm_default_categories_muted") do
SiteSetting.default_categories_muted = [SiteSetting.default_categories_muted, category.id].reject(&:blank?).join("|")
end

Просмотреть файл

@ -0,0 +1,19 @@
# frozen_string_literal: true
# shamelessly adapted from https://github.com/discourse/discourse/blob/4e4844f4dbcbab50502f9feb973dbd13c6a75246/app/controllers/admin/site_settings_controller.rb#L54-L71
task "mozilla_gcm:mute_all_externally_managed_categories" => :environment do
notification_level = NotificationLevels.all[:muted]
categories = MozillaGCM::Client.all.pluck(:category_id).map {|x| Category.find(x).subcategories.pluck(:id) }.flatten
CategoryUser.where(category_id: categories, notification_level: notification_level).delete_all
categories.each do |category_id|
skip_user_ids = CategoryUser.where(category_id: category_id).pluck(:user_id)
User.where.not(id: skip_user_ids).select(:id).find_in_batches do |users|
category_users = []
users.each { |user| category_users << { category_id: category_id, user_id: user.id, notification_level: notification_level } }
CategoryUser.insert_all!(category_users)
end
end
end

Просмотреть файл

@ -1,6 +1,6 @@
# name: mozilla-gcm
# about: API to create/manage namespaced groups and categories on a Discourse instance
# version: 0.1.3
# version: 0.1.4
# authors: Leo McArdle
# url: https://github.com/mozilla/discourse-mozilla-gcm

Просмотреть файл

@ -41,6 +41,7 @@ describe MozillaGCM::CategoriesController do
include_examples "insert_groups_if_allowed"
it "succeeds" do
normal_user = Fabricate(:user)
c1 = Fabricate(:category).id
c2 = Fabricate(:category).id
SiteSetting.default_categories_muted = "#{c1}|#{c2}"
@ -63,6 +64,7 @@ describe MozillaGCM::CategoriesController do
check_user_subscribed(user1, category)
check_user_subscribed(user2, category)
expect(SiteSetting.default_categories_muted.split("|")).to match_array([c1.to_s, c2.to_s, category.id.to_s])
check_user_subscribed(normal_user, category, :muted)
end
context "with no name" do