-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathstate.ex
More file actions
157 lines (129 loc) · 4.14 KB
/
state.ex
File metadata and controls
157 lines (129 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
defmodule Slack.State do
@moduledoc """
Slack state.
"""
@behaviour Access
def fetch(client, key)
defdelegate fetch(client, key), to: Map
def get(client, key, default)
defdelegate get(client, key, default), to: Map
def get_and_update(client, key, function)
defdelegate get_and_update(client, key, function), to: Map
def pop(client, key)
defdelegate pop(client, key), to: Map
defstruct [
:process,
:client,
:token,
:me,
:team,
bots: %{},
channels: %{},
groups: %{},
users: %{},
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.
"""
@spec update(Map, Map) :: {Symbol, Map}
def update(%{type: "channel_created", channel: channel}, slack) do
put_in(slack, [:channels, channel.id], channel)
end
def update(%{type: "channel_joined", channel: channel}, slack) do
slack
|> put_in([:channels, channel.id], channel)
|> put_in([:channels, channel.id, :is_member], true)
end
def update(%{type: "group_joined", channel: channel}, slack) do
put_in(slack, [:groups, channel.id], channel)
end
def update(%{type: "channel_left", channel: channel_id}, slack) do
put_in(slack, [:channels, channel_id, :is_member], false)
end
def update(%{type: "group_left", channel: channel}, slack) do
update_in(slack, [:groups], &Map.delete(&1, channel))
end
Enum.map(["channel", "group"], fn type ->
plural_atom = String.to_atom(type <> "s")
def update(%{type: unquote(type <> "_rename"), channel: channel}, slack) do
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), safe_map_getter(channel), :is_archived], true)
end
def update(%{type: unquote(type <> "_unarchive"), channel: channel}, slack) 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
) 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), safe_map_getter(channel), safe_list_getter(:members)],
&(&1 -- [user])
)
end
end)
def update(%{type: "team_rename", name: name}, slack) do
put_in(slack, [:team, :name], name)
end
def update(%{type: "presence_change", user: user, presence: presence}, slack) do
put_in(slack, [:users, user, :presence], presence)
end
def update(%{type: "presence_change", users: users, presence: presence}, slack) do
Enum.reduce(users, slack, fn user, acc ->
put_in(acc, [:users, user, :presence], presence)
end)
end
def update(%{type: "team_join", user: user}, slack) do
put_in(slack, [:users, user.id], user)
end
def update(%{type: "user_change", user: user}, slack) do
update_in(slack, [:users, Access.key(user.id, %{})], &Map.merge(&1, user))
end
Enum.map(["bot_added", "bot_changed"], fn type ->
def update(%{type: unquote(type), bot: bot}, slack) do
put_in(slack, [:bots, bot.id], bot)
end
end)
def update(%{type: "im_created", channel: channel}, slack) do
put_in(slack, [:ims, channel.id], channel)
end
def update(%{type: _type}, slack) do
slack
end
end