refs #100 Swap forward a account order

This commit is contained in:
AkiraFukushima 2018-04-02 09:07:09 +09:00
parent 3839e6c3e8
commit f39b881722
4 changed files with 76 additions and 48 deletions

View File

@ -99,6 +99,22 @@ export default class Account {
)
})
}
async forwardAccount (ac) {
if (ac.order <= 1) {
return ac.order
}
// Find account which is backwarded
const backwarded = await this.searchAccount(
{
order: ac.order - 1
}
)
await this.updateAccount(backwarded._id, Object.assign(backwarded, { order: (backwarded.order + 1) }))
// Forward account order.
const updated = await this.updateAccount(ac._id, Object.assign(ac, { order: (ac.order - 1) }))
return updated
}
}
class EmptyRecordError {

View File

@ -309,6 +309,17 @@ ipcMain.on('remove-account', (event, id) => {
})
})
ipcMain.on('forward-account', (event, acct) => {
const account = new Account(db)
account.forwardAccount(acct)
.then(() => {
event.sender.send('response-forward-account')
})
.catch((err) => {
event.sender.send('error-forward-account', err)
})
})
// streaming
let userStreaming = null

View File

@ -12,8 +12,7 @@
v-loading="accountLoading">
<el-table-column
prop="username"
label="Username"
width="240">
label="Username">
</el-table-column>
<el-table-column
prop="domain"
@ -29,6 +28,18 @@
</el-button>
</template>
</el-table-column>
<el-table-column
label="Order"
width="60">
<template slot-scope="scope">
<div>
<el-button class="arrow-up" type="text" icon="el-icon-arrow-up" @click.native.prevent="forward(scope.$index, accounts)"></el-button>
</div>
<div>
<el-button class="arrow-down" type="text" icon="el-icon-arrow-down" @click.native.prevent="backward(scope.$index, accounts)"></el-button>
</div>
</template>
</el-table-column>
</el-table>
</template>
</div>
@ -59,8 +70,7 @@ export default {
async loadAccounts () {
this.$store.commit('Preferences/Account/updateAccountLoading', true)
try {
const accounts = await this.$store.dispatch('Preferences/Account/loadAccounts')
await this.$store.dispatch('Preferences/Account/fetchUsername', accounts)
await this.$store.dispatch('Preferences/Account/loadAccounts')
this.$store.commit('Preferences/Account/updateAccountLoading', false)
} catch (err) {
this.$store.commit('Preferences/Account/updateAccountLoading', false)
@ -81,10 +91,29 @@ export default {
type: 'error'
})
})
},
forward (index, accounts) {
this.$store.dispatch('Preferences/Account/forwardAccount', accounts[index])
.then(() => {
this.loadAccounts()
})
},
backward (index, accounts) {
this.$store.dispatch('Preferences/Account/backwardAccount', accounts[index])
.then(() => {
this.loadAccounts()
})
}
}
}
</script>
<style lang="scss" scoped>
.allow-up {
padding: 0;
}
.allow-down {
padding: 0;
}
</style>

View File

@ -1,5 +1,4 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'
const Account = {
namespaced: true,
@ -11,20 +10,6 @@ const Account = {
updateAccounts (state, accounts) {
state.accounts = accounts
},
mergeAccounts (state, accounts) {
// TODO: Save username in local db after authorize.
// This function can not support if user add multiple accounts which are exist in same domain.
// So when username is saved in local db, please compare with reference to username@domain.
state.accounts = state.accounts.map((a) => {
let account = a
accounts.map((acct) => {
if (acct.domain === a.domain) {
account = acct
}
})
return account
})
},
updateAccountLoading (state, value) {
state.accountLoading = value
}
@ -44,35 +29,6 @@ const Account = {
})
})
},
fetchUsername ({ dispatch, commit }, accounts) {
return new Promise((resolve, reject) => {
dispatch('fetchAllAccounts', accounts)
.then((accounts) => {
commit('mergeAccounts', accounts)
resolve(accounts)
})
.catch((err) => {
reject(err)
})
})
},
fetchAllAccounts ({ commit }, accounts) {
return Promise.all(accounts.map((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)
// The response doesn't have domain, so I cann't confirm that response and account is same.
// Therefore I merge account.
resolve(Object.assign(data, account))
})
})
}))
},
removeAccount ({ commit }, account) {
return new Promise((resolve, reject) => {
ipcRenderer.send('remove-account', account._id)
@ -85,6 +41,22 @@ const Account = {
resolve()
})
})
},
forwardAccount ({ commit }, account) {
return new Promise((resolve, reject) => {
ipcRenderer.send('forward-account', account)
ipcRenderer.once('error-forward-account', (event, err) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-forward-account', (event) => {
ipcRenderer.removeAllListeners('error-forward-account')
resolve()
})
})
},
backwardAccount ({ commit }, account) {
}
}
}