Change to use nedb-promise instead of nedb

This commit is contained in:
kPherox
2019-05-11 20:37:45 +09:00
parent 8fd685a019
commit 8b2a9964a4
4 changed files with 153 additions and 79 deletions

View File

@@ -1,6 +1,6 @@
import { app } from "electron"
import Mastodon from 'megalodon'
import Datastore from "nedb"
import Datastore from "nedb-promises"
import { join } from "path"
type Protocol = 'http' | 'websocket'
@@ -14,7 +14,7 @@ export default class Client {
private static nonAuthorizedHTTP: Map<string, Mastodon> = new Map()
private static nonAuthorizedWebSocket: Map<string, Mastodon> = new Map()
public static getAuthClient(username: string, protocol: Protocol = 'http'): Mastodon {
public static async getAuthClient(username: string, protocol: Protocol = 'http'): Promise<Mastodon> {
let clients = protocol === 'http' ? this.authorizedHTTP : this.authorizedWebSocket
if (!clients.has(username)) {
@@ -23,13 +23,12 @@ export default class Client {
filename: join(app.getPath("userData"), "account.db"),
autoload: true
})
db.find({ full: username }, function (err: any, docs: { domain: string; accessToken: string; }) {
if (err) {
console.log(err)
} else {
Client.setAuthClient(protocol, username, Client.createAuthClient(protocol, docs.domain, docs.accessToken))
}
})
try {
let doc = await db.findOne<{ domain: string; accessToken: string; }>({ full: username })
Client.setAuthClient(protocol, username, Client.createAuthClient(protocol, doc.domain, doc.accessToken))
} catch (err) {
throw err
}
}
return clients.get(username)!
@@ -66,7 +65,7 @@ export default class Client {
private static createNoAuthClient(protocol: Protocol, domain: string): Mastodon {
let scheme = protocol === 'http' ? 'https://' : 'wss://'
return new Mastodon(
'', // pleromaでは空文字ではなくnullを指定しないと毎秒403エラーになるアクセスを繰り返すことになる。TypeScriptではnullを入れられないのでmegalodonの方を修正する必要あり
null, // pleromaでは空文字ではなくnullを指定しないと毎秒403エラーになるアクセスを繰り返すことになる。TypeScriptではnullを入れられないのでmegalodonの方を修正する必要あり
scheme + domain + '/api/v1'
)
}

View File

@@ -5,12 +5,11 @@ import {
} from 'electron'
import { Status, Response } from 'megalodon'
import { join } from "path"
import Datastore from "nedb"
import Datastore from "nedb-promises"
import Client from './Client'
interface TimelineDoc {
_id?: string
name: string
type: string
}
@@ -50,15 +49,14 @@ export default class Timeline {
type: type,
}
db.insert(docs, function (err, newDocs) {
if (err) {
let error = new Error("You cannot login already logined account.")
error.name = "ERROR_YOU_TRY_ANOTHER_ACCOUNT"
event.sender.send(`add-timeline`, undefined, error)
} else {
event.sender.send(`add-timeline`, newDocs)
}
})
try {
let newDoc = await db.insert(docs)
event.sender.send(`add-timeline`, newDoc)
} catch (err) {
let error = new Error("You cannot login already logined account.")
error.name = "ERROR_YOU_TRY_ANOTHER_ACCOUNT"
event.sender.send(`add-timeline`, undefined, error)
}
}
private static async onNoAuthTimeline(event: Event, name: string) {
@@ -82,7 +80,7 @@ export default class Timeline {
let url = this.endpoints[type]
try {
const client = Client.getAuthClient(name)
const client = await Client.getAuthClient(name)
let res: Response<[Status]> = await client.get<[Status]>(url)
event.sender.send(`timeline-${name}-${type}`, res.data)
} catch (error) {