module DirectchatSession
Constants
- PLUGIN
- SESSIONS_KEY
- STYLE_COUNT
- STYLE_NICK
- STYLE_TITLE
Public Instance Methods
Source
# File lib/directchat/session.rb, line 97 def active_for_bot(network:, bot_nick:, store: RabbotDb) key = Normalize.user_key(bot_nick) load_sessions(network: network, store: store).values.find do |session| next false unless session.is_a?(Hash) next false unless session["status"].to_s == "active" Array(session["assignments"]).any? do |entry| Normalize.user_key(entry["bot"]) == key end end.then { |session| symbolize_session(session) } end
Source
# File lib/directchat/session.rb, line 75 def active_for_channel(network:, channel:, store: RabbotDb) target = Normalize.channel(channel) load_sessions(network: network, store: store).values.find do |session| next false unless session.is_a?(Hash) next false unless session["status"].to_s == "active" Normalize.channel(session["channel"]) == target end.then { |session| symbolize_session(session) } end
Source
# File lib/directchat/session.rb, line 85 def active_for_participant(network:, participant:, store: RabbotDb) key = Normalize.user_key(participant) load_sessions(network: network, store: store).values.find do |session| next false unless session.is_a?(Hash) next false unless session["status"].to_s == "active" Array(session["assignments"]).any? do |entry| Normalize.user_key(entry["participant"]) == key end end.then { |session| symbolize_session(session) } end
Source
# File lib/directchat/session.rb, line 141 def assignment_for_bot(session:, bot_nick:) return nil unless session key = Normalize.user_key(bot_nick) session[:assignments].find { |entry| Normalize.user_key(entry[:bot]) == key } end
Source
# File lib/directchat/session.rb, line 109 def close(network:, session_id:, store: RabbotDb) sessions = load_sessions(network: network, store: store) sessions.delete(session_id.to_s) store.set_plugin_json( plugin: PLUGIN, key: SESSIONS_KEY, network: network, value: sessions ) true end
Source
# File lib/directchat/session.rb, line 27 def create(network:, channel:, requester:, assignments:, store: RabbotDb, created_at: nil, bot_count: nil) session = { "id" => SecureRandom.hex(4), "channel" => channel.to_s, "requester" => Normalize.nick(requester), "assignments" => assignments.map do |entry| { "bot" => Normalize.nick(entry[:bot] || entry["bot"]), "participant" => Normalize.nick(entry[:participant] || entry["participant"]), "invited_at" => entry[:invited_at] || entry["invited_at"] } end, "created_at" => store.iso_time(created_at || Time.now), "status" => "active", "bot_count" => bot_count || assignments.length } save(network: network, session: session, store: store) symbolize_session(session) end
Source
# File lib/directchat/session.rb, line 177 def format_invite(channel:, participant:, bot_nick:, peers:) peer_list = Array(peers).map { |nick| Normalize.nick(nick) }.reject(&:empty?).uniq meta = [ channel.to_s, "#{peer_list.length + 1} people", "via #{Normalize.nick(bot_nick)}" ].join(" ยท ") [ IrcFormat.report_header(tag: "DCHAT", title: "direct chat invite", style: STYLE_TITLE), "You are linked in #{IrcFormat.decorate(channel.to_s, STYLE_NICK)}.", "Reply here to talk with: #{peer_list.join(', ')}.", IrcFormat.report_footer(meta) ].join("\n") end
Source
# File lib/directchat/session.rb, line 231 def format_no_session_reply [ IrcFormat.tag("DCHAT"), "No active direct chat in your shared channel." ].join("\n") end
Source
# File lib/directchat/session.rb, line 173 def format_relay_line(from:, text:) "<#{Normalize.nick(from)}> #{text}" end
Source
# File lib/directchat/session.rb, line 192 def format_start_reply(session:) assignments = session[:assignments] lines = assignments.map do |entry| "#{IrcFormat.nick(entry[:bot])} โ #{Normalize.nick(entry[:participant])}" end meta = [ session[:channel], "#{assignments.length} link(s)", "#{session[:bot_count] || assignments.length} bot(s)" ].join(" ยท ") [ IrcFormat.report_header(tag: "DCHAT", title: "direct chat started", style: STYLE_TITLE), *lines, "Each bot will PM its participant โ reply to your bot to chat.", "Bots relay over a direct mesh link (separate from IRC).", IrcFormat.report_footer(meta) ].join("\n") end
Source
# File lib/directchat/session.rb, line 211 def format_status_reply(session:) lines = session[:assignments].map do |entry| "#{IrcFormat.nick(entry[:bot])} โ #{Normalize.nick(entry[:participant])}" end [ IrcFormat.report_header(tag: "DCHAT", title: "active session", style: STYLE_TITLE), *lines, IrcFormat.report_footer(session[:channel], "reply via PM to your bot") ].join("\n") end
Source
# File lib/directchat/session.rb, line 222 def format_stop_reply(channel:) [ IrcFormat.tag("DCHAT"), IrcFormat.menu_heading("direct chat ended", style: STYLE_TITLE), "Session closed for #{channel}.", IrcFormat.report_footer("direct chat") ].join("\n") end
Source
# File lib/directchat/session.rb, line 134 def invited?(session:, bot_nick:) assignment = assignment_for_bot(session: session, bot_nick: bot_nick) return false unless assignment !assignment[:invited_at].to_s.strip.empty? end
Source
# File lib/directchat/session.rb, line 60 def load(network:, session_id:, store: RabbotDb) session = load_sessions(network: network, store: store)[session_id.to_s] symbolize_session(session) end
Source
# File lib/directchat/session.rb, line 65 def load_sessions(network:, store: RabbotDb) raw = store.get_plugin_json( plugin: PLUGIN, key: SESSIONS_KEY, network: network, default: {} ) raw.is_a?(Hash) ? raw : {} end
Source
# File lib/directchat/session.rb, line 121 def mark_invited(network:, session_id:, bot_nick:, store: RabbotDb, invited_at: nil) session = load(network: network, session_id: session_id, store: store) return nil unless session key = Normalize.user_key(bot_nick) session[:assignments].each do |entry| next unless Normalize.user_key(entry[:bot]) == key entry[:invited_at] = store.iso_time(invited_at || Time.now) end save(network: network, session: session, store: store) end
Source
# File lib/directchat/session.rb, line 148 def participant_names(session:) session[:assignments].map { |entry| entry[:participant] } end
Source
# File lib/directchat/session.rb, line 152 def relay_targets(session:, from_participant:, text:) from_key = Normalize.user_key(from_participant) unless session[:assignments].any? { |entry| Normalize.user_key(entry[:participant]) == from_key } return nil end clean = text.to_s.strip return [] if clean.empty? session[:assignments].filter_map do |entry| next if Normalize.user_key(entry[:participant]) == from_key { bot: entry[:bot], participant: entry[:participant], from: Normalize.nick(from_participant), text: clean } end end
Source
# File lib/directchat/session.rb, line 47 def save(network:, session:, store: RabbotDb) sessions = load_sessions(network: network, store: store) payload = stringify_session(session) sessions[payload["id"]] = payload store.set_plugin_json( plugin: PLUGIN, key: SESSIONS_KEY, network: network, value: sessions ) symbolize_session(payload) end
Source
# File lib/directchat/session.rb, line 238 def stringify_session(session) DirectchatSessionCodec.stringify(session) end
Source
# File lib/directchat/session.rb, line 242 def symbolize_session(session) return nil unless session.is_a?(Hash) && !session.empty? DirectchatSessionCodec.symbolize(session) end