From 3839e6c3e8cdda3810a6625c083db172ba690e3b Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 1 Apr 2018 22:48:54 +0900 Subject: [PATCH 1/4] refs #100 Set order when save account record --- src/main/account.js | 11 ++++++++++- src/main/auth.js | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/account.js b/src/main/account.js index e7fae4e5..b3f393b3 100644 --- a/src/main/account.js +++ b/src/main/account.js @@ -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( diff --git a/src/main/auth.js b/src/main/auth.js index 46d2ce5e..c04bbfe1 100644 --- a/src/main/auth.js +++ b/src/main/auth.js @@ -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) From f39b8817226884b6e9ac8f72893a94fbd9d873d3 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 2 Apr 2018 09:07:09 +0900 Subject: [PATCH 2/4] refs #100 Swap forward a account order --- src/main/account.js | 16 +++++ src/main/index.js | 11 ++++ .../components/Preferences/Account.vue | 37 ++++++++++-- src/renderer/store/Preferences/Account.js | 60 +++++-------------- 4 files changed, 76 insertions(+), 48 deletions(-) diff --git a/src/main/account.js b/src/main/account.js index b3f393b3..896515af 100644 --- a/src/main/account.js +++ b/src/main/account.js @@ -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 { diff --git a/src/main/index.js b/src/main/index.js index 1e1146ed..85a4b23a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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 diff --git a/src/renderer/components/Preferences/Account.vue b/src/renderer/components/Preferences/Account.vue index d9ff98be..10ca0686 100644 --- a/src/renderer/components/Preferences/Account.vue +++ b/src/renderer/components/Preferences/Account.vue @@ -12,8 +12,7 @@ v-loading="accountLoading"> + label="Username"> + + + @@ -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() + }) } } } diff --git a/src/renderer/store/Preferences/Account.js b/src/renderer/store/Preferences/Account.js index d8ec1a4b..01d21535 100644 --- a/src/renderer/store/Preferences/Account.js +++ b/src/renderer/store/Preferences/Account.js @@ -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) { + } } } From cdea89e8ac9fd7b5e823aff519b9f1650870cdc5 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 2 Apr 2018 22:17:08 +0900 Subject: [PATCH 3/4] refs #100 Swap backward a account in preferences --- src/main/account.js | 19 ++++++++++++++++++- src/main/index.js | 11 +++++++++++ src/renderer/store/Preferences/Account.js | 12 +++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/account.js b/src/main/account.js index 896515af..eb88222f 100644 --- a/src/main/account.js +++ b/src/main/account.js @@ -111,10 +111,27 @@ export default class Account { } ) await this.updateAccount(backwarded._id, Object.assign(backwarded, { order: (backwarded.order + 1) })) - // Forward account order. + // 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 { diff --git a/src/main/index.js b/src/main/index.js index 85a4b23a..f9c45f0b 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -320,6 +320,17 @@ ipcMain.on('forward-account', (event, acct) => { }) }) +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 diff --git a/src/renderer/store/Preferences/Account.js b/src/renderer/store/Preferences/Account.js index 01d21535..82a69c50 100644 --- a/src/renderer/store/Preferences/Account.js +++ b/src/renderer/store/Preferences/Account.js @@ -56,7 +56,17 @@ const Account = { }) }, 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() + }) + }) } } } From e77e05c5568947cbb3f8b6e997c402b92bd47205 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 2 Apr 2018 22:26:22 +0900 Subject: [PATCH 4/4] refs #100 Refresh default active account when load global header --- src/renderer/components/GlobalHeader.vue | 1 + 1 file changed, 1 insertion(+) diff --git a/src/renderer/components/GlobalHeader.vue b/src/renderer/components/GlobalHeader.vue index 10ca1892..5f25ace2 100644 --- a/src/renderer/components/GlobalHeader.vue +++ b/src/renderer/components/GlobalHeader.vue @@ -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)