Skip to content

Commit

Permalink
Merge pull request #41 from tsg-ut/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
cookie-s authored Aug 30, 2022
2 parents 58d2902 + 5752de8 commit f60785b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 145 deletions.
82 changes: 82 additions & 0 deletions lib/collector/events_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'rack'

module Collector
class EventsAPI
attr_reader :logger

def initialize(team_id)
@team_id = team_id
raise ArgumentError unless @team_id and @team_id.start_with? 'T'
end

def start!(logger)
@logger = logger

Rack::Server.start(
app: lambda do |env|
req = Rack::Request.new(env)

begin
Slack::Events::Request.new(req).verify!
process(req.body)
rescue Slack::Events::Request::MissingSigningSecret,
Slack::Events::Request::InvalidSignature,
Slack::Events::Request::TimestampExpired => e
warn 'bad request: %s' % e
[400, {}, '']
end
end,
Port: 9293,
)
end

def process(body)
# https://api.slack.com/apis/connections/events-api

data = JSON.parse(body.read)
type = data['type']

case type
when 'url_verification'
[200, { 'content-type': 'text/plain' }, data['challenge']]
when 'event_callback'
process_event data['event'] if data['team_id'] == @team_id
[204, {}, '']
else
[400, {}, '']
end
end

def process_event(event)
type = event['type']
event.delete 'type'

case type
when 'message'
# https://api.slack.com/events/message
puts 'new message'
logger.new_message(event)

when 'team_join'
puts 'new user has joined'
logger.update_users

when 'user_change'
puts 'user data has changed'
logger.update_users

when 'channel_created'
puts 'channel has created'
logger.update_channels

when 'channel_rename'
puts 'channel has renamed'
logger.update_channels

when 'emoji_changed'
puts 'emoji has changed'
logger.update_emojis
end
end
end
end
46 changes: 46 additions & 0 deletions lib/collector/rtm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Collector
class RTM
def start!(logger)
loop do
realtime = Slack::RealTime::Client.new

realtime.on :message do |m|
puts 'new message'
logger.new_message(m)
end

realtime.on :team_join do |e|
puts "new user has joined"
logger.update_users
end

realtime.on :user_change do |e|
puts "user data has changed"
logger.update_users
end

realtime.on :channel_created do |c|
puts "channel has created"
logger.update_channels
end

realtime.on :channel_rename do |c|
puts "channel has renamed"
logger.update_channels
end

realtime.on :emoji_changed do |c|
puts "emoji has changed"
logger.update_emojis
end

# if connection closed, restart the realtime logger
realtime.on :close do
puts "websocket disconnected"
end

realtime.start!
end
end
end
end
84 changes: 0 additions & 84 deletions lib/slack_events_receiver.rb

This file was deleted.

19 changes: 12 additions & 7 deletions lib/slack_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ def is_direct_message(channel_name)
channel_name[0] == 'D'
end

alias :_insert_message :insert_message # FIXME!!!
def insert_message(message)
_insert_message(message)
def skip_message?(message)
channel = message['channel']
is_private_channel(channel) || is_direct_message(channel)
end

def new_message(message)
return if skip_message?(message)
insert_message(message)
end

def update_users
Expand Down Expand Up @@ -53,9 +58,9 @@ def fetch_history(target, channel)
end
end

def start(receiver)
def start(collector)
begin
receiver_thread = Thread.new { receiver.start!(self) }
collector_thread = Thread.new { collector.start!(self) }

update_emojis
update_users
Expand All @@ -70,9 +75,9 @@ def start(receiver)
end

# realtime event is joined and dont exit current thread
receiver_thread.join
collector_thread.join
ensure
receiver_thread.kill
collector_thread.kill
end
end
end
50 changes: 0 additions & 50 deletions lib/slack_rtm_receiver.rb

This file was deleted.

8 changes: 4 additions & 4 deletions logger/logger.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require './lib/slack'

require './lib/slack_logger'
require './lib/slack_events_receiver'
require './lib/slack_rtm_receiver'
require './lib/collector/events_api'
require './lib/collector/rtm'

config = YAML.load_file('./config.yml')

receiver = config['slack']['use_events_api'] ? SlackEventsReceiver.new(config['slack']['team_id']) : SlackRTMReceiver.new
SlackLogger.new.start receiver
collector = config['slack']['use_events_api'] ? Collector::EventsAPI.new(config['slack']['team_id']) : Collector::RTM.new
SlackLogger.new.start collector

0 comments on commit f60785b

Please sign in to comment.