|
7 | 7 | using namespace drogon;
|
8 | 8 |
|
9 | 9 | // send a message
|
10 |
| -void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) { |
| 10 | +void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) |
| 11 | +{ |
11 | 12 | auto body = req->getBody();
|
12 | 13 | Json::Value req_json, res_json;
|
13 | 14 | Json::Reader reader;
|
14 | 15 | std::string bodyStr(body);
|
15 |
| - if (!reader.parse(bodyStr, req_json)) { |
| 16 | + if (!reader.parse(bodyStr, req_json)) |
| 17 | + { |
16 | 18 | callback(HttpResponse::newHttpResponse());
|
17 | 19 | return;
|
18 | 20 | }
|
19 | 21 | Json::FastWriter writer;
|
20 | 22 | auto res = HttpResponse::newHttpResponse();
|
21 | 23 | res->addHeader("Access-Control-Allow-Origin", "*");
|
22 |
| - if (jwtVerify(req)) { |
| 24 | + if (jwtVerify(req)) |
| 25 | + { |
23 | 26 | std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
|
24 | 27 | std::string content = req_json["content"].asString();
|
25 | 28 | std::string receiver = req_json["receiver"].asString();
|
26 | 29 | sql_addhistory(sender, receiver, content, "0");
|
27 | 30 | std::string msg = req_json["content"].asString();
|
28 | 31 | auto output = writer.write(res_json);
|
29 | 32 | res->setBody(output);
|
30 |
| - } else { |
| 33 | + } |
| 34 | + else |
| 35 | + { |
31 | 36 | res->setBody("No Authorization");
|
32 | 37 | }
|
33 | 38 | callback(res);
|
34 | 39 | }
|
35 | 40 | // get message history
|
36 |
| -void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) { |
| 41 | +void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) |
| 42 | +{ |
37 | 43 | Json::Value res_json;
|
38 | 44 | Json::Reader reader;
|
39 | 45 | std::string me;
|
40 | 46 | Json::FastWriter writer;
|
41 | 47 | auto res = HttpResponse::newHttpResponse();
|
42 | 48 | res->addHeader("Access-Control-Allow-Origin", "*");
|
43 |
| - if (jwtVerify(req)) { |
| 49 | + if (jwtVerify(req)) |
| 50 | + { |
44 | 51 | me = jwtDecrypt(req->getHeader("Authorization").substr(7));
|
45 | 52 | auto output = writer.write(sql_find_my_msg(me));
|
46 | 53 | res->setBody(output);
|
47 |
| - } else { |
| 54 | + } |
| 55 | + else |
| 56 | + { |
48 | 57 | res->setBody("No Authorization");
|
49 | 58 | }
|
50 | 59 | callback(res);
|
51 | 60 | }
|
52 | 61 | // request new friend or cancel request
|
53 |
| -void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) { |
| 62 | +void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) |
| 63 | +{ |
54 | 64 | auto res = HttpResponse::newHttpResponse();
|
55 | 65 | res->addHeader("Access-Control-Allow-Origin", "*");
|
56 |
| - if (jwtVerify(req)) { |
| 66 | + if (jwtVerify(req)) |
| 67 | + { |
57 | 68 | std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
|
58 | 69 | std::string receiver = req->getParameter("username");
|
59 | 70 | std::string operation = req->getParameter("operation");
|
60 | 71 | if (operation == "add")
|
61 |
| - sql_addrequest(sender, receiver); |
| 72 | + { |
| 73 | + if (sql_findexist(receiver)) |
| 74 | + { |
| 75 | + sql_addrequest(sender, receiver); |
| 76 | + res->setBody("Success"); |
| 77 | + } |
| 78 | + else |
| 79 | + res->setBody("No this body"); |
| 80 | + } |
62 | 81 | else
|
| 82 | + { |
63 | 83 | sql_delete_operation(sender, receiver);
|
64 |
| - res->setBody("Success"); |
65 |
| - } else { |
| 84 | + res->setBody("Success"); |
| 85 | + } |
| 86 | + } |
| 87 | + else |
| 88 | + { |
66 | 89 | res->setBody("No Authorization");
|
67 | 90 | }
|
68 | 91 | callback(res);
|
69 | 92 | }
|
70 | 93 | // handle new friend request
|
71 |
| -void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) { |
| 94 | +void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) |
| 95 | +{ |
72 | 96 | auto res = HttpResponse::newHttpResponse();
|
73 | 97 | res->addHeader("Access-Control-Allow-Origin", "*");
|
74 |
| - if (jwtVerify(req)) { |
| 98 | + if (jwtVerify(req)) |
| 99 | + { |
75 | 100 | std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
|
76 | 101 | std::string sender = req->getParameter("username");
|
77 | 102 | std::string attitude = req->getParameter("info");
|
78 | 103 | sql_process_request(sender, receiver, attitude);
|
79 | 104 | res->setBody("Success");
|
80 |
| - } else { |
| 105 | + } |
| 106 | + else |
| 107 | + { |
81 | 108 | res->setBody("No Authorization");
|
82 | 109 | }
|
83 | 110 | callback(res);
|
84 | 111 | }
|
85 | 112 | // get chat info
|
86 |
| -void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) { |
| 113 | +void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) |
| 114 | +{ |
87 | 115 | auto body = req->getBody();
|
88 | 116 | Json::Value req_json, res_json;
|
89 | 117 | Json::Reader reader;
|
90 | 118 | std::string bodyStr(body);
|
91 |
| - if (!reader.parse(bodyStr, req_json)) { |
| 119 | + if (!reader.parse(bodyStr, req_json)) |
| 120 | + { |
92 | 121 | callback(HttpResponse::newHttpResponse());
|
93 | 122 | return;
|
94 | 123 | }
|
95 | 124 | std::string me, who_send_me;
|
96 | 125 | Json::FastWriter writer;
|
97 | 126 | auto res = HttpResponse::newHttpResponse();
|
98 | 127 | res->addHeader("Access-Control-Allow-Origin", "*");
|
99 |
| - if (jwtVerify(req)) { |
| 128 | + if (jwtVerify(req)) |
| 129 | + { |
100 | 130 | me = jwtDecrypt(req->getHeader("Authorization").substr(7));
|
101 |
| - if (req_json["person"].asString() == "") { |
| 131 | + if (req_json["person"].asString() == "") |
| 132 | + { |
102 | 133 | res->setBody(writer.write(get_chat_info(me, "")));
|
103 |
| - } else { |
| 134 | + } |
| 135 | + else |
| 136 | + { |
104 | 137 | who_send_me = req_json["person"].asString();
|
105 | 138 | res->setBody(writer.write(get_chat_info(me, who_send_me)));
|
106 | 139 | }
|
107 |
| - } else { |
| 140 | + } |
| 141 | + else |
| 142 | + { |
108 | 143 | res->setBody("No Authorization");
|
109 | 144 | }
|
110 | 145 | callback(res);
|
|
0 commit comments