Skip to content

Commit

Permalink
Merge pull request #379 from h3poteto/feature/megalodon
Browse files Browse the repository at this point in the history
Use megalodon instead of mastodon-api as mastodon api client
  • Loading branch information
h3poteto authored Jun 10, 2018
2 parents 4a87795 + 5d54961 commit e2bcfb8
Show file tree
Hide file tree
Showing 22 changed files with 617 additions and 1,057 deletions.
849 changes: 241 additions & 608 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"hawk": "^7.0.7",
"hoek": "^5.0.3",
"is-empty": "^1.2.0",
"mastodon-api": "github:h3poteto/mastodon-api#lib",
"megalodon": "^0.1.0",
"moment": "^2.21.0",
"mousetrap": "^1.6.2",
"nedb": "^1.8.0",
Expand Down
21 changes: 8 additions & 13 deletions src/main/account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import empty from 'is-empty'
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'

export default class Account {
constructor (db) {
Expand Down Expand Up @@ -165,24 +165,19 @@ export default class Account {
}

refresh (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)
const client = new Mastodon(
account.accessToken,
account.baseURL + '/api/v1'
)
return client.get('/accounts/verify_credentials')
.then(data => {
const json = {
username: data.username,
accountId: data.id,
avatar: data.avatar
}
this.updateAccount(account._id, json)
.then(ac => resolve(ac))
return this.updateAccount(account._id, json)
})
})
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/main/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'

const appName = 'Whalebird'
const appURL = 'https://whalebird.org'
Expand All @@ -23,15 +23,15 @@ export default class Authentication {

async getAuthorizationUrl (domain = 'mastodon.social') {
this.setOtherInstance(domain)
const res = await Mastodon.createOAuthApp(
this.baseURL + '/api/v1/apps',
appName,
scope,
'urn:ietf:wg:oauth:2.0:oob',
appURL
const res = await Mastodon.registerApp(
appName, {
scopes: scope,
website: appURL
},
this.baseURL
)
this.clientId = res.client_id
this.clientSecret = res.client_secret
this.clientId = res.clientId
this.clientSecret = res.clientSecret

const count = await this.db.countAuthorizedAccounts()
const json = {
Expand All @@ -46,21 +46,21 @@ export default class Authentication {
order: count + 1
}
await this.db.insertAccount(json)
const url = await Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL)
return url
return res.url
}

async getAccessToken (code) {
const token = await Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
const token = await Mastodon.fetchAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
const search = {
baseURL: this.baseURL,
domain: this.domain,
clientId: this.clientId,
clientSecret: this.clientSecret
}
const rec = await this.db.searchAccount(search)
await this.db.updateAccount(rec._id, { accessToken: token })
return token
const accessToken = token.access_token
await this.db.updateAccount(rec._id, { accessToken: accessToken })
return accessToken
}
// TODO: Refresh access token when expired
}
8 changes: 3 additions & 5 deletions src/main/streaming.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'
import log from 'electron-log'

export default class Streaming {
constructor (account) {
this.account = account
this.client = new Mastodon(
{
access_token: account.accessToken,
api_url: account.baseURL + '/api/v1'
}
account.accessToken,
account.baseURL + '/api/v1'
)
this.listener = null
}
Expand Down
90 changes: 32 additions & 58 deletions src/renderer/store/TimelineSpace/Contents/Cards/Toot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'
import { ipcRenderer } from 'electron'

const Toot = {
Expand All @@ -7,79 +7,53 @@ const Toot = {
mutations: {},
actions: {
reblog ({ state, commit, rootState }, message) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.post(`/statuses/${message.id}/reblog`, {}, (err, data, res) => {
if (err) return reject(err)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.post(`/statuses/${message.id}/reblog`)
.then(data => {
// API returns new status when reblog.
// Reblog target status is in the data.reblog.
// So I send data.reblog as status for update local timeline.
ipcRenderer.send('fav-rt-action-sound')
resolve(data.reblog)
return data.reblog
})
})
},
unreblog ({ state, commit, rootState }, message) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.post(`/statuses/${message.id}/unreblog`, {}, (err, data, res) => {
if (err) return reject(err)
resolve(data)
})
})
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.post(`/statuses/${message.id}/unreblog`)
},
addFavourite ({ state, commit, rootState }, message) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.post(`/statuses/${message.id}/favourite`, {}, (err, data, res) => {
if (err) return reject(err)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.post(`/statuses/${message.id}/favourite`)
.then(data => {
ipcRenderer.send('fav-rt-action-sound')
resolve(data)
return data
})
})
},
removeFavourite ({ state, commit, rootState }, message) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.post(`/statuses/${message.id}/unfavourite`, {}, (err, data, res) => {
if (err) return reject(err)
resolve(data)
})
})
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.post(`/statuses/${message.id}/unfavourite`)
},
deleteToot ({ state, commit, rootState }, message) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.delete(`/statuses/${message.id}`, {}, (err, data, res) => {
if (err) return reject(err)
resolve(message)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.del(`/statuses/${message.id}`)
.then(() => {
return message
})
})
}
}
}
Expand Down
57 changes: 27 additions & 30 deletions src/renderer/store/TimelineSpace/Contents/Favourites.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'

const Favourites = {
namespaced: true,
Expand Down Expand Up @@ -44,44 +44,41 @@ const Favourites = {
},
actions: {
fetchFavourites ({ commit }, account) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: account.accessToken,
api_url: account.baseURL + '/api/v1'
}
)
client.get('/favourites', { limit: 40 }, (err, data, res) => {
if (err) return reject(err)
const client = new Mastodon(
account.accessToken,
account.baseURL + '/api/v1'
)
return client.get('/favourites', { limit: 40 })
.then(data => {
commit('updateFavourites', data)
resolve(res)
return data
})
})
},
lazyFetchFavourites ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return null
return Promise.resolve(null)
}
return new Promise((resolve, reject) => {
if (state.lazyLoading) {
return resolve()
}
commit('changeLazyLoading', true)
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
})
// Note: Now this API's explanation and implementation are reversed.
// So if the bug has resolved, please use max_id instead of since_id.
// https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#favourites
client.get('/favourites', { since_id: last.id, limit: 40 }, (err, data, res) => {
if (err) return reject(err)
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
// Note: Now this API's explanation and implementation are reversed.
// So if the bug has resolved, please use max_id instead of since_id.
// https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#favourites
return client.get('/favourites', { since_id: last.id, limit: 40 })
.then(data => {
commit('changeLazyLoading', false)
commit('insertFavourites', data)
return data
})
.catch(err => {
commit('changeLazyLoading', false)
resolve(res)
throw err
})
})
}
}
}
Expand Down
47 changes: 22 additions & 25 deletions src/renderer/store/TimelineSpace/Contents/Hashtag/Tag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'
import Mastodon from 'megalodon'

const Tag = {
namespaced: true,
Expand Down Expand Up @@ -68,19 +68,14 @@ const Tag = {
},
actions: {
fetch ({ commit, rootState }, tag) {
return new Promise((resolve, reject) => {
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
}
)
client.get(`/timelines/tag/${tag}`, { limit: 40 }, (err, data, res) => {
if (err) return reject(err)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/timelines/tag/${tag}`, { limit: 40 })
.then(data => {
commit('updateTimeline', data)
resolve(res)
})
})
},
startStreaming ({ state, commit, rootState }, tag) {
ipcRenderer.on('update-start-tag-streaming', (event, update) => {
Expand Down Expand Up @@ -108,22 +103,24 @@ const Tag = {
})
},
lazyFetchTimeline ({ state, commit, rootState }, obj) {
return new Promise((resolve, reject) => {
if (state.lazyLoading) {
return resolve()
}
commit('changeLazyLoading', true)
const client = new Mastodon(
{
access_token: rootState.TimelineSpace.account.accessToken,
api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
})
client.get(`/timelines/tag/${obj.tag}`, { max_id: obj.last.id, limit: 40 }, (err, data, res) => {
if (err) return reject(err)
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/timelines/tag/${obj.tag}`, { max_id: obj.last.id, limit: 40 })
.then(data => {
commit('insertTimeline', data)
commit('changeLazyLoading', false)
return data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
})
}
}
}
Expand Down
Loading

0 comments on commit e2bcfb8

Please sign in to comment.