-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
477 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Api | ||
module Endpoints | ||
class DataEndpoint < Grape::API | ||
format :binary | ||
helpers Api::Helpers::AuthHelpers | ||
|
||
namespace :data do | ||
desc 'Get data.' | ||
params do | ||
requires :team_id, type: String, desc: 'Required team ID.' | ||
end | ||
get do | ||
team = Team.find(_id: params[:team_id]) || error!('Team Not Found', 404) | ||
|
||
authorize_short_lived_token! team | ||
|
||
path = File.join(Dir.tmpdir, 'slack-sup2', team.id) | ||
filename = team.export_filename(path) | ||
|
||
if !File.exist?(filename) || (File.mtime(filename) + 1.hour < Time.now) | ||
FileUtils.rm_rf(path) | ||
FileUtils.makedirs(path) | ||
Api::Middleware.logger.info "Generating data file for #{team}." | ||
filename = team.export_zip!(path) | ||
end | ||
|
||
Api::Middleware.logger.info "Sending #{ByteSize.new(File.size(filename))} data file for #{team}." | ||
content_type 'application/zip' | ||
header['Content-Disposition'] = "attachment; filename=#{File.basename(filename)}" | ||
File.binread filename | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module SlackSup | ||
module Commands | ||
class Data < SlackRubyBot::Commands::Base | ||
include SlackSup::Commands::Mixins::Subscribe | ||
|
||
subscribe_command 'data' do |client, data, _match| | ||
user = ::User.find_create_or_update_by_slack_id!(client, data.user) | ||
raise SlackSup::Error, "Sorry, only #{user.team.team_admins_slack_mentions} can download data." unless user.team_admin? | ||
|
||
dm = client.owner.slack_client.conversations_open(users: data.user) | ||
client.owner.slack_client.chat_postMessage( | ||
channel: dm.channel.id, | ||
as_user: true, | ||
text: 'Click here to download your team data.', | ||
attachments: [ | ||
{ | ||
text: '', | ||
attachment_type: 'default', | ||
actions: [ | ||
{ | ||
type: 'button', | ||
text: 'Download', | ||
url: "#{SlackRubyBotServer::Service.url}/api/data?team_id=#{client.owner.id}&access_token=#{CGI.escape(client.owner.short_lived_token)}" | ||
} | ||
] | ||
} | ||
] | ||
) | ||
|
||
client.say(channel: data.channel, text: "Hey #{user.slack_mention}, check your DMs for a link.") unless data.channel[0] == 'D' | ||
logger.info "DATA: #{data.team}, user=#{data.user}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require_relative 'mixins/export' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module SlackSup | ||
module Models | ||
module Mixins | ||
module Export | ||
extend ActiveSupport::Concern | ||
|
||
def export_filename(root, name) | ||
data_path = File.join(root, name) | ||
File.join(data_path, "#{name}.zip") | ||
end | ||
|
||
def export_zip!(root, name) | ||
data_path = File.join(root, name) | ||
data_zip = File.join(data_path, "#{name}.zip") | ||
export!(data_path) | ||
Zip::File.open(data_zip, create: true) do |zipfile| | ||
Dir.glob("#{data_path}/**/*").reject { |fn| File.directory?(fn) }.each do |file| | ||
zipfile.add(file.sub(data_path + '/', ''), file) | ||
end | ||
end | ||
data_zip | ||
end | ||
|
||
def export!(root, name = self.class.name, presenter = nil, coll = nil) | ||
presenter ||= Object.const_get("Api::Presenters::#{self.class.name}Presenter") | ||
keys = presenter.representable_attrs.keys - ['links'] | ||
data = coll || Array(self) | ||
FileUtils.makedirs(root) | ||
CSV.open(File.join(root, "#{name.downcase}.csv"), 'w', write_headers: true, headers: keys) do |csv| | ||
data.each do |entry| | ||
row = presenter.represent(entry).to_hash | ||
row.merge!(row['_embedded']) if row.key?('_embedded') | ||
csv << keys.map do |key| | ||
value = row[key] | ||
case value | ||
when Hash | ||
value.map do |k, v| | ||
"#{k}=#{v}" | ||
end.join("\n") | ||
when Array | ||
value.map do |v| | ||
v["#{key.singularize}_name"] || v['user_name'] # HACK: assume user id | ||
end.join("\n") | ||
else | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.