Class: Clacky::ChannelConfig
- Inherits:
-
Object
- Object
- Clacky::ChannelConfig
- Defined in:
- lib/clacky/server/channel/channel_config.rb
Overview
ChannelConfig manages IM platform credentials (Feishu, WeCom, etc.).
Config is stored in ~/.clacky/channels.yml:
channels:
feishu:
enabled: true
app_id: cli_xxx
app_secret: xxx
domain: https://open.feishu.cn
allowed_users:
- ou_xxx
wecom:
enabled: false
bot_id: xxx
secret: xxx
This class is only responsible for platform credentials. working_dir and permission_mode live in AgentConfig.
Constant Summary collapse
- CONFIG_DIR =
File.join(Dir.home, ".clacky")
- CONFIG_FILE =
File.join(CONFIG_DIR, "channels.yml")
Class Method Summary collapse
-
.load(config_file = CONFIG_FILE) ⇒ ChannelConfig
Load from disk.
Instance Method Summary collapse
-
#any_enabled? ⇒ Boolean
Returns true if at least one channel is enabled.
-
#deep_copy ⇒ ChannelConfig
Deep copy — prevents callers from mutating shared config state.
-
#disable_platform(platform) ⇒ Object
Disable a platform (keeps credentials, just sets enabled: false).
-
#enabled?(platform) ⇒ Boolean
Returns true if the given platform is configured and enabled.
-
#enabled_platforms ⇒ Array<Symbol>
Returns the list of enabled platform symbols.
-
#initialize(channels: {}) ⇒ ChannelConfig
constructor
A new instance of ChannelConfig.
-
#platform_config(platform) ⇒ Hash?
Return the symbol-keyed config hash expected by each adapter’s initializer.
-
#remove_platform(platform) ⇒ Object
Remove a platform entry entirely.
-
#save(config_file = CONFIG_FILE) ⇒ Object
Persist to disk.
-
#set_platform(platform, **fields) ⇒ Object
Set or update a platform’s credentials.
-
#to_yaml ⇒ String
Serialize to YAML string.
Constructor Details
#initialize(channels: {}) ⇒ ChannelConfig
Returns a new instance of ChannelConfig.
32 33 34 |
# File 'lib/clacky/server/channel/channel_config.rb', line 32 def initialize(channels: {}) @channels = channels || {} end |
Class Method Details
.load(config_file = CONFIG_FILE) ⇒ ChannelConfig
Load from disk. Returns an empty instance if the file does not exist.
39 40 41 42 43 44 45 46 47 |
# File 'lib/clacky/server/channel/channel_config.rb', line 39 def self.load(config_file = CONFIG_FILE) if File.exist?(config_file) data = YAMLCompat.safe_load(File.read(config_file), permitted_classes: [Symbol]) || {} else data = {} end new(channels: data["channels"] || {}) end |
Instance Method Details
#any_enabled? ⇒ Boolean
Returns true if at least one channel is enabled.
64 65 66 |
# File 'lib/clacky/server/channel/channel_config.rb', line 64 def any_enabled? @channels.any? { |_, cfg| cfg["enabled"] } end |
#deep_copy ⇒ ChannelConfig
Deep copy — prevents callers from mutating shared config state.
148 149 150 |
# File 'lib/clacky/server/channel/channel_config.rb', line 148 def deep_copy self.class.new(channels: JSON.parse(JSON.generate(@channels))) end |
#disable_platform(platform) ⇒ Object
Disable a platform (keeps credentials, just sets enabled: false).
134 135 136 137 138 |
# File 'lib/clacky/server/channel/channel_config.rb', line 134 def disable_platform(platform) key = platform.to_s return unless @channels.key?(key) @channels[key]["enabled"] = false end |
#enabled?(platform) ⇒ Boolean
Returns true if the given platform is configured and enabled.
79 80 81 82 |
# File 'lib/clacky/server/channel/channel_config.rb', line 79 def enabled?(platform) cfg = @channels[platform.to_s] cfg && cfg["enabled"] end |
#enabled_platforms ⇒ Array<Symbol>
Returns the list of enabled platform symbols.
70 71 72 73 74 75 |
# File 'lib/clacky/server/channel/channel_config.rb', line 70 def enabled_platforms @channels .select { |_, cfg| cfg["enabled"] } .keys .map(&:to_sym) end |
#platform_config(platform) ⇒ Hash?
Return the symbol-keyed config hash expected by each adapter’s initializer. Returns nil if the platform is not configured.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/clacky/server/channel/channel_config.rb', line 89 def platform_config(platform) raw = @channels[platform.to_s] return nil unless raw case platform.to_sym when :feishu { app_id: raw["app_id"], app_secret: raw["app_secret"], domain: raw["domain"], allowed_users: raw["allowed_users"] }.compact when :wecom { bot_id: raw["bot_id"], secret: raw["secret"] }.compact when :weixin { token: raw["token"], base_url: raw["base_url"], allowed_users: raw["allowed_users"] }.compact else # Unknown platform — pass all non-meta keys as symbol-keyed hash raw.reject { |k, _| k == "enabled" } .transform_keys(&:to_sym) end end |
#remove_platform(platform) ⇒ Object
Remove a platform entry entirely.
142 143 144 |
# File 'lib/clacky/server/channel/channel_config.rb', line 142 def remove_platform(platform) @channels.delete(platform.to_s) end |
#save(config_file = CONFIG_FILE) ⇒ Object
Persist to disk.
51 52 53 54 55 |
# File 'lib/clacky/server/channel/channel_config.rb', line 51 def save(config_file = CONFIG_FILE) FileUtils.mkdir_p(File.dirname(config_file)) File.write(config_file, to_yaml) FileUtils.chmod(0o600, config_file) end |
#set_platform(platform, **fields) ⇒ Object
Set or update a platform’s credentials. Merges provided fields into the existing entry. Automatically sets enabled: true unless explicitly provided.
125 126 127 128 129 130 |
# File 'lib/clacky/server/channel/channel_config.rb', line 125 def set_platform(platform, **fields) key = platform.to_s @channels[key] ||= {} fields.each { |k, v| @channels[key][k.to_s] = v } @channels[key]["enabled"] = true unless @channels[key].key?("enabled") end |
#to_yaml ⇒ String
Serialize to YAML string.
59 60 61 |
# File 'lib/clacky/server/channel/channel_config.rb', line 59 def to_yaml YAML.dump({ "channels" => @channels }) end |