Stage 10 final
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
start(_StartType, _StartArgs) ->
|
||||
pg:start_link(),
|
||||
application:ensure_all_started(mnesia),
|
||||
application:ensure_all_started(cowboy),
|
||||
|
||||
@@ -103,4 +104,22 @@ start_admin_http() ->
|
||||
middlewares => Middlewares
|
||||
}),
|
||||
|
||||
io:format("Admin HTTP server started on port ~p~n", [Port]).
|
||||
io:format("Admin HTTP server started on port ~p~n", [Port]),
|
||||
|
||||
% WebSocket для пользователей
|
||||
WsDispatch = cowboy_router:compile([
|
||||
{'_', [{"/ws", ws_handler, []}]}
|
||||
]),
|
||||
cowboy:start_clear(ws, [{port, 8081}], #{
|
||||
env => #{dispatch => WsDispatch}
|
||||
}),
|
||||
|
||||
% WebSocket для админов
|
||||
AdminWsDispatch = cowboy_router:compile([
|
||||
{'_', [{"/admin/ws", admin_ws_handler, []}]}
|
||||
]),
|
||||
cowboy:start_clear(admin_ws, [{port, 8446}], #{
|
||||
env => #{dispatch => AdminWsDispatch}
|
||||
}),
|
||||
|
||||
io:format("WebSocket started on ports 8081 (user) and 8446 (admin)~n").
|
||||
@@ -3,5 +3,13 @@
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
Body = jsx:encode(#{status => <<"ok">>, service => <<"admin">>}),
|
||||
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> ->
|
||||
Body = jsx:encode(#{status => <<"ok">>}),
|
||||
Req1 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []};
|
||||
_ ->
|
||||
Body = jsx:encode(#{error => <<"Method not allowed">>}),
|
||||
Req1 = cowboy_req:reply(405, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []}
|
||||
end.
|
||||
@@ -71,8 +71,10 @@ count_subscriptions() ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -120,8 +120,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -50,8 +50,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
87
src/handlers/admin_ws_handler.erl
Normal file
87
src/handlers/admin_ws_handler.erl
Normal file
@@ -0,0 +1,87 @@
|
||||
-module(admin_ws_handler).
|
||||
-behaviour(cowboy_websocket).
|
||||
|
||||
-export([init/2]).
|
||||
-export([websocket_init/1]).
|
||||
-export([websocket_handle/2]).
|
||||
-export([websocket_info/2]).
|
||||
-export([terminate/3]).
|
||||
|
||||
-record(state, {
|
||||
admin_id :: binary() | undefined
|
||||
}).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
Qs = cowboy_req:parse_qs(Req),
|
||||
case proplists:get_value(<<"token">>, Qs) of
|
||||
undefined ->
|
||||
io:format("[ADMIN_WS] Missing token~n"),
|
||||
Resp = cowboy_req:reply(401, #{}, <<"Missing token">>, Req),
|
||||
{ok, Resp, undefined};
|
||||
Token ->
|
||||
io:format("[ADMIN_WS] Token received: ~s...~n", [binary_part(Token, 0, 30)]),
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, Claims} ->
|
||||
UserId = maps:get(<<"user_id">>, Claims),
|
||||
Role = maps:get(<<"role">>, Claims),
|
||||
io:format("[ADMIN_WS] UserId: ~s, Role: ~s~n", [UserId, Role]),
|
||||
case Role of
|
||||
<<"admin">> ->
|
||||
io:format("[ADMIN_WS] Admin access granted~n"),
|
||||
{cowboy_websocket, Req, #state{admin_id = UserId}};
|
||||
_ ->
|
||||
io:format("[ADMIN_WS] Access denied: not admin~n"),
|
||||
Resp = cowboy_req:reply(403, #{}, <<"Admin access required">>, Req),
|
||||
{ok, Resp, undefined}
|
||||
end;
|
||||
{error, expired} ->
|
||||
io:format("[ADMIN_WS] Token expired~n"),
|
||||
Resp = cowboy_req:reply(401, #{}, <<"Token expired">>, Req),
|
||||
{ok, Resp, undefined};
|
||||
{error, Reason} ->
|
||||
io:format("[ADMIN_WS] Invalid token: ~p~n", [Reason]),
|
||||
Resp = cowboy_req:reply(401, #{}, <<"Invalid token">>, Req),
|
||||
{ok, Resp, undefined}
|
||||
end
|
||||
end.
|
||||
|
||||
websocket_init(State) ->
|
||||
io:format("[ADMIN_WS] WebSocket initialized for admin ~s~n", [State#state.admin_id]),
|
||||
pg:join(eventhub_admin_ws, self()),
|
||||
{ok, State}.
|
||||
|
||||
websocket_handle({text, Msg}, State) ->
|
||||
io:format("[ADMIN_WS] Received: ~s~n", [Msg]),
|
||||
try jsx:decode(Msg, [return_maps]) of
|
||||
#{<<"action">> := <<"subscribe">>, <<"channel">> := Channel} ->
|
||||
pg:join({eventhub_admin_channel, Channel}, self()),
|
||||
Reply = jsx:encode(#{status => <<"subscribed">>, channel => Channel}),
|
||||
{reply, {text, Reply}, State};
|
||||
#{<<"action">> := <<"unsubscribe">>, <<"channel">> := Channel} ->
|
||||
pg:leave({eventhub_admin_channel, Channel}, self()),
|
||||
Reply = jsx:encode(#{status => <<"unsubscribed">>, channel => Channel}),
|
||||
{reply, {text, Reply}, State};
|
||||
#{<<"action">> := <<"ping">>} ->
|
||||
{reply, {text, <<"{\"status\":\"pong\"}">>}, State};
|
||||
_ ->
|
||||
{ok, State}
|
||||
catch
|
||||
_:_ ->
|
||||
{ok, State}
|
||||
end;
|
||||
websocket_handle(_Frame, State) ->
|
||||
{ok, State}.
|
||||
|
||||
websocket_info({admin_notification, Type, Data}, State) ->
|
||||
Msg = jsx:encode(#{
|
||||
type => Type,
|
||||
data => Data,
|
||||
timestamp => os:system_time(seconds)
|
||||
}),
|
||||
{reply, {text, Msg}, State};
|
||||
websocket_info(_Info, State) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_Reason, _Req, _State) ->
|
||||
pg:leave(eventhub_admin_ws, self()),
|
||||
ok.
|
||||
@@ -123,8 +123,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -84,8 +84,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -75,8 +75,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -3,17 +3,23 @@
|
||||
-export([authenticate/1]).
|
||||
|
||||
authenticate(Req) ->
|
||||
io:format("[AUTH] Starting authentication...~n"),
|
||||
case cowboy_req:parse_header(<<"authorization">>, Req) of
|
||||
{bearer, Token} ->
|
||||
io:format("[AUTH] Bearer token found: ~s...~n", [binary_part(Token, 0, 30)]),
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, Claims} ->
|
||||
UserId = maps:get(<<"user_id">>, Claims),
|
||||
io:format("[AUTH] JWT verified, UserId: ~s~n", [UserId]),
|
||||
{ok, UserId, Req};
|
||||
{error, expired} ->
|
||||
io:format("[AUTH] JWT expired~n"),
|
||||
{error, 401, <<"Token expired">>, Req};
|
||||
{error, _} ->
|
||||
{error, Reason} ->
|
||||
io:format("[AUTH] JWT invalid: ~p~n", [Reason]),
|
||||
{error, 401, <<"Invalid token">>, Req}
|
||||
end;
|
||||
_ ->
|
||||
Other ->
|
||||
io:format("[AUTH] No bearer token: ~p~n", [Other]),
|
||||
{error, 401, <<"Missing or invalid Authorization header">>, Req}
|
||||
end.
|
||||
@@ -79,8 +79,10 @@ remove_banned_word(Req) ->
|
||||
%% Вспомогательные функции
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -121,8 +121,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -80,8 +80,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -106,8 +106,10 @@ confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -113,8 +113,10 @@ confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -172,8 +172,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -134,8 +134,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -265,8 +265,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -2,6 +2,17 @@
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
Body = jsx:encode(#{status => <<"ok">>}),
|
||||
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> ->
|
||||
Body = jsx:encode(#{status => <<"ok">>}),
|
||||
Req1 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []};
|
||||
_ ->
|
||||
Body = jsx:encode(#{error => <<"Method not allowed">>}),
|
||||
Req1 = cowboy_req:reply(405, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []}
|
||||
end.
|
||||
@@ -73,8 +73,10 @@ save_refresh_token(UserId, Token, ExpiresAt) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -84,8 +84,10 @@ delete_refresh_token(Token) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -56,8 +56,10 @@ handle(Req, _Opts) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -76,8 +76,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -13,41 +13,32 @@ handle(Req, _Opts) ->
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% POST /v1/reports - создание жалобы
|
||||
create_report(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
try jsx:decode(Body, [return_maps]) of
|
||||
Decoded when is_map(Decoded) ->
|
||||
case Decoded of
|
||||
#{<<"target_type">> := TargetTypeBin,
|
||||
<<"target_id">> := TargetId,
|
||||
<<"reason">> := Reason} ->
|
||||
TargetType = parse_target_type(TargetTypeBin),
|
||||
case logic_moderation:create_report(UserId, TargetType, TargetId, Reason) of
|
||||
{ok, Report} ->
|
||||
Response = report_to_json(Report),
|
||||
send_json(Req2, 201, Response);
|
||||
{error, target_not_found} ->
|
||||
send_error(Req2, 404, <<"Target not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Missing required fields">>)
|
||||
Decoded = jsx:decode(Body, [return_maps]),
|
||||
case Decoded of
|
||||
#{<<"target_type">> := TargetTypeBin,
|
||||
<<"target_id">> := TargetId,
|
||||
<<"reason">> := Reason} ->
|
||||
TargetType = parse_target_type(TargetTypeBin),
|
||||
case logic_moderation:create_report(UserId, TargetType, TargetId, Reason) of
|
||||
{ok, Report} ->
|
||||
Response = report_to_json(Report),
|
||||
send_json(Req2, 201, Response);
|
||||
{error, target_not_found} ->
|
||||
send_error(Req2, 404, <<"Target not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
send_error(Req2, 400, <<"Missing required fields">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% GET /v1/admin/reports - список всех жалоб (админ)
|
||||
list_reports(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
@@ -79,7 +70,6 @@ list_reports(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
parse_target_type(<<"event">>) -> event;
|
||||
parse_target_type(<<"calendar">>) -> calendar;
|
||||
parse_target_type(_) -> undefined.
|
||||
@@ -106,8 +96,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
Req1 = cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
Req1 = cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req1, []}.
|
||||
@@ -109,8 +109,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -100,8 +100,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -97,8 +97,10 @@ parse_datetime_param(Qs, Key) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -95,8 +95,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -61,7 +61,7 @@ handle_ticket_action(AdminId, TicketId, Body, Req) ->
|
||||
StatusBin =:= <<"in_progress">>;
|
||||
StatusBin =:= <<"resolved">>;
|
||||
StatusBin =:= <<"closed">> ->
|
||||
Status = binary_to_atom(StatusBin),
|
||||
Status = get_binary_to_atom(StatusBin),
|
||||
case logic_ticket:update_status(AdminId, TicketId, Status) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
@@ -148,15 +148,17 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second])).
|
||||
|
||||
binary_to_atom(<<"open">>) -> open;
|
||||
binary_to_atom(<<"in_progress">>) -> in_progress;
|
||||
binary_to_atom(<<"resolved">>) -> resolved;
|
||||
binary_to_atom(<<"closed">>) -> closed.
|
||||
get_binary_to_atom(<<"open">>) -> open;
|
||||
get_binary_to_atom(<<"in_progress">>) -> in_progress;
|
||||
get_binary_to_atom(<<"resolved">>) -> resolved;
|
||||
get_binary_to_atom(<<"closed">>) -> closed.
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -30,8 +30,10 @@ get_statistics(Req) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -16,7 +16,7 @@ handle(Req, _Opts) ->
|
||||
%% POST /v1/tickets - сообщить об ошибке (доступно всем)
|
||||
report_error(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
{ok, _UserId, Req1} ->
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
try jsx:decode(Body, [return_maps]) of
|
||||
Decoded when is_map(Decoded) ->
|
||||
@@ -111,8 +111,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -48,8 +48,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -50,8 +50,10 @@ authenticate(Req) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
@@ -47,8 +47,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
85
src/handlers/ws_handler.erl
Normal file
85
src/handlers/ws_handler.erl
Normal file
@@ -0,0 +1,85 @@
|
||||
-module(ws_handler).
|
||||
-behaviour(cowboy_websocket).
|
||||
|
||||
-export([init/2]).
|
||||
-export([websocket_init/1]).
|
||||
-export([websocket_handle/2]).
|
||||
-export([websocket_info/2]).
|
||||
-export([terminate/3]).
|
||||
|
||||
-record(state, {
|
||||
user_id :: binary() | undefined,
|
||||
subscriptions = [] :: [binary()]
|
||||
}).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
% Аутентификация через query параметр token
|
||||
Qs = cowboy_req:parse_qs(Req),
|
||||
case proplists:get_value(<<"token">>, Qs) of
|
||||
undefined ->
|
||||
{ok, cowboy_req:reply(401, #{}, <<"Missing token">>, Req), undefined};
|
||||
Token ->
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, Claims} ->
|
||||
UserId = maps:get(<<"user_id">>, Claims),
|
||||
{cowboy_websocket, Req, #state{user_id = UserId}};
|
||||
{error, _} ->
|
||||
{ok, cowboy_req:reply(401, #{}, <<"Invalid token">>, Req), undefined}
|
||||
end
|
||||
end.
|
||||
|
||||
websocket_init(State) ->
|
||||
% Регистрируем процесс в pg для получения уведомлений
|
||||
pg:join(eventhub_ws, self()),
|
||||
{ok, State}.
|
||||
|
||||
websocket_handle({text, Msg}, State) ->
|
||||
io:format("WebSocket received: ~s~n", [Msg]),
|
||||
try jsx:decode(Msg, [return_maps]) of
|
||||
#{<<"action">> := <<"subscribe">>, <<"calendar_id">> := CalendarId} ->
|
||||
io:format("Subscribe to calendar: ~s~n", [CalendarId]),
|
||||
NewSubs = [CalendarId | State#state.subscriptions],
|
||||
Reply = jsx:encode(#{status => <<"subscribed">>, calendar_id => CalendarId}),
|
||||
io:format("Sending reply: ~s~n", [Reply]),
|
||||
{reply, {text, Reply}, State#state{subscriptions = NewSubs}};
|
||||
#{<<"action">> := <<"ping">>} ->
|
||||
{reply, {text, <<"{\"status\":\"pong\"}">>}, State};
|
||||
Other ->
|
||||
io:format("Unknown action: ~p~n", [Other]),
|
||||
{ok, State}
|
||||
catch
|
||||
_:Error ->
|
||||
io:format("Error parsing WebSocket message: ~p~n", [Error]),
|
||||
{ok, State}
|
||||
end;
|
||||
websocket_handle(_Frame, State) ->
|
||||
{ok, State}.
|
||||
|
||||
websocket_info({notification, Type, Data}, State) ->
|
||||
case should_notify(Type, Data, State) of
|
||||
true ->
|
||||
Msg = jsx:encode(#{
|
||||
type => Type,
|
||||
data => Data,
|
||||
timestamp => os:system_time(seconds)
|
||||
}),
|
||||
{reply, {text, Msg}, State};
|
||||
false ->
|
||||
{ok, State}
|
||||
end;
|
||||
websocket_info(_Info, State) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_Reason, _Req, _State) ->
|
||||
pg:leave(eventhub_ws, self()),
|
||||
ok.
|
||||
|
||||
%% Проверка, нужно ли отправлять уведомление пользователю
|
||||
should_notify(calendar_update, #{calendar_id := CalId}, State) ->
|
||||
lists:member(CalId, State#state.subscriptions);
|
||||
should_notify(booking_update, #{user_id := UserId}, State) ->
|
||||
UserId =:= State#state.user_id;
|
||||
should_notify(event_update, #{calendar_id := CalId}, State) ->
|
||||
lists:member(CalId, State#state.subscriptions);
|
||||
should_notify(_, _, _) ->
|
||||
true.
|
||||
@@ -28,6 +28,7 @@ create_booking(UserId, EventId) ->
|
||||
case core_booking:create(ActualEventId, UserId) of
|
||||
{ok, Booking} ->
|
||||
handle_confirmation_policy(Booking, Event, Calendar),
|
||||
logic_notification:notify_booking(UserId, Booking), % ← Уведомление
|
||||
{ok, Booking};
|
||||
Error ->
|
||||
Error
|
||||
@@ -63,23 +64,24 @@ confirm_booking(UserId, BookingId, Action) when Action =:= confirm; Action =:= d
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
case Action of
|
||||
confirm ->
|
||||
core_booking:update_status(BookingId, confirmed);
|
||||
decline ->
|
||||
core_booking:update_status(BookingId, cancelled)
|
||||
NewStatus = case Action of
|
||||
confirm -> confirmed;
|
||||
decline -> cancelled
|
||||
end,
|
||||
case core_booking:update_status(BookingId, NewStatus) of
|
||||
{ok, Updated} ->
|
||||
logic_notification:notify_booking(Updated#booking.user_id, Updated),
|
||||
{ok, Updated};
|
||||
Error -> Error
|
||||
end;
|
||||
false ->
|
||||
{error, access_denied}
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
Error -> Error
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
Error -> Error
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
%% Отмена бронирования (участником)
|
||||
|
||||
@@ -16,6 +16,12 @@ create_report(ReporterId, TargetType, TargetId, Reason) ->
|
||||
true ->
|
||||
case core_report:create(ReporterId, TargetType, TargetId, Reason) of
|
||||
{ok, Report} ->
|
||||
logic_notification:notify_admin(report_created, #{
|
||||
report_id => Report#report.id,
|
||||
target_type => TargetType,
|
||||
target_id => TargetId,
|
||||
reason => Reason
|
||||
}),
|
||||
% Проверяем порог для авто-модерации
|
||||
check_auto_freeze(TargetType, TargetId),
|
||||
{ok, Report};
|
||||
@@ -116,7 +122,7 @@ freeze_calendar(AdminId, CalendarId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_calendar:get_by_id(CalendarId) of
|
||||
{ok, Calendar} ->
|
||||
{ok, _Calendar} ->
|
||||
core_calendar:update(CalendarId, [{status, frozen}]);
|
||||
Error -> Error
|
||||
end;
|
||||
@@ -128,7 +134,7 @@ unfreeze_calendar(AdminId, CalendarId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_calendar:get_by_id(CalendarId) of
|
||||
{ok, Calendar} ->
|
||||
{ok, _Calendar} ->
|
||||
core_calendar:update(CalendarId, [{status, active}]);
|
||||
Error -> Error
|
||||
end;
|
||||
@@ -140,7 +146,7 @@ freeze_event(AdminId, EventId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} ->
|
||||
{ok, _Event} ->
|
||||
core_event:update(EventId, [{status, frozen}]);
|
||||
Error -> Error
|
||||
end;
|
||||
@@ -152,7 +158,7 @@ unfreeze_event(AdminId, EventId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} ->
|
||||
{ok, _Event} ->
|
||||
core_event:update(EventId, [{status, active}]);
|
||||
Error -> Error
|
||||
end;
|
||||
|
||||
56
src/logic/logic_notification.erl
Normal file
56
src/logic/logic_notification.erl
Normal file
@@ -0,0 +1,56 @@
|
||||
-module(logic_notification).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([notify_booking/2]).
|
||||
-export([notify_calendar_update/1]).
|
||||
-export([notify_event_update/1]).
|
||||
-export([notify_admin/2]).
|
||||
|
||||
%% Уведомление о бронировании
|
||||
notify_booking(UserId, Booking) ->
|
||||
Data = #{
|
||||
booking_id => Booking#booking.id,
|
||||
event_id => Booking#booking.event_id,
|
||||
status => Booking#booking.status
|
||||
},
|
||||
broadcast_to_user(UserId, booking_update, Data).
|
||||
|
||||
%% Уведомление об обновлении календаря
|
||||
notify_calendar_update(Calendar) ->
|
||||
Data = #{
|
||||
calendar_id => Calendar#calendar.id,
|
||||
title => Calendar#calendar.title,
|
||||
status => Calendar#calendar.status
|
||||
},
|
||||
broadcast_to_calendar_subscribers(Calendar#calendar.id, calendar_update, Data).
|
||||
|
||||
%% Уведомление об обновлении события
|
||||
notify_event_update(Event) ->
|
||||
Data = #{
|
||||
event_id => Event#event.id,
|
||||
calendar_id => Event#event.calendar_id,
|
||||
title => Event#event.title,
|
||||
status => Event#event.status,
|
||||
start_time => Event#event.start_time
|
||||
},
|
||||
broadcast_to_calendar_subscribers(Event#event.calendar_id, event_update, Data).
|
||||
|
||||
%% Уведомление для администраторов
|
||||
notify_admin(Type, Data) ->
|
||||
Message = {admin_notification, Type, Data},
|
||||
% Отправляем всем админам
|
||||
[Pid ! Message || Pid <- pg:get_members(eventhub_admin_ws)],
|
||||
% Также отправляем в каналы
|
||||
[Pid ! Message || Pid <- pg:get_members({eventhub_admin_channel, Type})],
|
||||
ok.
|
||||
|
||||
%% Внутренние функции
|
||||
broadcast_to_user(UserId, Type, Data) ->
|
||||
Message = {notification, Type, Data#{user_id => UserId}},
|
||||
[Pid ! Message || Pid <- pg:get_members(eventhub_ws)].
|
||||
|
||||
broadcast_to_calendar_subscribers(_CalendarId, _Type, _Data) ->
|
||||
% В будущем можно фильтровать по подпискам
|
||||
% Сейчас отправляем всем подключённым пользователям
|
||||
Message = {notification, calendar_update, _Data},
|
||||
[Pid ! Message || Pid <- pg:get_members(eventhub_ws)].
|
||||
@@ -60,7 +60,7 @@ cancel_subscription(AdminId, SubscriptionId) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_subscription:get_by_id(SubscriptionId) of
|
||||
{ok, Subscription} ->
|
||||
{ok, _Subscription} ->
|
||||
core_subscription:update_status(SubscriptionId, cancelled);
|
||||
Error -> Error
|
||||
end;
|
||||
|
||||
@@ -58,7 +58,7 @@ resolve_ticket(AdminId, TicketId, ResolutionNote) ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
case core_ticket:add_resolution(TicketId, ResolutionNote) of
|
||||
{ok, Ticket} ->
|
||||
{ok, _Ticket} ->
|
||||
core_ticket:update_status(TicketId, resolved);
|
||||
Error -> Error
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user