]>
Commit | Line | Data |
---|---|---|
d8465018 | 1 | import * as Sequelize from 'sequelize' |
d48ff09d | 2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
265ba139 | 3 | import { AccountModel } from '../account/account' |
50d6de9c | 4 | import { ActorModel } from '../activitypub/actor' |
3fd3ab2d | 5 | import { VideoModel } from './video' |
265ba139 | 6 | import { VideoChannelModel } from './video-channel' |
d8465018 | 7 | |
d48ff09d C |
8 | enum ScopeNames { |
9 | FULL = 'FULL', | |
50d6de9c | 10 | WITH_ACTOR = 'WITH_ACTOR' |
d48ff09d C |
11 | } |
12 | ||
13 | @Scopes({ | |
14 | [ScopeNames.FULL]: { | |
15 | include: [ | |
16 | { | |
50d6de9c | 17 | model: () => ActorModel, |
d48ff09d C |
18 | required: true |
19 | }, | |
20 | { | |
21 | model: () => VideoModel, | |
22 | required: true | |
23 | } | |
24 | ] | |
25 | }, | |
50d6de9c | 26 | [ScopeNames.WITH_ACTOR]: { |
d48ff09d C |
27 | include: [ |
28 | { | |
50d6de9c | 29 | model: () => ActorModel, |
d48ff09d C |
30 | required: true |
31 | } | |
32 | ] | |
33 | } | |
34 | }) | |
3fd3ab2d C |
35 | @Table({ |
36 | tableName: 'videoShare', | |
37 | indexes: [ | |
d8465018 | 38 | { |
50d6de9c | 39 | fields: [ 'actorId' ] |
3fd3ab2d C |
40 | }, |
41 | { | |
42 | fields: [ 'videoId' ] | |
d8465018 | 43 | } |
d8465018 | 44 | ] |
3fd3ab2d C |
45 | }) |
46 | export class VideoShareModel extends Model<VideoShareModel> { | |
47 | @CreatedAt | |
48 | createdAt: Date | |
d8465018 | 49 | |
3fd3ab2d C |
50 | @UpdatedAt |
51 | updatedAt: Date | |
d8465018 | 52 | |
50d6de9c | 53 | @ForeignKey(() => ActorModel) |
3fd3ab2d | 54 | @Column |
50d6de9c | 55 | actorId: number |
d8465018 | 56 | |
50d6de9c | 57 | @BelongsTo(() => ActorModel, { |
d8465018 | 58 | foreignKey: { |
d8465018 C |
59 | allowNull: false |
60 | }, | |
61 | onDelete: 'cascade' | |
62 | }) | |
50d6de9c | 63 | Actor: ActorModel |
d8465018 | 64 | |
3fd3ab2d C |
65 | @ForeignKey(() => VideoModel) |
66 | @Column | |
67 | videoId: number | |
68 | ||
69 | @BelongsTo(() => VideoModel, { | |
d8465018 | 70 | foreignKey: { |
3fd3ab2d | 71 | allowNull: false |
d8465018 C |
72 | }, |
73 | onDelete: 'cascade' | |
74 | }) | |
3fd3ab2d | 75 | Video: VideoModel |
4e50b6a1 | 76 | |
50d6de9c C |
77 | static load (actorId: number, videoId: number, t: Sequelize.Transaction) { |
78 | return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({ | |
3fd3ab2d | 79 | where: { |
50d6de9c | 80 | actorId, |
3fd3ab2d C |
81 | videoId |
82 | }, | |
3fd3ab2d C |
83 | transaction: t |
84 | }) | |
d7d5611c C |
85 | } |
86 | ||
50d6de9c | 87 | static loadActorsByShare (videoId: number, t: Sequelize.Transaction) { |
3fd3ab2d C |
88 | const query = { |
89 | where: { | |
90 | videoId | |
91 | }, | |
92 | include: [ | |
93 | { | |
50d6de9c | 94 | model: ActorModel, |
3fd3ab2d C |
95 | required: true |
96 | } | |
97 | ], | |
98 | transaction: t | |
99 | } | |
100 | ||
d48ff09d | 101 | return VideoShareModel.scope(ScopeNames.FULL).findAll(query) |
50d6de9c | 102 | .then(res => res.map(r => r.Actor)) |
3fd3ab2d | 103 | } |
265ba139 C |
104 | |
105 | static loadActorsByVideoOwner (actorOwnerId: number, t: Sequelize.Transaction) { | |
106 | const query = { | |
107 | attributes: [], | |
108 | include: [ | |
109 | { | |
110 | model: ActorModel, | |
111 | required: true | |
112 | }, | |
113 | { | |
114 | attributes: [], | |
115 | model: VideoModel, | |
116 | required: true, | |
117 | include: [ | |
118 | { | |
119 | attributes: [], | |
120 | model: VideoChannelModel.unscoped(), | |
121 | required: true, | |
122 | include: [ | |
123 | { | |
124 | attributes: [], | |
125 | model: AccountModel.unscoped(), | |
126 | required: true, | |
127 | where: { | |
128 | actorId: actorOwnerId | |
129 | } | |
130 | } | |
131 | ] | |
132 | } | |
133 | ] | |
134 | } | |
135 | ], | |
136 | transaction: t | |
137 | } | |
138 | ||
139 | return VideoShareModel.scope(ScopeNames.FULL).findAll(query) | |
140 | .then(res => res.map(r => r.Actor)) | |
141 | } | |
d7d5611c | 142 | } |