Replace promise to await & trycatch

This commit is contained in:
kPherox 2019-04-27 18:59:20 +09:00
parent ed8514c8c7
commit c22484db7c
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D

View File

@ -1,5 +1,5 @@
import { ipcMain, Event, shell, app } from "electron" import { ipcMain, Event, shell, app } from "electron"
import Mastodon, { Response, Account } from "megalodon" import Mastodon, { Response, Account, OAuth } from "megalodon"
import { join } from "path" import { join } from "path"
import Datastore from "nedb" import Datastore from "nedb"
@ -20,94 +20,95 @@ export default class Auth {
ipcMain.on("new-account-setup", async (event: Event, instance: string) => { ipcMain.on("new-account-setup", async (event: Event, instance: string) => {
const SCOPES: string = "read write follow" const SCOPES: string = "read write follow"
let url: string | null let url: string | null
Mastodon.registerApp( try {
"TheDesk", let appData: OAuth.AppData = await Mastodon.registerApp(
{ "TheDesk",
scopes: SCOPES {
}, scopes: SCOPES
"https://" + instance },
) "https://" + instance
.then(appData => {
clientId = appData.clientId
clientSecret = appData.clientSecret
url = appData.url
if (url) {
shell.openExternal(
url,
{
activate: false
},
err => {
if (err) console.log(err)
}
)
} else {
event.sender.send(`error`, {
id: "ERROR_GET_AUTHURL",
message: "Failed to get auth URL to login."
})
}
})
.catch((err: Error) =>
event.sender.send(`error`, {
id: "ERROR_CONNECTION",
message: "Connection error",
meta: err
})
) )
clientId = appData.clientId
clientSecret = appData.clientSecret
url = appData.url
if (url === undefined || url === null) {
event.sender.send(`error`, {
id: "ERROR_GET_AUTHURL",
message: "Failed to get auth URL to login."
})
return
}
shell.openExternal(
url,
{
activate: false
},
err => {
if (err) console.log(err)
}
)
} catch (err) {
let error: Error = err
event.sender.send(`error`, {
id: "ERROR_CONNECTION",
message: "Connection error",
meta: error
})
}
}) })
ipcMain.on( ipcMain.on(
"new-account-auth", "new-account-auth",
async (event: Event, code: string, instance: string) => { async (event: Event, code: string, instance: string) => {
let url: string = "https://" + instance let url: string = "https://" + instance
Mastodon.fetchAccessToken(clientId, clientSecret, code, url) try {
.then((tokenData: Partial<{ accessToken: string }>) => { let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, url)
if (tokenData.accessToken) { if (tokenData.accessToken === undefined) {
const client = new Mastodon( event.sender.send(`error`, {
tokenData.accessToken, id: "ERROR_GET_TOKEN",
url + "/api/v1" message: "Failed to get access token."
) })
return
}
client const client = new Mastodon(
.get<Account>("/accounts/verify_credentials") tokenData.accessToken,
.then((resp: Response<Account>) => { url + "/api/v1"
let you = resp.data )
let db = new Datastore({
filename: join(app.getPath("userData"), "account.db"), let resp: Response<Account> = await client.get<Account>("/accounts/verify_credentials")
autoload: true let you = resp.data
})
let docs: AccountDoc = { let db = new Datastore({
domain: instance, filename: join(app.getPath("userData"), "account.db"),
acct: you.acct, autoload: true
avatar: you.avatar, })
avatarStatic: you.avatar_static,
accessToken: tokenData.accessToken, let docs: AccountDoc = {
} domain: instance,
db.insert(docs, function (err, newDocs) { acct: you.acct,
if (err) { avatar: you.avatar,
event.sender.send(`error`, { avatarStatic: you.avatar_static,
id: "ERROR_YOU_TRY_ANOTHER_ACCOUNT", accessToken: tokenData.accessToken,
message: "You cannot login already logined account." }
})
} else { db.insert(docs, function (err, newDocs) {
event.sender.send(`login-complete`, newDocs) if (err) {
}
})
})
} else {
event.sender.send(`error`, { event.sender.send(`error`, {
id: "ERROR_GET_TOKEN", id: "ERROR_YOU_TRY_ANOTHER_ACCOUNT",
message: "Failed to get access token." message: "You cannot login already logined account."
}) })
} else {
event.sender.send(`login-complete`, newDocs)
} }
}) })
.catch((err: Error) => } catch (err) {
event.sender.send(`error`, { let error: Error = err
id: "ERROR_CONNECTION", event.sender.send(`error`, {
message: "Connection error", id: "ERROR_CONNECTION",
meta: err message: "Connection error",
}) meta: error
) })
}
} }
) )
} }