|
@@ -4,12 +4,14 @@ require 'json'
|
4
|
4
|
|
5
|
5
|
module SlackMattermostEmoji
|
6
|
6
|
class MattermostClient
|
7
|
|
- def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
|
|
7
|
+ def initialize(username:, password:, domain:, emoji_path: nil, headless: true, log: true)
|
8
|
8
|
@username = username
|
9
|
9
|
@password = password
|
10
|
10
|
@domain = domain
|
11
|
|
- @download_path = File.expand_path(download_path || './slack-emojis')
|
|
11
|
+ @emoji_path = File.expand_path(emoji_path || './slack-emojis')
|
|
12
|
+ @user_id = nil
|
12
|
13
|
@token = nil
|
|
14
|
+ @log = log
|
13
|
15
|
|
14
|
16
|
if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
|
15
|
17
|
raise 'Mattermost username, password, and domain are all required'
|
|
@@ -24,17 +26,49 @@ module SlackMattermostEmoji
|
24
|
26
|
password: @password,
|
25
|
27
|
}),
|
26
|
28
|
headers: {
|
27
|
|
- 'Content-Type' => 'application/json'
|
|
29
|
+ 'Content-Type' => 'application/json',
|
28
|
30
|
}
|
29
|
31
|
})
|
30
|
32
|
|
31
|
|
- if response.code == 401
|
32
|
|
- raise 'Invalid Mattermost credentials'
|
33
|
|
- end
|
|
33
|
+ raise 'Invalid Mattermost credentials' if response.code == 401
|
34
|
34
|
|
|
35
|
+ user = JSON.parse(response.body)
|
|
36
|
+ @user_id = user['id']
|
35
|
37
|
@token = response.headers['token']
|
36
|
38
|
end
|
37
|
39
|
|
|
40
|
+ def upload_emoji
|
|
41
|
+ emoji = Dir["#{@emoji_path}/*"]
|
|
42
|
+ emoji_count = emoji.size
|
|
43
|
+ digits = emoji_count.to_s.size
|
|
44
|
+
|
|
45
|
+ puts "Uploading #{emoji_count} emoji... to #{@domain}"
|
|
46
|
+
|
|
47
|
+ emoji.each_with_index do |file_path, index|
|
|
48
|
+ emoji_name = File.basename(file_path, '.*')
|
|
49
|
+ index = (index + 1).to_s.rjust(digits, ' ')
|
|
50
|
+ print "#{index}/#{emoji_count} :#{emoji_name}:... " if @log
|
|
51
|
+
|
|
52
|
+ url = "https://#{@domain}/api/v4/emoji"
|
|
53
|
+ HTTParty.post(url, {
|
|
54
|
+ multipart: true,
|
|
55
|
+ body: {
|
|
56
|
+ emoji: JSON.generate({
|
|
57
|
+ creator_id: @user_id,
|
|
58
|
+ name: emoji_name,
|
|
59
|
+ }),
|
|
60
|
+ image: File.open(file_path),
|
|
61
|
+ },
|
|
62
|
+ headers: {
|
|
63
|
+ 'Content-Type' => 'multipart/form-data',
|
|
64
|
+ 'Authorization': "Bearer #{@token}",
|
|
65
|
+ }
|
|
66
|
+ })
|
|
67
|
+
|
|
68
|
+ puts 'success' if @log
|
|
69
|
+ end
|
|
70
|
+ end
|
|
71
|
+
|
38
|
72
|
def authenticated?
|
39
|
73
|
!@token.nil?
|
40
|
74
|
end
|