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/2 |
7 |
|
]). |
8 |
|
|
9 |
|
-ignore_xref([check_condition/3, verify_support/2]). |
10 |
|
|
11 |
|
-include("amp.hrl"). |
12 |
|
-include("mongoose.hrl"). |
13 |
|
-include("jlib.hrl"). |
14 |
|
|
15 |
|
-spec verify_support(any(), amp_rules()) -> [ amp_rule_support() ]. |
16 |
|
verify_support(HookAcc, Rules) -> |
17 |
271 |
HookAcc ++ [ verify_rule_support(Rule) || Rule <- Rules ]. |
18 |
|
|
19 |
|
-spec verify_rule_support(amp_rule()) -> amp_rule_support(). |
20 |
|
verify_rule_support(#amp_rule{action = alert} = Rule) -> |
21 |
1 |
{error, 'unsupported-actions', Rule}; |
22 |
|
verify_rule_support(#amp_rule{condition = 'expire-at'} = Rule) -> |
23 |
1 |
{error, 'unsupported-conditions', Rule}; |
24 |
|
verify_rule_support(Rule) -> |
25 |
403 |
{supported, Rule}. |
26 |
|
|
27 |
|
-spec check_condition(amp_match_result(), amp_strategy(), amp_rule()) -> amp_match_result(). |
28 |
|
check_condition(HookAcc, Strategy, Rule) -> |
29 |
544 |
case HookAcc of |
30 |
544 |
no_match -> resolve(Strategy, Rule); |
31 |
:-( |
MatchResult -> MatchResult |
32 |
|
end. |
33 |
|
|
34 |
|
-spec resolve(amp_strategy(), amp_rule()) -> amp_match_result(). |
35 |
81 |
resolve(#amp_strategy{deliver = [Value]}, #amp_rule{condition = deliver, value = Value}) -> match; |
36 |
|
resolve(#amp_strategy{deliver = Values}, #amp_rule{condition = deliver, value = Value, action = notify}) |
37 |
|
when is_list(Values) -> |
38 |
301 |
case lists:member(Value, Values) of |
39 |
149 |
true -> undecided; |
40 |
152 |
false -> no_match |
41 |
|
end; |
42 |
|
resolve(#amp_strategy{deliver = [Value | _]}, #amp_rule{condition = deliver, value = Value, action = Action}) |
43 |
36 |
when Action == drop orelse Action == error -> match; |
44 |
|
resolve(#amp_strategy{'match-resource' = Value}, #amp_rule{condition = 'match-resource', value = any}) |
45 |
1 |
when Value /= undefined -> match; |
46 |
|
resolve(#amp_strategy{'match-resource' = Value}, #amp_rule{condition = 'match-resource', value = Value}) -> |
47 |
3 |
match; |
48 |
122 |
resolve(#amp_strategy{}, #amp_rule{}) -> no_match. |