Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 2. Final #3
This commit is contained in:
@@ -1,16 +1,27 @@
|
||||
-module(logic_moderation).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([create_report/4, get_reports/1, get_reports_by_target/3, resolve_report/3]).
|
||||
-export([add_banned_word/2, remove_banned_word/2, list_banned_words/1]).
|
||||
-export([check_content/1, auto_moderate/1]).
|
||||
-export([freeze_calendar/2, unfreeze_calendar/2, freeze_event/2, unfreeze_event/2]).
|
||||
-export([create_report/4,
|
||||
get_reports/1,
|
||||
get_reports_by_target/3,
|
||||
resolve_report/3]).
|
||||
|
||||
-define(REPORT_THRESHOLD, 3). % Количество жалоб для авто-заморозки
|
||||
-export([add_banned_word/2,
|
||||
remove_banned_word/2,
|
||||
list_banned_words/1]).
|
||||
|
||||
%% ============ Жалобы ============
|
||||
-export([check_content/1,
|
||||
auto_moderate/1]).
|
||||
|
||||
-export([freeze_calendar/2,
|
||||
unfreeze_calendar/2,
|
||||
freeze_event/2,
|
||||
unfreeze_event/2]).
|
||||
|
||||
-define(REPORT_THRESHOLD, 3).
|
||||
|
||||
%% ============ Жалобы =====================================
|
||||
|
||||
%% Создание жалобы
|
||||
create_report(ReporterId, TargetType, TargetId, Reason) ->
|
||||
case target_exists(TargetType, TargetId) of
|
||||
true ->
|
||||
@@ -22,30 +33,29 @@ create_report(ReporterId, TargetType, TargetId, Reason) ->
|
||||
target_id => TargetId,
|
||||
reason => Reason
|
||||
}),
|
||||
% Проверяем порог для авто-модерации
|
||||
check_auto_freeze(TargetType, TargetId),
|
||||
{ok, Report};
|
||||
Error -> Error
|
||||
Error ->
|
||||
Error
|
||||
end;
|
||||
false -> {error, target_not_found}
|
||||
false ->
|
||||
{error, target_not_found}
|
||||
end.
|
||||
|
||||
%% Получить все жалобы (для админа)
|
||||
get_reports(AdminId) ->
|
||||
case is_admin(AdminId) of
|
||||
true -> core_report:list_all();
|
||||
true -> core_report:list_all();
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Получить жалобы на конкретную цель
|
||||
get_reports_by_target(AdminId, TargetType, TargetId) ->
|
||||
case is_admin(AdminId) of
|
||||
true -> core_report:list_by_target(TargetType, TargetId);
|
||||
true -> core_report:list_by_target(TargetType, TargetId);
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Рассмотреть жалобу (подтвердить или отклонить)
|
||||
resolve_report(AdminId, ReportId, Action) when Action =:= reviewed; Action =:= dismissed ->
|
||||
resolve_report(AdminId, ReportId, Action)
|
||||
when Action =:= reviewed; Action =:= dismissed ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_report:get_by_id(ReportId) of
|
||||
@@ -53,19 +63,23 @@ resolve_report(AdminId, ReportId, Action) when Action =:= reviewed; Action =:= d
|
||||
case Report#report.status of
|
||||
pending ->
|
||||
core_report:update_status(ReportId, Action, AdminId);
|
||||
_ -> {error, already_resolved}
|
||||
_ ->
|
||||
{error, already_resolved}
|
||||
end;
|
||||
Error -> Error
|
||||
Error ->
|
||||
Error
|
||||
end;
|
||||
false -> {error, access_denied}
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end.
|
||||
|
||||
%% Проверка порога для авто-заморозки
|
||||
check_auto_freeze(TargetType, TargetId) ->
|
||||
Count = core_report:get_count_by_target(TargetType, TargetId),
|
||||
if Count >= ?REPORT_THRESHOLD ->
|
||||
auto_freeze(TargetType, TargetId);
|
||||
true -> ok
|
||||
if
|
||||
Count >= ?REPORT_THRESHOLD ->
|
||||
auto_freeze(TargetType, TargetId);
|
||||
true ->
|
||||
ok
|
||||
end.
|
||||
|
||||
auto_freeze(event, EventId) ->
|
||||
@@ -82,42 +96,52 @@ auto_freeze(calendar, CalendarId) ->
|
||||
end;
|
||||
auto_freeze(_, _) -> ok.
|
||||
|
||||
%% ============ Бан-лист ============
|
||||
%% ============ Бан-лист ===================================
|
||||
|
||||
%% Добавить запрещённое слово
|
||||
add_banned_word(AdminId, Word) ->
|
||||
case is_admin(AdminId) of
|
||||
true -> core_banned_word:add(Word);
|
||||
true -> core_banned_words:add_banned_word(Word, AdminId);
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Удалить запрещённое слово
|
||||
remove_banned_word(AdminId, Word) ->
|
||||
case is_admin(AdminId) of
|
||||
true -> core_banned_word:remove(Word);
|
||||
true -> core_banned_words:remove_banned_word(Word);
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Список запрещённых слов
|
||||
list_banned_words(AdminId) ->
|
||||
case is_admin(AdminId) of
|
||||
true -> core_banned_word:list_all();
|
||||
true -> {ok, core_banned_words:list_banned_words()};
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% ============ Контент-фильтр ============
|
||||
%% ============ Контент-фильтр =============================
|
||||
|
||||
%% Проверить контент на запрещённые слова
|
||||
check_content(Text) ->
|
||||
core_banned_word:check_text(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).
|
||||
|
||||
%% Автоматическая модерация контента (замена запрещённых слов)
|
||||
auto_moderate(Text) ->
|
||||
core_banned_word:filter_text(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).
|
||||
|
||||
%% ============ Заморозка/разморозка ============
|
||||
%% ============ Заморозка/разморозка =======================
|
||||
|
||||
%% Заморозить календарь
|
||||
freeze_calendar(AdminId, CalendarId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
@@ -129,7 +153,6 @@ freeze_calendar(AdminId, CalendarId) ->
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Разморозить календарь
|
||||
unfreeze_calendar(AdminId, CalendarId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
@@ -141,7 +164,6 @@ unfreeze_calendar(AdminId, CalendarId) ->
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Заморозить событие
|
||||
freeze_event(AdminId, EventId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
@@ -153,7 +175,6 @@ freeze_event(AdminId, EventId) ->
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% Разморозить событие
|
||||
unfreeze_event(AdminId, EventId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
@@ -165,7 +186,7 @@ unfreeze_event(AdminId, EventId) ->
|
||||
false -> {error, access_denied}
|
||||
end.
|
||||
|
||||
%% ============ Вспомогательные функции ============
|
||||
%% ============ Вспомогательные функции ====================
|
||||
|
||||
target_exists(event, EventId) ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
@@ -176,7 +197,7 @@ target_exists(calendar, CalendarId) ->
|
||||
case core_calendar:get_by_id(CalendarId) of
|
||||
{ok, _} -> true;
|
||||
_ -> false
|
||||
end; % ← точка с запятой здесь!
|
||||
end;
|
||||
target_exists(_, _) -> false.
|
||||
|
||||
is_admin(UserId) ->
|
||||
|
||||
Reference in New Issue
Block a user