-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsteemHelpers.js
More file actions
201 lines (181 loc) · 7.9 KB
/
Copy pathsteemHelpers.js
File metadata and controls
201 lines (181 loc) · 7.9 KB
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
async function getAccountActivities(account, startTime, apiEndpoint) {
const startTimeStamp = new Date(startTime).getTime();
let postList = [], commentList = [], replyList = [];
let lastId = -1;
const chunkSize = 20;
async function fetchAccountHistory(lastId, limit, retries = 10) {
while (retries > 0) {
try {
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: "2.0",
method: "condenser_api.get_account_history",
params: [account, lastId, limit],
id: 1
})
});
const jsonResponse = await response.json();
if (jsonResponse.error) {
// if (jsonResponse.error.code === -32801 || jsonResponse.error.code === -32603) {
await new Promise(resolve => setTimeout(resolve, 1000));
retries--;
// } else {
// return jsonResponse;
// }
} else {
return jsonResponse;
}
} catch (error) {
console.warn(`Error fetching account history, retrying. Attempts left: ${retries}`);
await new Promise(resolve => setTimeout(resolve, 2000));
retries--;
}
}
console.warn('Failed to fetch account history after all retries');
return null;
}
while (true) {
const activities = await fetchAccountHistory(lastId, chunkSize);
if (!activities.result || activities.result.length === 0) break;
// Process activities in reverse order (from most recent to oldest)
for (let i = activities.result.length - 1; i >= 0; i--) {
// console.debug(`Checking ${activities.result[i]} in fetchAccountHistory`);
const activity = activities.result[i];
const [id, { timestamp, op }] = activity;
const transactionTimeStamp = new Date(`${timestamp}Z`).getTime();
if (transactionTimeStamp <= startTimeStamp || new Date(`${timestamp}Z`) < new Date() - 2 * 60 * 60 * 1000 ) {
// console.debug(`Returning from getAccountActivities for ${account}`);
// console.debug(` transactionTimeStamp: ${transactionTimeStamp}, startTimeStamp: ${startTimeStamp}`);
// console.debug(` transactionTimeStamp: ${timestamp}, startTimeStamp: ${startTime}`);
return { postList, commentList, replyList };
}
// console.debug(`id: ${id}, timestamp: ${timestamp}, ttstamp: ${transactionTimeStamp}, startTime: ${startTimeStamp}, Operation: ${op[0]}`);
if (op[0] === "comment") {
if ( ! op[1].body.startsWith("@@") ) {
// Throw out edits. They're more clutter than value.
const [, { parent_author, author, permlink }] = op;
isUnique = await maintainDuplicateTable (author, permlink ); // No need to display the same comment/reply twice.
if (isUnique) {
if (!parent_author) {
postList.push(activity);
} else if (author === account) {
commentList.push(activity);
} else {
replyList.push(activity);
}
} else {
console.debug(`steemHelpers:getAccountActivities - Throwing out duplicate for ${author}/${permlink}`);
}
} else {
console.debug(`steemHelpers:getAccountActivities - Throwing out edit activity for ${op[1].body}`);
}
}
lastId = id - 1;
}
}
return { postList, commentList, replyList };
}
async function getRootInfo(author, permlink, apiEndpoint) {
const url = apiEndpoint;
const data = JSON.stringify({
jsonrpc: "2.0",
method: "condenser_api.get_content",
params: [author, permlink],
id: 1
});
try {
const response = await fetch(url, {
method: "POST",
body: data,
headers: { "Content-Type": "application/json" }
});
if (!response.ok) {
throw new Error(`Error fetching content: ${response.status}`);
}
const jsonData = await response.json();
const content = jsonData.result;
return {
root_author: content.root_author,
root_permlink: content.root_permlink,
root_title: content.root_title
};
} catch (error) {
console.warn("Error:", error);
return null; // Or handle the error differently
}
}
/*
*
* - fastActivityCheckWithRetry and fastActivityCheck come from the first iteration of getAcctivityTime, but
* they are problematic 'cause they don't report on reply times. They may eventually be useful, but they're
* not active as-of now.
*/
async function fastActivityCheckWithRetry(followedAccount, apiNode, retries = 3) {
try {
const currentActivityTime = await fastActivityCheck(followedAccount, apiNode);
if (currentActivityTime !== null) {
return currentActivityTime;
} else {
console.warn(`Failed to get activity time for ${followedAccount}. Retrying in 1 second... (${retries} retries left)`);
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
if (retries > 0) {
return fastActivityCheckWithRetry(followedAccount, apiNode, retries - 1);
} else {
console.warn(`Failed to get activity time for ${followedAccount} after maximum retries.`);
return null;
}
}
} catch (error) {
console.warn(`Error in fastActivityCheckWithRetry for ${followedAccount}:`, error);
if (retries > 0) {
console.warn(`Retrying fastActivityCheckWithRetry for ${followedAccount} in 1 second... (${retries} retries left)`);
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
return fastActivityCheckWithRetry(followedAccount, apiNode, retries - 1);
} else {
console.warn(`Failed to get activity time for ${followedAccount} after maximum retries due to error.`);
return null;
}
}
}
async function getSteemAccountInfo(user, apiNode) {
try {
response = await fetch(apiNode, {
keepalive: true,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'database_api.list_accounts',
params: {
start: user,
limit: 1,
order: 'by_name'
},
id: 1
})
});
if (!response.ok) {
const errorText = await response.text();
console.log(`Error response: ${errorText}`);
return null;
}
const data = await response.json();
if (data.error) {
console.warn(`Data error while fetching last post time for ${user}:`, data.error.message);
return null; // Return null on error
}
if (!data.result || !data.result.accounts || data.result.accounts.length === 0) {
console.warn(`No accounts found for ${user}`);
return null; // No accounts found
}
return data;
} catch (error) {
console.warn(`Error fetching last post time for ${user}:`, error);
return null; // Return null on exception
} finally {
}
}