grape-slack-bot 1.2.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/slack_bot/callback.rb +39 -13
- data/lib/slack_bot/command.rb +1 -2
- data/lib/slack_bot/grape_extension.rb +1 -1
- data/lib/slack_bot/interaction.rb +0 -4
- data/lib/slack_bot.rb +1 -1
- data/spec/slack_bot/callback_spec.rb +19 -30
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f8fc51cbef0f57cb132347ce71c02fba8ca5130749cca04ed3ffdbefb482ce1
|
4
|
+
data.tar.gz: a1a96768a308005a8bf8aa709c3c3a29f927c31a624c4ad9bfc6d7e60b9b01ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d372b748e6242a8766799b762afbfb205023491cb5c1fb6f453cc2454d389d89eecfb0e3f7257ef92b545b3215e579103c325dcc5ba0a40bb5a48f6ab4451d3
|
7
|
+
data.tar.gz: 2c72725ce9911a95c74ae015e6f24aa77eae793afd97b7c2d5c934598c3c9195c61bd386bdd40661b2ce3d89bd875e426ed926c3ec711f709090ed2e591ffaf8
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
# 1.5.0
|
2
|
+
|
3
|
+
* Complete upgrade of callback storage logic
|
4
|
+
|
5
|
+
# 1.4.0
|
6
|
+
|
7
|
+
* Allow setting callback expiration time on save and update
|
8
|
+
|
9
|
+
# 1.3.0
|
10
|
+
|
11
|
+
* Clean up callback arguments, remove unused `method_name`
|
12
|
+
|
1
13
|
# 1.2.3
|
2
14
|
|
3
15
|
* Minor fix for Events API
|
data/lib/slack_bot/callback.rb
CHANGED
@@ -4,6 +4,7 @@ require 'active_support/core_ext/numeric/time'
|
|
4
4
|
module SlackBot
|
5
5
|
class Callback
|
6
6
|
CALLBACK_CACHE_KEY = "slack-bot-callback".freeze
|
7
|
+
CALLBACK_RECORD_EXPIRES_IN = 15.minutes.freeze
|
7
8
|
|
8
9
|
def self.find(id, config: nil)
|
9
10
|
return if id.blank?
|
@@ -14,32 +15,33 @@ module SlackBot
|
|
14
15
|
nil
|
15
16
|
end
|
16
17
|
|
17
|
-
def self.create(class_name:,
|
18
|
+
def self.create(class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil)
|
18
19
|
callback =
|
19
|
-
new(class_name: class_name,
|
20
|
+
new(class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in)
|
20
21
|
callback.save
|
21
22
|
callback
|
22
23
|
end
|
23
24
|
|
24
|
-
attr_reader :id, :data, :args, :config
|
25
|
-
def initialize(id: nil, class_name: nil,
|
25
|
+
attr_reader :id, :data, :args, :config, :expires_in
|
26
|
+
def initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil)
|
26
27
|
@id = id
|
27
28
|
@data = {
|
28
29
|
class_name: class_name,
|
29
|
-
method_name: method_name,
|
30
30
|
user_id: user&.id,
|
31
31
|
channel_id: channel_id,
|
32
|
-
|
32
|
+
payload: payload
|
33
33
|
}
|
34
34
|
@args = SlackBot::Args.new
|
35
35
|
@config = config || SlackBot::Config.current_instance
|
36
|
+
@expires_in = expires_in || CALLBACK_RECORD_EXPIRES_IN
|
36
37
|
end
|
37
38
|
|
38
39
|
def reload
|
39
|
-
|
40
|
-
SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{
|
41
|
-
raise SlackBot::Errors::CallbackNotFound if
|
40
|
+
cached_data = read_data
|
41
|
+
SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{cached_data}")
|
42
|
+
raise SlackBot::Errors::CallbackNotFound if cached_data.nil?
|
42
43
|
|
44
|
+
@data = cached_data
|
43
45
|
parse_args
|
44
46
|
self
|
45
47
|
end
|
@@ -56,7 +58,12 @@ module SlackBot
|
|
56
58
|
return if id.blank?
|
57
59
|
return if data.blank?
|
58
60
|
|
59
|
-
@data
|
61
|
+
if @data[:payload].is_a?(Hash)
|
62
|
+
@data[:payload] = @data[:payload].merge(payload)
|
63
|
+
else
|
64
|
+
@data[:payload] = payload
|
65
|
+
end
|
66
|
+
|
60
67
|
save
|
61
68
|
end
|
62
69
|
|
@@ -73,6 +80,23 @@ module SlackBot
|
|
73
80
|
end
|
74
81
|
end
|
75
82
|
|
83
|
+
def user=(user)
|
84
|
+
@user = user
|
85
|
+
@data[:user_id] = user&.id
|
86
|
+
end
|
87
|
+
|
88
|
+
def class_name=(class_name)
|
89
|
+
@data[:class_name] = class_name
|
90
|
+
end
|
91
|
+
|
92
|
+
def channel_id=(channel_id)
|
93
|
+
@data[:channel_id] = channel_id
|
94
|
+
end
|
95
|
+
|
96
|
+
def payload=(payload)
|
97
|
+
@data[:payload] = payload
|
98
|
+
end
|
99
|
+
|
76
100
|
def handler_class
|
77
101
|
return if class_name.blank?
|
78
102
|
|
@@ -81,6 +105,7 @@ module SlackBot
|
|
81
105
|
|
82
106
|
def method_missing(method_name, *args, &block)
|
83
107
|
return data[method_name.to_sym] if data.key?(method_name.to_sym)
|
108
|
+
return data[:payload][method_name.to_s] if data[:payload].is_a?(Hash) && data[:payload].key?(method_name.to_s)
|
84
109
|
|
85
110
|
super
|
86
111
|
end
|
@@ -92,7 +117,7 @@ module SlackBot
|
|
92
117
|
end
|
93
118
|
|
94
119
|
def serialize_args
|
95
|
-
data[:args] = args.to_s
|
120
|
+
@data[:args] = args.to_s
|
96
121
|
end
|
97
122
|
|
98
123
|
def generate_id
|
@@ -103,8 +128,9 @@ module SlackBot
|
|
103
128
|
config.callback_storage_instance.read("#{CALLBACK_CACHE_KEY}:#{id}")
|
104
129
|
end
|
105
130
|
|
106
|
-
def write_data(data)
|
107
|
-
|
131
|
+
def write_data(data, expires_in: nil)
|
132
|
+
expires_in ||= CALLBACK_RECORD_EXPIRES_IN
|
133
|
+
config.callback_storage_instance.write("#{CALLBACK_CACHE_KEY}:#{id}", data, expires_in: expires_in)
|
108
134
|
end
|
109
135
|
|
110
136
|
def delete_data
|
data/lib/slack_bot/command.rb
CHANGED
@@ -48,7 +48,7 @@ module SlackBot
|
|
48
48
|
|
49
49
|
private
|
50
50
|
|
51
|
-
def open_modal(view_name,
|
51
|
+
def open_modal(view_name, context: nil)
|
52
52
|
view = self.class.view_klass.new(
|
53
53
|
args: args,
|
54
54
|
current_user: @current_user,
|
@@ -61,7 +61,6 @@ module SlackBot
|
|
61
61
|
trigger_id: params[:trigger_id],
|
62
62
|
channel_id: params[:channel_id],
|
63
63
|
class_name: self.class.name,
|
64
|
-
method_name: method_name,
|
65
64
|
user: @current_user,
|
66
65
|
payload: payload,
|
67
66
|
config: config
|
@@ -88,7 +88,7 @@ module SlackBot
|
|
88
88
|
callback = SlackBot::Callback.find(callback_id, config: config)
|
89
89
|
raise SlackBot::Errors::CallbackNotFound.new if callback.blank?
|
90
90
|
|
91
|
-
SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.
|
91
|
+
SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.payload} #{callback.user_id} #{user&.id}"
|
92
92
|
|
93
93
|
if callback.user_id != user.id
|
94
94
|
raise "Callback user is not equal to action user"
|
@@ -12,7 +12,6 @@ module SlackBot
|
|
12
12
|
trigger_id:,
|
13
13
|
payload:,
|
14
14
|
class_name:,
|
15
|
-
method_name:,
|
16
15
|
user:,
|
17
16
|
channel_id:,
|
18
17
|
config: nil
|
@@ -20,7 +19,6 @@ module SlackBot
|
|
20
19
|
callback =
|
21
20
|
Callback.create(
|
22
21
|
class_name: class_name,
|
23
|
-
method_name: method_name,
|
24
22
|
user: user,
|
25
23
|
channel_id: channel_id,
|
26
24
|
config: config
|
@@ -45,7 +43,6 @@ module SlackBot
|
|
45
43
|
view_id:,
|
46
44
|
payload:,
|
47
45
|
class_name: nil,
|
48
|
-
method_name: nil,
|
49
46
|
user: nil,
|
50
47
|
channel_id: nil,
|
51
48
|
config: nil
|
@@ -54,7 +51,6 @@ module SlackBot
|
|
54
51
|
callback ||=
|
55
52
|
Callback.create(
|
56
53
|
class_name: class_name,
|
57
|
-
method_name: method_name,
|
58
54
|
user: user,
|
59
55
|
channel_id: channel_id,
|
60
56
|
config: config
|
data/lib/slack_bot.rb
CHANGED
@@ -4,10 +4,9 @@ describe SlackBot::Callback do
|
|
4
4
|
subject(:callback) {
|
5
5
|
described_class.new(
|
6
6
|
class_name: "Test",
|
7
|
-
method_name: "test",
|
8
7
|
user: user,
|
9
8
|
channel_id: "test_channel_id",
|
10
|
-
|
9
|
+
payload: { test: "test" },
|
11
10
|
config: config
|
12
11
|
)
|
13
12
|
}
|
@@ -34,7 +33,7 @@ describe SlackBot::Callback do
|
|
34
33
|
end
|
35
34
|
|
36
35
|
context "when callback is found" do
|
37
|
-
let(:data) { { class_name: "Test",
|
36
|
+
let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: { test: "test" }, args: "" } }
|
38
37
|
|
39
38
|
it "returns callback" do
|
40
39
|
expect(find).to be_a(described_class)
|
@@ -43,8 +42,7 @@ describe SlackBot::Callback do
|
|
43
42
|
expect(find.user).to eq(user)
|
44
43
|
expect(find.user_id).to eq(1)
|
45
44
|
expect(find.channel_id).to eq("test_channel_id")
|
46
|
-
expect(find.
|
47
|
-
expect(find.extra).to eq({ test: "test" })
|
45
|
+
expect(find.payload).to eq({ test: "test" })
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
@@ -58,21 +56,20 @@ describe SlackBot::Callback do
|
|
58
56
|
end
|
59
57
|
|
60
58
|
describe ".create" do
|
61
|
-
subject(:create) { described_class.create(class_name: "Test",
|
59
|
+
subject(:create) { described_class.create(class_name: "Test", user: user, channel_id: "test_channel_id", config: config) }
|
62
60
|
|
63
61
|
before do
|
64
62
|
allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
|
65
63
|
allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
|
66
64
|
args: "",
|
67
65
|
class_name: "Test",
|
68
|
-
method_name: "test",
|
69
66
|
user_id: 1,
|
70
67
|
channel_id: "test_channel_id",
|
71
|
-
|
72
|
-
}, expires_in:
|
68
|
+
payload: nil
|
69
|
+
}, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
|
73
70
|
end
|
74
71
|
|
75
|
-
let(:data) { { class_name: "Test",
|
72
|
+
let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: nil } }
|
76
73
|
|
77
74
|
it "returns callback" do
|
78
75
|
expect(create).to be_a(described_class)
|
@@ -81,8 +78,7 @@ describe SlackBot::Callback do
|
|
81
78
|
expect(create.user).to eq(user)
|
82
79
|
expect(create.user_id).to eq(1)
|
83
80
|
expect(create.channel_id).to eq("test_channel_id")
|
84
|
-
expect(create.
|
85
|
-
expect(create.extra).to eq(nil)
|
81
|
+
expect(create.payload).to eq(nil)
|
86
82
|
end
|
87
83
|
end
|
88
84
|
|
@@ -94,10 +90,9 @@ describe SlackBot::Callback do
|
|
94
90
|
let(:data) {
|
95
91
|
{
|
96
92
|
class_name: "Test",
|
97
|
-
method_name: "test",
|
98
93
|
user_id: 1,
|
99
94
|
channel_id: "test_channel_id",
|
100
|
-
|
95
|
+
payload: { test: "test payload" },
|
101
96
|
args: ""
|
102
97
|
}
|
103
98
|
}
|
@@ -113,8 +108,7 @@ describe SlackBot::Callback do
|
|
113
108
|
expect(reload.user).to eq(user)
|
114
109
|
expect(reload.user_id).to eq(1)
|
115
110
|
expect(reload.channel_id).to eq("test_channel_id")
|
116
|
-
expect(reload.
|
117
|
-
expect(reload.extra).to eq({ test: "test" })
|
111
|
+
expect(reload.payload).to eq({ test: "test payload" })
|
118
112
|
end
|
119
113
|
|
120
114
|
context "when callback is not found" do
|
@@ -131,10 +125,9 @@ describe SlackBot::Callback do
|
|
131
125
|
let(:callback) {
|
132
126
|
described_class.new(
|
133
127
|
class_name: "Test",
|
134
|
-
method_name: "test",
|
135
128
|
user: user,
|
136
129
|
channel_id: "test_channel_id",
|
137
|
-
|
130
|
+
payload: { test: "test payload" },
|
138
131
|
config: config
|
139
132
|
)
|
140
133
|
}
|
@@ -144,15 +137,14 @@ describe SlackBot::Callback do
|
|
144
137
|
allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
|
145
138
|
args: "",
|
146
139
|
class_name: "Test",
|
147
|
-
method_name: "test",
|
148
140
|
user_id: 1,
|
149
141
|
channel_id: "test_channel_id",
|
150
|
-
|
151
|
-
}, expires_in:
|
142
|
+
payload: { test: "test payload" }
|
143
|
+
}, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
|
152
144
|
end
|
153
145
|
|
154
146
|
it "returns callback" do
|
155
|
-
|
147
|
+
save
|
156
148
|
expect(callback).to be_a(described_class)
|
157
149
|
expect(callback.id).to eq("test_callback_id")
|
158
150
|
expect(callback.args).to be_a(SlackBot::Args)
|
@@ -165,10 +157,9 @@ describe SlackBot::Callback do
|
|
165
157
|
described_class.new(
|
166
158
|
id: "test_callback_id",
|
167
159
|
class_name: "Test",
|
168
|
-
method_name: "test",
|
169
160
|
user: user,
|
170
161
|
channel_id: "test_channel_id",
|
171
|
-
|
162
|
+
payload: { test: "test payload" },
|
172
163
|
config: config
|
173
164
|
)
|
174
165
|
}
|
@@ -177,18 +168,16 @@ describe SlackBot::Callback do
|
|
177
168
|
allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
|
178
169
|
args: "",
|
179
170
|
class_name: "Test",
|
180
|
-
method_name: "test",
|
181
171
|
user_id: 1,
|
182
172
|
channel_id: "test_channel_id",
|
183
|
-
|
184
|
-
|
185
|
-
}, expires_in: 1.hour)
|
173
|
+
payload: { test: "test payload 2" }
|
174
|
+
}, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
|
186
175
|
end
|
187
176
|
|
188
|
-
let(:payload) { { test: "test" } }
|
177
|
+
let(:payload) { { test: "test payload 2" } }
|
189
178
|
|
190
179
|
it "returns callback" do
|
191
|
-
|
180
|
+
update
|
192
181
|
expect(callback).to be_a(described_class)
|
193
182
|
expect(callback.id).to eq("test_callback_id")
|
194
183
|
expect(callback.args).to be_a(SlackBot::Args)
|