immosquare-slack 0.2.1 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 382c2959d5d7d3869803f78d02a7247288a194045513777288caff5c2fa7abae
4
- data.tar.gz: 403787e3740082733bb3966bd58bde6ccb45d4e3768e8f06027c1470e66872d3
3
+ metadata.gz: 12ae386c41e3ec21e398b12d2ac347fe368d1372df757c2369333256bcee4e8b
4
+ data.tar.gz: d18fb89c4db393a1cc0adddbcb4a66444a38dfb31096ff60f2fb89bd4c5a1517
5
5
  SHA512:
6
- metadata.gz: c93db1d79831bfbaab04b9e00b8fba3236c5e0e5bd040207498d573c05897575de36ef6ec38ecfadfc35481c4469e0a1fd8dce97c674d846ec57ee9606f3c78d
7
- data.tar.gz: 233f3fe54d124c757f3c07369ca7a9d1e615d42b6c09d98d86e931d5dd98e22d7cd9abd9d042375d1a70b2c2aa9878ed79719ce11864a91ecf32e333b92d1c22
6
+ metadata.gz: 86cd275178aaaf100cb42df5eed83b76b1a4fe01a58229c16df81312923ad2f224f5316fed68c9110bc51c42eb17eaf9b886d333cf042e355dfa06b943c2fbcb
7
+ data.tar.gz: f4abd19d80848018fe73a01e137bf231ab4d1a6133d599e36642bde23b6097c9cc22cb03ce8b004181de8dbb4c860e5a9f58497398aa136f0dc19cee98837411
@@ -3,6 +3,7 @@ module ImmosquareSlack
3
3
  extend SharedMethods
4
4
 
5
5
  GENERAL_CHANNEL = "general".freeze
6
+ CHANNELS_LOCK = Mutex.new
6
7
 
7
8
  class << self
8
9
 
@@ -13,18 +14,24 @@ module ImmosquareSlack
13
14
  ##
14
15
  ## Piège : le cache ne reflète que l'état du workspace au
15
16
  ## premier appel. Un channel créé ou renommé ensuite n'y est
16
- ## pas. `force: true` vide le cache et refetch — à utiliser sur
17
- ## un miss de lookup pour distinguer un channel réellement
18
- ## absent d'un cache périmé avant tout fallback.
17
+ ## pas. `force: true` rafraîchit le cache sur un échec de
18
+ ## recherche. Les requêtes simultanées partagent le même
19
+ ## rafraîchissement pour préserver le quota de conversations.list.
20
+ ## Une erreur Slack laisse la dernière liste utilisable en cache.
19
21
  ##============================================================##
20
22
  def list_channels(force: false)
21
- @list_channels = nil if force
22
- @list_channels ||= begin
23
+ cached_channels = @list_channels
24
+ return cached_channels if cached_channels && !force
25
+
26
+ CHANNELS_LOCK.synchronize do
27
+ return @list_channels if @list_channels && !@list_channels.equal?(cached_channels)
28
+
23
29
  extra_params = {
24
30
  :types => ["public_channel", "private_channel"].join(","),
25
- :exclude_archived => false
31
+ :exclude_archived => false,
32
+ :limit => 200
26
33
  }
27
- fetch_paginated_data("https://slack.com/api/conversations.list", "channels", extra_params)
34
+ @list_channels = fetch_paginated_data("https://slack.com/api/conversations.list", "channels", extra_params)
28
35
  end
29
36
  end
30
37
 
@@ -40,6 +47,13 @@ module ImmosquareSlack
40
47
  ## If no channel can be resolved after fallback, raises an
41
48
  ## ArgumentError immediately (clearer than the cryptic error
42
49
  ## returned by the Slack API).
50
+ ##
51
+ ## Slack only notifies a mention to members of the channel the
52
+ ## message lands in. When `notify` is an Array of emails, the
53
+ ## mentioned users outside that channel (all of them when the
54
+ ## message falls back to the general channel) also receive it
55
+ ## as a direct message from the bot, unless
56
+ ## `direct_message_unreachable_mentions` is disabled.
43
57
  ##============================================================##
44
58
  def post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
45
59
  channel_name ||= ImmosquareSlack.configuration.default_channel
@@ -52,28 +66,22 @@ module ImmosquareSlack
52
66
  ## the memoized list predates its creation. Retry once on a
53
67
  ## freshly fetched list before declaring it missing.
54
68
  ##============================================================##
55
- channel_id = get_channel_id_by_name(channel_name)
56
- channel_id = get_channel_id_by_name(channel_name, :force => true) if channel_id.nil?
69
+ channel_id = get_channel_id_by_name(channel_name)
70
+ channel_id = get_channel_id_by_name(channel_name, :force => true) if channel_id.nil?
71
+ mentioned_members = notify.is_a?(Array) ? members_with_emails(notify) : []
57
72
 
58
73
  if channel_id.nil?
59
- text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}"
60
- return post_message(text, :channel_name => GENERAL_CHANNEL, :notify => :channel, :notify_text => "", :bot_name => bot_name, :notify_general_if_invalid_channel => false) if channel_name != GENERAL_CHANNEL && notify_general_if_invalid_channel
74
+ raise("channel '#{channel_name}' not found on slack") if channel_name == GENERAL_CHANNEL || !notify_general_if_invalid_channel
61
75
 
62
- raise("channel '#{channel_name}' not found on slack")
76
+ direct_message_unreachable_members(mentioned_members, nil, "Mentioned in *##{channel_name}* (channel not found):\n#{text}", bot_name)
77
+ text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}"
78
+ return post_message(text, :channel_name => GENERAL_CHANNEL, :notify => :channel, :notify_text => "", :bot_name => bot_name, :notify_general_if_invalid_channel => false)
63
79
  end
64
80
 
65
- url = "https://slack.com/api/chat.postMessage"
66
- notification_text = notify ? build_notification_text(channel_id, notify, *notify_text) : nil
67
- text = "#{notification_text}#{text}"
68
-
69
- body = {
70
- :channel => channel_id,
71
- :text => text
72
- }
73
-
74
- body[:username] = bot_name if bot_name
75
-
76
- make_slack_api_call(url, :method => :post, :body => body)
81
+ notification_text = notify ? build_notification_text(channel_id, notify, mentioned_members, *notify_text) : nil
82
+ response = send_message(channel_id, "#{notification_text}#{text}", bot_name)
83
+ direct_message_unreachable_members(mentioned_members, channel_id, "Mentioned in <##{channel_id}>:\n#{text}", bot_name)
84
+ response
77
85
  end
78
86
 
79
87
  private
@@ -102,6 +110,54 @@ module ImmosquareSlack
102
110
  fetch_paginated_data("https://slack.com/api/conversations.members", "members", {:channel => channel_id})
103
111
  end
104
112
 
113
+ ##============================================================##
114
+ ## Posts `text` to a conversation: a channel id, or a user id
115
+ ## for a direct message from the bot.
116
+ ##============================================================##
117
+ def send_message(conversation_id, text, bot_name)
118
+ body = {
119
+ :channel => conversation_id,
120
+ :text => text
121
+ }
122
+
123
+ body[:username] = bot_name if bot_name
124
+
125
+ make_slack_api_call("https://slack.com/api/chat.postMessage", :method => :post, :body => body)
126
+ end
127
+
128
+ ##============================================================##
129
+ ## Active workspace members whose `profile.email` is in
130
+ ## `emails`. Without the `users:read.email` scope no profile
131
+ ## carries an email, so nobody matches.
132
+ ##
133
+ ## Piège : `users.list` renvoie aussi les comptes désactivés,
134
+ ## qui gardent leur email mais ne peuvent recevoir aucun DM.
135
+ ##============================================================##
136
+ def members_with_emails(emails)
137
+ ImmosquareSlack::User.list_users.select {|m| !m["deleted"] && emails.include?(m.dig("profile", "email")) }
138
+ end
139
+
140
+ ##============================================================##
141
+ ## Sends `text` as a direct message to the mentioned members who
142
+ ## are not in the channel (`channel_id` nil: none of them is
143
+ ## reachable), since Slack would not notify them.
144
+ ##
145
+ ## Best effort : un DM refusé par Slack est ignoré. Le laisser
146
+ ## remonter ferait échouer `post_message` après le post dans le
147
+ ## channel (doublon au retry de l'appelant) ou avant le repli
148
+ ## sur general (message perdu).
149
+ ##============================================================##
150
+ def direct_message_unreachable_members(mentioned_members, channel_id, text, bot_name)
151
+ return if mentioned_members.empty? || !ImmosquareSlack.configuration.direct_message_unreachable_mentions
152
+
153
+ channel_member_ids = channel_id ? get_channel_members(channel_id) : []
154
+ mentioned_members.reject {|m| channel_member_ids.include?(m["id"]) }.each do |member|
155
+ send_message(member["id"], text, bot_name)
156
+ rescue RuntimeError
157
+ next
158
+ end
159
+ end
160
+
105
161
  ##============================================================##
106
162
  ## Builds the notification prefix prepended to the message
107
163
  ## body. `notify` may be:
@@ -112,12 +168,10 @@ module ImmosquareSlack
112
168
  ## Note: avoids `in?` and `present?` so the gem stays usable
113
169
  ## from plain Ruby (no Rails / ActiveSupport required).
114
170
  ##============================================================##
115
- def build_notification_text(channel_id, notify, text = "Hello")
171
+ def build_notification_text(channel_id, notify, mentioned_members, text = "Hello")
116
172
  final =
117
173
  if notify.is_a?(Array)
118
- members = ImmosquareSlack::User.list_users
119
- members = members.select {|m| notify.include?(m["profile"]["email"]) }
120
- members.map {|m| "<@#{m["id"]}>" }.join(", ")
174
+ mentioned_members.map {|m| "<@#{m["id"]}>" }.join(", ")
121
175
  elsif notify.to_sym == :all
122
176
  members = get_channel_members(channel_id)
123
177
  members.map {|m| "<@#{m}>" }.join(", ")
@@ -1,12 +1,13 @@
1
1
  module ImmosquareSlack
2
2
  class Configuration
3
3
 
4
- attr_accessor :slack_api_token_bot, :default_channel, :default_bot_name
4
+ attr_accessor :slack_api_token_bot, :default_channel, :default_bot_name, :direct_message_unreachable_mentions
5
5
 
6
6
  def initialize
7
- @slack_api_token_bot = nil
8
- @default_channel = nil
9
- @default_bot_name = nil
7
+ @slack_api_token_bot = nil
8
+ @default_channel = nil
9
+ @default_bot_name = nil
10
+ @direct_message_unreachable_mentions = true
10
11
  end
11
12
 
12
13
  end
@@ -1,6 +1,7 @@
1
1
  module ImmosquareSlack
2
2
  module User
3
3
  extend SharedMethods
4
+
4
5
  class << self
5
6
 
6
7
 
@@ -1,3 +1,3 @@
1
1
  module ImmosquareSlack
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
@@ -4,15 +4,15 @@ require_relative "immosquare-slack/channel"
4
4
  require_relative "immosquare-slack/user"
5
5
 
6
6
 
7
- ##===========================================================================##
8
- ##
9
- ##===========================================================================##
7
+ ##============================================================##
8
+ ## ...
9
+ ##============================================================##
10
10
  module ImmosquareSlack
11
11
  class << self
12
12
 
13
- ##===========================================================================##
13
+ ##============================================================##
14
14
  ## Gem configuration
15
- ##===========================================================================##
15
+ ##============================================================##
16
16
  attr_writer :configuration
17
17
 
18
18
  def configuration
metadata CHANGED
@@ -1,65 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
- - immosquare
7
+ - immosquare
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: httparty
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '0'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '0'
26
- description: ImmosquareSlack is a Ruby gem that provides an easy and efficient way
27
- to integrate Ruby applications with Slack. It leverages the Slack API to enable
28
- functionalities such as posting messages, managing channels, and other Slack-related
29
- operations. Designed for simplicity and ease of use, this gem is ideal for developers
30
- looking to enhance their Ruby applications with Slack's communication capabilities.
12
+ - !ruby/object:Gem::Dependency
13
+ name: httparty
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ description: "ImmosquareSlack is a Ruby gem that provides an easy and efficient way to integrate Ruby applications with Slack. It leverages the Slack API to enable functionalities such as posting messages, managing channels, and other Slack-related operations. Designed for simplicity and ease of use, this gem is ideal for developers looking to enhance their Ruby applications with Slack's communication capabilities."
31
29
  email:
32
- - jules@immosquare.com
30
+ - jules@immosquare.com
33
31
  executables: []
34
32
  extensions: []
35
33
  extra_rdoc_files: []
36
34
  files:
37
- - lib/immosquare-slack.rb
38
- - lib/immosquare-slack/channel.rb
39
- - lib/immosquare-slack/configuration.rb
40
- - lib/immosquare-slack/shared_methods.rb
41
- - lib/immosquare-slack/user.rb
42
- - lib/immosquare-slack/version.rb
43
- homepage: https://github.com/immosquare/immosquare-slack
35
+ - lib/immosquare-slack.rb
36
+ - lib/immosquare-slack/channel.rb
37
+ - lib/immosquare-slack/configuration.rb
38
+ - lib/immosquare-slack/shared_methods.rb
39
+ - lib/immosquare-slack/user.rb
40
+ - lib/immosquare-slack/version.rb
41
+ homepage: "https://github.com/immosquare/immosquare-slack"
44
42
  licenses:
45
- - MIT
43
+ - MIT
46
44
  metadata: {}
47
45
  rdoc_options: []
48
46
  require_paths:
49
- - lib
47
+ - lib
50
48
  required_ruby_version: !ruby/object:Gem::Requirement
51
49
  requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: 3.2.6
50
+ -
51
+ - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.6
55
54
  required_rubygems_version: !ruby/object:Gem::Requirement
56
55
  requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: '0'
56
+ -
57
+ - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
60
  requirements: []
61
- rubygems_version: 4.0.12
61
+ rubygems_version: 4.1.0
62
62
  specification_version: 4
63
- summary: A Ruby gem for integrating with Slack API to perform various actions like
64
- posting messages.
63
+ summary: A Ruby gem for integrating with Slack API to perform various actions like posting messages.
65
64
  test_files: []