This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.js
159 lines (142 loc) · 4.74 KB
/
api.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var common = require ('./lib/common');
var config = require ('./config').values;
exports.configure = function (app, redis, module_users, module_cats, module_tags){
function format_tags (simple_arr_tags){
var tags = [];
for (var t=0, tl=simple_arr_tags.length;t<tl; t++){
tags.push ({t: simple_arr_tags[t], n: 0});
}
return sort_tags(tags);
}
function get_unique_tags_by_users(users){
var tags = []
for(var u=0,l=users.length; u<l; u++){
for (var t=0, tl=users[u].tags.length;t<tl; t++){
var found=false;
for (var i=0,il=tags.length;i<il; i++){
if (tags[i].t==users[u].tags[t]){
found=true;
tags[i].n++;
}
}
if (!found)
tags.push ({t: users[u].tags[t], n: 1});
}
}
return sort_tags(tags);
}
function sort_tags (tags){
function sorter (a,b){
return ((a.t < b.t) ? -1 : ((a.t > b.t) ? 1 : 0));
}
return tags.sort(sorter);
}
function tags_with_at_least (tags, n){ //return tags with minimum number of items
return tags.filter(function(tag){return tag.n>=n}, tags);
}
function PrepareForDisplayUsers (req, users){ //users or user
if (!users) return null;
if (!Array.isArray(users)) //single object, not array
return common.removeUnwantedFields(users);
var sortfield = req.query["sort"] || 'name';
var desc=false
if (sortfield[sortfield.length-1]=="_"){
desc=true;
sortfield = sortfield.substring(0, sortfield.length-1);
}
return common.sort(common.removeUnwantedFields(users), sortfield, desc);
}
app.get('/api/tags', function(req, res){
module_tags.GetTags (redis, {}, function (err, tags){
common.renderJSON(req, res, {tags:sort_tags(tags)} , 200, req.query["callback"])
});
});
app.get('/api/cats', function(req, res){
module_cats.GetCats (redis, {}, function (err, cats){
common.renderJSON(req, res, {cats:cats} , 200, req.query["callback"])
});
});
app.get('/api/tagsautocomplete', function(req, res){
module_tags.GetTags (redis, {}, function (err, tags){
var tags_text=[]
for (var i=0, l=tags.length;i<l;i++){
tags_text.push(tags[i].t)
}
res.send (tags_text.join('\n'))
});
});
app.get('/api/users', function(req, res){
var params = {
id_cat : req.query["id_cat"] || '',
tag : req.query["tag"] || '',
scope: req.query["scope"],
sort: req.query["sort"],
pagination : {from : req.query["from"] || 0, pagesize : req.query["page"] || config.default_page_size},
logged_user: req.session.user || null
}
module_cats.GetCats (redis, params, function (err, cats){
module_tags.GetTags (redis, params, function (err, tags){
module_users.GetUsers(redis, params, function(err, users, total_records){
var cat=null;
for (var i=0;i<cats.length;i++){
if (cats[i].id==params.id_cat)
{
cat=cats[i];
break;
}
}
common.renderJSON(req, res, {
users: common.removeUnwantedFields(users),
tags: sort_tags(tags_with_at_least(tags,config.min_tags_count_to_show)),
cats: cats,
cat: cat,
pagination: {
pagesize: params.pagination.pagesize,
from: params.pagination.from,
total: Math.ceil(total_records / params.pagination.pagesize),
total_records: total_records
},
tag: params.tag,
scope: params.scope,
logged_user: common.removeUnwantedFields(req.session.user)
}, 200, req.query["callback"])
});
});
});
});
app.get('/api/users/byid', function(req, res){
var params = {id : req.query["id"], logged_user: req.session.user}
module_users.GetUser (redis, params, function (err, user){
common.renderJSON(req, res, {user: user ? common.removeUnwantedFields(user) : null, logged_user: common.removeUnwantedFields(req.session.user)}, 200, req.query["callback"])
})
});
app.get('/api/search', function(req, res){
var params = {
q : req.query["search"] || req.query["q"] || null,
scope: req.query["scope"],
pagination : {from : req.query["from"] || 0, pagesize : req.query["page"] || config.default_page_size},
logged_user: req.session.user
}
module_cats.GetCats (redis, params, function (err, cats){
module_users.Search (redis, params, function (err, users, total_records){
if (err){
common.renderJSON(req, res, {error: err});
}
else{
common.renderJSON(req, res, {
cats: cats,
users: PrepareForDisplayUsers(req, users),
tags: tags_with_at_least(get_unique_tags_by_users(users),config.min_tags_count_to_show),
logged_user: common.removeUnwantedFields(req.session.user),
pagination: {
pagesize: params.pagination.pagesize,
from: params.pagination.from,
total: Math.ceil(total_records / params.pagination.pagesize),
total_records: total_records
}
}, 200, req.query["callback"])
}
})
})
});
}