diff --git a/apps/roster/include/static/rest_text.hrl b/apps/roster/include/static/rest_text.hrl index 4f46c4a842b7c20d1e2f772f460db43be26abaf2..692e23675de19fc5c4f2773e9bc21cd01b20c6f4 100644 --- a/apps/roster/include/static/rest_text.hrl +++ b/apps/roster/include/static/rest_text.hrl @@ -11,7 +11,6 @@ -define(ERROR_403, <<"Forbidden">>). -define(ERROR_404, <<"Not Found">>). -define(ERROR_405, <<"Method Not Allowed">>). --define(ERROR_422, <<"Unprocessable Entity">>). -define(ERROR_INVALID_JSON, <<"Invalid Request Json">>). -define(ERROR_MISSING_PARAM, <<"Missing Request Parameter(s)">>). -define(ERROR_INVALID_REQUEST_PARAM, <<"Invalid Request Parameter(s)">>). diff --git a/apps/roster/include/static/rest_var.hrl b/apps/roster/include/static/rest_var.hrl index 22c1afb79f6823720461792dc046594d4923630d..b31bbc5cf0a405aed841e2777565e0afdfed090a 100644 --- a/apps/roster/include/static/rest_var.hrl +++ b/apps/roster/include/static/rest_var.hrl @@ -46,5 +46,4 @@ -define(HTTP_CODE_401, 401). -define(HTTP_CODE_403, 403). -define(HTTP_CODE_404, 404). --define(HTTP_CODE_405, 405). --define(HTTP_CODE_422, 422). \ No newline at end of file +-define(HTTP_CODE_405, 405). \ No newline at end of file diff --git a/apps/roster/src/rest/helpers/rest_response_helper.erl b/apps/roster/src/rest/helpers/rest_response_helper.erl index 3fc70cc5e99b4e7131c8a1f27857957db8f27e19..9db602ac82c34bce13f64f40416b3a210943fac1 100644 --- a/apps/roster/src/rest/helpers/rest_response_helper.erl +++ b/apps/roster/src/rest/helpers/rest_response_helper.erl @@ -5,9 +5,9 @@ -export([ description/0, response/3, response/4, - error_400/0, error_401/0, error_404/0, error_405/0, error_422/0, - error_400_response/1, error_404_response/1, error_405_response/1, error_422_response/1, - error_response/0, error_response/1, error_response/2, + error_400/0, error_401/0, error_404/0, error_405/0, + error_400_response/1, error_405_response/1, + error_response/0, error_response/1, error_response/2, error_response/3, success_response/0, success_response/1, success_response/2, success_200/0 ]). @@ -38,9 +38,6 @@ error_404() -> error_405() -> error_response(?HTTP_CODE_405, ?ERROR_405). -error_422() -> - error_response(?HTTP_CODE_422, ?ERROR_422). - %% Dynamic error_response() -> @@ -55,22 +52,18 @@ error_response(Status, Msg) -> response_json(Status, jsx:encode([{<<"message">>, nitro:to_binary(Msg)}])). +error_response(Req, Status, Message) -> + roster:info(?MODULE, "NotFound:~p:~p:~p, Error: ~p", [Req:get(method), Req:get(path), Req:parse_post(), Message]), + response(Req, Status, error_response(Status, Message)). + error_400_response(Req) -> roster:info(?MODULE, "BadRequest:~p:~p:~p", [Req:get(method), Req:get(path), Req:parse_post()]), response(Req, ?HTTP_CODE_400, rest_response_helper:error_400()). -error_404_response(Req) -> - roster:info(?MODULE, "NotFound:~p:~p:~p", [Req:get(method), Req:get(path), Req:parse_post()]), - response(Req, ?HTTP_CODE_404, rest_response_helper:error_404()). - error_405_response(Req) -> roster:info(?MODULE, "MethodNotAllowed:~p:~p", [Req:get(method), Req:get(path)]), response(Req, ?HTTP_CODE_405, rest_response_helper:error_405()). -error_422_response(Req) -> - roster:info(?MODULE, "UnprocessableEntity:~p:~p:~p", [Req:get(method), Req:get(path), Req:parse_post()]), - response(Req, ?HTTP_CODE_422, rest_response_helper:error_422()). - %% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% SUCCESS %% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/apps/roster/src/rest/rest_chat_csv.erl b/apps/roster/src/rest/rest_chat_csv.erl index 6cd0f109c9ec8aea652d8cc53e3cedec12b8b8e3..4109eb02101aaa2d104a4bc0d14d3bd56d48a481 100644 --- a/apps/roster/src/rest/rest_chat_csv.erl +++ b/apps/roster/src/rest/rest_chat_csv.erl @@ -26,7 +26,7 @@ %%% API %%%============================================================================= -handle_request('POST', Path, Req) when Path == ?CSV_P2P_ENDPOINT; Path == ?CSV_MUC_ENDPOINT -> +handle_request('POST', Path, Req) when Path =:= ?CSV_P2P_ENDPOINT; Path =:= ?CSV_MUC_ENDPOINT -> ReqData = Req:parse_post(), roster:info(?MODULE, "~p:Request:~p:~p", [Req:get(method), Req:get(path), ReqData]), @@ -41,21 +41,36 @@ handle_request('POST', Path, Req) when Path == ?CSV_P2P_ENDPOINT; Path == ?CSV_M Filename = ["NynjaPrivateChat-", FromUsername, "-", ToUsername, ".csv"], send_response(CsvText, Filename, Req) catch - throw:no_username_match -> rest_response_helper:error_422_response(Req); - throw:no_chat_history -> rest_response_helper:error_404_response(Req); - throw:no_username -> rest_response_helper:error_422_response(Req); - throw:no_muc_name -> rest_response_helper:error_404_response(Req); - throw:no_room -> rest_response_helper:error_404_response(Req); - _:_ -> rest_response_helper:error_404_response(Req) + throw:{no_chat_history, {p2p, User1, User2}} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, io_lib:format("No chat history for: ~s/~s", [User1, User2])); + throw:{no_username_match, PhoneId} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "No User matches phone_id: " ++ PhoneId); + throw:{no_username, Id} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "No User with id: " ++ Id); + _:_ -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "Something went wrong!") end; {ok, RoomId} -> - ChatHistory = get_chat_history({muc, RoomId}), - Members = get_group_chat_members(RoomId), - GroupChatName = get_group_chat_name(RoomId), - CsvText = conversation_to_csv(ChatHistory, Members, GroupChatName), - Filename = ["NynjaGroupChat-", GroupChatName, ".csv"], - send_response(CsvText, Filename, Req); + try + ChatHistory = get_chat_history({muc, RoomId}), + Members = get_group_chat_members(RoomId), + GroupChatName = get_group_chat_name(RoomId), + CsvText = conversation_to_csv(ChatHistory, Members, GroupChatName), + Filename = ["NynjaGroupChat-", GroupChatName, ".csv"], + send_response(CsvText, Filename, Req) + catch + throw:{no_chat_history, {muc, RoomId}} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, io_lib:format("No chat history for: ~s", [RoomId])); + throw:{no_username_match, PhoneId} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "No member matches phone_id: " ++ PhoneId); + throw:{no_muc_name, RoomId} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "No group chat is associated with room_id: " ++ RoomId); + throw:{no_room, RoomId} -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, io_lib:format("Group chat with room_id: ~s doesn't exist", [RoomId])); + _:_ -> + rest_response_helper:error_response(Req, ?HTTP_CODE_404, "Something went wrong!") + end; {error, wrong_data} -> rest_response_helper:error_400_response(Req) @@ -74,6 +89,11 @@ send_response(CsvText, Filename, Req) -> roster:info(?MODULE, "ResponseData:~p", [ResponseData]), Req:respond({ResponseStatus, ResponseHeader, ResponseData}). +-spec parse_incomming_data(list(tuple())) -> {ok, term()} | {error, wrong_data}. +parse_incomming_data([{"from", FromPhoneId}, {"to", ToPhoneId}]) -> {ok, {list_to_binary(FromPhoneId), list_to_binary(ToPhoneId)}}; +parse_incomming_data([{"muc", RoomId}]) -> {ok, list_to_binary(RoomId)}; +parse_incomming_data(_) -> {error, wrong_data}. + -spec conversation_to_csv(ChatHistory::type_chat_history(), Members::list(type_user_data()), CsvColumnsData::string()) -> string(). conversation_to_csv(ChatHistory, Members, CsvColumnsData) -> ChatData = extract_chat_data(ChatHistory, Members), @@ -81,11 +101,6 @@ conversation_to_csv(ChatHistory, Members, CsvColumnsData) -> CsvColumns = build_csv_columns(ChatSpan, CsvColumnsData), build_csv_text(ChatData, CsvColumns). --spec parse_incomming_data(list(tuple())) -> {ok, term()} | {error, wrong_data}. -parse_incomming_data([{"from", FromPhoneId}, {"to", ToPhoneId}]) -> {ok, {list_to_binary(FromPhoneId), list_to_binary(ToPhoneId)}}; -parse_incomming_data([{"muc", RoomId}]) -> {ok, list_to_binary(RoomId)}; -parse_incomming_data(_) -> {error, wrong_data}. - -spec build_csv_text(type_chat(), Acc::string()) -> string(). build_csv_text([{Date, Time, Username, Message} | T], Acc) -> build_csv_text(T, [Acc, [$\n, Date, $,, Time, $,, Username, $,, Message]]); @@ -117,10 +132,9 @@ extract_chat_data(ChatHistory, Members) -> Message = format_message(PayloadType, Payload), {FormattedDate, FormattedTime, Username, Message} end - || - #'Message'{created = TimeCreated, - from = PhoneId, - files = [#'Desc'{mime = PayloadType, payload = Payload}]} <- ChatHistory + || #'Message'{created = TimeCreated, + from = PhoneId, + files = [#'Desc'{mime = PayloadType, payload = Payload}]} <- ChatHistory ]. -spec extract_user_id(binary()) -> non_neg_integer(). @@ -131,9 +145,9 @@ extract_user_id(PhoneId) -> -spec match_member_to_phone_id(Members::list(type_user_data()), PhoneId::binary()) -> string(). match_member_to_phone_id(Members, PhoneId) -> case lists:dropwhile(fun({Id, _}) -> Id /= PhoneId end, Members) of - [] -> throw(no_username_match); + [] -> throw({no_username_match, PhoneId}); [{_, Username} | _] -> Username; - _ -> throw(no_username_match) + _ -> throw({no_username_match, PhoneId}) end. -spec format_date(tuple()) -> string(). @@ -154,21 +168,20 @@ format_message(<<"location">>, Payload) -> [<<"Location: ">>, Payload]. get_username_by_id(Id) -> case kvs:get('Roster', Id) of {ok, #'Roster'{names = Name, surnames = Surname}} -> io_lib:format("~s ~s", [Name, Surname]); - {error, _} -> throw(no_username) + {error, _} -> throw({no_username, Id}) end. -spec get_group_chat_members(RoomId::binary()) -> list(#'Member'{}). get_group_chat_members(RoomId) -> case roster:members({muc, RoomId}) of [] -> - throw(no_room); + throw({no_room, RoomId}); Members -> [ {PhoneId, io_lib:format("~s ~s", [Name, Surname])} - || - #'Member'{names = Name, - surnames = Surname, - phone_id = PhoneId} <- Members + || #'Member'{names = Name, + surnames = Surname, + phone_id = PhoneId} <- Members ] end. @@ -176,13 +189,13 @@ get_group_chat_members(RoomId) -> get_group_chat_name(RoomId) -> case kvs:get('Room', RoomId) of {ok, #'Room'{name = RoomName}} -> RoomName; - {error, _} -> throw(no_muc_name) + {error, _} -> throw({no_muc_name, RoomId}) end. -spec get_chat_history(Feed::tuple()) -> list(#'Message'{}). get_chat_history(Feed) -> case roster_db:get_chain('Message', Feed) of - [] -> throw(no_chat_history); + [] -> throw({no_chat_history, Feed}); History -> History end.