PeerTube/server/initializers/migrations/0100-activitypub.ts

217 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-11-27 02:47:21 -06:00
import * as Sequelize from 'sequelize'
import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
2017-12-15 10:34:38 -06:00
import { shareVideoByServerAndChannel } from '../../lib/activitypub/share'
2020-11-20 04:21:08 -06:00
import { getLocalVideoActivityPubUrl, getLocalVideoChannelActivityPubUrl } from '../../lib/activitypub/url'
2017-11-27 02:47:21 -06:00
import { createLocalAccountWithoutKeys } from '../../lib/user'
2017-12-12 10:53:50 -06:00
import { ApplicationModel } from '../../models/application/application'
import { SERVER_ACTOR_NAME } from '../constants'
2017-11-27 02:47:21 -06:00
async function up (utils: {
2020-01-31 09:56:52 -06:00
transaction: Sequelize.Transaction
queryInterface: Sequelize.QueryInterface
sequelize: Sequelize.Sequelize
2017-12-12 10:53:50 -06:00
db: any
2017-11-27 02:47:21 -06:00
}): Promise<void> {
const q = utils.queryInterface
const db = utils.db
// Assert there are no friends
{
const query = 'SELECT COUNT(*) as total FROM "Pods"'
const options = {
type: Sequelize.QueryTypes.SELECT
}
2019-04-18 04:28:17 -05:00
const res = await utils.sequelize.query(query, options) as any
2017-11-27 02:47:21 -06:00
if (!res[0] || res[0].total !== 0) {
throw new Error('You need to quit friends.')
}
}
// Pods -> Servers
await utils.queryInterface.renameTable('Pods', 'Servers')
// Create Account table
await db.Account.sync()
// Create AccountFollows table
await db.AccountFollow.sync()
// Modify video abuse table
await db.VideoAbuse.destroy({ truncate: true })
await utils.queryInterface.removeColumn('VideoAbuses', 'reporterPodId')
await utils.queryInterface.removeColumn('VideoAbuses', 'reporterUsername')
// Create column link with Account table
{
const data = {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Accounts',
key: 'id'
},
onDelete: 'CASCADE'
}
await q.addColumn('VideoAbuses', 'reporterAccountId', data)
}
// Drop request tables
await utils.queryInterface.dropTable('RequestToPods')
await utils.queryInterface.dropTable('RequestVideoEvents')
await utils.queryInterface.dropTable('RequestVideoQadus')
await utils.queryInterface.dropTable('Requests')
// Create application account
{
2017-12-12 10:53:50 -06:00
const applicationInstance = await ApplicationModel.findOne()
const accountCreated = await createLocalAccountWithoutKeys({
name: SERVER_ACTOR_NAME,
userId: null,
applicationId: applicationInstance.id,
t: undefined
})
2017-11-27 02:47:21 -06:00
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
2019-04-18 04:28:17 -05:00
accountCreated.Actor.publicKey = publicKey
accountCreated.Actor.privateKey = privateKey
2017-11-27 02:47:21 -06:00
await accountCreated.save()
}
// Drop old video channel foreign key (referencing Authors)
{
const query = 'ALTER TABLE "VideoChannels" DROP CONSTRAINT "VideoChannels_authorId_fkey"'
await utils.sequelize.query(query)
}
// Recreate accounts for each user
const users = await db.User.findAll()
for (const user of users) {
const account = await createLocalAccountWithoutKeys({ name: user.username, userId: user.id, applicationId: null, t: undefined })
2017-11-27 02:47:21 -06:00
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
2019-04-18 04:28:17 -05:00
account.Actor.publicKey = publicKey
account.Actor.privateKey = privateKey
2017-11-27 02:47:21 -06:00
await account.save()
}
{
const data = {
type: Sequelize.INTEGER,
allowNull: true,
onDelete: 'CASCADE',
reference: {
model: 'Account',
key: 'id'
}
}
await q.addColumn('VideoChannels', 'accountId', data)
{
const query = 'UPDATE "VideoChannels" SET "accountId" = ' +
'(SELECT "Accounts"."id" FROM "Accounts" INNER JOIN "Authors" ON "Authors"."userId" = "Accounts"."userId" ' +
'WHERE "VideoChannels"."authorId" = "Authors"."id")'
await utils.sequelize.query(query)
}
data.allowNull = false
await q.changeColumn('VideoChannels', 'accountId', data)
await q.removeColumn('VideoChannels', 'authorId')
}
// Add url column to "Videos"
{
const data = {
type: Sequelize.STRING,
defaultValue: null,
allowNull: true
}
await q.addColumn('Videos', 'url', data)
const videos = await db.Video.findAll()
for (const video of videos) {
2020-11-20 04:21:08 -06:00
video.url = getLocalVideoActivityPubUrl(video)
2017-11-27 02:47:21 -06:00
await video.save()
}
data.allowNull = false
await q.changeColumn('Videos', 'url', data)
}
// Add url column to "VideoChannels"
{
const data = {
type: Sequelize.STRING,
defaultValue: null,
allowNull: true
}
await q.addColumn('VideoChannels', 'url', data)
const videoChannels = await db.VideoChannel.findAll()
for (const videoChannel of videoChannels) {
2020-11-20 04:21:08 -06:00
videoChannel.url = getLocalVideoChannelActivityPubUrl(videoChannel)
2017-11-27 02:47:21 -06:00
await videoChannel.save()
}
data.allowNull = false
await q.changeColumn('VideoChannels', 'url', data)
}
// Loss old video rates, whatever
await utils.queryInterface.dropTable('UserVideoRates')
await db.AccountVideoRate.sync()
{
const data = {
type: Sequelize.ENUM('transcoding', 'activitypub-http'),
2017-11-27 02:47:21 -06:00
defaultValue: 'transcoding',
allowNull: false
}
await q.addColumn('Jobs', 'category', data)
}
await db.VideoShare.sync()
await db.VideoChannelShare.sync()
{
const videos = await db.Video.findAll({
include: [
{
model: db.Video['sequelize'].models.VideoChannel,
include: [
{
model: db.Video['sequelize'].models.Account,
include: [ { model: db.Video['sequelize'].models.Server, required: false } ]
}
]
},
{
model: db.Video['sequelize'].models.AccountVideoRate,
include: [ db.Video['sequelize'].models.Account ]
},
{
model: db.Video['sequelize'].models.VideoShare,
include: [ db.Video['sequelize'].models.Account ]
},
db.Video['sequelize'].models.Tag,
db.Video['sequelize'].models.VideoFile
]
})
for (const video of videos) {
2017-12-15 10:34:38 -06:00
await shareVideoByServerAndChannel(video, undefined)
2017-11-27 02:47:21 -06:00
}
}
}
function down (options) {
throw new Error('Not implemented.')
}
export {
up,
down
}