2000000000000-Views.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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"->>'comment' IS NULL OR EXISTS(SELECT 1 FROM "comment" WHERE "id"="data"->>'comment'))`
  58. ],
  59. storage_data_object: [
  60. `("type"->>'channel' IS NULL OR EXISTS(SELECT 1 FROM "channel" WHERE "id"="type"->>'channel'))`,
  61. `("type"->>'video' IS NULL OR EXISTS(SELECT 1 FROM "video" WHERE "id"="type"->>'video'))`
  62. ],
  63. notification: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  64. nft_history_entry: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  65. nft_activity: [`EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")`],
  66. // HIDDEN entities
  67. video_view_event: ['FALSE'],
  68. channel_follow: ['FALSE'],
  69. report: ['FALSE'],
  70. nft_featuring_request: ['FALSE'],
  71. }
  72. }
  73. async up(db) {
  74. // Create new schema for the processor in order to be easily able to access "hidden" entities
  75. await db.query(`CREATE SCHEMA "processor"`)
  76. const viewDefinitions = this.getViewDefinitions(db)
  77. for (const [tableName, viewConditions] of Object.entries(viewDefinitions)) {
  78. await db.query(`ALTER TABLE "${tableName}" SET SCHEMA "processor"`)
  79. if (Array.isArray(viewConditions)) {
  80. await db.query(`
  81. CREATE VIEW "${tableName}" AS
  82. SELECT *
  83. FROM "processor"."${tableName}" AS "this"
  84. WHERE ${viewConditions.map(cond => `(${cond})`).join(" AND\n")}
  85. `)
  86. } else {
  87. await db.query(`
  88. CREATE VIEW "${tableName}" AS (${viewConditions})`)
  89. }
  90. }
  91. }
  92. async down(db) {
  93. const viewDefinitions = this.getViewDefinitions(db)
  94. for (const viewName of Object.keys(viewDefinitions)) {
  95. await db.query(`DROP VIEW "${viewName}"`)
  96. await db.query(`ALTER TABLE "processor"."${viewName}" SET SCHEMA "public"`)
  97. }
  98. await db.query(`DROP SCHEMA "processor"`)
  99. }
  100. }