2021-07-05 03:19:04 +00:00
|
|
|
import { actions } from './mastodon-data.js'
|
|
|
|
import { users } from '../tests/users.js'
|
|
|
|
import { postStatus } from '../src/routes/_api/statuses.js'
|
|
|
|
import { followAccount } from '../src/routes/_api/follow.js'
|
|
|
|
import { favoriteStatus } from '../src/routes/_api/favorite.js'
|
|
|
|
import { reblogStatus } from '../src/routes/_api/reblog.js'
|
2018-03-06 04:51:42 +00:00
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import FileApi from 'file-api'
|
2021-07-05 03:19:04 +00:00
|
|
|
import { pinStatus } from '../src/routes/_api/pin.js'
|
|
|
|
import { submitMedia } from '../tests/submitMedia.js'
|
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 () {
|
2018-03-05 18:10:50 +00:00
|
|
|
console.log('Restoring mastodon data...')
|
2019-08-03 20:49:37 +00:00
|
|
|
const internalIdsToIds = {}
|
|
|
|
for (const action of actions) {
|
2022-11-18 01:09:24 +00:00
|
|
|
if (!action.post || /@/.test(action.post.text)) {
|
|
|
|
// If the action is a boost, favorite, mention, etc., then it needs to
|
2018-11-11 19:12:41 +00:00
|
|
|
// be delayed, otherwise it may appear in an unpredictable order and break the tests.
|
2022-11-18 01:09:24 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1500))
|
2018-11-11 19:12:41 +00:00
|
|
|
}
|
2018-03-06 04:51:42 +00:00
|
|
|
console.log(JSON.stringify(action))
|
2019-08-03 20:49:37 +00:00
|
|
|
const accessToken = users[action.user].accessToken
|
2018-03-06 17:03:59 +00:00
|
|
|
|
2018-03-06 04:29:49 +00:00
|
|
|
if (action.post) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { text, media, sensitive, spoiler, privacy, inReplyTo, internalId } = action.post
|
|
|
|
const mediaIds = media && await Promise.all(media.map(async mediaItem => {
|
|
|
|
const mediaResponse = await submitMedia(accessToken, mediaItem, 'kitten')
|
2018-03-06 04:29:49 +00:00
|
|
|
return mediaResponse.id
|
|
|
|
}))
|
2019-08-03 20:49:37 +00:00
|
|
|
const inReplyToId = inReplyTo && internalIdsToIds[inReplyTo]
|
|
|
|
const status = await postStatus('localhost:3000', accessToken, text, inReplyToId, mediaIds,
|
2018-03-06 04:29:49 +00:00
|
|
|
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 05:21:28 +00:00
|
|
|
console.log('Restored mastodon data')
|
2018-03-06 07:56:48 +00:00
|
|
|
}
|