Browse Source

Upload emoji to Mattermost

master
Dylan Baker 4 years ago
parent
commit
68a763d872

+ 40
- 6
lib/slack_mattermost_emoji/mattermost_client.rb View File

4
 
4
 
5
 module SlackMattermostEmoji
5
 module SlackMattermostEmoji
6
   class MattermostClient
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
       @username = username
8
       @username = username
9
       @password = password
9
       @password = password
10
       @domain = domain
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
       @token = nil
13
       @token = nil
14
+      @log = log
13
 
15
 
14
       if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
16
       if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
15
         raise 'Mattermost username, password, and domain are all required'
17
         raise 'Mattermost username, password, and domain are all required'
24
           password: @password,
26
           password: @password,
25
         }),
27
         }),
26
         headers: {
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
       @token = response.headers['token']
37
       @token = response.headers['token']
36
     end
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
     def authenticated?
72
     def authenticated?
39
       !@token.nil?
73
       !@token.nil?
40
     end
74
     end

+ 3
- 3
lib/slack_mattermost_emoji/slack_client.rb View File

60
       body = @driver.find_element(tag_name: 'body')
60
       body = @driver.find_element(tag_name: 'body')
61
       data = JSON.parse(body.text)
61
       data = JSON.parse(body.text)
62
       emoji_count = data['emoji'].size
62
       emoji_count = data['emoji'].size
63
+      digits = emoji_count.to_s.size
63
 
64
 
64
       puts "found #{emoji_count} emojis" if @log
65
       puts "found #{emoji_count} emojis" if @log
65
 
66
 
66
       data['emoji'].each_with_index do |emoji, index|
67
       data['emoji'].each_with_index do |emoji, index|
68
+        next if URI(image_url).scheme == 'alias'
69
+
67
         name, image_url = emoji
70
         name, image_url = emoji
68
 
71
 
69
-        digits = emoji_count.to_s.size
70
         puts "#{(index + 1).to_s.rjust(digits, ' ')}/#{emoji_count} :#{name}:" if @log
72
         puts "#{(index + 1).to_s.rjust(digits, ' ')}/#{emoji_count} :#{name}:" if @log
71
 
73
 
72
-        next if URI(image_url).scheme == 'alias'
73
-
74
         extension = File.extname(image_url)
74
         extension = File.extname(image_url)
75
         file_name = "#{name}#{extension}"
75
         file_name = "#{name}#{extension}"
76
         file_path = File.join(@download_path, file_name)
76
         file_path = File.join(@download_path, file_name)

+ 21
- 0
spec/mattermost_client_spec.rb View File

31
     )
31
     )
32
     expect { client.authenticate }.to raise_error('Invalid Mattermost credentials')
32
     expect { client.authenticate }.to raise_error('Invalid Mattermost credentials')
33
   end
33
   end
34
+
35
+  it 'should upload emojis to mattermost' do
36
+    client = SlackMattermostEmoji::MattermostClient.new(
37
+      username: ENV['MATTERMOST_USERNAME'],
38
+      password: ENV['MATTERMOST_PASSWORD'],
39
+      domain: ENV['MATTERMOST_DOMAIN'],
40
+      emoji_path: '~/Desktop/slack-emoji',
41
+    )
42
+    token = client.authenticate
43
+    client.upload_emoji
44
+    url = "https://#{ENV['MATTERMOST_DOMAIN']}/api/v4/emoji"
45
+    response = HTTParty.get(url, {
46
+      headers: {
47
+        'Authorization': "Bearer #{token}",
48
+      }
49
+    })
50
+
51
+    emoji = JSON.parse(response.body).map { |e| e['name'] }
52
+    emoji_on_disk = Dir[File.expand_path('~/Desktop/slack-emoji/*')].map { |e| File.basename(e, '.*') }
53
+    expect(emoji).to eq(emoji_on_disk)
54
+  end
34
 end
55
 end

Loading…
Cancel
Save