Skip to content

Commit fbc3eea

Browse files
committed
SubredditsUser gets reset on login
no need to run it on the backend as well
1 parent 09a1934 commit fbc3eea

1 file changed

Lines changed: 14 additions & 63 deletions

File tree

scripts/sync_moderators.rb

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative 'default_bot'
44

55
class SyncModerators < DefaultBot
6+
EXCLUDE_BOTS = %w[automoderator floodassistant comment-nuke trendingtattler modmailtodiscord].freeze
7+
68
def initialize(subreddits: [])
79
super(purpose: 'Sync Moderators')
810

@@ -18,76 +20,25 @@ def run = subreddits.each_value { process_subreddit(it) }
1820
def skip_subreddit?(name) = @subreddit_names.any? && !@subreddit_names.include?(name.downcase)
1921

2022
def process_subreddit(subreddit)
21-
return if skip_subreddit?(subreddit.name)
22-
23-
@subreddit = subreddit
24-
@mod_names = moderator_names
25-
26-
reset_moderator_names
27-
28-
remove_unmodded_users
29-
30-
add_modded_users
23+
reset_moderator_names(subreddit)
3124

3225
sleep 3
3326
rescue Redd::Errors::Forbidden
34-
# BaseballBot can't access /r/troxellophilus
27+
# BaseballBot can't access this subreddit, likely because it's private.
3528
nil
3629
end
3730

38-
def moderator_names
39-
# This can be filtered on :mod_permissions as well if necessary
40-
@subreddit.subreddit.moderators.map { it[:name].downcase }
41-
end
42-
43-
def reset_moderator_names
44-
Baseballbot::Models::Subreddit
45-
.where(id: @subreddit.id)
46-
.update(moderators: Sequel.pg_array(@mod_names))
47-
end
48-
49-
def remove_unmodded_users
50-
remove_ids = user_ids_to_remove
51-
52-
return if remove_ids.none?
53-
54-
Baseballbot::Models::SubredditsUser
55-
.where(subreddit_id: @subreddit.id)
56-
.where(user_id: remove_ids)
57-
.delete
58-
end
59-
60-
def user_ids_to_remove
61-
existing_relations[@subreddit.id]
62-
.reject { @mod_names.include?(it[:username]) }
63-
.map { it[:user_id].to_i }
64-
end
65-
66-
def add_modded_users
67-
mod_user_ids = Baseballbot::Models::User
68-
.left_join(:subreddits_users, subreddit_id: @subreddit.id, user_id: :id)
69-
.where(subreddits_users__user_id: nil)
70-
.where(username: @mod_names)
71-
.select(:id)
72-
.all
73-
.map { it[:id] }
74-
75-
mod_user_ids.each { add_modded_user(it) }
76-
end
77-
78-
def add_modded_user(user_id)
79-
Baseballbot::Models::SubredditsUser.insert(subreddit_id: @subreddit.id, user_id: user_id.to_i)
80-
end
31+
def reset_moderator_names(subreddit)
32+
return if skip_subreddit?(subreddit.name)
8133

82-
def existing_relations
83-
@existing_relations ||= begin
84-
rows = Baseballbot::Models::SubredditsUser
85-
.join(:subreddits, id: :subreddit_id)
86-
.join(:users, id: Sequel[:subreddits_users][:user_id])
87-
.select(:subreddit_id, :user_id, Sequel.function(:lower, :username).as(:username))
88-
.all
34+
# If you have posts and/or config permissions, you're likely allowed to administrate this subreddit's game threads.
35+
moderator_names = subreddit.subreddit.moderators
36+
.filter { it[:mod_permissions].intersect?(%w[all posts config]) }
37+
.map { it[:name].downcase }
38+
.reject { EXCLUDE_BOTS.include?(it) }
8939

90-
rows.group_by { it[:subreddit_id].to_i }.tap { it.default = [] }
91-
end
40+
Baseballbot::Models::Subreddit
41+
.where(id: subreddit.id)
42+
.update(moderators: Sequel.pg_array(moderator_names))
9243
end
9344
end

0 commit comments

Comments
 (0)