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