-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_search.c
117 lines (98 loc) · 3.27 KB
/
api_search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <onion/block.h>
#include "bmy/search.h"
#include "api.h"
#include "apilib.h"
struct json_object *convert_search_result_to_json(const struct fileheader_utf *search_result, size_t search_size);
int api_search_content(ONION_FUNC_PROTO_STR) {
DEFINE_COMMON_SESSION_VARS;
int rc;
size_t search_size;
struct fileheader_utf *search_result;
struct json_object *req_json, *obj_tmp, *result = NULL;
const char *body, *board, *query;
const struct boardmem *bmem;
const onion_block *block;
if (!api_check_method(req, OR_POST)) {
return api_error(p, req, res, API_RT_WRONGMETHOD);
}
rc = api_check_session(req, cookie_buf, sizeof(cookie_buf), &cookie, &utmp_idx, &ptr_info);
if (rc != API_RT_SUCCESSFUL) {
return api_error(p, req, res, rc);
}
if ((block = onion_request_get_data(req)) == NULL || (body = onion_block_data(block)) == NULL || body[0] == '\0') {
return api_error(p, req, res, API_RT_WRONGPARAM);
}
if ((req_json = json_tokener_parse(body)) != NULL) {
if ((obj_tmp = json_object_object_get(req_json, "board")) != NULL) {
board = json_object_get_string(obj_tmp);
} else {
rc = API_RT_WRONGPARAM;
}
if (rc == API_RT_SUCCESSFUL) {
if ((obj_tmp = json_object_object_get(req_json, "query")) != NULL) {
query = json_object_get_string(obj_tmp);
} else {
rc = API_RT_WRONGPARAM;
}
}
if (rc == API_RT_SUCCESSFUL) {
if ((bmem = ythtbbs_cache_Board_get_board_by_name(board)) != NULL) {
if (check_user_read_perm_x(ptr_info, bmem)) {
if ((search_result = bmy_search_board(bmem->header.filename, query, &search_size)) != NULL) {
result = convert_search_result_to_json(search_result, search_size);
free(search_result);
} else {
rc = API_RT_NOTINRCD;
}
} else {
rc = API_RT_NOSUCHBRD;
}
} else {
rc = API_RT_NOSUCHBRD;
}
}
json_object_put(req_json);
}
if (result != NULL) {
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
json_object_put(result);
return OCS_PROCESSED;
} else {
return api_error(p, req, res, rc);
}
}
struct json_object *convert_search_result_to_json(const struct fileheader_utf *search_result, size_t search_size) {
struct json_object *result = NULL, *array, *obj, *field;
size_t i;
const struct fileheader_utf *p;
if (search_result) {
if ((result = json_object_new_object()) != NULL) {
if ((field = json_object_new_int(API_RT_SUCCESSFUL)) != NULL) {
json_object_object_add(result, "errcode", field);
}
if ((array = json_object_new_array_ext(search_size)) != NULL) {
for (i = 0; i < search_size; i++) {
p = &search_result[i];
if ((obj = json_object_new_object()) != NULL) {
if ((field = json_object_new_string(p->title)) != NULL) {
json_object_object_add(obj, "title", field);
}
if ((field = json_object_new_string(p->owner)) != NULL) {
json_object_object_add(obj, "owner", field);
}
if ((field = json_object_new_int64(p->filetime)) != NULL) {
json_object_object_add(obj, "aid", field);
}
if ((field = json_object_new_int64(p->thread)) != NULL) {
json_object_object_add(obj, "tid", field);
}
json_object_array_add(array, obj);
}
}
json_object_object_add(result, "result", array);
}
}
}
return result;
}