-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialect.appdotnet_official.callbacks.js
155 lines (145 loc) · 4.27 KB
/
dialect.appdotnet_official.callbacks.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
function sendrepsonse(json, resp) {
if (resp.prettyPrint) {
json=JSON.stringify(JSON.parse(json),null,4);
}
//resp.set('Content-Type', 'text/javascript');
resp.type('application/json');
resp.send(json);
}
function ISODateString(d) {
if (!d.getUTCFullYear) {
console.log('created_at is type (!date): ',d);
return d;
}
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(parseInt(d.getUTCSeconds()))+'Z';
}
function formatuser(user) {
if (user) {
user.id=''+user.id;
user.username=''+user.username; // 530 was cast as an int
user.created_at=ISODateString(user.created_at);
user.counts.following=parseInt(0+user.counts.following);
user.counts.posts=parseInt(0+user.counts.posts);
user.counts.followers=parseInt(0+user.counts.followers);
user.counts.stars=parseInt(0+user.counts.stars);
if (user.name) {
user.name=''+user.name;
}
}
return user;
}
function formatpost(post) {
// cast fields to make sure they're the correct type
// Now to cast
if (post) {
post.id=''+post.id; // cast to String
post.num_replies=parseInt(0+post.num_replies); // cast to Number
post.num_reposts=parseInt(0+post.num_reposts); // cast to Number
post.num_stars=parseInt(0+post.num_stars); // cast to Number
post.machine_only=post.machine_only?true:false;
post.thread_id=''+post.thread_id; // cast to String (Number is too big for js?)
if (post.reply_to) {
post.reply_to=''+post.reply_to; // cast to String (Number is too big for js?)
}
if (post.repost_of) {
post.repost_of=''+post.repost_of; // cast to String (Number is too big for js?)
}
// remove microtime
post.created_at=ISODateString(post.created_at);
}
return post;
}
module.exports = {
'postsCallback' : function(resp) {
return function(posts, err, meta) {
for(var i in posts) {
var post=posts[i];
posts[i].you_reposted=false;
posts[i].you_starred=false;
posts[i].user.follows_you=false;
posts[i].user.you_blocked=false;
posts[i].user.you_follow=false;
posts[i].user.you_muted=false;
posts[i].user.you_can_subscribe=false;
posts[i].user.you_can_follow=true;
}
// meta order: min_id, code, max_id, more
var res={
meta: meta,
data: posts
};
if (res.meta==undefined) {
res.meta={ code: 200 };
}
sendrepsonse(JSON.stringify(res), resp);
}
},
'usersCallback' : function(resp) {
return function(posts, err, meta) {
var users=[];
for(var i in posts) {
var post=posts[i];
posts[i].you_reposted=false;
posts[i].you_starred=false;
posts[i].user.follows_you=false;
posts[i].user.you_blocked=false;
posts[i].user.you_follow=false;
posts[i].user.you_muted=false;
posts[i].user.you_can_subscribe=false;
posts[i].user.you_can_follow=true;
users.push(post.user);
}
// meta order: min_id, code, max_id, more
var res={
meta: meta,
data: users
};
if (res.meta==undefined) {
res.meta={ code: 200 };
}
sendrepsonse(JSON.stringify(res), resp);
}
},
'postCallback' : function(resp) {
return function(post, err, meta) {
var res={
meta: { code: 200 },
data: formatpost(post)
};
if (post && post.user) {
res.data.user=formatuser(post.user);
}
if (meta) {
res.meta=meta;
}
sendrepsonse(JSON.stringify(res), resp);
}
},
'dataCallback' : function(resp) {
return function(data, err, meta) {
err = typeof err !== 'undefined' ? err : undefined;
meta = typeof meta !== 'undefined' ? meta : undefined;
var res={
meta: meta,
data: data
};
if (res.meta==undefined) {
res.meta={ code: 200 };
}
sendrepsonse(JSON.stringify(res), resp);
}
},
'oembedCallback' : function(resp) {
return function(oembed, err) {
// there's no data/meta envelope for oembed
//console.log('ADNO::oembed got ',oembed);
sendrepsonse(JSON.stringify(oembed), resp);
}
},
}