Merge pull request #21 from h3poteto/iss-10

closes #10 Save domain name and display it in sidemenu
This commit is contained in:
AkiraFukushima 2018-03-11 01:52:54 +09:00 committed by GitHub
commit 9c695b2b72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 74 deletions

View File

@ -15,25 +15,6 @@ export default class Account {
})
}
getInstance (id) {
return new Promise((resolve, reject) => {
this.db.findOne(
{
_id: id
},
(err, doc) => {
if (err) return reject(err)
if (empty(doc)) return reject(new EmptyRecordError('empty'))
const instance = {
baseURL: doc.baseURL,
id: doc._id
}
resolve(instance)
}
)
})
}
getAccount (id) {
return new Promise((resolve, reject) => {
this.db.findOne(

View File

@ -7,18 +7,21 @@ export default class Authentication {
constructor (db) {
this.db = db
this.baseURL = ''
this.domain = ''
this.clientId = ''
this.clientSecret = ''
this.protocol = 'https'
}
setOtherInstance (domain) {
this.baseURL = `${this.protocol}://${domain}`
this.domain = domain
this.clientId = ''
this.clientSecret = ''
}
setOtherInstance (baseURL) {
this.baseURL = baseURL
this.clientId = ''
this.clientSecret = ''
}
getAuthorizationUrl (baseURL = 'https://mastodon.social') {
this.setOtherInstance(baseURL)
getAuthorizationUrl (domain = 'mastodon.social') {
this.setOtherInstance(domain)
return Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope)
.catch(err => console.error(err))
.then((res) => {
@ -27,6 +30,7 @@ export default class Authentication {
const json = {
baseURL: this.baseURL,
domain: this.domain,
clientId: this.clientId,
clientSecret: this.clientSecret,
accessToken: ''
@ -46,6 +50,7 @@ export default class Authentication {
.then((token) => {
const search = {
baseURL: this.baseURL,
domain: this.domain,
clientId: this.clientId,
clientSecret: this.clientSecret
}

View File

@ -60,7 +60,7 @@ app.on('activate', () => {
let auth = new Authentication(db)
ipcMain.on('get-auth-url', (event, domain) => {
auth.getAuthorizationUrl(`https://${domain}`)
auth.getAuthorizationUrl(domain)
.catch((err) => {
console.error(err)
event.sender.send('error-get-auth-url', err)
@ -115,25 +115,14 @@ ipcMain.on('list-accounts', (event, _) => {
})
})
ipcMain.on('get-instance', (event, id) => {
const account = new Account(db)
account.getInstance(id)
.catch((err) => {
event.sender.send('error-get-instance', err)
})
.then((instance) => {
event.sender.send('response-get-instance', instance)
})
})
ipcMain.on('get-local-account', (event, id) => {
const account = new Account(db)
account.getAccount(id)
.catch((err) => {
event.sender.send('error-get-local-account', err)
})
.then((token) => {
event.sender.send('response-get-local-account', token)
.then((account) => {
event.sender.send('response-get-local-account', account)
})
})

View File

@ -12,7 +12,7 @@
active-text-color="#ffffff">
<el-menu-item :index="index.toString()" v-for="(account, index) in accounts" v-bind:key="account.id" :route="{path: `/${account.id}/home`}">
<i class="el-icon-menu"></i>
<span slot="title">{{ account.baseURL }}</span>
<span slot="title">{{ account.domain }}</span>
</el-menu-item>
</el-menu>
<div class="space">

View File

@ -3,7 +3,7 @@
<div class="profile-wrapper">
<div class="profile">
<span>@{{ username }}</span>
<span>{{ instance.baseURL }}</span>
<span>{{ account.domain }}</span>
</div>
</div>
<el-menu
@ -44,13 +44,21 @@ export default {
name: 'side-menu',
computed: {
...mapState({
instance: state => state.TimelineSpace.SideMenu.instance,
account: state => state.TimelineSpace.SideMenu.account,
username: state => state.TimelineSpace.SideMenu.username
})
},
created () {
this.$store.dispatch('TimelineSpace/SideMenu/fetchInstance', this.$route.params.id)
this.$store.dispatch('TimelineSpace/SideMenu/username', this.$route.params.id)
this.$store.dispatch('TimelineSpace/SideMenu/fetchAccount', this.$route.params.id)
.then((account) => {
this.$store.dispatch('TimelineSpace/SideMenu/username', account)
})
.catch(() => {
this.$message({
message: 'Could not find account',
type: 'error'
})
})
},
methods: {
id () {

View File

@ -4,51 +4,49 @@ import Mastodon from 'mastodon-api'
const SideMenu = {
namespaced: true,
state: {
instance: {
baseURL: '',
account: {
domain: '',
id: ''
},
username: ''
},
mutations: {
updateInstance (state, instance) {
state.instance = instance
updateAccount (state, account) {
state.account = account
},
updateUsername (state, body) {
state.username = body.username
updateUsername (state, username) {
state.username = username
}
},
actions: {
fetchInstance ({ commit }, id) {
ipcRenderer.send('get-instance', id)
ipcRenderer.once('error-get-instance', (event, err) => {
// TODO: handle error
console.log(err)
})
ipcRenderer.once('response-get-instance', (event, instance) => {
commit('updateInstance', instance)
})
},
username ({ commit }, id) {
fetchAccount ({ commit }, id) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-local-account', id)
ipcRenderer.once('error-get-local-account', (event, err) => {
// TODO: handle error
console.log(err)
reject(err)
})
ipcRenderer.once('response-get-local-account', (event, account) => {
const client = new Mastodon(
{
access_token: account.accessToken,
api_url: account.baseURL + '/api/v1'
})
client.get('/accounts/verify_credentials', {})
.then((res) => {
commit('updateUsername', res.data)
resolve(res)
})
commit('updateAccount', account)
resolve(account)
})
})
},
username ({ commit }, 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', {})
.then((res) => {
commit('updateUsername', res.data.username)
resolve(res)
})
})
}
}
}