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