-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbackend.js
180 lines (178 loc) · 5.72 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint no-param-reassign: ["error", { "ignorePropertyModificationsFor": ["state"] }] */
import Vue from 'vue';
import { times } from 'lodash-es';
import Backend from '../../utils/backend';
import { createDeepLinkUrl } from '../../utils';
export default {
namespaced: true,
state: {
tips: {},
tipsPageCount: {},
tipsReloading: {},
tipsNextPageLoading: {},
tipsEndReached: {},
userComments: {},
tip: {},
comment: {},
stats: null,
prices: {},
},
getters: {
minTipAmount: ({ prices: { usd } }) => 0.01 / usd,
},
mutations: {
setTips({ tips, tipsReloading }, { args, value }) {
Vue.set(tips, args, value);
tipsReloading[args] = false;
},
addTips({
tips, tipsPageCount, tipsEndReached, tipsNextPageLoading,
}, { args, value }) {
if (value.length) {
tips[args].push(...value);
tipsPageCount[args] += 1;
} else Vue.set(tipsEndReached, args, true);
tipsNextPageLoading[args] = false;
},
tipsReloading({ tipsReloading, tipsPageCount }, args) {
Vue.set(tipsReloading, args, true);
if (!tipsPageCount[args]) Vue.set(tipsPageCount, args, 1);
},
tipsNextPageLoading({ tipsNextPageLoading, tipsPageCount }, args) {
Vue.set(tipsNextPageLoading, args, true);
if (!tipsPageCount[args]) Vue.set(tipsPageCount, args, 1);
},
setUserComments({ userComments }, { address, value }) {
Vue.set(userComments, address, value);
},
setTip({ tip }, { id, value }) {
Vue.set(tip, id, value);
},
setComment({ comment }, { id, value }) {
Vue.set(comment, id, value);
},
setStats(state, stats) {
state.stats = stats;
},
setPrices(state, prices) {
state.prices = prices;
},
},
actions: {
async reloadTips({ commit, state: { tipsPageCount, tipsReloading } }, args) {
if (tipsReloading[args]) return;
commit('tipsReloading', args);
const value = (await Promise.all(
times(tipsPageCount[args], (p) => Backend.getFeed(p + 1, ...args)),
)).flat().filter((p) => p);
commit('setTips', { args, value });
},
async loadNextPageOfTips({ commit, state }, args) {
if (state.tipsEndReached[args] || state.tipsNextPageLoading[args]) return;
commit('tipsNextPageLoading', args);
const value = await Backend.getFeed(state.tipsPageCount[args] + 1, ...args);
commit('addTips', { args, value });
},
async reloadUserComments({ commit }, address) {
commit('setUserComments', {
address,
value: (await Backend.getUserComments(address))
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)),
});
},
async reloadTip({ commit, rootState: { chainNames } }, id) {
const [tip, comments] = await Promise.all([
Backend.getTipById(id),
Backend.getTipComments(id),
]);
// TODO: Remove after backend methods start to throw exceptions on not found
if (!tip) throw new Error(`Can't find tip with id: ${id}`);
commit('setTip', {
id,
value: {
...tip,
comments: comments.map((comment) => ({
...comment,
chainName: chainNames[comment.author],
})),
},
});
},
async reloadComment({ commit }, id) {
const value = await Backend.getCommentById(id);
// TODO: Remove after backend methods start to throw exceptions on not found
if (!value) throw new Error(`Can't find comment with id: ${id}`);
commit('setComment', { id, value });
},
async awaitTip(_, id) {
await Backend.awaitTip(id);
},
async awaitRetip(_, id) {
await Backend.awaitRetip(id);
},
async reloadStats({ commit, rootState: { aeternity: { sdk } } }) {
const [stats1, stats2, height] = await Promise.all([
Backend.getTipStats(),
Backend.getStats(),
sdk.height(),
]);
commit('setStats', { ...stats1, ...stats2, height });
},
async reloadPrices({ commit }) {
commit('setPrices', (await Backend.getPrice())?.aeternity);
},
// eslint-disable-next-line consistent-return
async callWithAuth({
rootState: {
aeternity: {
useSdkWallet,
sdk,
},
address,
},
}, { method, arg, to }) {
const { challenge } = await Backend[method](address, arg);
if (useSdkWallet) {
const signature = await sdk.signMessage(challenge);
const response = await Backend[method](address, { challenge, signature });
return response;
}
const url = new URL(to || window.location, window.location);
url.search = '';
window.location = createDeepLinkUrl({
type: 'sign-message',
message: challenge,
'x-success': `${url}?method=${method}&address=${address}&challenge=${challenge}&signature={signature}`,
});
},
async sendComment(
{ dispatch, rootState: { address, aeternity: { useSdkWallet } } },
{ tipId, text, parentId },
) {
if (!useSdkWallet) {
window.location = createDeepLinkUrl({
type: 'comment', id: tipId, text, parentId,
});
return;
}
await dispatch('callWithAuth', {
method: 'sendTipComment',
arg: {
tipId, text, author: address, parentId,
},
});
await Promise.all([
dispatch('reloadTip', tipId),
...parentId ? [dispatch('reloadComment', parentId)] : [],
dispatch('reloadStats'),
]);
},
async setCookies({ dispatch, commit }, { scope, status }) {
await dispatch('callWithAuth', {
method: 'setCookiesConsent',
arg: { scope, status },
});
commit('setCookiesConsent', { scope, status }, { root: true });
},
},
};