aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/video-constant-manager-factory.ts
blob: f04dde29f2ed5f3c05047e66cc23076d211b9193 (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
import { logger } from '@server/helpers/logger'
import {
  VIDEO_CATEGORIES,
  VIDEO_LANGUAGES,
  VIDEO_LICENCES,
  VIDEO_PLAYLIST_PRIVACIES,
  VIDEO_PRIVACIES
} from '@server/initializers/constants'
import { ConstantManager } from '@shared/models/plugins/server/plugin-constant-manager.model'

type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
type VideoConstant = Record<number | string, string>

type UpdatedVideoConstant = {
  [name in AlterableVideoConstant]: {
    [ npmName: string]: {
      added: VideoConstant[]
      deleted: VideoConstant[]
    }
  }
}

const constantsHash: { [key in AlterableVideoConstant]: VideoConstant } = {
  language: VIDEO_LANGUAGES,
  licence: VIDEO_LICENCES,
  category: VIDEO_CATEGORIES,
  privacy: VIDEO_PRIVACIES,
  playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
}

export class VideoConstantManagerFactory {
  private readonly updatedVideoConstants: UpdatedVideoConstant = {
    playlistPrivacy: { },
    privacy: { },
    language: { },
    licence: { },
    category: { }
  }

  constructor (
    private readonly npmName: string
  ) {}

  public resetVideoConstants (npmName: string) {
    const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
    for (const type of types) {
      this.resetConstants({ npmName, type })
    }
  }

  private resetConstants (parameters: { npmName: string, type: AlterableVideoConstant }) {
    const { npmName, type } = parameters
    const updatedConstants = this.updatedVideoConstants[type][npmName]

    if (!updatedConstants) return

    for (const added of updatedConstants.added) {
      delete constantsHash[type][added.key]
    }

    for (const deleted of updatedConstants.deleted) {
      constantsHash[type][deleted.key] = deleted.label
    }

    delete this.updatedVideoConstants[type][npmName]
  }

  public createVideoConstantManager<K extends number | string>(type: AlterableVideoConstant): ConstantManager<K> {
    const { npmName } = this
    return {
      addConstant: (key: K, label: string) => this.addConstant({ npmName, type, key, label }),
      deleteConstant: (key: K) => this.deleteConstant({ npmName, type, key }),
      getConstantValue: (key: K) => constantsHash[type][key],
      getConstants: () => constantsHash[type] as Record<K, string>,
      resetConstants: () => this.resetConstants({ npmName, type })
    }
  }

  private addConstant<T extends string | number> (parameters: {
    npmName: string
    type: AlterableVideoConstant
    key: T
    label: string
  }) {
    const { npmName, type, key, label } = parameters
    const obj = constantsHash[type]

    if (obj[key]) {
      logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
      return false
    }

    if (!this.updatedVideoConstants[type][npmName]) {
      this.updatedVideoConstants[type][npmName] = {
        added: [],
        deleted: []
      }
    }

    this.updatedVideoConstants[type][npmName].added.push({ key: key, label } as VideoConstant)
    obj[key] = label

    return true
  }

  private deleteConstant<T extends string | number> (parameters: {
    npmName: string
    type: AlterableVideoConstant
    key: T
  }) {
    const { npmName, type, key } = parameters
    const obj = constantsHash[type]

    if (!obj[key]) {
      logger.warn('Cannot delete %s by plugin %s: key %s does not exist.', type, npmName, key)
      return false
    }

    if (!this.updatedVideoConstants[type][npmName]) {
      this.updatedVideoConstants[type][npmName] = {
        added: [],
        deleted: []
      }
    }

    const updatedConstants = this.updatedVideoConstants[type][npmName]

    const alreadyAdded = updatedConstants.added.find(a => a.key === key)
    if (alreadyAdded) {
      updatedConstants.added.filter(a => a.key !== key)
    } else if (obj[key]) {
      updatedConstants.deleted.push({ key, label: obj[key] } as VideoConstant)
    }

    delete obj[key]

    return true
  }
}