From e4f97babf701481b55cc10fb3448feab5f97c867 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Nov 2017 17:51:58 +0100 Subject: Begin activitypub --- server/models/account/account-video-rate.ts | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 server/models/account/account-video-rate.ts (limited to 'server/models/account/account-video-rate.ts') diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts new file mode 100644 index 000000000..7f7c97606 --- /dev/null +++ b/server/models/account/account-video-rate.ts @@ -0,0 +1,78 @@ +/* + Account rates per video. +*/ +import { values } from 'lodash' +import * as Sequelize from 'sequelize' + +import { VIDEO_RATE_TYPES } from '../../initializers' + +import { addMethodsToModel } from '../utils' +import { + AccountVideoRateInstance, + AccountVideoRateAttributes, + + AccountVideoRateMethods +} from './account-video-rate-interface' + +let AccountVideoRate: Sequelize.Model +let load: AccountVideoRateMethods.Load + +export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { + AccountVideoRate = sequelize.define('AccountVideoRate', + { + type: { + type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), + allowNull: false + } + }, + { + indexes: [ + { + fields: [ 'videoId', 'accountId', 'type' ], + unique: true + } + ] + } + ) + + const classMethods = [ + associate, + + load + ] + addMethodsToModel(AccountVideoRate, classMethods) + + return AccountVideoRate +} + +// ------------------------------ STATICS ------------------------------ + +function associate (models) { + AccountVideoRate.belongsTo(models.Video, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'CASCADE' + }) + + AccountVideoRate.belongsTo(models.Account, { + foreignKey: { + name: 'accountId', + allowNull: false + }, + onDelete: 'CASCADE' + }) +} + +load = function (accountId: number, videoId: number, transaction: Sequelize.Transaction) { + const options: Sequelize.FindOptions = { + where: { + accountId, + videoId + } + } + if (transaction) options.transaction = transaction + + return AccountVideoRate.findOne(options) +} -- cgit v1.2.3