-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
pub_sub.ex
220 lines (182 loc) · 6.58 KB
/
pub_sub.ex
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
defmodule Ash.Notifier.PubSub do
@publish %Ash.Dsl.Entity{
name: :publish,
target: Ash.Notifier.PubSub.Publication,
describe: """
Configure a given action to publish its results over a given topic.
If you have multiple actions with the same name (only possible if they have different types),
use the `type` option, to specify which type you are referring to. Otherwise the message will
be broadcast for all actions with that name.
To include attribute values of the resource in the message, pass a list
of strings and attribute names. They will ultimately be joined with `:`.
For example:
```elixir
prefix "user"
publish :create, ["created", :user_id]
```
This might publish a message to \"user:created:1\"" for example.
For updates, if the field in the template is being changed, a message is sent
to *both* values. So if you change `user 1` to `user 2`, the same message would
be published to `user:updated:1` and `user:updated:2`. If there are multiple
attributes in the template, and they are all being changed, a message is sent for
every combination of substitutions.
## Template parts
Templates may contain lists, in which case all combinations of values in the list will be used. Add
`nil` to the list if you want to produce a pattern where that entry is ommitted.
The atom `:_tenant` may be used. If the changeset has a tenant set on it, that
value will be used, otherwise that combination of values is ignored. For example:
The atom `:_skip` may be used. It only makes sense to use it in the context of a list of alternatives,
and adds a pattern where that part is skipped.
```elixir
publish :updated, [[:team_id, :_tenant], "updated", [:id, nil]]
```
Would produce the following messages, given a `team_id` of 1, a `tenant` of `org_1`, and an `id` of `50`:
```elixir
"1:updated:50"
"1:updated"
"org_1:updated:50"
"org_1:updated"
```
""",
examples: [
"publish :create, \"created\"",
"""
publish :assign, "assigned"
"""
],
schema: Ash.Notifier.PubSub.Publication.schema(),
args: [:action, :topic]
}
@publish_all %Ash.Dsl.Entity{
name: :publish_all,
target: Ash.Notifier.PubSub.Publication,
describe: """
Works just like `publish`, except that it takes a type
and publishes all actions of that type
""",
examples: [
"publish_all :create, \"created\""
],
schema: Ash.Notifier.PubSub.Publication.publish_all_schema(),
args: [:type, :topic]
}
@pub_sub %Ash.Dsl.Section{
name: :pub_sub,
describe: """
A section for configuring how resource actions are published over pubsub
""",
examples: [
"""
pub_sub do
module MyEndpoint
prefix "post"
publish :destroy, ["foo", :id]
publish :update, ["bar", :name] event: "name_change"
publish_all :create, "created"
end
"""
],
entities: [
@publish,
@publish_all
],
modules: [:module],
schema: [
module: [
type: :atom,
doc: "The module to call `broadcast/3` on e.g module.broadcast(topic, event, message).",
required: true
],
prefix: [
type: :string,
doc:
"A prefix for all pubsub messages, e.g `users`. A message with `created` would be published as `users:created`"
]
]
}
@sections [@pub_sub]
@moduledoc """
A pubsub notifier extension
# Table of Contents
#{Ash.Dsl.Extension.doc_index(@sections)}
#{Ash.Dsl.Extension.doc(@sections)}
"""
use Ash.Dsl.Extension, sections: @sections
def publications(resource) do
Ash.Dsl.Extension.get_entities(resource, [:pub_sub])
end
def module(resource) do
Ash.Dsl.Extension.get_opt(resource, [:pub_sub], :module, nil)
end
def prefix(resource) do
Ash.Dsl.Extension.get_opt(resource, [:pub_sub], :prefix, nil)
end
def notify(%Ash.Notifier.Notification{resource: resource} = notification) do
resource
|> publications()
|> Enum.filter(&matches?(&1, notification))
|> Enum.each(&publish_notification(&1, notification))
end
defp publish_notification(publish, notification) do
publish.topic
|> fill_template(notification)
|> Enum.each(fn topic ->
event = publish.event || to_string(notification.action.name)
prefix = prefix(notification.resource) || ""
prefixed_topic = prefix <> ":" <> topic
module(notification.resource).broadcast(
prefixed_topic,
event,
notification
)
end)
end
defp fill_template(topic, _) when is_binary(topic), do: [topic]
defp fill_template(topic, notification) do
topic
|> all_combinations_of_values(notification, notification.action.type)
|> Enum.map(&List.flatten/1)
|> Enum.map(&Enum.join(&1, ":"))
end
defp all_combinations_of_values(items, notification, action_type, trail \\ [])
defp all_combinations_of_values([], _, _, trail), do: [Enum.reverse(trail)]
defp all_combinations_of_values([item | rest], notification, action_type, trail)
when is_binary(item) do
all_combinations_of_values(rest, notification, action_type, [item | trail])
end
defp all_combinations_of_values([item | rest], notification, :update, trail)
when is_atom(item) do
value_before_change = Map.get(notification.changeset.data, item)
value_after_change = Map.get(notification.data, item)
[value_before_change, value_after_change]
|> Enum.reject(&is_nil/1)
|> Enum.uniq()
|> Enum.flat_map(fn possible_value ->
all_combinations_of_values(rest, notification, :update, [possible_value | trail])
end)
end
defp all_combinations_of_values([:_tenant | rest], notification, action_type, trail) do
if notification.changeset.tenant do
all_combinations_of_values(rest, notification, action_type, [
notification.changeset.tenant | trail
])
else
[]
end
end
defp all_combinations_of_values([item | rest], notification, action_type, trail)
when is_atom(item) do
all_combinations_of_values(rest, notification, action_type, [
Map.get(notification.data, item) | trail
])
end
defp all_combinations_of_values([item | rest], notification, action_type, trail)
when is_list(item) do
Enum.flat_map(item, fn possible_value ->
all_combinations_of_values([possible_value | rest], notification, action_type, trail)
end)
end
defp matches?(%{action: action}, %{action: %{name: action}}), do: true
defp matches?(%{type: type}, %{action: %{type: type}}), do: true
defp matches?(_, _), do: false
end