-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbackend.js
160 lines (127 loc) · 5.3 KB
/
backend.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
// copied from https://github.com/aeternity/superhero-ui/blob/178bc2d63cb362ddc3b2163fed58deb2e253ec00/src/utils/backend.js
const BACKEND_URL = 'https://raendom-backend.z52da5wt.xyz';
const wrapTry = async promise => {
try {
return Promise.race([
promise.then(async res => {
const response = res.json();
const { err, error } = await response;
if (err || error) {
const e = { message: err || error, type: 'backend' };
throw e;
}
if (!res.ok) throw new Error(`Request failed with ${res.status}`);
return response;
}),
new Promise((resolve, reject) => {
setTimeout(reject, 3000, 'TIMEOUT');
}),
]);
} catch (e) {
console.error('backend error', e);
return null;
}
};
const backendFetch = (path, ...args) => wrapTry(fetch(`${BACKEND_URL}/${path}`, ...args));
export default class Backend {
static getTipComments = async tipId =>
backendFetch(`comment/api/tip/${encodeURIComponent(tipId)}`);
static async sendTipComment(tipId, text, author, signCb, parentId) {
const sendComment = async postParam =>
backendFetch('comment/api/', {
method: 'post',
body: JSON.stringify(postParam),
headers: { 'Content-Type': 'application/json' },
});
const responseChallenge = await sendComment({ tipId, text, author });
const signedChallenge = await signCb(responseChallenge.challenge);
const respondChallenge = {
challenge: responseChallenge.challenge,
signature: signedChallenge,
parentId,
};
return sendComment(respondChallenge);
}
static async modifyNotification(notifId, status, author, signCb) {
const modifyNotif = async postParam =>
backendFetch(`notification/${notifId}`, {
method: 'post',
body: JSON.stringify(postParam),
headers: { 'Content-Type': 'application/json' },
});
const responseChallenge = await modifyNotif({ author, status });
const signedChallenge = await signCb(responseChallenge.challenge);
const respondChallenge = {
challenge: responseChallenge.challenge,
signature: signedChallenge,
};
return modifyNotif(respondChallenge);
}
static getAllComments = async () => backendFetch('comment/api/');
static getProfile = async address => backendFetch(`profile/${address}`);
static async getAllNotifications(address, signCb) {
const responseChallenge = await backendFetch(`notification/user/${address}`);
const signedChallenge = await signCb(responseChallenge.challenge);
const respondChallenge = {
challenge: responseChallenge.challenge,
signature: signedChallenge,
};
const url = new URL(`${BACKEND_URL}/notification/user/${address}`);
Object.keys(respondChallenge).forEach(key =>
url.searchParams.append(key, respondChallenge[key]),
);
return wrapTry(fetch(url.toString()));
}
static sendProfileData = async postParam =>
backendFetch('profile', {
method: 'post',
body: JSON.stringify(postParam),
headers: { 'Content-Type': 'application/json' },
});
static setProfileImage = async (address, data, image = true) => {
const request = {
method: 'post',
body: image ? data : JSON.stringify(data),
};
Object.assign(request, !image && { headers: { 'Content-Type': 'application/json' } });
return wrapTry(fetch(Backend.getProfileImageUrl(address), request));
};
static deleteProfileImage = async (address, postParam = false) => {
const request = {
method: 'delete',
headers: {
'Content-Type': 'application/json',
},
...(postParam && { body: JSON.stringify(postParam) }),
};
return backendFetch(`profile/image/${address}`, request);
};
static getProfileImageUrl = address => `${BACKEND_URL}/profile/image/${address}`;
static getStats = async () => backendFetch('static/stats/');
static getCacheTipById = async id => backendFetch(`cache/tip?id=${id}`);
static getCacheUserStats = async address => backendFetch(`cache/userStats?address=${address}`);
static getCacheTips = async (ordering, page, address = null, search = null) => {
let query = `?ordering=${ordering}&page=${page}`;
if (address) query += `&address=${address}`;
if (search) query += `&search=${encodeURIComponent(search)}`;
return backendFetch(`cache/tips${query}`);
};
static getCacheStats = async () => backendFetch('cache/stats');
static getCacheChainNames = async () => backendFetch('cache/chainnames');
static getPrice = async () =>
wrapTry(
fetch(
'https://api.coingecko.com/api/v3/simple/price?ids=aeternity&vs_currencies=usd,eur,cny',
),
);
// quick workaround because of CORS issue in the backend.
// static getPrice = async () => backendFetch('cache/price');
static getOracleCache = async () => backendFetch('cache/oracle');
static getTopicsCache = async () => backendFetch('cache/topics');
static cacheInvalidateTips = async () => backendFetch('cache/invalidate/tips');
static getCommentCountForAddress = async address =>
backendFetch(`comment/count/author/${address}`);
static getTipPreviewUrl = previewLink => `${BACKEND_URL}${previewLink}`;
static getProfileImageUrl = address => `${BACKEND_URL}/profile/image/${address}`;
static getCommentById = async id => backendFetch(`comment/api/${id}`);
}