]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel-share.ts
Fix upgrade script
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel-share.ts
1 import * as Sequelize from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { AccountModel } from '../account/account'
4 import { VideoChannelModel } from './video-channel'
5
6 @Table({
7 tableName: 'videoChannelShare',
8 indexes: [
9 {
10 fields: [ 'accountId' ]
11 },
12 {
13 fields: [ 'videoChannelId' ]
14 }
15 ]
16 })
17 export class VideoChannelShareModel extends Model<VideoChannelShareModel> {
18 @CreatedAt
19 createdAt: Date
20
21 @UpdatedAt
22 updatedAt: Date
23
24 @ForeignKey(() => AccountModel)
25 @Column
26 accountId: number
27
28 @BelongsTo(() => AccountModel, {
29 foreignKey: {
30 allowNull: false
31 },
32 onDelete: 'cascade'
33 })
34 Account: AccountModel
35
36 @ForeignKey(() => VideoChannelModel)
37 @Column
38 videoChannelId: number
39
40 @BelongsTo(() => VideoChannelModel, {
41 foreignKey: {
42 allowNull: false
43 },
44 onDelete: 'cascade'
45 })
46 VideoChannel: VideoChannelModel
47
48 static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
49 return VideoChannelShareModel.findOne({
50 where: {
51 accountId,
52 videoChannelId
53 },
54 include: [
55 AccountModel,
56 VideoChannelModel
57 ],
58 transaction: t
59 })
60 }
61
62 static loadAccountsByShare (videoChannelId: number, t: Sequelize.Transaction) {
63 const query = {
64 where: {
65 videoChannelId
66 },
67 include: [
68 {
69 model: AccountModel,
70 required: true
71 }
72 ],
73 transaction: t
74 }
75
76 return VideoChannelShareModel.findAll(query)
77 .then(res => res.map(r => r.Account))
78 }
79 }