From 4e50b6a1c9a3eb261e04ede73241648e6edf21d6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 27 Nov 2017 14:44:51 +0100 Subject: Add shares forward and collection on videos/video channels --- server/models/video/video-channel-interface.ts | 2 ++ server/models/video/video-channel-share-interface.ts | 2 ++ server/models/video/video-channel-share.ts | 15 +++++++++++++++ server/models/video/video-channel.ts | 17 ++++++++++++++++- server/models/video/video-share-interface.ts | 6 ++++-- server/models/video/video-share.ts | 16 +++++++++++++++- server/models/video/video.ts | 19 +++++++++++++++++-- 7 files changed, 71 insertions(+), 6 deletions(-) (limited to 'server/models') diff --git a/server/models/video/video-channel-interface.ts b/server/models/video/video-channel-interface.ts index b409d1db3..21f81e901 100644 --- a/server/models/video/video-channel-interface.ts +++ b/server/models/video/video-channel-interface.ts @@ -6,6 +6,7 @@ import { VideoChannelObject } from '../../../shared/models/activitypub/objects/v import { VideoChannel as FormattedVideoChannel } from '../../../shared/models/videos/video-channel.model' import { AccountInstance } from '../account/account-interface' import { VideoInstance } from './video-interface' +import { VideoChannelShareInstance } from './video-channel-share-interface' export namespace VideoChannelMethods { export type ToFormattedJSON = (this: VideoChannelInstance) => FormattedVideoChannel @@ -47,6 +48,7 @@ export interface VideoChannelAttributes { Account?: AccountInstance Videos?: VideoInstance[] + VideoChannelShares?: VideoChannelShareInstance[] } export interface VideoChannelInstance extends VideoChannelClass, VideoChannelAttributes, Sequelize.Instance { diff --git a/server/models/video/video-channel-share-interface.ts b/server/models/video/video-channel-share-interface.ts index 8bb531af2..bcb3a0e24 100644 --- a/server/models/video/video-channel-share-interface.ts +++ b/server/models/video/video-channel-share-interface.ts @@ -5,10 +5,12 @@ import { VideoChannelInstance } from './video-channel-interface' export namespace VideoChannelShareMethods { export type LoadAccountsByShare = (videoChannelId: number) => Bluebird + export type Load = (accountId: number, videoId: number) => Bluebird } export interface VideoChannelShareClass { loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare + load: VideoChannelShareMethods.Load } export interface VideoChannelShareAttributes { diff --git a/server/models/video/video-channel-share.ts b/server/models/video/video-channel-share.ts index 01f84c806..e47c0dae7 100644 --- a/server/models/video/video-channel-share.ts +++ b/server/models/video/video-channel-share.ts @@ -5,6 +5,7 @@ import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelSha let VideoChannelShare: Sequelize.Model let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare +let load: VideoChannelShareMethods.Load export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { VideoChannelShare = sequelize.define('VideoChannelShare', @@ -23,6 +24,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da const classMethods = [ associate, + load, loadAccountsByShare ] addMethodsToModel(VideoChannelShare, classMethods) @@ -50,6 +52,19 @@ function associate (models) { }) } +load = function (accountId: number, videoChannelId: number) { + return VideoChannelShare.findOne({ + where: { + accountId, + videoChannelId + }, + include: [ + VideoChannelShare['sequelize'].models.Account, + VideoChannelShare['sequelize'].models.VideoChannel + ] + }) +} + loadAccountsByShare = function (videoChannelId: number) { const query = { where: { diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 64130310d..e11268b2c 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -6,6 +6,8 @@ import { sendDeleteVideoChannel } from '../../lib/activitypub/send/send-delete' import { addMethodsToModel, getSort } from '../utils' import { VideoChannelAttributes, VideoChannelInstance, VideoChannelMethods } from './video-channel-interface' +import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url' +import { activityPubCollection } from '../../helpers/activitypub' let VideoChannel: Sequelize.Model let toFormattedJSON: VideoChannelMethods.ToFormattedJSON @@ -139,6 +141,18 @@ toFormattedJSON = function (this: VideoChannelInstance) { } toActivityPubObject = function (this: VideoChannelInstance) { + let sharesObject + if (Array.isArray(this.VideoChannelShares)) { + const shares: string[] = [] + + for (const videoChannelShare of this.VideoChannelShares) { + const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account) + shares.push(shareUrl) + } + + sharesObject = activityPubCollection(shares) + } + const json = { type: 'VideoChannel' as 'VideoChannel', id: this.url, @@ -146,7 +160,8 @@ toActivityPubObject = function (this: VideoChannelInstance) { content: this.description, name: this.name, published: this.createdAt.toISOString(), - updated: this.updatedAt.toISOString() + updated: this.updatedAt.toISOString(), + shares: sharesObject } return json diff --git a/server/models/video/video-share-interface.ts b/server/models/video/video-share-interface.ts index 569568842..ad23444b6 100644 --- a/server/models/video/video-share-interface.ts +++ b/server/models/video/video-share-interface.ts @@ -1,14 +1,16 @@ +import * as Bluebird from 'bluebird' import * as Sequelize from 'sequelize' import { AccountInstance } from '../account/account-interface' import { VideoInstance } from './video-interface' -import * as Bluebird from 'bluebird' export namespace VideoShareMethods { - export type LoadAccountsByShare = (videoChannelId: number) => Bluebird + export type LoadAccountsByShare = (videoId: number) => Bluebird + export type Load = (accountId: number, videoId: number) => Bluebird } export interface VideoShareClass { loadAccountsByShare: VideoShareMethods.LoadAccountsByShare + load: VideoShareMethods.Load } export interface VideoShareAttributes { diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index 22ac31a4a..fe5d56d42 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -5,6 +5,7 @@ import { VideoShareAttributes, VideoShareInstance, VideoShareMethods } from './v let VideoShare: Sequelize.Model let loadAccountsByShare: VideoShareMethods.LoadAccountsByShare +let load: VideoShareMethods.Load export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { VideoShare = sequelize.define('VideoShare', @@ -23,7 +24,8 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da const classMethods = [ associate, - loadAccountsByShare + loadAccountsByShare, + load ] addMethodsToModel(VideoShare, classMethods) @@ -50,6 +52,18 @@ function associate (models) { }) } +load = function (accountId: number, videoId: number) { + return VideoShare.findOne({ + where: { + accountId, + videoId + }, + include: [ + VideoShare['sequelize'].models.Account + ] + }) +} + loadAccountsByShare = function (videoId: number) { const query = { where: { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 457bfce77..4956b57ee 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -32,6 +32,7 @@ import { isVideoNameValid, isVideoLicenceValid, isVideoNSFWValid, isVideoDescrip import { logger } from '../../helpers/logger' import { generateImageFromVideoFile, transcode, getVideoFileHeight } from '../../helpers/ffmpeg-utils' import { createTorrentPromise, writeFilePromise, unlinkPromise, renamePromise, statPromise } from '../../helpers/core-utils' +import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url' let Video: Sequelize.Model let getOriginalFile: VideoMethods.GetOriginalFile @@ -573,6 +574,18 @@ toActivityPubObject = function (this: VideoInstance) { dislikesObject = activityPubCollection(dislikes) } + let sharesObject + if (Array.isArray(this.VideoShares)) { + const shares: string[] = [] + + for (const videoShare of this.VideoShares) { + const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Account) + shares.push(shareUrl) + } + + sharesObject = activityPubCollection(shares) + } + const url = [] for (const file of this.VideoFiles) { url.push({ @@ -630,7 +643,8 @@ toActivityPubObject = function (this: VideoInstance) { }, url, likes: likesObject, - dislikes: dislikesObject + dislikes: dislikesObject, + shares: sharesObject } return videoObject @@ -823,7 +837,8 @@ listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, accountId } ] - } + }, + include: [ Video['sequelize'].models.Account ] }, { model: Video['sequelize'].models.VideoChannel, -- cgit v1.2.3