mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-07 19:22:14 +09:00
docs: simplify the reference and tighten the verifier (#2736)
This commit is contained in:
@@ -11,26 +11,28 @@
|
||||
]).
|
||||
|
||||
-define(MAX_BACKOFF_EXPONENT, 32).
|
||||
-define(BASE_BACKOFF_MS, 1000).
|
||||
-define(DEFAULT_MAX_BACKOFF_MS, 30000).
|
||||
|
||||
-spec calculate(non_neg_integer()) -> non_neg_integer().
|
||||
calculate(Attempt) ->
|
||||
calculate(Attempt, 30000).
|
||||
calculate(Attempt, ?DEFAULT_MAX_BACKOFF_MS).
|
||||
|
||||
-spec calculate(non_neg_integer(), pos_integer()) -> non_neg_integer().
|
||||
calculate(Attempt, MaxMs) ->
|
||||
Exponent = min(cap_exponent(Attempt), ?MAX_BACKOFF_EXPONENT),
|
||||
BackoffMs = round(1000 * math:pow(2, Exponent)),
|
||||
Exponent = cap_exponent(Attempt),
|
||||
BackoffMs = ?BASE_BACKOFF_MS bsl Exponent,
|
||||
min(BackoffMs, MaxMs).
|
||||
|
||||
-spec cap_exponent(non_neg_integer()) -> non_neg_integer().
|
||||
cap_exponent(Attempt) when is_integer(Attempt), Attempt >= 0 ->
|
||||
Attempt;
|
||||
min(Attempt, ?MAX_BACKOFF_EXPONENT);
|
||||
cap_exponent(_) ->
|
||||
0.
|
||||
|
||||
-spec calculate_with_jitter(non_neg_integer()) -> non_neg_integer().
|
||||
calculate_with_jitter(Attempt) ->
|
||||
calculate_with_jitter(Attempt, 30000).
|
||||
calculate_with_jitter(Attempt, ?DEFAULT_MAX_BACKOFF_MS).
|
||||
|
||||
-spec calculate_with_jitter(non_neg_integer(), pos_integer()) -> non_neg_integer().
|
||||
calculate_with_jitter(Attempt, MaxMs) ->
|
||||
|
||||
@@ -22,20 +22,8 @@ build_request(UserId, CustomStatus) ->
|
||||
|
||||
-spec build_custom_status_payload(map()) -> map().
|
||||
build_custom_status_payload(CustomStatus) ->
|
||||
Fields = [
|
||||
{<<"text">>, maps:get(<<"text">>, CustomStatus, undefined)},
|
||||
{<<"expires_at">>, maps:get(<<"expires_at">>, CustomStatus, undefined)},
|
||||
{<<"emoji_id">>, maps:get(<<"emoji_id">>, CustomStatus, undefined)},
|
||||
{<<"emoji_name">>, maps:get(<<"emoji_name">>, CustomStatus, undefined)}
|
||||
],
|
||||
lists:foldl(
|
||||
fun
|
||||
({_Key, undefined}, Acc) -> Acc;
|
||||
({Key, Value}, Acc) -> Acc#{Key => Value}
|
||||
end,
|
||||
#{},
|
||||
Fields
|
||||
).
|
||||
Fields = [<<"text">>, <<"expires_at">>, <<"emoji_id">>, <<"emoji_name">>],
|
||||
maps:filter(fun(_Key, Value) -> Value =/= undefined end, maps:with(Fields, CustomStatus)).
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
@@ -22,13 +22,11 @@ partial_user_fields() ->
|
||||
|
||||
-spec normalize_user(map() | term()) -> map().
|
||||
normalize_user(User) when is_map(User) ->
|
||||
CleanPairs =
|
||||
lists:foldl(
|
||||
fun(Key, Acc) -> add_normalized_field(Key, User, Acc) end,
|
||||
[],
|
||||
partial_user_fields()
|
||||
),
|
||||
maps:from_list(lists:reverse(CleanPairs));
|
||||
lists:foldl(
|
||||
fun(Key, Acc) -> add_normalized_field(Key, User, Acc) end,
|
||||
#{},
|
||||
partial_user_fields()
|
||||
);
|
||||
normalize_user(_) ->
|
||||
#{}.
|
||||
|
||||
@@ -46,19 +44,18 @@ normalize_field(<<"mention_flags">>, Value) ->
|
||||
normalize_field(_Key, Value) ->
|
||||
Value.
|
||||
|
||||
-spec add_normalized_field(binary(), map(), [{binary(), term()}]) -> [{binary(), term()}].
|
||||
-spec add_normalized_field(binary(), map(), map()) -> map().
|
||||
add_normalized_field(Key, User, Acc) ->
|
||||
case maps:get(Key, User, undefined) of
|
||||
undefined -> Acc;
|
||||
Value -> maybe_add_normalized_field(Key, normalize_field(Key, Value), Acc)
|
||||
end.
|
||||
|
||||
-spec maybe_add_normalized_field(binary(), term(), [{binary(), term()}]) ->
|
||||
[{binary(), term()}].
|
||||
-spec maybe_add_normalized_field(binary(), term(), map()) -> map().
|
||||
maybe_add_normalized_field(_Key, undefined, Acc) ->
|
||||
Acc;
|
||||
maybe_add_normalized_field(Key, Normalized, Acc) ->
|
||||
[{Key, Normalized} | Acc].
|
||||
Acc#{Key => Normalized}.
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
Reference in New Issue
Block a user