refs #258 Refresh all accounts when opened

This commit is contained in:
AkiraFukushima 2018-04-22 15:48:20 +09:00
parent 9fcb873d6f
commit 71a98ad815
4 changed files with 46 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import empty from 'is-empty'
import Mastodon from 'mastodon-api'
export default class Account {
constructor (db) {
@ -150,6 +151,39 @@ export default class Account {
}
return null
}
async refreshAccounts () {
const accounts = await this.listAccounts()
if (accounts.length < 1) {
return accounts.length
}
const results = await Promise.all(accounts.map(async (account) => {
const refresh = await this.refresh(account)
return refresh
}))
return results
}
refresh (account) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: account.accessToken,
api_url: account.baseURL + '/api/v1'
}
)
client.get('/accounts/verify_credentials', (err, data, res) => {
if (err) return reject(err)
const json = {
username: data.username,
accountId: data.id,
avatar: data.avatar
}
this.updateAccount(account._id, json)
.then(ac => resolve(ac))
})
})
}
}
class EmptyRecordError {

View File

@ -35,6 +35,7 @@ export default class Authentication {
accessToken: '',
username: '',
accountId: '',
avatar: '',
order: count + 1
}
await this.db.insertAccount(json)

View File

@ -354,6 +354,11 @@ ipcMain.on('backward-account', (event, acct) => {
})
})
ipcMain.on('refresh-accounts', (event, _) => {
const account = new Account(accountDB)
account.refreshAccounts()
})
// streaming
let userStreaming = null

View File

@ -20,7 +20,7 @@ const GlobalHeader = {
}
},
actions: {
listAccounts ({ commit }) {
listAccounts ({ dispatch, commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('list-accounts', 'list')
ipcRenderer.once('error-list-accounts', (event, err) => {
@ -30,10 +30,15 @@ const GlobalHeader = {
ipcRenderer.once('response-list-accounts', (event, accounts) => {
ipcRenderer.removeAllListeners('error-list-accounts')
commit('updateAccounts', accounts)
dispatch('refreshAccounts')
resolve(accounts)
})
})
},
// Fetch account informations and save current state when GlobalHeader is displayed
refreshAccounts ({ commit, state }) {
ipcRenderer.send('refresh-accounts')
},
watchShortcutEvents ({ state, commit }) {
ipcRenderer.on('change-account', (event, account) => {
if (state.changing) {