forked from SiTLar/pyt-vanilla-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freefeed.js
307 lines (304 loc) · 8.18 KB
/
freefeed.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"use strict";
define(["./utils", "./freefeed_rt"],function(utils, RtUpdate ){
return function(config){
function get(token, url){
return utils.xhr( {"url":config.serverURL +url ,"token":token });
}
function get2(token, url){
return utils.xhr( {"url":config.serverURLV2 +url ,"token":token });
}
return{
"name": "FreeFeed"
,"protocol":{
"get": get
,"getTimeline": function(token, timeline, skip, mode) {
if (timeline == "filter/best_of") return get2( token
,"bestof" + "?offset="+skip
);
if (timeline == "filter/everything") return get2( token
,"everything" + "?offset="+skip
);
if (timeline == "home"){
var modeQ = "";
if(typeof mode == "string" )
modeQ = "&homefeed-mode="+ mode;
return get2( token
,"timelines/home" + "?offset="+skip + modeQ
);
}
return get2( token
,"timelines/"+ timeline + "?offset="+skip
);
}
,"getSearch": function(token, search, skip) {
return get2( token
,"search?qs=" + search + "&offset="+skip
).then(function(res){
res = JSON.parse(res);
res.timelines = {"id":search};
return res;
});
}
,"getNotifications": function(token, search, skip) {
return get2( token
,"notifications?offset="+skip+(search?("&"+search):"")
).then(function(res){
utils.xhr({
"url": config.serverURLV2
+"users/markAllNotificationsAsRead"
,"method":"post"
,"token": token
,"headers":{"Content-type":"application/json"}
});
return res;
});
}
,"getSummary": function(token,source,interval){
var address = "summary/" +(source?source+"/":"") +interval;
return get2(token ,address);
;
}
,"getMemories": function(token,source,interval, skip){
var address = "timelines/" + source
+"/?created-before="+interval.toISOString()
+"&sort=created"
+"&offset="+skip;
return get2(token ,address);
;
}
,"getPost": function(token, path, arrOptions) {
var id = path.split("/")[1];
var likes = "0";
var cmts = "0";
if(Array.isArray(arrOptions))arrOptions.forEach(function(option){
switch(option){
case "likes":
likes = "all";
break;
case "comments":
cmts = "all";
}
});
var suffix = "?maxComments=" + cmts + "&maxLikes=" + likes;
return get2(token, "posts/"+ id + suffix);
}
,"sendPost": function(token, postdata){
return utils.xhr(
{ "url": config.serverURL + "posts"
,"token": token
,"method": "post"
,"data": JSON.stringify(postdata)
,"headers":{"Content-type":"application/json"}
}
);
}
,"editPost": function(token, id, postdata){
return utils.xhr(
{ "url": config.serverURL + "posts/"+id
,"token": token
,"method": "put"
,"data": JSON.stringify(postdata)
,"headers":{"Content-type":"application/json"}
}
);
}
,"deletePost": function(token, id){
return utils.xhr(
{ "url": config.serverURL + "posts/"+id
,"token": token
,"method": "delete"
}
);
}
,"switchCmts": function(token, id, action){
return utils.xhr(
{ "url":config.serverURL
+"posts/" + id
+ (action?"/disableComments":"/enableComments")
,"token":token
,"method":"post"
}
);
}
,"sendHide": function(token, id, action){
return utils.xhr(
{ "url": config.serverURL
+ "posts/" + id + "/"
+ (action?"hide":"unhide")
,"token": token
,"method": "post"
}
);
}
,"getUser": function(token, req) {return get(token, "users/" + req );}
,"getSubs": function(token, req) {return get(token, "users/" + req );}
,"doBan": function(token, username, action){
return utils.xhr(
{ "url": config.serverURL
+ "users/" + username + "/"
+ (action?"ban":"unban")
,"token": token
,"method": "post"
}
);
}
,"get": function(token, timeline) {return get(token, timeline);}
,"updProfile" : function(token, id, data){
return utils.xhr(
{ "url": config.serverURL + "users/" + id
,"token": token
,"method": "put"
,"data": JSON.stringify(data)
,"headers":{"Content-type":"application/json"}
}
);
}
,"chngAvatar": function (token, file){
var data = new FormData();
data.append( "file",file) ;
return utils.xhr(
{ "url": config.serverURL + "users/updateProfilePicture"
,"token": token
,"method": "post"
,"data": data
}
);
}
,"_getWhoami": function(token){
var whoami = get2( token ,"users/whoami");
var groupReqs = get2( token ,"managedGroups" );
return utils._Promise.all([whoami,groupReqs])
.then(function(res){
var whoami = JSON.parse(res[0]);
if (!Array.isArray(whoami.requests))
whoami.requests = new Array();
if (!Array.isArray(whoami.users.subscriptionRequests))
whoami.users.subscriptionRequests = new Array();
whoami.requests.forEach(function(req){
req.type = "user";
if (whoami.users.subscriptionRequests.indexOf(req.id ) != -1){
req.dest = whoami.users.id;
req.src = req.id;
} else {
req.dest = req.id;
req.src = whoami.users.id;
}
});
var grps = JSON.parse(res[1]);
var grpReqs = new Array();
grps.forEach(function(grp){
grp.requests.forEach(function(req){
req.type = "group";
req.dest = grp.id;
res.src = req.id;
grpReqs.push(req);
whoami.users.subscriptionRequests.push(grp.id);
});
});
whoami.requests = whoami.requests.concat(grpReqs);
return whoami;
});
}
,"login":function(username, password){
var data = "username="+utils.encodeURIForm(username)
+ "&password="+utils.encodeURIForm(password);
return utils.xhr(
{ "url": config.serverURL + "session"
,"headers":{"Content-type":"application/x-www-form-urlencoded"}
,"method": "post"
,"data":data
}
);
}
,"sendLike": function(token, id, action){
return utils.xhr(
{ "url": config.serverURL
+ "posts/" + id + "/"
+ (action?"like":"unlike")
,"token": token
,"method": "post"
}
);
}
,"sendComment": function(token, postdata){
return utils.xhr(
{ "url": config.serverURL + "comments"
,"token": token
,"method": "post"
,"data": JSON.stringify(postdata)
,"headers":{"Content-type":"application/json"}
}
);
}
,"editComment": function(token, id, postdata){
return utils.xhr(
{ "url": config.serverURL + "comments/"+id
,"token": token
,"method": "put"
,"data": JSON.stringify(postdata)
,"headers":{"Content-type":"application/json"}
}
);
}
,"deleteComment": function(token, id){
return utils.xhr(
{ "url": config.serverURL + "comments/"+id
,"token": token
,"method": "delete"
}
);
}
,"reqSub": function(token,username ){
return utils.xhr(
{ "url": config.serverURL
+ "users/" + username
+ "/sendRequest/"
,"token": token
,"method": "post"
}
);
}
,"evtSub": function(token,username, subscribed ){
return utils.xhr(
{ "url": config.serverURL
+ "users/" + username
+ (subscribed?"/unsubscribe":"/subscribe")
,"token": token
,"method": "post"
}
);
}
,"reqResp": function(token, user, action, reqid, type, dest){
var req = (type == "user"? [type + "s", action, user]
:[type + "s", dest, action, user]
).join("/");
return utils.xhr(
{ "url": config.serverURL + req
,"token": token
,"method": "post"
}
);
}
,"sendAttachment": function(token,file, filename){
var data = new FormData();
data.append( "name", "attachment[file]");
data.append( "attachment[file]",file, filename);
return utils.xhr(
{ "url": config.serverURL + "attachments"
,"token": token
,"method": "post"
,"data": data
}
);
}
,"resetDirectsCount":function(token){
return get2( token ,"users/markAllDirectsAsRead");
}
}
,"parse":function (res){
return (typeof res == "string")?JSON.parse(res):res;
}
,"oRT":RtUpdate
};
};
});