Skip to content

Commit a288ef6

Browse files
authored
Merge pull request #224 from entertainyou/master
Fix crash of put_in/update_in
2 parents a43c7b7 + 0d67523 commit a288ef6

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def application do
2424
end
2525

2626
def deps do
27-
[{:slack, "~> 0.23.3"}]
27+
[{:slack, "~> 0.23.4"}]
2828
end
2929
```
3030

lib/slack/state.ex

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ defmodule Slack.State do
2727
ims: %{}
2828
]
2929

30+
defp safe_map_getter(key) do
31+
Access.key(key, %{})
32+
end
33+
34+
defp safe_list_getter(key) do
35+
Access.key(key, [])
36+
end
37+
3038
@doc """
3139
Pattern matches against messages and returns updated Slack state.
3240
"""
@@ -45,35 +53,6 @@ defmodule Slack.State do
4553
put_in(slack, [:groups, channel.id], channel)
4654
end
4755

48-
def update(%{type: "message", subtype: "channel_join", channel: channel, user: user}, slack) do
49-
put_in(slack, [:channels, channel, :members], [user | slack[:channels][channel][:members]])
50-
end
51-
52-
def update(%{type: "message", subtype: "group_join", channel: channel, user: user}, slack) do
53-
update_in(slack, [:groups, channel, :members], &Enum.uniq([user | &1]))
54-
end
55-
56-
def update(
57-
%{type: "message", subtype: "channel_topic", channel: channel, user: user, topic: topic},
58-
slack
59-
) do
60-
put_in(slack, [:channels, channel, :topic], %{
61-
creator: user,
62-
last_set: System.system_time(:second),
63-
value: topic
64-
})
65-
end
66-
67-
def update(
68-
%{type: "message", subtype: "group_topic", channel: channel, user: user, topic: topic},
69-
slack
70-
) do
71-
put_in(slack, [:groups, channel, :topic], %{
72-
creator: user,
73-
last_set: System.system_time(:second),
74-
value: topic
75-
})
76-
end
7756

7857
def update(%{type: "channel_left", channel: channel_id}, slack) do
7958
put_in(slack, [:channels, channel_id, :is_member], false)
@@ -87,22 +66,40 @@ defmodule Slack.State do
8766
plural_atom = String.to_atom(type <> "s")
8867

8968
def update(%{type: unquote(type <> "_rename"), channel: channel}, slack) do
90-
put_in(slack, [unquote(plural_atom), channel.id, :name], channel.name)
69+
put_in(slack, [unquote(plural_atom), safe_map_getter(channel.id), :name], channel.name)
9170
end
9271

9372
def update(%{type: unquote(type <> "_archive"), channel: channel}, slack) do
94-
put_in(slack, [unquote(plural_atom), channel, :is_archived], true)
73+
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :is_archived], true)
9574
end
9675

9776
def update(%{type: unquote(type <> "_unarchive"), channel: channel}, slack) do
98-
put_in(slack, [unquote(plural_atom), channel, :is_archived], false)
77+
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :is_archived], false)
78+
end
79+
80+
def update(
81+
%{type: "message", subtype: unquote(type <> "_topic"), channel: channel, user: user, topic: topic},
82+
slack
83+
) do
84+
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :topic], %{
85+
creator: user,
86+
last_set: System.system_time(:second),
87+
value: topic
88+
})
89+
end
90+
91+
def update(
92+
%{type: "message", subtype: unquote(type <> "_join"), channel: channel, user: user},
93+
slack
94+
) do
95+
update_in(slack, [unquote(plural_atom), safe_map_getter(channel), safe_list_getter(:members)], &Enum.uniq([user | &1]))
9996
end
10097

10198
def update(
10299
%{type: "message", subtype: unquote(type <> "_leave"), channel: channel, user: user},
103100
slack
104101
) do
105-
update_in(slack, [unquote(plural_atom), channel, :members], &(&1 -- [user]))
102+
update_in(slack, [unquote(plural_atom), safe_map_getter(channel), safe_list_getter(:members)], &(&1 -- [user]))
106103
end
107104
end)
108105

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Slack.Mixfile do
44
def project do
55
[
66
app: :slack,
7-
version: "0.23.3",
7+
version: "0.23.4",
88
elixir: "~> 1.7",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
name: "Slack",

0 commit comments

Comments
 (0)