mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:49:14 +08:00
FEATURE: Save ignored usernames in user preferences (#7117)
* FEATURE: Save ignored usernames in user preferences
This commit is contained in:
parent
974e756369
commit
0a9a11094d
|
@ -5,6 +5,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
|
|||
export default Ember.Controller.extend(PreferencesTabController, {
|
||||
saveAttrNames: [
|
||||
"muted_usernames",
|
||||
"ignored_usernames",
|
||||
"new_topic_duration_minutes",
|
||||
"auto_track_topics_after_msecs",
|
||||
"notification_level_when_replying",
|
||||
|
|
|
@ -249,6 +249,7 @@ const User = RestModel.extend({
|
|||
"custom_fields",
|
||||
"user_fields",
|
||||
"muted_usernames",
|
||||
"ignored_usernames",
|
||||
"profile_background",
|
||||
"card_background",
|
||||
"muted_tags",
|
||||
|
|
|
@ -1226,6 +1226,7 @@ class UsersController < ApplicationController
|
|||
:title,
|
||||
:date_of_birth,
|
||||
:muted_usernames,
|
||||
:ignored_usernames,
|
||||
:theme_ids,
|
||||
:locale,
|
||||
:bio_raw,
|
||||
|
|
|
@ -128,6 +128,10 @@ class UserUpdater
|
|||
update_muted_users(attributes[:muted_usernames])
|
||||
end
|
||||
|
||||
if attributes.key?(:ignored_usernames)
|
||||
update_ignored_users(attributes[:ignored_usernames])
|
||||
end
|
||||
|
||||
name_changed = user.name_changed?
|
||||
if (saved = (!save_options || user.user_option.save) && user_profile.save && user.save) &&
|
||||
(name_changed && old_user_name.casecmp(attributes.fetch(:name)) != 0)
|
||||
|
@ -157,13 +161,27 @@ class UserUpdater
|
|||
INSERT into muted_users(user_id, muted_user_id, created_at, updated_at)
|
||||
SELECT :user_id, id, :now, :now
|
||||
FROM users
|
||||
WHERE
|
||||
id in (:desired_ids) AND
|
||||
id NOT IN (
|
||||
SELECT muted_user_id
|
||||
FROM muted_users
|
||||
WHERE user_id = :user_id
|
||||
)
|
||||
WHERE id in (:desired_ids)
|
||||
ON CONFLICT DO NOTHING
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
def update_ignored_users(usernames)
|
||||
usernames ||= ""
|
||||
desired_ids = User.where(username: usernames.split(",")).pluck(:id)
|
||||
if desired_ids.empty?
|
||||
IgnoredUser.where(user_id: user.id).destroy_all
|
||||
else
|
||||
IgnoredUser.where('user_id = ? AND ignored_user_id not in (?)', user.id, desired_ids).destroy_all
|
||||
|
||||
# SQL is easier here than figuring out how to do the same in AR
|
||||
DB.exec(<<~SQL, now: Time.now, user_id: user.id, desired_ids: desired_ids)
|
||||
INSERT into ignored_users(user_id, ignored_user_id, created_at, updated_at)
|
||||
SELECT :user_id, id, :now, :now
|
||||
FROM users
|
||||
WHERE id in (:desired_ids)
|
||||
ON CONFLICT DO NOTHING
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,7 +22,27 @@ describe UserUpdater do
|
|||
expect(MutedUser.where(user_id: u2.id).count).to eq 2
|
||||
expect(MutedUser.where(user_id: u1.id).count).to eq 2
|
||||
expect(MutedUser.where(user_id: u3.id).count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update_ignored_users' do
|
||||
it 'updates ignored users' do
|
||||
u1 = Fabricate(:user)
|
||||
u2 = Fabricate(:user)
|
||||
u3 = Fabricate(:user)
|
||||
|
||||
updater = UserUpdater.new(u1, u1)
|
||||
updater.update_ignored_users("#{u2.username},#{u3.username}")
|
||||
|
||||
updater = UserUpdater.new(u2, u2)
|
||||
updater.update_ignored_users("#{u3.username},#{u1.username}")
|
||||
|
||||
updater = UserUpdater.new(u3, u3)
|
||||
updater.update_ignored_users("")
|
||||
|
||||
expect(IgnoredUser.where(user_id: u2.id).count).to eq 2
|
||||
expect(IgnoredUser.where(user_id: u1.id).count).to eq 2
|
||||
expect(IgnoredUser.where(user_id: u3.id).count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user