refact
This commit is contained in:
270
bbs.rb
270
bbs.rb
@@ -3,51 +3,65 @@
|
||||
require 'bbs'
|
||||
require 'time'
|
||||
|
||||
require_relative 'lib/repository/online_users_repository'
|
||||
require_relative 'lib/repository/board_repository'
|
||||
require_relative 'lib/repository/wiki_repository'
|
||||
require_relative 'lib/repository/catalog_repository'
|
||||
require_relative 'lib/repository/last_callers_repository'
|
||||
require_relative 'lib/repository/profile_repository'
|
||||
require_relative 'lib/service/online_service'
|
||||
require_relative 'lib/service/board_service'
|
||||
require_relative 'lib/service/profile_service'
|
||||
require_relative 'lib/service/wiki_service'
|
||||
require_relative 'lib/service/games_service'
|
||||
require_relative 'lib/service/chat_service'
|
||||
require_relative 'lib/service/last_callers_service'
|
||||
require_relative 'lib/ui/formatters'
|
||||
require_relative 'lib/domain/repository/online_users_repository'
|
||||
require_relative 'lib/domain/repository/board_repository'
|
||||
require_relative 'lib/domain/repository/wiki_repository'
|
||||
require_relative 'lib/domain/repository/catalog_repository'
|
||||
require_relative 'lib/domain/repository/last_callers_repository'
|
||||
require_relative 'lib/domain/repository/profile_repository'
|
||||
require_relative 'lib/domain/service/online_service'
|
||||
require_relative 'lib/domain/service/board_service'
|
||||
require_relative 'lib/domain/service/profile_service'
|
||||
require_relative 'lib/domain/service/wiki_service'
|
||||
require_relative 'lib/domain/service/games_service'
|
||||
require_relative 'lib/domain/service/chat_service'
|
||||
require_relative 'lib/domain/service/last_callers_service'
|
||||
require_relative 'lib/bbs/ui/formatters'
|
||||
|
||||
ONLINE = OnlineService.new(OnlineUsersRepository.new)
|
||||
BOARDS = BoardService.new(BoardRepository.new(
|
||||
online_service = OnlineService.new(OnlineUsersRepository.new)
|
||||
board_service = BoardService.new(BoardRepository.new(
|
||||
ENV.fetch('BOARDS_PATH', 'data/boards'),
|
||||
legacy_path: ENV.fetch('MESSAGES_PATH', 'data/messages.dat')))
|
||||
WIKI = WikiService.new(WikiRepository.new(token: ENV['WEBAPP_WIKIJS_TOKEN']))
|
||||
GAMES = GamesService.new(CatalogRepository.new)
|
||||
CHAT = ChatService.new
|
||||
LAST_CALLERS = LastCallersService.new(LastCallersRepository.new(
|
||||
wiki_service = WikiService.new(WikiRepository.new(token: ENV['WEBAPP_WIKIJS_TOKEN']))
|
||||
games_service = GamesService.new(CatalogRepository.new)
|
||||
chat_service = ChatService.new
|
||||
last_callers_service = LastCallersService.new(LastCallersRepository.new(
|
||||
ENV.fetch('LAST_CALLERS_PATH', 'data/last_callers.csv')))
|
||||
PROFILE = ProfileService.new(ProfileRepository.new(
|
||||
profile_service = ProfileService.new(ProfileRepository.new(
|
||||
ENV.fetch('PROFILE_PATH', 'data/profiles.csv')))
|
||||
|
||||
STARTED_AT = Time.now
|
||||
SYSOPS = ENV.fetch('BBS_SYSOPS', '').split(',').map { |s| s.strip.downcase }.reject(&:empty?)
|
||||
SYSOPS = ENV.fetch('BBS_SYSOPS', '').split(',').map { |name| name.strip.downcase }.reject(&:empty?)
|
||||
DESKTOP_ART = ENV.fetch('BBS_DESKTOP_ART', 'data/art/teletype.ansi')
|
||||
|
||||
SERVICES = {
|
||||
online: ONLINE,
|
||||
boards: BOARDS,
|
||||
wiki: WIKI,
|
||||
games: GAMES,
|
||||
chat: CHAT,
|
||||
last_callers: LAST_CALLERS,
|
||||
profile: PROFILE,
|
||||
uptime: STARTED_AT
|
||||
}.freeze
|
||||
BBS.configure do |config|
|
||||
config.mouse = true
|
||||
config.idle_seconds = 600
|
||||
|
||||
MAIN_APP = BBS::Application.define do
|
||||
config.on_session_end = lambda do |session|
|
||||
online_service.remove(session.session_id)
|
||||
chat_service.unsubscribe(session.session_id)
|
||||
end
|
||||
|
||||
config.flow = BBS::Flow.define do
|
||||
big_banner 'TELETYPE BBS', style: :success
|
||||
ask :username, prompt: 'Name (blank for Anonymous)',
|
||||
transform: ->(value) { value.strip.empty? ? 'Anonymous' : value.strip[0...20] }
|
||||
call do |ctx|
|
||||
online_service.add(ctx[:session_id], ctx[:username])
|
||||
chat_service.subscribe(ctx[:session_id])
|
||||
last_callers_service.record(ctx[:username], ctx[:session_id])
|
||||
end
|
||||
app(BBS::Application.define do
|
||||
theme BBS::Theme.neumanntronics
|
||||
services SERVICES
|
||||
services online: online_service,
|
||||
boards: board_service,
|
||||
wiki: wiki_service,
|
||||
games: games_service,
|
||||
chat: chat_service,
|
||||
last_callers: last_callers_service,
|
||||
profile: profile_service,
|
||||
uptime: STARTED_AT
|
||||
|
||||
helpers do
|
||||
def sysop?
|
||||
@@ -58,11 +72,11 @@ MAIN_APP = BBS::Application.define do
|
||||
|
||||
# ── Windows ────────────────────────────────────────────────────────────────
|
||||
|
||||
window :bulletin, type: :info do |w|
|
||||
w.title ' Welcome '
|
||||
w.width 70
|
||||
w.height 17
|
||||
w.body do |ctx|
|
||||
window :bulletin, type: :info do |window_spec|
|
||||
window_spec.title ' Welcome '
|
||||
window_spec.width 70
|
||||
window_spec.height 17
|
||||
window_spec.body do |ctx|
|
||||
UI::Formatters::Bulletin.body(
|
||||
username: ctx[:username] || 'Anonymous',
|
||||
online_count: ctx[:online].count,
|
||||
@@ -70,14 +84,14 @@ MAIN_APP = BBS::Application.define do
|
||||
recent_callers: ctx[:last_callers].recent(3)
|
||||
)
|
||||
end
|
||||
w.button label: '&Continue', primary: true, cancel: true
|
||||
window_spec.button label: '&Continue', primary: true, cancel: true
|
||||
end
|
||||
|
||||
window :system_info, type: :info do |w|
|
||||
w.title ' System Info '
|
||||
w.width 60
|
||||
w.height 14
|
||||
w.body do |ctx|
|
||||
window :system_info, type: :info do |window_spec|
|
||||
window_spec.title ' System Info '
|
||||
window_spec.width 60
|
||||
window_spec.height 14
|
||||
window_spec.body do |ctx|
|
||||
rows = [
|
||||
['Online users', ctx[:online].count.to_s],
|
||||
['Messages', ctx[:boards].total_count.to_s],
|
||||
@@ -92,36 +106,36 @@ MAIN_APP = BBS::Application.define do
|
||||
end
|
||||
end
|
||||
|
||||
window :messages, type: :master_detail do |w|
|
||||
w.title ' Message Boards '
|
||||
w.list_width 20
|
||||
w.empty_message ' (no boards configured)'
|
||||
w.items { |ctx| ctx[:boards].boards }
|
||||
w.item_label { |board, _ctx| board.title }
|
||||
w.render_detail do |board, width, ctx|
|
||||
window :messages, type: :master_detail do |window_spec|
|
||||
window_spec.title ' Message Boards '
|
||||
window_spec.list_width 20
|
||||
window_spec.empty_message ' (no boards configured)'
|
||||
window_spec.items { |ctx| ctx[:boards].boards }
|
||||
window_spec.item_label { |board, _ctx| board.title }
|
||||
window_spec.render_detail do |board, width, ctx|
|
||||
UI::Formatters::Board.lines(ctx[:boards].messages(board.id, 100), board.title, width)
|
||||
end
|
||||
w.action label: '&New', align: :left do |window, ctx|
|
||||
window_spec.action label: '&New', align: :left do |window, ctx|
|
||||
board = window.list_widget.selected_item
|
||||
ctx[:app].open_window(:compose_message, board: board,
|
||||
on_posted: -> { window.reload_detail })
|
||||
:keep
|
||||
end
|
||||
w.action label: '&Refresh', align: :left do |window, _ctx|
|
||||
window_spec.action label: '&Refresh', align: :left do |window, _ctx|
|
||||
window.reload_detail
|
||||
:keep
|
||||
end
|
||||
end
|
||||
|
||||
window :compose_message, type: :form do |w|
|
||||
w.title { |ctx| ctx[:board] ? " Post to #{ctx[:board].title} " : ' Compose ' }
|
||||
w.width { |ctx| [ctx[:app].cols - 8, 70].min }
|
||||
w.height 14
|
||||
w.hint { |ctx| "From: #{ctx[:username] || 'Anonymous'} — Ctrl-Enter sends" }
|
||||
w.submit_label '&Send'
|
||||
w.cancel_label '&Cancel'
|
||||
w.field :body, type: :textarea, max_length: 1000
|
||||
w.on_submit do |values, form, ctx|
|
||||
window :compose_message, type: :form do |window_spec|
|
||||
window_spec.title { |ctx| ctx[:board] ? " Post to #{ctx[:board].title} " : ' Compose ' }
|
||||
window_spec.width { |ctx| [ctx[:app].cols - 8, 70].min }
|
||||
window_spec.height 14
|
||||
window_spec.hint { |ctx| "From: #{ctx[:username] || 'Anonymous'} — Ctrl-Enter sends" }
|
||||
window_spec.submit_label '&Send'
|
||||
window_spec.cancel_label '&Cancel'
|
||||
window_spec.field :body, type: :textarea, max_length: 1000
|
||||
window_spec.on_submit do |values, form, ctx|
|
||||
if ctx[:board].nil?
|
||||
form.error('Select a board first.')
|
||||
:keep
|
||||
@@ -138,66 +152,66 @@ MAIN_APP = BBS::Application.define do
|
||||
end
|
||||
end
|
||||
|
||||
window :wiki, type: :master_detail do |w|
|
||||
w.title { |ctx| " #{ctx[:title]} " }
|
||||
w.list_width { |ctx| [32, ctx[:app].cols / 3].max }
|
||||
w.empty_message ' (no pages found)'
|
||||
w.close_button false
|
||||
w.items { |ctx| ctx[:wiki].list(ctx[:category]) }
|
||||
w.item_label { |page, _ctx| page.title }
|
||||
w.render_detail do |page, width, ctx|
|
||||
window :wiki, type: :master_detail do |window_spec|
|
||||
window_spec.title { |ctx| " #{ctx[:title]} " }
|
||||
window_spec.list_width { |ctx| [32, ctx[:app].cols / 3].max }
|
||||
window_spec.empty_message ' (no pages found)'
|
||||
window_spec.close_button false
|
||||
window_spec.items { |ctx| ctx[:wiki].list(ctx[:category]) }
|
||||
window_spec.item_label { |page, _ctx| page.title }
|
||||
window_spec.render_detail do |page, width, ctx|
|
||||
text = ctx[:wiki].content(page.id)
|
||||
BBS::Markdown.to_lines(text, width: width - 1, theme: ctx[:app].theme)
|
||||
end
|
||||
w.bottom_meta do |page, ctx|
|
||||
window_spec.bottom_meta do |page, ctx|
|
||||
"#{page.created_at.to_s[0, 10]} #{ctx[:wiki].page_url(page.locale, page.path)}"
|
||||
end
|
||||
end
|
||||
|
||||
window :games, type: :master_detail do |w|
|
||||
w.title ' Game Catalog '
|
||||
w.list_width 30
|
||||
w.empty_message ' (no games)'
|
||||
w.items { |ctx| ctx[:games].all }
|
||||
w.item_label { |game, _ctx| game.title }
|
||||
w.render_detail { |game, width, _ctx| UI::Formatters::Game.card(game, width) }
|
||||
window :games, type: :master_detail do |window_spec|
|
||||
window_spec.title ' Game Catalog '
|
||||
window_spec.list_width 30
|
||||
window_spec.empty_message ' (no games)'
|
||||
window_spec.items { |ctx| ctx[:games].all }
|
||||
window_spec.item_label { |game, _ctx| game.title }
|
||||
window_spec.render_detail { |game, width, _ctx| UI::Formatters::Game.card(game, width) }
|
||||
end
|
||||
|
||||
window :online_users, type: :info do |w|
|
||||
w.title ' Online Users '
|
||||
w.width 50
|
||||
w.scroll true
|
||||
w.height { |ctx| [ctx[:online].user_list.size + 6, 10].max }
|
||||
w.body { |ctx| UI::Formatters::Users.online_list(ctx[:online].user_list, ctx[:username]) }
|
||||
window :online_users, type: :info do |window_spec|
|
||||
window_spec.title ' Online Users '
|
||||
window_spec.width 50
|
||||
window_spec.scroll true
|
||||
window_spec.height { |ctx| [ctx[:online].user_list.size + 6, 10].max }
|
||||
window_spec.body { |ctx| UI::Formatters::Users.online_list(ctx[:online].user_list, ctx[:username]) }
|
||||
end
|
||||
|
||||
window :last_callers, type: :info do |w|
|
||||
w.title ' Last Callers '
|
||||
w.width 64
|
||||
w.scroll true
|
||||
w.height { |ctx| [ctx[:last_callers].recent(15).size + 6, 10].max }
|
||||
w.body { |ctx| UI::Formatters::Users.last_callers_list(ctx[:last_callers].recent(15)) }
|
||||
window :last_callers, type: :info do |window_spec|
|
||||
window_spec.title ' Last Callers '
|
||||
window_spec.width 64
|
||||
window_spec.scroll true
|
||||
window_spec.height { |ctx| [ctx[:last_callers].recent(15).size + 6, 10].max }
|
||||
window_spec.body { |ctx| UI::Formatters::Users.last_callers_list(ctx[:last_callers].recent(15)) }
|
||||
end
|
||||
|
||||
window :chat, type: :stream do |w|
|
||||
w.title ' Live Chat '
|
||||
w.placeholder 'Type a message and press Enter…'
|
||||
w.initial_lines do |ctx|
|
||||
window :chat, type: :stream do |window_spec|
|
||||
window_spec.title ' Live Chat '
|
||||
window_spec.placeholder 'Type a message and press Enter…'
|
||||
window_spec.initial_lines do |ctx|
|
||||
ctx[:chat].history.map { |line| UI::Formatters::Chat.line(line) }
|
||||
end
|
||||
w.on_submit do |text, _app, ctx|
|
||||
window_spec.on_submit do |text, _app, ctx|
|
||||
ctx[:chat].broadcast(ctx[:username] || 'Anonymous', text.strip)
|
||||
end
|
||||
w.on_poll do |app_, ctx|
|
||||
window_spec.on_poll do |app_, ctx|
|
||||
ctx[:chat].drain(app_.session_id).map { |line| UI::Formatters::Chat.line(line) }
|
||||
end
|
||||
end
|
||||
|
||||
window :profile, type: :form do |w|
|
||||
w.title { |ctx| " Profile — #{ctx[:username] || 'Anonymous'} " }
|
||||
w.width 64
|
||||
w.height 14
|
||||
w.prefill do |ctx|
|
||||
window :profile, type: :form do |window_spec|
|
||||
window_spec.title { |ctx| " Profile — #{ctx[:username] || 'Anonymous'} " }
|
||||
window_spec.width 64
|
||||
window_spec.height 14
|
||||
window_spec.prefill do |ctx|
|
||||
existing = ctx[:profile].find(ctx[:username] || 'Anonymous') || {}
|
||||
{
|
||||
signature: existing['signature'].to_s,
|
||||
@@ -206,23 +220,23 @@ MAIN_APP = BBS::Application.define do
|
||||
notes: existing['notes'].to_s
|
||||
}
|
||||
end
|
||||
w.field :signature, label: 'Signature'
|
||||
w.field :location, label: 'Location'
|
||||
w.field :homepage, label: 'Homepage'
|
||||
w.field :notes, label: 'Notes'
|
||||
w.on_submit do |values, _form, ctx|
|
||||
window_spec.field :signature, label: 'Signature'
|
||||
window_spec.field :location, label: 'Location'
|
||||
window_spec.field :homepage, label: 'Homepage'
|
||||
window_spec.field :notes, label: 'Notes'
|
||||
window_spec.on_submit do |values, _form, ctx|
|
||||
ctx[:profile].update(ctx[:username] || 'Anonymous', **values)
|
||||
BBS::Dialogs.message(ctx[:app], 'Profile saved.', title: ' Saved ')
|
||||
:close
|
||||
end
|
||||
end
|
||||
|
||||
window :sysop_console, type: :info do |w|
|
||||
w.title ' Sysop Console '
|
||||
w.width 70
|
||||
w.height 18
|
||||
w.scroll true
|
||||
w.body do |ctx|
|
||||
window :sysop_console, type: :info do |window_spec|
|
||||
window_spec.title ' Sysop Console '
|
||||
window_spec.width 70
|
||||
window_spec.height 18
|
||||
window_spec.scroll true
|
||||
window_spec.body do |ctx|
|
||||
UI::Formatters::Sysop.console_body(
|
||||
username: ctx[:username],
|
||||
online_count: ctx[:online].count,
|
||||
@@ -232,7 +246,7 @@ MAIN_APP = BBS::Application.define do
|
||||
recent_callers: ctx[:last_callers].recent(5)
|
||||
)
|
||||
end
|
||||
w.button label: '&Broadcast', align: :left do |_window, ctx|
|
||||
window_spec.button label: '&Broadcast', align: :left do |_window, ctx|
|
||||
BBS::Dialogs.input(ctx[:app], prompt: 'Sysop broadcast message:',
|
||||
title: ' Broadcast ', width: 64,
|
||||
on_submit: lambda do |text|
|
||||
@@ -241,7 +255,7 @@ MAIN_APP = BBS::Application.define do
|
||||
end)
|
||||
:keep
|
||||
end
|
||||
w.button label: '&Close', cancel: true
|
||||
window_spec.button label: '&Close', cancel: true
|
||||
end
|
||||
|
||||
# ── Menubar ────────────────────────────────────────────────────────────────
|
||||
@@ -257,7 +271,7 @@ MAIN_APP = BBS::Application.define do
|
||||
menu '&Messages' do
|
||||
item '&Boards…', shortcut: 'F2' do open_window(:messages) end
|
||||
item '&New Post…', shortcut: 'F3' do
|
||||
board = SERVICES[:boards].boards.first
|
||||
board = board_service.boards.first
|
||||
if board
|
||||
open_window(:compose_message, board: board)
|
||||
else
|
||||
@@ -311,7 +325,7 @@ MAIN_APP = BBS::Application.define do
|
||||
status_hint('F1', 'Help') { BBS::Dialogs.message(self, 'Top menu: Alt+letter — Windows: Tab/Shift-Tab — Close: Esc') }
|
||||
status_hint('F2', 'Boards') { open_window(:messages) }
|
||||
status_hint('F3', 'Post') {
|
||||
board = SERVICES[:boards].boards.first
|
||||
board = board_service.boards.first
|
||||
if board
|
||||
open_window(:compose_message, board: board)
|
||||
else
|
||||
@@ -331,27 +345,7 @@ MAIN_APP = BBS::Application.define do
|
||||
art.layout(1, 1, cols, rows - 2)
|
||||
add(art)
|
||||
end
|
||||
end
|
||||
|
||||
BBS.configure do |c|
|
||||
c.mouse = true
|
||||
c.idle_seconds = 600
|
||||
|
||||
c.on_session_end = lambda do |session|
|
||||
ONLINE.remove(session.session_id)
|
||||
CHAT.unsubscribe(session.session_id)
|
||||
end
|
||||
|
||||
c.flow = BBS::Flow.define do
|
||||
big_banner 'TELETYPE BBS', style: :success
|
||||
ask :username, prompt: 'Name (blank for Anonymous)',
|
||||
transform: ->(v) { v.strip.empty? ? 'Anonymous' : v.strip[0...20] }
|
||||
call do |ctx|
|
||||
ONLINE.add(ctx[:session_id], ctx[:username])
|
||||
CHAT.subscribe(ctx[:session_id])
|
||||
LAST_CALLERS.record(ctx[:username], ctx[:session_id])
|
||||
end
|
||||
app MAIN_APP
|
||||
end)
|
||||
say 'Goodbye!', style: :muted
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user