Skip to content

Commit

Permalink
Merge pull request #224 from entertainyou/master
Browse files Browse the repository at this point in the history
Fix crash of put_in/update_in
  • Loading branch information
acconrad committed May 27, 2020
2 parents a43c7b7 + 0d67523 commit a288ef6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def application do
end

def deps do
[{:slack, "~> 0.23.3"}]
[{:slack, "~> 0.23.4"}]
end
```

Expand Down
63 changes: 30 additions & 33 deletions lib/slack/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ defmodule Slack.State do
ims: %{}
]

defp safe_map_getter(key) do
Access.key(key, %{})
end

defp safe_list_getter(key) do
Access.key(key, [])
end

@doc """
Pattern matches against messages and returns updated Slack state.
"""
Expand All @@ -45,35 +53,6 @@ defmodule Slack.State do
put_in(slack, [:groups, channel.id], channel)
end

def update(%{type: "message", subtype: "channel_join", channel: channel, user: user}, slack) do
put_in(slack, [:channels, channel, :members], [user | slack[:channels][channel][:members]])
end

def update(%{type: "message", subtype: "group_join", channel: channel, user: user}, slack) do
update_in(slack, [:groups, channel, :members], &Enum.uniq([user | &1]))
end

def update(
%{type: "message", subtype: "channel_topic", channel: channel, user: user, topic: topic},
slack
) do
put_in(slack, [:channels, channel, :topic], %{
creator: user,
last_set: System.system_time(:second),
value: topic
})
end

def update(
%{type: "message", subtype: "group_topic", channel: channel, user: user, topic: topic},
slack
) do
put_in(slack, [:groups, channel, :topic], %{
creator: user,
last_set: System.system_time(:second),
value: topic
})
end

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

def update(%{type: unquote(type <> "_rename"), channel: channel}, slack) do
put_in(slack, [unquote(plural_atom), channel.id, :name], channel.name)
put_in(slack, [unquote(plural_atom), safe_map_getter(channel.id), :name], channel.name)
end

def update(%{type: unquote(type <> "_archive"), channel: channel}, slack) do
put_in(slack, [unquote(plural_atom), channel, :is_archived], true)
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :is_archived], true)
end

def update(%{type: unquote(type <> "_unarchive"), channel: channel}, slack) do
put_in(slack, [unquote(plural_atom), channel, :is_archived], false)
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :is_archived], false)
end

def update(
%{type: "message", subtype: unquote(type <> "_topic"), channel: channel, user: user, topic: topic},
slack
) do
put_in(slack, [unquote(plural_atom), safe_map_getter(channel), :topic], %{
creator: user,
last_set: System.system_time(:second),
value: topic
})
end

def update(
%{type: "message", subtype: unquote(type <> "_join"), channel: channel, user: user},
slack
) do
update_in(slack, [unquote(plural_atom), safe_map_getter(channel), safe_list_getter(:members)], &Enum.uniq([user | &1]))
end

def update(
%{type: "message", subtype: unquote(type <> "_leave"), channel: channel, user: user},
slack
) do
update_in(slack, [unquote(plural_atom), channel, :members], &(&1 -- [user]))
update_in(slack, [unquote(plural_atom), safe_map_getter(channel), safe_list_getter(:members)], &(&1 -- [user]))
end
end)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Slack.Mixfile do
def project do
[
app: :slack,
version: "0.23.3",
version: "0.23.4",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
name: "Slack",
Expand Down

0 comments on commit a288ef6

Please sign in to comment.