aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0100-activitypub.ts
blob: a7ebd804cd51e82447c2d5298b8378c9faeab7ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as Sequelize from 'sequelize'
import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
import { shareVideoByServerAndChannel } from '../../lib/activitypub/share'
import { getVideoActivityPubUrl, getVideoChannelActivityPubUrl } from '../../lib/activitypub/url'
import { createLocalAccountWithoutKeys } from '../../lib/user'
import { ApplicationModel } from '../../models/application/application'
import { SERVER_ACTOR_NAME } from '../constants'

async function up (utils: {
  transaction: Sequelize.Transaction,
  queryInterface: Sequelize.QueryInterface,
  sequelize: Sequelize.Sequelize,
  db: any
}): 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
    }
    const res = await utils.sequelize.query(query, options)

    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
  {
    const applicationInstance = await ApplicationModel.findOne()
    const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationInstance.id, undefined)

    const { publicKey, privateKey } = await createPrivateAndPublicKeys()
    accountCreated.set('publicKey', publicKey)
    accountCreated.set('privateKey', privateKey)

    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(user.username, user.id, null, undefined)

    const { publicKey, privateKey } = await createPrivateAndPublicKeys()
    account.set('publicKey', publicKey)
    account.set('privateKey', privateKey)
    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) {
      video.url = getVideoActivityPubUrl(video)
      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) {
      videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
      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'),
      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) {
      await shareVideoByServerAndChannel(video, undefined)
    }
  }
}

function down (options) {
  throw new Error('Not implemented.')
}

export {
  up,
  down
}