2000000000000-Views.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const noCategoryVideosSupportedByDefault =
  2. process.env.SUPPORT_NO_CATEGORY_VIDEOS === 'true' ||
  3. process.env.SUPPORT_NO_CATEGORY_VIDEOS === '1'
  4. module.exports = class Views2000000000000 {
  5. name = 'Views2000000000000'
  6. getViewDefinitions(db) {
  7. return {
  8. channel: [`is_excluded='0'`, `is_censored='0'`],
  9. banned_member: [`EXISTS(SELECT 1 FROM "channel" WHERE "id"="channel_id")`],
  10. video: [
  11. `is_excluded='0'`,
  12. `is_censored='0'`,
  13. `EXISTS(SELECT 1 FROM "channel" WHERE "id"="channel_id")`,
  14. `EXISTS(SELECT 1 FROM "video_category" WHERE "id"="category_id" AND "is_supported"='1')
  15. OR (
  16. "category_id" IS NULL
  17. AND COALESCE(
  18. (SELECT "value" FROM "gateway_config" WHERE "id"='SUPPORT_NO_CATEGORY_VIDEOS'),
  19. ${noCategoryVideosSupportedByDefault ? "'1'" : "'0'"}
  20. )='1'
  21. )`
  22. ],
  23. video_category: [`"is_supported" = '1'`],
  24. owned_nft: [`EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`],
  25. auction: [`EXISTS(SELECT 1 FROM "owned_nft" WHERE "id"="nft_id")`],
  26. bid: [`EXISTS(SELECT 1 FROM "owned_nft" WHERE "id"="nft_id")`],
  27. comment: `
  28. SELECT
  29. ${db.connection
  30. .getMetadata('Comment')
  31. .columns.filter((c) => c.databaseName !== 'text')
  32. .map((c) => `"${c.databaseName}"`)
  33. .join(',')},
  34. CASE WHEN "is_excluded" = '1' THEN '' ELSE "comment"."text" END as "text"
  35. FROM
  36. "processor"."comment"
  37. WHERE EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")
  38. `,
  39. comment_reaction: [`EXISTS(SELECT 1 FROM "comment" WHERE "id"="comment_id")`],
  40. license: [`EXISTS(SELECT 1 FROM "video" WHERE "license_id"="this"."id")`],
  41. video_media_metadata: [`EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`],
  42. video_media_encoding: [`EXISTS(SELECT 1 FROM "video_media_metadata" WHERE "encoding_id"="this"."id")`],
  43. video_reaction: [`EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`],
  44. video_subtitle: [`EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`],
  45. video_featured_in_category: [
  46. `EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`,
  47. `EXISTS(SELECT 1 FROM "video_category" WHERE "id"="category_id")`
  48. ],
  49. video_hero: [`EXISTS(SELECT 1 FROM "video" WHERE "id"="video_id")`],
  50. // TODO: Consider all events having ref to a video they're related to - this will make filtering much easier
  51. event: [
  52. `("data"->>'channel' IS NULL OR EXISTS(SELECT 1 FROM "channel" WHERE "id"="data"->>'channel'))`,
  53. `("data"->>'video' IS NULL OR EXISTS(SELECT 1 FROM "video" WHERE "id"="data"->>'video'))`,
  54. `("data"->>'nft' IS NULL OR EXISTS(SELECT 1 FROM "owned_nft" WHERE "id"="data"->>'nft'))`,
  55. `("data"->>'auction' IS NULL OR EXISTS(SELECT 1 FROM "auction" WHERE "id"="data"->>'auction'))`,
  56. `("data"->>'bid' IS NULL OR EXISTS(SELECT 1 FROM "bid" WHERE "id"="data"->>'bid'))`,
  57. `("data"->>'winningBid' IS NULL OR EXISTS(SELECT 1 FROM "bid" WHERE "id"="data"->>'winningBid'))`,
  58. `("data"->>'comment' IS NULL OR EXISTS(SELECT 1 FROM "comment" WHERE "id"="data"->>'comment'))`
  59. ],
  60. storage_data_object: [
  61. `("type"->>'channel' IS NULL OR EXISTS(SELECT 1 FROM "channel" WHERE "id"="type"->>'channel'))`,
  62. `("type"->>'video' IS NULL OR EXISTS(SELECT 1 FROM "video" WHERE "id"="type"->>'video'))`
  63. ],
  64. notification: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  65. nft_history_entry: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  66. nft_activity: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  67. // HIDDEN entities
  68. video_view_event: ['FALSE'],
  69. channel_follow: ['FALSE'],
  70. report: ['FALSE'],
  71. nft_featuring_request: ['FALSE'],
  72. }
  73. }
  74. async up(db) {
  75. // Create new schema for the processor in order to be easily able to access "hidden" entities
  76. await db.query(`CREATE SCHEMA "processor"`)
  77. const viewDefinitions = this.getViewDefinitions(db)
  78. for (const [tableName, viewConditions] of Object.entries(viewDefinitions)) {
  79. await db.query(`ALTER TABLE "${tableName}" SET SCHEMA "processor"`)
  80. if (Array.isArray(viewConditions)) {
  81. await db.query(`
  82. CREATE VIEW "${tableName}" AS
  83. SELECT *
  84. FROM "processor"."${tableName}" AS "this"
  85. WHERE ${viewConditions.map(cond => `(${cond})`).join(" AND\n")}
  86. `)
  87. } else {
  88. await db.query(`
  89. CREATE VIEW "${tableName}" AS (${viewConditions})`)
  90. }
  91. }
  92. }
  93. async down(db) {
  94. const viewDefinitions = this.getViewDefinitions(db)
  95. for (const viewName of Object.keys(viewDefinitions)) {
  96. await db.query(`DROP VIEW "${viewName}"`)
  97. await db.query(`ALTER TABLE "processor"."${viewName}" SET SCHEMA "public"`)
  98. }
  99. await db.query(`DROP SCHEMA "processor"`)
  100. }
  101. }