From e2eddc8ee92829ea364f75c0b344e972a92d6091 Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Wed, 27 May 2020 20:39:10 +0200 Subject: [PATCH 1/4] Move {channel, group}_join to marco generation --- lib/slack/state.ex | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/slack/state.ex b/lib/slack/state.ex index e89d3bd..433ccdc 100644 --- a/lib/slack/state.ex +++ b/lib/slack/state.ex @@ -45,14 +45,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 @@ -98,6 +90,13 @@ defmodule Slack.State do put_in(slack, [unquote(plural_atom), channel, :is_archived], false) end + def update( + %{type: "message", subtype: unquote(type <> "_join"), channel: channel, user: user}, + slack + ) do + update_in(slack, [unquote(plural_atom), channel, :members], &Enum.uniq([user | &1])) + end + def update( %{type: "message", subtype: unquote(type <> "_leave"), channel: channel, user: user}, slack From 8bc12cfea4e2ac48c8c68554d7796729b3a9166f Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Wed, 27 May 2020 20:58:15 +0200 Subject: [PATCH 2/4] Make group channel handling safe --- lib/slack/state.ex | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/slack/state.ex b/lib/slack/state.ex index 433ccdc..6dd769c 100644 --- a/lib/slack/state.ex +++ b/lib/slack/state.ex @@ -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. """ @@ -79,29 +87,29 @@ 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 <> "_join"), channel: channel, user: user}, slack ) do - update_in(slack, [unquote(plural_atom), channel, :members], &Enum.uniq([user | &1])) + 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) From 4b0e359448343b7aa85e3537df4869a0831e0f0a Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Wed, 27 May 2020 21:18:54 +0200 Subject: [PATCH 3/4] Move {channel, group}_topic handle to macro --- lib/slack/state.ex | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/slack/state.ex b/lib/slack/state.ex index 6dd769c..fdca424 100644 --- a/lib/slack/state.ex +++ b/lib/slack/state.ex @@ -53,27 +53,6 @@ defmodule Slack.State do put_in(slack, [:groups, channel.id], channel) 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) @@ -98,6 +77,17 @@ defmodule Slack.State do 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 From 0d67523ec0bbcb0dbcd6b7c8cb325e2ab5e79298 Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Wed, 27 May 2020 21:49:56 +0200 Subject: [PATCH 4/4] Bump version --- README.md | 2 +- mix.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0181de2..4bbdb1c 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ def application do end def deps do - [{:slack, "~> 0.23.3"}] + [{:slack, "~> 0.23.4"}] end ``` diff --git a/mix.exs b/mix.exs index 809d70d..544e35c 100644 --- a/mix.exs +++ b/mix.exs @@ -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",