Class: Waylon::Skills::Groups

Inherits:
Waylon::Skill show all
Defined in:
lib/waylon/skills/groups.rb

Overview

Built-in skills for managing groups

Instance Attribute Summary

Attributes inherited from Waylon::Skill

#request, #route, #sense, #tokens

Instance Method Summary collapse

Methods inherited from Waylon::Skill

#acknowledgement, #codify, config_namespace, #details, #initialize, #mention, #message, #named_tokens, perform, queue, #react, #reply, #reply_with_blocks, route, #threaded_reply

Methods included from BaseComponent

included

Constructor Details

This class inherits a constructor from Waylon::Skill

Instance Method Details

#add_to_groupObject

rubocop:disable Metrics/AbcSize



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/waylon/skills/groups.rb', line 56

def add_to_group # rubocop:disable Metrics/AbcSize
  user_list = tokens.first
  group_name = tokens.last

  if group_name == "global admins"
    reply "Sorry, I can't manipulate global admins this way..."
    return
  end

  group = Group.new(group_name)

  log "Adding #{user_list} to group #{group}"

  failures = []
  user_list.split(",").each do |this_user|
    found = found_user(this_user)
    failures << found unless group.add(found)
  end

  unless failures.empty?
    text = failures.size > 1 ? "were already members" : "was already a member"
    reply "Looks like [#{failures.map { |u| mention(u) }.join(", ")}] #{text} of #{group_name}"
  end

  reply("Done adding users to #{group_name}!")
end

#cleanup_groupsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/waylon/skills/groups.rb', line 83

def cleanup_groups
  # perform a key scan in Redis for all group keys and find empty groups
  group_keys = all_group_keys.select do |group|
    name = group.split(".").last
    Group.new(name).to_a.empty?
  end

  # delete the empty group keys
  group_keys.each { |g| db.delete(g) }

  group_names = group_keys.map { |g| g.split(".").last }

  reply "I removed these empty groups: #{group_names.join(", ")}"
end

#list_all_groupsObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/waylon/skills/groups.rb', line 124

def list_all_groups
  groups = {}
  groups["global admins"] = global_admins unless global_admins.empty?
  all_group_keys.each do |group|
    name = group.split(".").last
    groups[name] = Group.new(name).members
  end

  reply(codify(groups.to_yaml))
end

#list_my_groupsObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/waylon/skills/groups.rb', line 135

def list_my_groups
  groups = []
  groups << "global admins" if global_admins.include?(message.author.email)
  all_group_keys.each do |group|
    name = group.split(".").last
    groups << name if Group.new(name).include?(message.author)
  end

  reply(codify(groups.to_yaml))
end

#remove_from_groupObject

rubocop:disable Metrics/AbcSize



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/waylon/skills/groups.rb', line 98

def remove_from_group # rubocop:disable Metrics/AbcSize
  user_list = tokens.first
  group_name = tokens.last

  if group_name == "global admins"
    reply "Sorry, I can't manipulate global admins this way..."
    return
  end

  group = Group.new(group_name)

  log "Removing #{user_list} from group '#{group_name}'", :debug

  failures = []
  user_list.split(",").each do |this_user|
    found = found_user(this_user)
    failures << found unless group.remove(found)
  end

  unless failures.empty?
    text = failures.size > 1 ? "were members" : "was a member"
    reply("I don't think [#{failures.map { |u| mention(u) }.join(", ")}] #{text} of #{group_name}")
  end
  reply("Done removing users from groups!")
end