X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-share.ts;h=c252fd64696bcc2714b29470a8dcbf46ba571ec3;hb=47564bbe2eeb2baae9b7e3f9b2b8d16522bc7e04;hp=fe5d56d42b16b4e151a465bc847320fdb70838e0;hpb=4e50b6a1c9a3eb261e04ede73241648e6edf21d6;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index fe5d56d42..c252fd646 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -1,82 +1,102 @@ import * as Sequelize from 'sequelize' +import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { ActorModel } from '../activitypub/actor' +import { VideoModel } from './video' -import { addMethodsToModel } from '../utils' -import { VideoShareAttributes, VideoShareInstance, VideoShareMethods } from './video-share-interface' - -let VideoShare: Sequelize.Model -let loadAccountsByShare: VideoShareMethods.LoadAccountsByShare -let load: VideoShareMethods.Load +enum ScopeNames { + FULL = 'FULL', + WITH_ACTOR = 'WITH_ACTOR' +} -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - VideoShare = sequelize.define('VideoShare', - { }, +@Scopes({ + [ScopeNames.FULL]: { + include: [ + { + model: () => ActorModel, + required: true + }, + { + model: () => VideoModel, + required: true + } + ] + }, + [ScopeNames.WITH_ACTOR]: { + include: [ + { + model: () => ActorModel, + required: true + } + ] + } +}) +@Table({ + tableName: 'videoShare', + indexes: [ { - indexes: [ - { - fields: [ 'accountId' ] - }, - { - fields: [ 'videoId' ] - } - ] + fields: [ 'actorId' ] + }, + { + fields: [ 'videoId' ] } - ) - - const classMethods = [ - associate, - loadAccountsByShare, - load ] - addMethodsToModel(VideoShare, classMethods) +}) +export class VideoShareModel extends Model { + @CreatedAt + createdAt: Date - return VideoShare -} + @UpdatedAt + updatedAt: Date -// ------------------------------ METHODS ------------------------------ + @ForeignKey(() => ActorModel) + @Column + actorId: number -function associate (models) { - VideoShare.belongsTo(models.Account, { + @BelongsTo(() => ActorModel, { foreignKey: { - name: 'accountId', allowNull: false }, onDelete: 'cascade' }) + Actor: ActorModel + + @ForeignKey(() => VideoModel) + @Column + videoId: number - VideoShare.belongsTo(models.Video, { + @BelongsTo(() => VideoModel, { foreignKey: { - name: 'videoId', - allowNull: true + allowNull: false }, onDelete: 'cascade' }) -} + Video: VideoModel -load = function (accountId: number, videoId: number) { - return VideoShare.findOne({ - where: { - accountId, - videoId - }, - include: [ - VideoShare['sequelize'].models.Account - ] - }) -} - -loadAccountsByShare = function (videoId: number) { - const query = { - where: { - videoId - }, - include: [ - { - model: VideoShare['sequelize'].models.Account, - required: true - } - ] + static load (actorId: number, videoId: number, t: Sequelize.Transaction) { + return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({ + where: { + actorId, + videoId + }, + transaction: t + }) } - return VideoShare.findAll(query) - .then(res => res.map(r => r.Account)) + static loadActorsByShare (videoId: number, t: Sequelize.Transaction) { + const query = { + where: { + videoId + }, + include: [ + { + model: ActorModel, + required: true + } + ], + transaction: t + } + + return VideoShareModel.scope(ScopeNames.FULL).findAll(query) + .then(res => res.map(r => r.Actor)) + } }