2018-03-06 04:29:49 +00:00
|
|
|
import { actions } from './mastodon-data'
|
|
|
|
import { users } from '../tests/users'
|
2018-03-06 06:36:54 +00:00
|
|
|
import { pinStatus, postStatus } from '../routes/_api/statuses'
|
2018-03-06 04:29:49 +00:00
|
|
|
import { uploadMedia } from '../routes/_api/media'
|
|
|
|
import { followAccount } from '../routes/_api/follow'
|
|
|
|
import { favoriteStatus } from '../routes/_api/favorite'
|
|
|
|
import { reblogStatus } from '../routes/_api/reblog'
|
2018-03-06 04:51:42 +00:00
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import FileApi from 'file-api'
|
2018-03-06 04:29:49 +00:00
|
|
|
import path from 'path'
|
2018-03-06 05:21:28 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import pify from 'pify'
|
|
|
|
|
|
|
|
const readFile = pify(fs.readFile.bind(fs))
|
2018-03-05 18:10:50 +00:00
|
|
|
|
2018-03-06 04:51:42 +00:00
|
|
|
global.File = FileApi.File
|
|
|
|
global.FormData = FileApi.FormData
|
|
|
|
global.fetch = fetch
|
|
|
|
|
|
|
|
export async function restoreMastodonData () {
|
|
|
|
debugger
|
2018-03-05 18:10:50 +00:00
|
|
|
console.log('Restoring mastodon data...')
|
2018-03-06 04:29:49 +00:00
|
|
|
let internalIdsToIds = {}
|
|
|
|
for (let action of actions) {
|
2018-03-06 04:51:42 +00:00
|
|
|
console.log(JSON.stringify(action))
|
2018-03-06 04:29:49 +00:00
|
|
|
let accessToken = users[action.user].accessToken
|
|
|
|
if (action.post) {
|
|
|
|
let { text, media, sensitive, spoiler, privacy, inReplyTo, internalId } = action.post
|
|
|
|
if (typeof inReplyTo !== 'undefined') {
|
|
|
|
inReplyTo = internalIdsToIds[inReplyTo]
|
|
|
|
}
|
|
|
|
let mediaIds = media && await Promise.all(media.map(async mediaItem => {
|
2018-03-06 05:21:28 +00:00
|
|
|
let type = mediaItem.endsWith('gif') ? 'image/gif'
|
|
|
|
: mediaItem.endsWith('jpg') ? 'image/jpg' : 'video/mp4'
|
|
|
|
let file = new File({
|
|
|
|
name: mediaItem,
|
|
|
|
type: type,
|
|
|
|
buffer: await readFile(path.join(__dirname, '../tests/images/' + mediaItem))
|
|
|
|
})
|
2018-03-06 06:36:54 +00:00
|
|
|
let mediaResponse = await uploadMedia('localhost:3000', accessToken, file, 'kitten')
|
2018-03-06 04:29:49 +00:00
|
|
|
return mediaResponse.id
|
|
|
|
}))
|
|
|
|
let status = await postStatus('localhost:3000', accessToken, text, inReplyTo, mediaIds,
|
|
|
|
sensitive, spoiler, privacy || 'public')
|
|
|
|
if (typeof internalId !== 'undefined') {
|
|
|
|
internalIdsToIds[internalId] = status.id
|
|
|
|
}
|
|
|
|
} else if (action.follow) {
|
2018-03-06 04:51:42 +00:00
|
|
|
await followAccount('localhost:3000', accessToken, users[action.follow].id)
|
2018-03-06 04:29:49 +00:00
|
|
|
} else if (action.favorite) {
|
|
|
|
await favoriteStatus('localhost:3000', accessToken, internalIdsToIds[action.favorite])
|
|
|
|
} else if (action.boost) {
|
2018-03-06 04:51:42 +00:00
|
|
|
await reblogStatus('localhost:3000', accessToken, internalIdsToIds[action.boost])
|
2018-03-06 06:36:54 +00:00
|
|
|
} else if (action.pin) {
|
|
|
|
await pinStatus('localhost:3000', accessToken, internalIdsToIds[action.pin])
|
2018-03-06 04:29:49 +00:00
|
|
|
}
|
2018-03-06 06:36:54 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1500))
|
2018-03-06 04:29:49 +00:00
|
|
|
}
|
2018-03-06 05:21:28 +00:00
|
|
|
console.log('Restored mastodon data')
|
2018-03-06 04:53:52 +00:00
|
|
|
}
|