1 |
|
-module(amp_resolver). |
2 |
|
%% @doc This module is responsible for checking whether particular AMP semantics |
3 |
|
%% apply for a given message. |
4 |
|
|
5 |
|
-export([check_condition/3, |
6 |
|
verify_support/3 |
7 |
|
]). |
8 |
|
|
9 |
|
-include("amp.hrl"). |
10 |
|
-include("mongoose.hrl"). |
11 |
|
-include("jlib.hrl"). |
12 |
|
|
13 |
|
-spec verify_support(Acc, Params, Extra) -> {ok, Acc} when |
14 |
|
Acc :: [amp_rule_support()], |
15 |
|
Params :: #{rules := amp_rules()}, |
16 |
|
Extra :: map(). |
17 |
|
verify_support(HookAcc, #{rules := Rules}, _) -> |
18 |
175 |
{ok, HookAcc ++ [ verify_rule_support(Rule) || Rule <- Rules ]}. |
19 |
|
|
20 |
|
-spec verify_rule_support(amp_rule()) -> amp_rule_support(). |
21 |
|
verify_rule_support(#amp_rule{action = alert} = Rule) -> |
22 |
1 |
{error, 'unsupported-actions', Rule}; |
23 |
|
verify_rule_support(#amp_rule{condition = 'expire-at'} = Rule) -> |
24 |
1 |
{error, 'unsupported-conditions', Rule}; |
25 |
|
verify_rule_support(Rule) -> |
26 |
259 |
{supported, Rule}. |
27 |
|
|
28 |
|
-spec check_condition(Acc, Params, Extra) -> {ok, Acc} when |
29 |
|
Acc :: amp_match_result(), |
30 |
|
Params :: #{strategy := amp_strategy(), rule := amp_rule()}, |
31 |
|
Extra :: map(). |
32 |
|
check_condition(HookAcc, #{strategy := Strategy, rule := Rule}, _) -> |
33 |
332 |
NewAcc = case HookAcc of |
34 |
332 |
no_match -> resolve(Strategy, Rule); |
35 |
:-( |
MatchResult -> MatchResult |
36 |
|
end, |
37 |
332 |
{ok, NewAcc}. |
38 |
|
|
39 |
|
-spec resolve(amp_strategy(), amp_rule()) -> amp_match_result(). |
40 |
53 |
resolve(#amp_strategy{deliver = [Value]}, #amp_rule{condition = deliver, value = Value}) -> match; |
41 |
|
resolve(#amp_strategy{deliver = Values}, #amp_rule{condition = deliver, value = Value, action = notify}) |
42 |
|
when is_list(Values) -> |
43 |
181 |
case lists:member(Value, Values) of |
44 |
77 |
true -> undecided; |
45 |
104 |
false -> no_match |
46 |
|
end; |
47 |
|
resolve(#amp_strategy{deliver = [Value | _]}, #amp_rule{condition = deliver, value = Value, action = Action}) |
48 |
20 |
when Action == drop orelse Action == error -> match; |
49 |
|
resolve(#amp_strategy{'match-resource' = Value}, #amp_rule{condition = 'match-resource', value = any}) |
50 |
1 |
when Value /= undefined -> match; |
51 |
|
resolve(#amp_strategy{'match-resource' = Value}, #amp_rule{condition = 'match-resource', value = Value}) -> |
52 |
3 |
match; |
53 |
74 |
resolve(#amp_strategy{}, #amp_rule{}) -> no_match. |