]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-channel-share.ts
First step upload with new design
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel-share.ts
CommitLineData
d8465018
C
1import * as Sequelize from 'sequelize'
2
3import { addMethodsToModel } from '../utils'
d7d5611c 4import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface'
d8465018
C
5
6let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes>
d7d5611c 7let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare
4e50b6a1 8let load: VideoChannelShareMethods.Load
d8465018
C
9
10export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
11 VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare',
12 { },
13 {
14 indexes: [
15 {
16 fields: [ 'accountId' ]
17 },
18 {
19 fields: [ 'videoChannelId' ]
20 }
21 ]
22 }
23 )
24
25 const classMethods = [
d7d5611c 26 associate,
4e50b6a1 27 load,
d7d5611c 28 loadAccountsByShare
d8465018
C
29 ]
30 addMethodsToModel(VideoChannelShare, classMethods)
31
32 return VideoChannelShare
33}
34
35// ------------------------------ METHODS ------------------------------
36
37function associate (models) {
38 VideoChannelShare.belongsTo(models.Account, {
39 foreignKey: {
40 name: 'accountId',
41 allowNull: false
42 },
43 onDelete: 'cascade'
44 })
45
46 VideoChannelShare.belongsTo(models.VideoChannel, {
47 foreignKey: {
48 name: 'videoChannelId',
49 allowNull: true
50 },
51 onDelete: 'cascade'
52 })
53}
d7d5611c 54
25ed141c 55load = function (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
4e50b6a1
C
56 return VideoChannelShare.findOne({
57 where: {
58 accountId,
59 videoChannelId
60 },
61 include: [
62 VideoChannelShare['sequelize'].models.Account,
63 VideoChannelShare['sequelize'].models.VideoChannel
25ed141c
C
64 ],
65 transaction: t
4e50b6a1
C
66 })
67}
68
25ed141c 69loadAccountsByShare = function (videoChannelId: number, t: Sequelize.Transaction) {
d7d5611c
C
70 const query = {
71 where: {
72 videoChannelId
73 },
74 include: [
75 {
76 model: VideoChannelShare['sequelize'].models.Account,
77 required: true
78 }
25ed141c
C
79 ],
80 transaction: t
d7d5611c
C
81 }
82
83 return VideoChannelShare.findAll(query)
84 .then(res => res.map(r => r.Account))
85}