Добавить автомодерацию: политики, порог жалоб, settings/hits API и тесты.
Refs EventHub/EventHubBack#37 Refs EventHub/EventHubBack#38 Refs EventHub/EventHubBack#39 Refs EventHub/EventHubBack#40
This commit is contained in:
@@ -0,0 +1,352 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% Автомодерация: сканирование бан-слов, политики, soft-hide, очередь.
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(logic_automoderation).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([
|
||||
scan_text/1,
|
||||
scan_texts/1,
|
||||
evaluate_texts/1,
|
||||
apply_after_save/4,
|
||||
soft_hide/3,
|
||||
restore_entity/2,
|
||||
censor_text/2,
|
||||
get_settings/0,
|
||||
update_settings/2,
|
||||
list_hits/2,
|
||||
resolve_hit/3,
|
||||
settings_to_json/1,
|
||||
hit_to_json/1,
|
||||
record_report_threshold_hit/2,
|
||||
reason_reports/0
|
||||
]).
|
||||
|
||||
-define(SYSTEM_ID, <<"system">>).
|
||||
-define(SYSTEM_EMAIL, <<"system@eventhub">>).
|
||||
-define(REASON_KEYWORD, <<"auto:keyword">>).
|
||||
-define(REASON_REPORTS, <<"auto:report_threshold">>).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Scan / evaluate
|
||||
%%%===================================================================
|
||||
|
||||
-spec scan_text(binary()) -> clean | {hit, [binary()]}.
|
||||
scan_text(Text) when is_binary(Text) ->
|
||||
Settings = core_automod_settings:get(),
|
||||
Words = case catch core_banned_words:list_banned_words() of
|
||||
List when is_list(List) ->
|
||||
[W#banned_word.word || W <- List];
|
||||
_ ->
|
||||
[]
|
||||
end,
|
||||
Hits = [W || W <- Words, word_matches(Text, W, Settings#automod_settings.match_mode)],
|
||||
case Hits of
|
||||
[] -> clean;
|
||||
_ -> {hit, lists:usort(Hits)}
|
||||
end;
|
||||
scan_text(_) ->
|
||||
clean.
|
||||
|
||||
-spec scan_texts([binary()]) -> clean | {hit, [binary()]}.
|
||||
scan_texts(Texts) ->
|
||||
lists:foldl(fun(Text, Acc) ->
|
||||
case scan_text(Text) of
|
||||
clean -> Acc;
|
||||
{hit, Words} ->
|
||||
case Acc of
|
||||
clean -> {hit, Words};
|
||||
{hit, Prev} -> {hit, lists:usort(Prev ++ Words)}
|
||||
end
|
||||
end
|
||||
end, clean, Texts).
|
||||
|
||||
%% @doc До сохранения: reject | {ok, Action, TextsOut, Words}.
|
||||
%% Action = none | flag | censor. TextsOut — возможно с цензурой.
|
||||
-spec evaluate_texts([binary()]) ->
|
||||
{reject, [binary()]} |
|
||||
{ok, none | flag | censor, [binary()], [binary()]}.
|
||||
evaluate_texts(Texts) ->
|
||||
case scan_texts(Texts) of
|
||||
clean ->
|
||||
{ok, none, Texts, []};
|
||||
{hit, Words} ->
|
||||
Settings = core_automod_settings:get(),
|
||||
case Settings#automod_settings.keyword_action of
|
||||
reject ->
|
||||
{reject, Words};
|
||||
censor ->
|
||||
Censored = [censor_text(T, Words) || T <- Texts],
|
||||
{ok, censor, Censored, Words};
|
||||
flag ->
|
||||
{ok, flag, Texts, Words}
|
||||
end
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% After save
|
||||
%%%===================================================================
|
||||
|
||||
-spec apply_after_save(calendar | event | review, binary(),
|
||||
none | flag | censor, [binary()]) -> ok.
|
||||
apply_after_save(_Type, _Id, none, _) ->
|
||||
ok;
|
||||
apply_after_save(EntityType, EntityId, Action, Words)
|
||||
when Action =:= flag; Action =:= censor ->
|
||||
case Action of
|
||||
flag -> soft_hide(EntityType, EntityId, ?REASON_KEYWORD);
|
||||
censor -> ok
|
||||
end,
|
||||
{ok, Hit} = core_automod_hit:create(EntityType, EntityId, keyword, Words, Action),
|
||||
log_system(<<"automod_keyword">>, entity_type_bin(EntityType), EntityId, ?REASON_KEYWORD),
|
||||
logic_notification:notify_admin(automod_hit, #{
|
||||
hit_id => Hit#automod_hit.id,
|
||||
entity_type => entity_type_bin(EntityType),
|
||||
entity_id => EntityId,
|
||||
trigger => <<"keyword">>,
|
||||
action => atom_to_binary(Action, utf8),
|
||||
matched_words => Words
|
||||
}),
|
||||
ok.
|
||||
|
||||
-spec soft_hide(calendar | event | review, binary(), binary()) -> ok.
|
||||
soft_hide(event, Id, Reason) ->
|
||||
case core_event:get_by_id(Id) of
|
||||
{ok, #event{status = active}} ->
|
||||
_ = core_event:freeze(Id, Reason),
|
||||
ok;
|
||||
_ -> ok
|
||||
end;
|
||||
soft_hide(calendar, Id, Reason) ->
|
||||
case core_calendar:get_by_id(Id) of
|
||||
{ok, #calendar{status = active}} ->
|
||||
_ = core_calendar:freeze(Id, Reason),
|
||||
ok;
|
||||
_ -> ok
|
||||
end;
|
||||
soft_hide(review, Id, Reason) ->
|
||||
case core_review:get_by_id(Id) of
|
||||
{ok, #review{status = visible}} ->
|
||||
_ = core_review:hide(Id, Reason),
|
||||
ok;
|
||||
_ -> ok
|
||||
end.
|
||||
|
||||
-spec restore_entity(calendar | event | review, binary()) -> ok | {error, term()}.
|
||||
restore_entity(event, Id) ->
|
||||
case core_event:unfreeze(Id, <<"automod:approved">>) of
|
||||
{ok, _} -> ok;
|
||||
Err -> Err
|
||||
end;
|
||||
restore_entity(calendar, Id) ->
|
||||
case core_calendar:unfreeze(Id, <<"automod:approved">>) of
|
||||
{ok, _} -> ok;
|
||||
Err -> Err
|
||||
end;
|
||||
restore_entity(review, Id) ->
|
||||
case core_review:unhide(Id, <<"automod:approved">>) of
|
||||
{ok, _} -> ok;
|
||||
Err -> Err
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Settings / hits (admin)
|
||||
%%%===================================================================
|
||||
|
||||
get_settings() ->
|
||||
core_automod_settings:ensure_defaults(),
|
||||
{ok, core_automod_settings:get()}.
|
||||
|
||||
update_settings(AdminId, Updates) ->
|
||||
case admin_utils:check_role(AdminId, [superadmin, admin]) of
|
||||
true ->
|
||||
case core_automod_settings:update(Updates, AdminId) of
|
||||
{ok, S} -> {ok, S};
|
||||
Error -> Error
|
||||
end;
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end.
|
||||
|
||||
list_hits(AdminId, Filters) ->
|
||||
case admin_utils:check_role(AdminId, [superadmin, admin, moderator]) of
|
||||
true -> {ok, core_automod_hit:list(Filters)};
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
resolve_hit(AdminId, HitId, Decision) when Decision =:= approved; Decision =:= rejected ->
|
||||
case admin_utils:check_role(AdminId, [superadmin, admin, moderator]) of
|
||||
true ->
|
||||
case core_automod_hit:resolve(HitId, Decision, AdminId) of
|
||||
{ok, Hit} ->
|
||||
case Decision of
|
||||
approved ->
|
||||
_ = restore_entity(Hit#automod_hit.entity_type, Hit#automod_hit.entity_id);
|
||||
rejected ->
|
||||
soft_hide(Hit#automod_hit.entity_type, Hit#automod_hit.entity_id,
|
||||
<<"automod:rejected">>)
|
||||
end,
|
||||
{ok, Hit};
|
||||
Error ->
|
||||
Error
|
||||
end;
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Text helpers
|
||||
%%%===================================================================
|
||||
|
||||
-spec censor_text(binary(), [binary()]) -> binary().
|
||||
censor_text(Text, Words) ->
|
||||
lists:foldl(fun(Word, Acc) ->
|
||||
replace_word(Acc, Word, <<"***">>)
|
||||
end, Text, Words).
|
||||
|
||||
replace_word(Text, Word, Replacement) ->
|
||||
Settings = core_automod_settings:get(),
|
||||
case Settings#automod_settings.match_mode of
|
||||
substring ->
|
||||
replace_substring_ci(Text, Word, Replacement);
|
||||
word_boundary ->
|
||||
replace_boundary_ci(Text, Word, Replacement)
|
||||
end.
|
||||
|
||||
word_matches(Text, Word, Mode) ->
|
||||
TextL = unicode:characters_to_list(string:lowercase(Text)),
|
||||
WordL = unicode:characters_to_list(string:lowercase(Word)),
|
||||
case Mode of
|
||||
substring -> string:str(TextL, WordL) > 0;
|
||||
word_boundary -> find_boundary(TextL, WordL, 1)
|
||||
end.
|
||||
|
||||
find_boundary(_Text, [], _) -> false;
|
||||
find_boundary(Text, Word, Start) when Start > length(Text) -> false;
|
||||
find_boundary(Text, Word, Start) ->
|
||||
Tail = lists:nthtail(Start - 1, Text),
|
||||
case string:str(Tail, Word) of
|
||||
0 -> false;
|
||||
Rel ->
|
||||
Pos = Start + Rel - 1,
|
||||
BeforeOk = Pos =:= 1 orelse not is_word_char(lists:nth(Pos - 1, Text)),
|
||||
AfterPos = Pos + length(Word),
|
||||
AfterOk = AfterPos > length(Text) orelse not is_word_char(lists:nth(AfterPos, Text)),
|
||||
case BeforeOk andalso AfterOk of
|
||||
true -> true;
|
||||
false -> find_boundary(Text, Word, Pos + 1)
|
||||
end
|
||||
end.
|
||||
|
||||
is_word_char(C) when C >= $a, C =< $z -> true;
|
||||
is_word_char(C) when C >= $0, C =< $9 -> true;
|
||||
is_word_char($_) -> true;
|
||||
is_word_char(C) when C >= 16#0400, C =< 16#04FF -> true; % Cyrillic block
|
||||
is_word_char(_) -> false.
|
||||
|
||||
replace_substring_ci(Text, Word, Replacement) ->
|
||||
TextL = string:lowercase(Text),
|
||||
WordL = string:lowercase(Word),
|
||||
case binary:match(TextL, WordL) of
|
||||
nomatch -> Text;
|
||||
{Pos, Len} ->
|
||||
<<Prefix:Pos/binary, _:Len/binary, Rest/binary>> = Text,
|
||||
replace_substring_ci(<<Prefix/binary, Replacement/binary, Rest/binary>>, Word, Replacement)
|
||||
end.
|
||||
|
||||
replace_boundary_ci(Text, Word, Replacement) ->
|
||||
TextList = unicode:characters_to_list(Text),
|
||||
WordList = unicode:characters_to_list(Word),
|
||||
TextLower = unicode:characters_to_list(string:lowercase(Text)),
|
||||
WordLower = unicode:characters_to_list(string:lowercase(Word)),
|
||||
case find_boundary(TextLower, WordLower, 1) of
|
||||
false -> Text;
|
||||
true ->
|
||||
case string:str(TextLower, WordLower) of
|
||||
0 -> Text;
|
||||
Pos ->
|
||||
%% may be false positive on first substring; walk like find_boundary
|
||||
case find_boundary_pos(TextLower, WordLower, 1) of
|
||||
undefined -> Text;
|
||||
RealPos ->
|
||||
Prefix = lists:sublist(TextList, RealPos - 1),
|
||||
Rest = lists:nthtail(RealPos - 1 + length(WordList), TextList),
|
||||
New = unicode:characters_to_binary(Prefix ++ unicode:characters_to_list(Replacement) ++ Rest),
|
||||
replace_boundary_ci(New, Word, Replacement)
|
||||
end
|
||||
end
|
||||
end.
|
||||
|
||||
find_boundary_pos(_Text, [], _) -> undefined;
|
||||
find_boundary_pos(Text, Word, Start) when Start > length(Text) -> undefined;
|
||||
find_boundary_pos(Text, Word, Start) ->
|
||||
Tail = lists:nthtail(Start - 1, Text),
|
||||
case string:str(Tail, Word) of
|
||||
0 -> undefined;
|
||||
Rel ->
|
||||
Pos = Start + Rel - 1,
|
||||
BeforeOk = Pos =:= 1 orelse not is_word_char(lists:nth(Pos - 1, Text)),
|
||||
AfterPos = Pos + length(Word),
|
||||
AfterOk = AfterPos > length(Text) orelse not is_word_char(lists:nth(AfterPos, Text)),
|
||||
case BeforeOk andalso AfterOk of
|
||||
true -> Pos;
|
||||
false -> find_boundary_pos(Text, Word, Pos + 1)
|
||||
end
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% JSON / audit helpers
|
||||
%%%===================================================================
|
||||
|
||||
settings_to_json(#automod_settings{} = S) ->
|
||||
#{
|
||||
keyword_action => atom_to_binary(S#automod_settings.keyword_action, utf8),
|
||||
match_mode => atom_to_binary(S#automod_settings.match_mode, utf8),
|
||||
report_threshold => S#automod_settings.report_threshold,
|
||||
updated_at => handler_utils:datetime_to_iso8601(S#automod_settings.updated_at),
|
||||
updated_by => S#automod_settings.updated_by
|
||||
}.
|
||||
|
||||
hit_to_json(#automod_hit{} = H) ->
|
||||
#{
|
||||
id => H#automod_hit.id,
|
||||
entity_type => atom_to_binary(H#automod_hit.entity_type, utf8),
|
||||
entity_id => H#automod_hit.entity_id,
|
||||
trigger => atom_to_binary(H#automod_hit.trigger, utf8),
|
||||
matched_words => H#automod_hit.matched_words,
|
||||
action_taken => atom_to_binary(H#automod_hit.action_taken, utf8),
|
||||
status => atom_to_binary(H#automod_hit.status, utf8),
|
||||
created_at => handler_utils:datetime_to_iso8601(H#automod_hit.created_at),
|
||||
resolved_at => case H#automod_hit.resolved_at of
|
||||
undefined -> null;
|
||||
Dt -> handler_utils:datetime_to_iso8601(Dt)
|
||||
end,
|
||||
resolved_by => H#automod_hit.resolved_by
|
||||
}.
|
||||
|
||||
log_system(Action, EntityType, EntityId, Reason) ->
|
||||
core_admin_audit:log(?SYSTEM_ID, ?SYSTEM_EMAIL, system, Action,
|
||||
EntityType, EntityId, <<"127.0.0.1">>, Reason).
|
||||
|
||||
entity_type_bin(calendar) -> <<"calendar">>;
|
||||
entity_type_bin(event) -> <<"event">>;
|
||||
entity_type_bin(review) -> <<"review">>.
|
||||
|
||||
record_report_threshold_hit(EntityType, EntityId) ->
|
||||
Action = case EntityType of
|
||||
review -> hide;
|
||||
_ -> freeze
|
||||
end,
|
||||
soft_hide(EntityType, EntityId, ?REASON_REPORTS),
|
||||
{ok, Hit} = core_automod_hit:create(EntityType, EntityId, report_threshold, [], Action),
|
||||
log_system(<<"automod_report_threshold">>, entity_type_bin(EntityType), EntityId, ?REASON_REPORTS),
|
||||
logic_notification:notify_admin(automod_hit, #{
|
||||
hit_id => Hit#automod_hit.id,
|
||||
entity_type => entity_type_bin(EntityType),
|
||||
entity_id => EntityId,
|
||||
trigger => <<"report_threshold">>,
|
||||
action => atom_to_binary(Action, utf8)
|
||||
}),
|
||||
ok.
|
||||
|
||||
reason_reports() -> ?REASON_REPORTS.
|
||||
@@ -20,16 +20,28 @@ create_calendar(UserId, Title, Description, Confirmation, Type) ->
|
||||
{ok, User} ->
|
||||
case User#user.status of
|
||||
active ->
|
||||
case Type of
|
||||
commercial ->
|
||||
case logic_subscription:can_create_commercial_calendar(UserId) of
|
||||
true ->
|
||||
core_calendar:create(UserId, Title, Description, Confirmation, Type);
|
||||
false ->
|
||||
{error, subscription_required}
|
||||
end;
|
||||
personal ->
|
||||
core_calendar:create(UserId, Title, Description, Confirmation, Type)
|
||||
case logic_automoderation:evaluate_texts([Title, Description]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Title2, Desc2], Words} ->
|
||||
Result = case Type of
|
||||
commercial ->
|
||||
case logic_subscription:can_create_commercial_calendar(UserId) of
|
||||
true ->
|
||||
core_calendar:create(UserId, Title2, Desc2, Confirmation, Type);
|
||||
false ->
|
||||
{error, subscription_required}
|
||||
end;
|
||||
personal ->
|
||||
core_calendar:create(UserId, Title2, Desc2, Confirmation, Type)
|
||||
end,
|
||||
case Result of
|
||||
{ok, Cal} ->
|
||||
logic_automoderation:apply_after_save(calendar, Cal#calendar.id, Action, Words),
|
||||
core_calendar:get_by_id(Cal#calendar.id);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end;
|
||||
_ ->
|
||||
{error, user_inactive}
|
||||
@@ -61,7 +73,24 @@ update_calendar(UserId, CalendarId, Updates) ->
|
||||
case can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
ValidUpdates = validate_updates(Updates),
|
||||
core_calendar:update(CalendarId, ValidUpdates);
|
||||
case content_fields(ValidUpdates, [title, description]) of
|
||||
{[], []} ->
|
||||
core_calendar:update(CalendarId, ValidUpdates);
|
||||
{Fields, Texts} ->
|
||||
case logic_automoderation:evaluate_texts(Texts) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, OutTexts, Words} ->
|
||||
FinalUpdates = apply_text_results(ValidUpdates, Fields, OutTexts),
|
||||
case core_calendar:update(CalendarId, FinalUpdates) of
|
||||
{ok, _Updated} ->
|
||||
logic_automoderation:apply_after_save(calendar, CalendarId, Action, Words),
|
||||
core_calendar:get_by_id(CalendarId);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end
|
||||
end;
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end;
|
||||
@@ -69,6 +98,18 @@ update_calendar(UserId, CalendarId, Updates) ->
|
||||
Error
|
||||
end.
|
||||
|
||||
content_fields(Updates, Fields) ->
|
||||
lists:foldl(fun(F, {Fs, Ts}) ->
|
||||
case lists:keyfind(F, 1, Updates) of
|
||||
{F, V} when is_binary(V) -> {Fs ++ [F], Ts ++ [V]};
|
||||
_ -> {Fs, Ts}
|
||||
end
|
||||
end, {[], []}, Fields).
|
||||
|
||||
apply_text_results(Updates, [], []) -> Updates;
|
||||
apply_text_results(Updates, [F | Fs], [T | Ts]) ->
|
||||
apply_text_results(lists:keystore(F, 1, Updates, {F, T}), Fs, Ts).
|
||||
|
||||
%% Удаление календаря
|
||||
delete_calendar(UserId, CalendarId) ->
|
||||
case core_calendar:get_by_id(CalendarId) of
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-module(logic_event).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([create_event/5, create_recurring_event/6, get_event/2, list_events/2,
|
||||
update_event/3, delete_event/2]).
|
||||
-export([create_event/5, create_event/6, create_recurring_event/6, create_recurring_event/7,
|
||||
get_event/2, list_events/2, update_event/3, delete_event/2]).
|
||||
-export([validate_event_time/1, validate_event_time/2, get_occurrences/3, cancel_occurrence/3]).
|
||||
-export([materialize_for_booking/3]).
|
||||
-export([list_all_events/1, get_event_admin/1, update_event_admin/2, delete_event_admin/1]).
|
||||
@@ -12,13 +12,31 @@
|
||||
|
||||
%% Создание одиночного события
|
||||
create_event(UserId, CalendarId, Title, StartTime, Duration) ->
|
||||
create_event(UserId, CalendarId, Title, StartTime, Duration, <<>>).
|
||||
|
||||
create_event(UserId, CalendarId, Title, StartTime, Duration, Description) ->
|
||||
case logic_calendar:get_calendar(UserId, CalendarId) of
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
case validate_event_time(StartTime, UserId) of
|
||||
ok ->
|
||||
core_event:create(CalendarId, Title, StartTime, Duration);
|
||||
case logic_automoderation:evaluate_texts([Title, Description]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Title2, Desc2], Words} ->
|
||||
case core_event:create(CalendarId, Title2, StartTime, Duration) of
|
||||
{ok, Event} ->
|
||||
case Desc2 of
|
||||
<<>> -> ok;
|
||||
_ -> _ = core_event:update(Event#event.id, [{description, Desc2}])
|
||||
end,
|
||||
logic_automoderation:apply_after_save(event, Event#event.id, Action, Words),
|
||||
core_event:get_by_id(Event#event.id);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end;
|
||||
{error, _} = Error ->
|
||||
Error
|
||||
end;
|
||||
@@ -31,6 +49,9 @@ create_event(UserId, CalendarId, Title, StartTime, Duration) ->
|
||||
|
||||
%% Создание повторяющегося события
|
||||
create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule) ->
|
||||
create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule, <<>>).
|
||||
|
||||
create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule, Description) ->
|
||||
case logic_calendar:get_calendar(UserId, CalendarId) of
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
@@ -39,7 +60,22 @@ create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule) ->
|
||||
ok ->
|
||||
case logic_recurrence:validate_rrule(RRule) of
|
||||
true ->
|
||||
core_event:create_recurring(CalendarId, Title, StartTime, Duration, RRule);
|
||||
case logic_automoderation:evaluate_texts([Title, Description]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Title2, Desc2], Words} ->
|
||||
case core_event:create_recurring(CalendarId, Title2, StartTime, Duration, RRule) of
|
||||
{ok, Event} ->
|
||||
case Desc2 of
|
||||
<<>> -> ok;
|
||||
_ -> _ = core_event:update(Event#event.id, [{description, Desc2}])
|
||||
end,
|
||||
logic_automoderation:apply_after_save(event, Event#event.id, Action, Words),
|
||||
core_event:get_by_id(Event#event.id);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end;
|
||||
false ->
|
||||
{error, invalid_rrule}
|
||||
end;
|
||||
@@ -141,7 +177,34 @@ update_event(UserId, EventId, Updates) ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
ValidUpdates = validate_updates(Updates, UserId),
|
||||
core_event:update(EventId, ValidUpdates);
|
||||
Title = proplists:get_value(title, ValidUpdates, Event#event.title),
|
||||
Desc = proplists:get_value(description, ValidUpdates, Event#event.description),
|
||||
case logic_automoderation:evaluate_texts([Title, Desc]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Title2, Desc2], Words} ->
|
||||
Final0 = case lists:keymember(title, 1, ValidUpdates) of
|
||||
true -> lists:keystore(title, 1, ValidUpdates, {title, Title2});
|
||||
false -> ValidUpdates
|
||||
end,
|
||||
Final = case lists:keymember(description, 1, Final0) orelse Desc2 =/= Event#event.description of
|
||||
true when Action =:= censor ->
|
||||
lists:keystore(description, 1, Final0, {description, Desc2});
|
||||
true ->
|
||||
case lists:keymember(description, 1, Final0) of
|
||||
true -> lists:keystore(description, 1, Final0, {description, Desc2});
|
||||
false -> Final0
|
||||
end;
|
||||
false -> Final0
|
||||
end,
|
||||
case core_event:update(EventId, Final) of
|
||||
{ok, _} ->
|
||||
logic_automoderation:apply_after_save(event, EventId, Action, Words),
|
||||
core_event:get_by_id(EventId);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end;
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end;
|
||||
|
||||
@@ -18,8 +18,6 @@
|
||||
freeze_event/2,
|
||||
unfreeze_event/2]).
|
||||
|
||||
-define(REPORT_THRESHOLD, 3).
|
||||
|
||||
%% ============ Жалобы =====================================
|
||||
|
||||
create_report(ReporterId, TargetType, TargetId, Reason) ->
|
||||
@@ -74,27 +72,19 @@ resolve_report(AdminId, ReportId, Action)
|
||||
end.
|
||||
|
||||
check_auto_freeze(TargetType, TargetId) ->
|
||||
Count = core_report:get_count_by_target(TargetType, TargetId),
|
||||
Settings = core_automod_settings:get(),
|
||||
Threshold = Settings#automod_settings.report_threshold,
|
||||
Count = pending_count(TargetType, TargetId),
|
||||
if
|
||||
Count >= ?REPORT_THRESHOLD ->
|
||||
auto_freeze(TargetType, TargetId);
|
||||
Count >= Threshold ->
|
||||
logic_automoderation:record_report_threshold_hit(TargetType, TargetId);
|
||||
true ->
|
||||
ok
|
||||
end.
|
||||
|
||||
auto_freeze(event, EventId) ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} when Event#event.status =:= active ->
|
||||
core_event:update(EventId, [{status, frozen}]);
|
||||
_ -> ok
|
||||
end;
|
||||
auto_freeze(calendar, CalendarId) ->
|
||||
case core_calendar:get_by_id(CalendarId) of
|
||||
{ok, Calendar} when Calendar#calendar.status =:= active ->
|
||||
core_calendar:update(CalendarId, [{status, frozen}]);
|
||||
_ -> ok
|
||||
end;
|
||||
auto_freeze(_, _) -> ok.
|
||||
pending_count(TargetType, TargetId) ->
|
||||
length([R || R <- core_report:list_by_target(TargetType, TargetId),
|
||||
R#report.status =:= pending]).
|
||||
|
||||
%% ============ Бан-лист ===================================
|
||||
|
||||
@@ -119,26 +109,13 @@ list_banned_words(AdminId) ->
|
||||
%% ============ Контент-фильтр =============================
|
||||
|
||||
check_content(Text) ->
|
||||
Words = core_banned_words:list_banned_words(),
|
||||
LowerText = string:lowercase(binary_to_list(Text)),
|
||||
lists:any(fun(W) ->
|
||||
string:str(LowerText, binary_to_list(W#banned_word.word)) > 0
|
||||
end, Words).
|
||||
logic_automoderation:scan_text(Text) =/= clean.
|
||||
|
||||
auto_moderate(Text) ->
|
||||
Words = core_banned_words:list_banned_words(),
|
||||
lists:foldl(fun(W, Acc) ->
|
||||
WordStr = binary_to_list(W#banned_word.word),
|
||||
LowerAccStr = string:lowercase(binary_to_list(Acc)),
|
||||
case string:str(LowerAccStr, WordStr) of
|
||||
0 -> Acc;
|
||||
Pos ->
|
||||
Len = length(WordStr),
|
||||
Start = binary:part(Acc, {0, Pos-1}),
|
||||
Rest = binary:part(Acc, {Pos-1+Len, byte_size(Acc)-Pos+1-Len}),
|
||||
<<Start/binary, "***", Rest/binary>>
|
||||
end
|
||||
end, Text, Words).
|
||||
case logic_automoderation:scan_text(Text) of
|
||||
clean -> Text;
|
||||
{hit, Words} -> logic_automoderation:censor_text(Text, Words)
|
||||
end.
|
||||
|
||||
%% ============ Заморозка/разморозка =======================
|
||||
|
||||
@@ -198,4 +175,9 @@ target_exists(calendar, CalendarId) ->
|
||||
{ok, _} -> true;
|
||||
_ -> false
|
||||
end;
|
||||
target_exists(_, _) -> false.
|
||||
target_exists(review, ReviewId) ->
|
||||
case core_review:get_by_id(ReviewId) of
|
||||
{ok, _} -> true;
|
||||
_ -> false
|
||||
end;
|
||||
target_exists(_, _) -> false.
|
||||
|
||||
+28
-11
@@ -15,12 +15,18 @@ create_review(UserId, TargetType, TargetId, Rating, Comment) ->
|
||||
{ok, true} ->
|
||||
case core_review:has_user_reviewed(UserId, TargetType, TargetId) of
|
||||
false ->
|
||||
case core_review:create(UserId, TargetType, TargetId, Rating, Comment) of
|
||||
{ok, Review} ->
|
||||
update_target_rating(TargetType, TargetId),
|
||||
{ok, Review};
|
||||
Error ->
|
||||
Error
|
||||
case logic_automoderation:evaluate_texts([Comment]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Comment2], Words} ->
|
||||
case core_review:create(UserId, TargetType, TargetId, Rating, Comment2) of
|
||||
{ok, Review} ->
|
||||
update_target_rating(TargetType, TargetId),
|
||||
logic_automoderation:apply_after_save(review, Review#review.id, Action, Words),
|
||||
core_review:get_by_id(Review#review.id);
|
||||
Error ->
|
||||
Error
|
||||
end
|
||||
end;
|
||||
true ->
|
||||
{error, already_reviewed}
|
||||
@@ -81,11 +87,22 @@ update_review(UserId, ReviewId, Updates) ->
|
||||
case ValidUpdates of
|
||||
[] -> {error, no_valid_updates};
|
||||
_ ->
|
||||
case core_review:update(ReviewId, ValidUpdates) of
|
||||
{ok, Updated} ->
|
||||
update_target_rating(Review#review.target_type, Review#review.target_id),
|
||||
{ok, Updated};
|
||||
Error -> Error
|
||||
Comment = proplists:get_value(comment, ValidUpdates, Review#review.comment),
|
||||
case logic_automoderation:evaluate_texts([Comment]) of
|
||||
{reject, Words} ->
|
||||
{error, {content_banned, Words}};
|
||||
{ok, Action, [Comment2], Words} ->
|
||||
Final = case lists:keymember(comment, 1, ValidUpdates) of
|
||||
true -> lists:keystore(comment, 1, ValidUpdates, {comment, Comment2});
|
||||
false -> ValidUpdates
|
||||
end,
|
||||
case core_review:update(ReviewId, Final) of
|
||||
{ok, _Updated} ->
|
||||
update_target_rating(Review#review.target_type, Review#review.target_id),
|
||||
logic_automoderation:apply_after_save(review, ReviewId, Action, Words),
|
||||
core_review:get_by_id(ReviewId);
|
||||
Error -> Error
|
||||
end
|
||||
end
|
||||
end;
|
||||
false -> {error, access_denied}
|
||||
|
||||
Reference in New Issue
Block a user