Merge pull request #176 from h3poteto/iss-100

closes #100 Set order in preferences
This commit is contained in:
AkiraFukushima 2018-04-02 22:27:32 +09:00 committed by GitHub
commit f8a47125c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 128 additions and 51 deletions

View File

@ -17,7 +17,7 @@ export default class Account {
listAccounts () {
return new Promise((resolve, reject) => {
this.db.find({accessToken: { $ne: '' }}, (err, docs) => {
this.db.find({accessToken: { $ne: '' }}).sort({ order: 1 }).exec((err, docs) => {
if (err) return reject(err)
if (empty(docs)) return reject(new EmptyRecordError('empty'))
resolve(docs)
@ -25,6 +25,15 @@ export default class Account {
})
}
countAuthorizedAccounts () {
return new Promise((resolve, reject) => {
this.db.count({accessToken: { $ne: '' }}, (err, count) => {
if (err) return reject(err)
resolve(count)
})
})
}
getAccount (id) {
return new Promise((resolve, reject) => {
this.db.findOne(
@ -90,6 +99,39 @@ 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
}
async backwardAccount (ac) {
const length = await this.countAuthorizedAccounts()
if (ac.order >= length) {
return ac.order
}
// Find account which is forwarded
const forwarded = await this.searchAccount(
{
order: ac.order + 1
}
)
await this.updateAccount(forwarded._id, Object.assign(forwarded, { order: (forwarded.order - 1) }))
// Backward account order
const updated = await this.updateAccount(ac._id, Object.assign(ac, { order: (ac.order + 1) }))
return updated
}
}
class EmptyRecordError {

View File

@ -26,7 +26,7 @@ export default class Authentication {
this.clientId = res.client_id
this.clientSecret = res.client_secret
// TODO: Save order number
const count = await this.db.countAuthorizedAccounts()
const json = {
baseURL: this.baseURL,
domain: this.domain,
@ -34,7 +34,8 @@ export default class Authentication {
clientSecret: this.clientSecret,
accessToken: '',
username: '',
accountId: ''
accountId: '',
order: count + 1
}
await this.db.insertAccount(json)
const url = await Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL)

View File

@ -309,6 +309,28 @@ 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)
})
})
ipcMain.on('backward-account', (event, acct) => {
const account = new Account(db)
account.backwardAccount(acct)
.then(() => {
event.sender.send('response-backward-account')
})
.catch((err) => {
event.sender.send('error-backward-account', err)
})
})
// streaming
let userStreaming = null

View File

@ -44,6 +44,7 @@ export default {
try {
const accounts = await this.$store.dispatch('GlobalHeader/listAccounts')
if (this.$route.params.id === undefined) {
this.$store.dispatch('GlobalHeader/schmearMenu', accounts[0]._id)
return this.$router.push({ path: `/${accounts[0]._id}/home` })
} else {
return this.$store.dispatch('GlobalHeader/schmearMenu', this.$route.params.id)

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,32 @@ 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) {
return new Promise((resolve, reject) => {
ipcRenderer.send('backward-account', account)
ipcRenderer.once('error-backward-account', (event, err) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-backward-account', (event) => {
ipcRenderer.removeAllListeners('error-backward-account')
resolve()
})
})
}
}
}