]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/tag.js
Server: propagate video update to other pods
[github/Chocobozzz/PeerTube.git] / server / models / tag.js
CommitLineData
7920c273
C
1'use strict'
2
4ff0d862
C
3const each = require('async/each')
4
7920c273
C
5// ---------------------------------------------------------------------------
6
7module.exports = function (sequelize, DataTypes) {
8 const Tag = sequelize.define('Tag',
9 {
10 name: {
67bf9b96
C
11 type: DataTypes.STRING,
12 allowNull: false
7920c273
C
13 }
14 },
15 {
319d072e
C
16 timestamps: false,
17 indexes: [
18 {
19 fields: [ 'name' ],
20 unique: true
21 }
22 ],
7920c273 23 classMethods: {
4ff0d862
C
24 associate,
25
26 findOrCreateTags
7920c273
C
27 }
28 }
29 )
30
31 return Tag
32}
33
34// ---------------------------------------------------------------------------
35
36function associate (models) {
37 this.belongsToMany(models.Video, {
38 foreignKey: 'tagId',
39 through: models.VideoTag,
40 onDelete: 'cascade'
41 })
42}
4ff0d862
C
43
44function findOrCreateTags (tags, transaction, callback) {
45 if (!callback) {
46 callback = transaction
47 transaction = null
48 }
49
50 const self = this
51 const tagInstances = []
52
53 each(tags, function (tag, callbackEach) {
54 const query = {
55 where: {
56 name: tag
57 },
58 defaults: {
59 name: tag
60 }
61 }
62
63 if (transaction) query.transaction = transaction
64
65 self.findOrCreate(query).asCallback(function (err, res) {
66 if (err) return callbackEach(err)
67
68 // res = [ tag, isCreated ]
69 const tag = res[0]
70 tagInstances.push(tag)
71 return callbackEach()
72 })
73 }, function (err) {
74 return callback(err, tagInstances)
75 })
76}