Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slack-api -> slack-ruby-client #35

Merged
merged 2 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source 'https://rubygems.org'

gem 'slack-api'
gem 'sinatra'
gem 'puma'
gem 'rubyzip'
Expand All @@ -11,3 +10,6 @@ gem 'nokogiri'
group :development do
gem 'thin'
end

gem "slack-ruby-client"
gem "async-websocket", '~> 0.8.0' # dependency of slack-ruby-client RTM
hakatashi marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 35 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
async (1.30.3)
console (~> 1.10)
nio4r (~> 2.3)
timers (~> 4.1)
async-io (1.33.0)
async
async-websocket (0.8.0)
async-io
websocket-driver (~> 0.7.0)
aws-eventstream (1.2.0)
aws-partitions (1.578.0)
aws-sdk (3.1.0)
Expand Down Expand Up @@ -1228,22 +1237,28 @@ GEM
aws-sigv4 (1.4.0)
aws-eventstream (~> 1, >= 1.0.2)
bson (4.14.1)
console (1.15.3)
fiber-local
daemons (1.4.1)
eventmachine (1.2.7)
faraday (0.17.5)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.10.1)
faraday (>= 0.7.4, < 1.0)
faye-websocket (0.10.9)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.5.1)
faraday (2.3.0)
faraday-net_http (~> 2.0)
ruby2_keywords (>= 0.0.4)
faraday-mashify (0.1.0)
faraday (~> 2.0)
hashie
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (2.0.3)
fiber-local (1.0.0)
gli (2.21.0)
hashie (5.0.0)
jmespath (1.6.1)
json (2.6.1)
mini_portile2 (2.8.0)
mongo (2.17.1)
bson (>= 4.8.2, < 5.0.0)
multi_json (1.15.0)
multipart-post (2.1.1)
multipart-post (2.2.3)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nio4r (2.5.8)
Expand All @@ -1263,16 +1278,19 @@ GEM
rack (~> 2.2)
rack-protection (= 2.2.0)
tilt (~> 2.0)
slack-api (1.6.1)
faraday (~> 0.11)
faraday_middleware (~> 0.10.0)
faye-websocket (~> 0.10.6)
multi_json (~> 1.0, >= 1.0.3)
slack-ruby-client (1.1.0)
faraday (>= 2.0)
faraday-mashify
faraday-multipart
gli
hashie
websocket-driver
thin (1.8.1)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
tilt (2.0.10)
timers (4.3.3)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -1281,15 +1299,16 @@ PLATFORMS
ruby

DEPENDENCIES
async-websocket (~> 0.8.0)
aws-sdk
json
mongo
nokogiri
puma
rubyzip
sinatra
slack-api
slack-ruby-client
thin

BUNDLED WITH
2.2.20
2.3.16
4 changes: 0 additions & 4 deletions bin/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
'secret_access_key' => nil,
},
'default_channel' => 'general',
'logger' => {
'enable_private_channel' => true,
'enable_direct_message' => true
},
'database' => {
'uri' => 'mongo:27017',
'database' => 'slack_logger'
Expand Down
9 changes: 2 additions & 7 deletions lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ def replace_channels(channels)
end
end

# Ims
hakatashi marked this conversation as resolved.
Show resolved Hide resolved
# for backward compatibility. should be removed.
Ims = db['ims']
Ims.indexes.create_one({ :id => 1 }, :unique => true)
def replace_ims(ims)
unless ims.nil?
ids = ims.map{ |im| im['id'] }
Ims.find(id: { '$in' => ids }).delete_many
Ims.insert_many(ims)
end
end

Emojis = db['emojis']
Emojis.indexes.create_one({ :name => 1 }, :unique => true)
Expand Down
7 changes: 6 additions & 1 deletion lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
require 'yaml'
require 'slack'
require 'slack-ruby-client'

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

Slack.configure do |c|
c.token = config['slack']['token']
end

Slack::RealTime::Client.configure do |c|
c.start_method = :rtm_connect
hakatashi marked this conversation as resolved.
Show resolved Hide resolved
c.store_class = Slack::RealTime::Stores::Starter
end
68 changes: 16 additions & 52 deletions lib/slack_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
require './lib/db'

config = YAML.load_file('./config.yml')
ENABLE_PRIVATE_CHANNEL = config['logger']['enable_private_channel']
ENABLE_DIRECT_MESSAGE = config['logger']['enable_direct_message']

class SlackLogger
attr_reader :client

def initialize
@client = Slack::Web::Client.new
end

def is_private_channel(channel_name)
channel_name[0] == 'G'
end
Expand All @@ -16,37 +20,27 @@ def is_direct_message(channel_name)
end

def update_users
users = Slack.users_list['members']
users = client.users_list['members']
replace_users(users)
end

def update_channels
channels = Slack.channels_list['channels']
channels = client.conversations_list({type: 'public_channel'})['channels']
replace_channels(channels)
end

def update_groups
groups = Slack.groups_list['groups']
replace_channels(groups)
end

def update_ims
ims = Slack.im_list['ims']
replace_ims(ims)
end

def update_emojis
emojis = Slack.emoji_list['emoji']
emojis = client.emoji_list['emoji'] rescue nil
hakatashi marked this conversation as resolved.
Show resolved Hide resolved
replace_emojis(emojis)
end

# log history messages
def fetch_history(target, channel)
messages = Slack.send(
messages = client.send(
target,
channel: channel,
count: 1000,
)['messages']
)['messages'] rescue nil
hakatashi marked this conversation as resolved.
Show resolved Hide resolved

unless messages.nil?
messages.each do |m|
Expand All @@ -58,13 +52,13 @@ def fetch_history(target, channel)

# realtime events
def log_realtime
realtime = Slack.realtime
realtime = Slack::RealTime::Client.new

realtime.on :message do |m|
if !ENABLE_PRIVATE_CHANNEL and is_private_channel(m['channel'])
if is_private_channel(m['channel'])
next
end
if !ENABLE_DIRECT_MESSAGE and is_direct_message(m['channel'])
if is_direct_message(m['channel'])
next
end

Expand Down Expand Up @@ -97,32 +91,13 @@ def log_realtime
update_emojis
end

if ENABLE_PRIVATE_CHANNEL
realtime.on :group_joined do |c|
puts "group has joined"
update_groups
end

realtime.on :group_rename do |c|
puts "group has renamed"
update_groups
end
end

if ENABLE_DIRECT_MESSAGE
realtime.on :im_created do |c|
puts "direct message has created"
update_ims
end
end

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

realtime.start
realtime.start!
hakatashi marked this conversation as resolved.
Show resolved Hide resolved
end

def start
Expand All @@ -134,26 +109,15 @@ def start
update_emojis
update_users
update_channels
update_groups if ENABLE_PRIVATE_CHANNEL
update_ims if ENABLE_DIRECT_MESSAGE

Channels.find.each do |c|
puts "loading messages from #{c[:name]}"
if c[:is_channel]
fetch_history(:channels_history, c[:id])
elsif c[:is_group] && ENABLE_PRIVATE_CHANNEL
fetch_history(:groups_history, c[:id])
fetch_history(:conversations_history, c[:id])
end
sleep(1)
end

if ENABLE_DIRECT_MESSAGE
Ims.find.each do |i|
fetch_history(:im_history, i[:id])
sleep(1)
end
end

# realtime event is joined and dont exit current thread
realtime_thread.join
ensure
Expand Down
3 changes: 2 additions & 1 deletion viewer/viewer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ def search(params)
get '/team.json' do
content_type :json
# TODO: cache in redis or mongodb or in memory?
Slack.team_info['team'].to_json
client = Slack::Web::Client.new
client.team_info['team'].to_json
end

post '/import_backup' do
Expand Down