Skip to content
Permalink
Browse files

Adding support for looking up user names for user_ids that start with…

… 'W'.
  • Loading branch information...
jameskbride committed Mar 30, 2019
1 parent 0f4c7b5 commit 7890b9ca0d753d8392062fe9353757b245207033
Showing with 34 additions and 3 deletions.
  1. +18 −1 lib/slack/lookups.ex
  2. +16 −2 test/slack/lookups_test.exs
@@ -3,6 +3,9 @@ defmodule Slack.Lookups do

@doc ~S"""
Turns a string like `"@USER_NAME"` into the ID that Slack understands (`"U…"`).
NOTE: Referencing `"@USER_NAME"` is deprecated, and should not be used.
For more information see https://api.slack.com/changelog/2017-09-the-one-about-usernames
"""
def lookup_user_id("@" <> user_name, slack) do
slack.users
@@ -17,6 +20,9 @@ defmodule Slack.Lookups do
Turns a string like `"@USER_NAME"` or a user ID (`"U…"`) into the ID for the
direct message channel of that user (`"D…"`). `nil` is returned if a direct
message channel has not yet been opened.
NOTE: Referencing `"@USER_NAME"` is deprecated, and should not be used.
For more information see https://api.slack.com/changelog/2017-09-the-one-about-usernames
"""
def lookup_direct_message_id(user = "@" <> _user_name, slack) do
user
@@ -47,13 +53,20 @@ defmodule Slack.Lookups do
@doc ~S"""
Turns a Slack user ID (`"U…"`), direct message ID (`"D…"`), or bot ID (`"B…"`)
into a string in the format "@USER_NAME".
NOTE: Referencing `"@USER_NAME"` is deprecated, and should not be used.
For more information see https://api.slack.com/changelog/2017-09-the-one-about-usernames
"""
def lookup_user_name(direct_message_id = "D" <> _id, slack) do
lookup_user_name(slack.ims[direct_message_id].user, slack)
end

def lookup_user_name(user_id = "U" <> _id, slack) do
"@" <> slack.users[user_id].name
find_user_by_name(user_id, slack)
end

def lookup_user_name(user_id = "W" <> _id, slack) do
find_user_by_name(user_id, slack)
end

def lookup_user_name(bot_id = "B" <> _id, slack) do
@@ -77,4 +90,8 @@ defmodule Slack.Lookups do
defp find_channel_by_name(nested_map, name) do
Enum.find_value(nested_map, fn {_id, map} -> if map.name == name, do: map, else: nil end)
end

defp find_user_by_name(user_id, slack) do
"@" <> slack.users[user_id].name
end
end
@@ -46,12 +46,17 @@ defmodule Slack.LookupsTest do
assert Lookups.lookup_channel_id("#unknown", slack) == nil
end

test "turns a user identifier into @user" do
test "turns a user identifier into @user (Uxxxx)" do
slack = %{users: %{"U123" => %{name: "user", id: "U123", profile: %{display_name: "user"}}}}
assert Lookups.lookup_user_name("U123", slack) == "@user"
end

test "turns a direct message identifier into @user" do
test "turns a user identifier into @user (Wxxxx)" do
slack = %{users: %{"W123" => %{name: "user", id: "W123", profile: %{display_name: "user"}}}}
assert Lookups.lookup_user_name("W123", slack) == "@user"
end

test "turns a direct message identifier into @user (Uxxxx)" do
slack = %{
users: %{"U123" => %{name: "user", id: "U123", profile: %{display_name: "user"}}},
ims: %{"D789" => %{user: "U123", id: "D789"}}
@@ -60,6 +65,15 @@ defmodule Slack.LookupsTest do
assert Lookups.lookup_user_name("D789", slack) == "@user"
end

test "turns a direct message identifier into @user (Wxxxx)" do
slack = %{
users: %{"W123" => %{name: "user", id: "W123", profile: %{display_name: "user"}}},
ims: %{"D789" => %{user: "W123", id: "D789"}}
}

assert Lookups.lookup_user_name("D789", slack) == "@user"
end

test "turns a channel identifier into #channel" do
slack = %{channels: %{"C456" => %{name: "channel", id: "C456"}}}
assert Lookups.lookup_channel_name("C456", slack) == "#channel"

0 comments on commit 7890b9c

Please sign in to comment.
You can’t perform that action at this time.