2
1

0135-video-channel-actor.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import * as Sequelize from 'sequelize'
  2. import { DataType } from 'sequelize-typescript'
  3. import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
  4. async function up (utils: {
  5. transaction: Sequelize.Transaction,
  6. queryInterface: Sequelize.QueryInterface,
  7. sequelize: Sequelize.Sequelize
  8. }): Promise<void> {
  9. // Create actor table
  10. {
  11. const queries = [
  12. `DROP TYPE IF EXISTS enum_actor_type`,
  13. `
  14. CREATE TYPE enum_actor_type AS ENUM (
  15. 'Group',
  16. 'Person',
  17. 'Application'
  18. )
  19. `,
  20. `
  21. CREATE TABLE actor (
  22. id integer NOT NULL,
  23. type enum_actor_type NOT NULL,
  24. uuid uuid NOT NULL,
  25. "preferredUsername" character varying(255) NOT NULL,
  26. url character varying(2000) NOT NULL,
  27. "publicKey" character varying(5000),
  28. "privateKey" character varying(5000),
  29. "followersCount" integer NOT NULL,
  30. "followingCount" integer NOT NULL,
  31. "inboxUrl" character varying(2000) NOT NULL,
  32. "outboxUrl" character varying(2000) NOT NULL,
  33. "sharedInboxUrl" character varying(2000) NOT NULL,
  34. "followersUrl" character varying(2000) NOT NULL,
  35. "followingUrl" character varying(2000) NOT NULL,
  36. "avatarId" integer,
  37. "serverId" integer,
  38. "createdAt" timestamp with time zone NOT NULL,
  39. "updatedAt" timestamp with time zone NOT NULL
  40. );`,
  41. `CREATE SEQUENCE actor_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1`,
  42. `ALTER SEQUENCE actor_id_seq OWNED BY actor.id`,
  43. `ALTER TABLE ONLY actor ALTER COLUMN id SET DEFAULT nextval('actor_id_seq'::regclass)`,
  44. `ALTER TABLE ONLY actor ADD CONSTRAINT actor_pkey PRIMARY KEY (id);`,
  45. `CREATE UNIQUE INDEX actor_preferred_username_server_id ON actor USING btree ("preferredUsername", "serverId")`,
  46. `ALTER TABLE ONLY actor
  47. ADD CONSTRAINT "actor_avatarId_fkey" FOREIGN KEY ("avatarId") REFERENCES avatar(id) ON UPDATE CASCADE ON DELETE CASCADE`,
  48. `ALTER TABLE ONLY actor
  49. ADD CONSTRAINT "actor_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES server(id) ON UPDATE CASCADE ON DELETE CASCADE;`
  50. ]
  51. for (const query of queries) {
  52. await utils.sequelize.query(query)
  53. }
  54. }
  55. {
  56. // tslint:disable:no-trailing-whitespace
  57. const query1 =
  58. `
  59. INSERT INTO "actor"
  60. (
  61. type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
  62. "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
  63. )
  64. SELECT
  65. 'Application', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
  66. "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
  67. FROM account
  68. WHERE "applicationId" IS NOT NULL
  69. `
  70. await utils.sequelize.query(query1)
  71. const query2 =
  72. `
  73. INSERT INTO "actor"
  74. (
  75. type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
  76. "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
  77. )
  78. SELECT
  79. 'Person', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
  80. "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
  81. FROM account
  82. WHERE "applicationId" IS NULL
  83. `
  84. await utils.sequelize.query(query2)
  85. }
  86. {
  87. const data = {
  88. type: DataType.INTEGER,
  89. allowNull: true,
  90. references: {
  91. model: 'actor',
  92. key: 'id'
  93. },
  94. onDelete: 'CASCADE'
  95. }
  96. await utils.queryInterface.addColumn('account', 'actorId', data)
  97. const query1 = `UPDATE account SET "actorId" = (SELECT id FROM actor WHERE actor.url = account.url)`
  98. await utils.sequelize.query(query1)
  99. data.allowNull = false
  100. await utils.queryInterface.changeColumn('account', 'actorId', data)
  101. }
  102. {
  103. const query = `
  104. INSERT INTO actor
  105. (
  106. type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
  107. "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
  108. )
  109. SELECT
  110. 'Group', "videoChannel".uuid, "videoChannel".uuid, "videoChannel".url, null, null, 0, 0, "videoChannel".url || '/inbox',
  111. "videoChannel".url || '/outbox', "videoChannel".url || '/inbox', "videoChannel".url || '/followers', "videoChannel".url || '/following',
  112. null, account."serverId", "videoChannel"."createdAt", "videoChannel"."updatedAt"
  113. FROM "videoChannel"
  114. INNER JOIN "account" on "videoChannel"."accountId" = "account".id
  115. `
  116. await utils.sequelize.query(query)
  117. }
  118. {
  119. const data = {
  120. type: DataType.INTEGER,
  121. allowNull: true,
  122. references: {
  123. model: 'actor',
  124. key: 'id'
  125. },
  126. onDelete: 'CASCADE'
  127. }
  128. await utils.queryInterface.addColumn('videoChannel', 'actorId', data)
  129. const query1 = `UPDATE "videoChannel" SET "actorId" = (SELECT id FROM actor WHERE actor.url = "videoChannel".url)`
  130. await utils.sequelize.query(query1)
  131. data.allowNull = false
  132. await utils.queryInterface.changeColumn('videoChannel', 'actorId', data)
  133. }
  134. {
  135. await utils.queryInterface.renameTable('accountFollow', 'actorFollow')
  136. await utils.queryInterface.renameColumn('actorFollow', 'accountId', 'actorId')
  137. await utils.queryInterface.renameColumn('actorFollow', 'targetAccountId', 'targetActorId')
  138. try {
  139. await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_accountId_fkey')
  140. await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_targetAccountId_fkey')
  141. } catch {
  142. await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_accountId_fkey')
  143. await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_targetAccountId_fkey')
  144. }
  145. {
  146. const query1 = `UPDATE "actorFollow"
  147. SET "actorId" =
  148. (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."actorId")`
  149. await utils.sequelize.query(query1)
  150. const query2 = `UPDATE "actorFollow"
  151. SET "targetActorId" =
  152. (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."targetActorId")`
  153. await utils.sequelize.query(query2)
  154. }
  155. {
  156. const query1 = `ALTER TABLE ONLY "actorFollow"
  157. ADD CONSTRAINT "actorFollow_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
  158. await utils.sequelize.query(query1)
  159. const query2 = `ALTER TABLE ONLY "actorFollow"
  160. ADD CONSTRAINT "actorFollow_targetActorId_fkey" FOREIGN KEY ("targetActorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
  161. await utils.sequelize.query(query2)
  162. }
  163. }
  164. {
  165. await utils.queryInterface.renameColumn('videoShare', 'accountId', 'actorId')
  166. try {
  167. await utils.queryInterface.removeConstraint('videoShare', 'VideoShares_accountId_fkey')
  168. } catch {
  169. await utils.queryInterface.removeConstraint('videoShare', 'videoShare_accountId_fkey')
  170. }
  171. const query = `UPDATE "videoShare"
  172. SET "actorId" =
  173. (SELECT "actorId" FROM account WHERE id = "videoShare"."actorId")`
  174. await utils.sequelize.query(query)
  175. {
  176. const query1 = `ALTER TABLE ONLY "videoShare"
  177. ADD CONSTRAINT "videoShare_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
  178. await utils.sequelize.query(query1)
  179. const query2 = `ALTER TABLE ONLY "videoShare"
  180. ADD CONSTRAINT "videoShare_videoId_fkey" FOREIGN KEY ("videoId") REFERENCES video(id) ON UPDATE CASCADE ON DELETE CASCADE;`
  181. await utils.sequelize.query(query2)
  182. }
  183. }
  184. {
  185. const columnsToDelete = [
  186. 'uuid',
  187. 'url',
  188. 'publicKey',
  189. 'privateKey',
  190. 'followersCount',
  191. 'followingCount',
  192. 'inboxUrl',
  193. 'outboxUrl',
  194. 'sharedInboxUrl',
  195. 'followersUrl',
  196. 'followingUrl',
  197. 'serverId',
  198. 'avatarId'
  199. ]
  200. for (const columnToDelete of columnsToDelete) {
  201. await utils.queryInterface.removeColumn('account', columnToDelete)
  202. }
  203. }
  204. {
  205. const columnsToDelete = [
  206. 'uuid',
  207. 'remote',
  208. 'url'
  209. ]
  210. for (const columnToDelete of columnsToDelete) {
  211. await utils.queryInterface.removeColumn('videoChannel', columnToDelete)
  212. }
  213. }
  214. {
  215. const query = 'SELECT * FROM "actor" WHERE "serverId" IS NULL AND "publicKey" IS NULL'
  216. const [ res ] = await utils.sequelize.query(query)
  217. for (const actor of res) {
  218. const { privateKey, publicKey } = await createPrivateAndPublicKeys()
  219. const queryUpdate = `UPDATE "actor" SET "publicKey" = '${publicKey}', "privateKey" = '${privateKey}' WHERE id = ${actor.id}`
  220. await utils.sequelize.query(queryUpdate)
  221. }
  222. }
  223. }
  224. function down (options) {
  225. throw new Error('Not implemented.')
  226. }
  227. export {
  228. up,
  229. down
  230. }