From 16b90975941b78d01d7202d441bf731a10048c16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 23 Nov 2017 16:55:13 +0100 Subject: Fetch video likes/dislikes too --- server/models/video/video-interface.ts | 3 +++ server/models/video/video.ts | 48 +++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-interface.ts b/server/models/video/video-interface.ts index b97f163ab..89e528acf 100644 --- a/server/models/video/video-interface.ts +++ b/server/models/video/video-interface.ts @@ -8,6 +8,8 @@ import { TagAttributes, TagInstance } from './tag-interface' import { VideoChannelInstance } from './video-channel-interface' import { VideoFileAttributes, VideoFileInstance } from './video-file-interface' import { VideoShareInstance } from './video-share-interface' +import { UserVideoRate } from '../../../shared/models/videos/user-video-rate.model' +import { AccountVideoRateInstance } from '../account/account-video-rate-interface' export namespace VideoMethods { export type GetThumbnailName = (this: VideoInstance) => string @@ -123,6 +125,7 @@ export interface VideoAttributes { Tags?: TagInstance[] VideoFiles?: VideoFileInstance[] VideoShares?: VideoShareInstance[] + AccountVideoRates?: AccountVideoRateInstance[] } export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.Instance { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 052fc0ae8..592fc2d59 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -47,6 +47,7 @@ import { VideoFileInstance, VideoFileModel } from './video-file-interface' import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface' import { sendDeleteVideo } from '../../lib/index' import * as Bluebird from 'bluebird' +import { activityPubCollection } from '../../helpers/activitypub' const Buffer = safeBuffer.Buffer @@ -359,6 +360,14 @@ function associate (models) { }, onDelete: 'cascade' }) + + Video.hasMany(models.AccountVideoRate, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'cascade' + }) } function afterDestroy (video: VideoInstance) { @@ -575,6 +584,25 @@ toActivityPubObject = function (this: VideoInstance) { } } + let likesObject + let dislikesObject + + if (Array.isArray(this.AccountVideoRates)) { + const likes: string[] = [] + const dislikes: string[] = [] + + for (const rate of this.AccountVideoRates) { + if (rate.type === 'like') { + likes.push(rate.Account.url) + } else if (rate.type === 'dislike') { + dislikes.push(rate.Account.url) + } + } + + likesObject = activityPubCollection(likes) + dislikesObject = activityPubCollection(dislikes) + } + const url = [] for (const file of this.VideoFiles) { url.push({ @@ -630,7 +658,9 @@ toActivityPubObject = function (this: VideoInstance) { width: THUMBNAILS_SIZE.width, height: THUMBNAILS_SIZE.height }, - url // FIXME: needed? + url, + likes: likesObject, + dislikes: dislikesObject } return videoObject @@ -845,8 +875,12 @@ listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, } ] }, - Video['sequelize'].models.Tag, - Video['sequelize'].models.VideoFile + { + model: Video['sequelize'].models.AccountVideoRate, + include: [ Video['sequelize'].models.Account ] + }, + Video['sequelize'].models.VideoFile, + Video['sequelize'].models.Tag ] } @@ -1106,6 +1140,10 @@ loadAndPopulateAccountAndServerAndTags = function (id: number) { } ] }, + { + model: Video['sequelize'].models.AccountVideoRate, + include: [ Video['sequelize'].models.Account ] + }, Video['sequelize'].models.Tag, Video['sequelize'].models.VideoFile ] @@ -1129,6 +1167,10 @@ loadByUUIDAndPopulateAccountAndServerAndTags = function (uuid: string) { } ] }, + { + model: Video['sequelize'].models.AccountVideoRate, + include: [ Video['sequelize'].models.Account ] + }, Video['sequelize'].models.Tag, Video['sequelize'].models.VideoFile ] -- cgit v1.2.3