mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-10-06 01:57:29 +00:00
50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
|
const constants = require("../constants")
|
||
|
const collectors = require("../collectors")
|
||
|
const Assistant = require("./Assistant")
|
||
|
const db = require("../db")
|
||
|
|
||
|
class AssistantSwitcher {
|
||
|
constructor() {
|
||
|
this.assistants = constants.assistant.origins.map(origin => new Assistant(origin))
|
||
|
}
|
||
|
|
||
|
enabled() {
|
||
|
return constants.assistant.enabled && this.assistants.length
|
||
|
}
|
||
|
|
||
|
getAvailableAssistants() {
|
||
|
return this.assistants.filter(assistant => assistant.available()).sort((a, b) => (a.lastRequest - b.lastRequest))
|
||
|
}
|
||
|
|
||
|
requestUser(username) {
|
||
|
return new Promise(async (resolve, reject) => {
|
||
|
const assistants = this.getAvailableAssistants()
|
||
|
while (assistants.length) {
|
||
|
const assistant = assistants.shift()
|
||
|
try {
|
||
|
const user = await assistant.requestUser(username)
|
||
|
return resolve(user)
|
||
|
} catch (e) {
|
||
|
// that assistant broke. try the next one.
|
||
|
}
|
||
|
}
|
||
|
return reject(constants.symbols.NO_ASSISTANTS_AVAILABLE)
|
||
|
}).then(user => {
|
||
|
const bind = {...user}
|
||
|
bind.created = Date.now()
|
||
|
bind.updated = Date.now()
|
||
|
bind.updated_version = constants.database_version
|
||
|
bind.is_private = +user.is_private
|
||
|
bind.is_verified = +user.is_verified
|
||
|
db.prepare(
|
||
|
"REPLACE INTO Users (username, user_id, created, updated, updated_version, biography, post_count, following_count, followed_by_count, external_url, full_name, is_private, is_verified, profile_pic_url) VALUES "
|
||
|
+"(@username, @user_id, @created, @updated, @updated_version, @biography, @post_count, @following_count, @followed_by_count, @external_url, @full_name, @is_private, @is_verified, @profile_pic_url)"
|
||
|
).run(bind)
|
||
|
collectors.userRequestCache.cache.delete(`user/${username}`)
|
||
|
return collectors.fetchUserFromSaved(user)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = AssistantSwitcher
|