module BotMesh::GroupResolver
Public Instance Methods
Source
# File lib/bot_mesh/group_resolver.rb, line 88 def add_member(group_id:, bot_id:, added_by: nil) bot_id = bot_id.to_s.strip return false if bot_id.empty? RabbotDb.with_connection do |db| db.execute(<<~SQL, [group_id.to_i, bot_id, added_by.to_s, RabbotDb.iso_time]) INSERT INTO bot_group_members (group_id, bot_id, added_by, created_at) VALUES (?, ?, ?, ?) ON CONFLICT (group_id, bot_id) DO NOTHING SQL db.changes.positive? end end
Source
# File lib/bot_mesh/group_resolver.rb, line 72 def create_group(name:, network:, channel:, owner_user:, implicit: false) network = RabbotDb.normalize_network(network) channel = RabbotDb.normalize_channel(channel) name = name.to_s.strip now = RabbotDb.iso_time RabbotDb.with_connection do |db| id = db.get_first_value(<<~SQL, [name, network, channel, owner_user.to_s, implicit ? 1 : 0, now]) INSERT INTO bot_groups (name, network, channel, owner_user, implicit, created_at) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (name, network, channel) DO UPDATE SET owner_user = excluded.owner_user RETURNING id SQL find_group(name: name, network: network, channel: channel, db: db) || { id: id.to_i, name: name } end end
Source
# File lib/bot_mesh/group_resolver.rb, line 128 def custom_group_for(bot_id:, network:, channel:) network = RabbotDb.normalize_network(network) channel = RabbotDb.normalize_channel(channel) RabbotDb.with_connection do |db| row = db.get_first_row(<<~SQL, [bot_id.to_s, network, channel]) SELECT g.id, g.name, g.network, g.channel, g.owner_user, g.implicit, g.created_at FROM bot_groups g JOIN bot_group_members m ON m.group_id = g.id WHERE m.bot_id = ? AND g.network = ? AND g.channel = ? AND g.implicit = 0 ORDER BY g.id ASC LIMIT 1 SQL group_row(row) end end
Source
# File lib/bot_mesh/group_resolver.rb, line 19 def ensure_implicit_group!(bot_id:, network:, channel:) bot = RabbotDb.find_bot(bot_id: bot_id) return nil unless bot config_user = bot[:config_user].to_s name = implicit_group_name(config_user) group = find_group(name: name, network: network, channel: channel) || create_group(name: name, network: network, channel: channel, owner_user: config_user, implicit: true) ensure_owner_members!(group[:id], config_user) add_member(group_id: group[:id], bot_id: bot_id, added_by: "system") unless member?(group[:id], bot_id) group end
Source
# File lib/bot_mesh/group_resolver.rb, line 144 def ensure_owner_members!(group_id, config_user) RabbotDb.list_bots.each do |bot| next unless bot[:config_user].to_s == config_user.to_s add_member(group_id: group_id, bot_id: bot[:bot_id], added_by: "system") end end
Source
# File lib/bot_mesh/group_resolver.rb, line 112 def find_group(name:, network:, channel:, db: nil) network = RabbotDb.normalize_network(network) channel = RabbotDb.normalize_channel(channel) if db row = db.get_first_row( "SELECT id, name, network, channel, owner_user, implicit, created_at " \ "FROM bot_groups WHERE name = ? AND network = ? AND channel = ?", [name.to_s, network, channel] ) return group_row(row) if row return nil end RabbotDb.with_connection { |conn| find_group(name: name, network: network, channel: channel, db: conn) } end
Source
# File lib/bot_mesh/group_resolver.rb, line 152 def group_row(row) return nil unless row { id: row["id"].to_i, name: row["name"], network: row["network"], channel: row["channel"], owner_user: row["owner_user"].to_s, implicit: row["implicit"].to_i == 1, created_at: row["created_at"] } end
Source
# File lib/bot_mesh/group_resolver.rb, line 40 def groups_for(network:, channel:) network = RabbotDb.normalize_network(network) channel = RabbotDb.normalize_channel(channel) RabbotDb.with_connection do |db| db.execute(<<~SQL, [network, channel]).map { |row| group_row(row) } SELECT id, name, network, channel, owner_user, implicit, created_at FROM bot_groups WHERE network = ? AND channel = ? ORDER BY implicit DESC, lower(name) ASC SQL end end
Source
# File lib/bot_mesh/group_resolver.rb, line 15 def implicit_group_name(config_user) "_owner:#{config_user.to_s.strip}" end
Source
# File lib/bot_mesh/group_resolver.rb, line 63 def member?(group_id, bot_id) RabbotDb.with_connection do |db| !db.get_first_row( "SELECT 1 AS ok FROM bot_group_members WHERE group_id = ? AND bot_id = ?", [group_id.to_i, bot_id.to_s] ).nil? end end
Source
# File lib/bot_mesh/group_resolver.rb, line 53 def members(group_id:) RabbotDb.with_connection do |db| db.execute(<<~SQL, [group_id.to_i]).map { |row| row["bot_id"] } SELECT bot_id FROM bot_group_members WHERE group_id = ? ORDER BY lower(bot_id) ASC SQL end end
Source
# File lib/bot_mesh/group_resolver.rb, line 102 def remove_member(group_id:, bot_id:) RabbotDb.with_connection do |db| db.execute( "DELETE FROM bot_group_members WHERE group_id = ? AND bot_id = ?", [group_id.to_i, bot_id.to_s] ) db.changes.positive? end end
Source
# File lib/bot_mesh/group_resolver.rb, line 32 def resolve_group_id(bot_id:, network:, channel:) custom = custom_group_for(bot_id: bot_id, network: network, channel: channel) return custom[:id] if custom group = ensure_implicit_group!(bot_id: bot_id, network: network, channel: channel) group&.fetch(:id) end