From e71bcc0f4b31ecfd84a786411febfc6d18a85258 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 21 Nov 2017 18:23:10 +0100 Subject: Add outbox --- server/models/account/account-follow.ts | 4 +-- server/models/video/video-interface.ts | 8 +++++ server/models/video/video.ts | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account-follow.ts b/server/models/account/account-follow.ts index 34ba3f8db..578bcda39 100644 --- a/server/models/account/account-follow.ts +++ b/server/models/account/account-follow.ts @@ -221,8 +221,8 @@ async function createListAcceptedFollowForApiQuery ( 'INNER JOIN "Accounts" AS "Follows" ON "AccountFollows"."' + secondJoin + '" = "Follows"."id" ' + 'WHERE "Accounts"."id" = ANY ($accountIds) AND "AccountFollows"."state" = \'accepted\' ' - if (start !== undefined) query += 'LIMIT ' + start - if (count !== undefined) query += ', ' + count + if (count !== undefined) query += 'LIMIT ' + count + if (start !== undefined) query += ' OFFSET ' + start const options = { bind: { accountIds }, diff --git a/server/models/video/video-interface.ts b/server/models/video/video-interface.ts index 9f29c842c..391ecff43 100644 --- a/server/models/video/video-interface.ts +++ b/server/models/video/video-interface.ts @@ -7,6 +7,7 @@ import { Video as FormattedVideo, VideoDetails as FormattedDetailsVideo } from ' 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' export namespace VideoMethods { export type GetThumbnailName = (this: VideoInstance) => string @@ -44,6 +45,11 @@ export namespace VideoMethods { export type ListOwnedAndPopulateAccountAndTags = () => Bluebird export type ListOwnedByAccount = (account: string) => Bluebird + export type ListAllAndSharedByAccountForOutbox = ( + accountId: number, + start: number, + count: number + ) => Bluebird< ResultList > export type ListForApi = (start: number, count: number, sort: string) => Bluebird< ResultList > export type ListUserVideosForApi = (userId: number, start: number, count: number, sort: string) => Bluebird< ResultList > export type SearchAndPopulateAccountAndServerAndTags = ( @@ -73,6 +79,7 @@ export namespace VideoMethods { export interface VideoClass { generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData list: VideoMethods.List + listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox listForApi: VideoMethods.ListForApi listUserVideosForApi: VideoMethods.ListUserVideosForApi listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags @@ -115,6 +122,7 @@ export interface VideoAttributes { VideoChannel?: VideoChannelInstance Tags?: TagInstance[] VideoFiles?: VideoFileInstance[] + VideoShare?: VideoShareInstance } export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.Instance { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index e2069eb0c..3b7e83779 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -78,6 +78,7 @@ let getLanguageLabel: VideoMethods.GetLanguageLabel let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData let list: VideoMethods.List let listForApi: VideoMethods.ListForApi +let listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox let listUserVideosForApi: VideoMethods.ListUserVideosForApi let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID let listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags @@ -266,6 +267,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da generateThumbnailFromData, list, + listAllAndSharedByAccountForOutbox, listForApi, listUserVideosForApi, listOwnedAndPopulateAccountAndTags, @@ -348,6 +350,14 @@ function associate (models) { }, onDelete: 'cascade' }) + + Video.hasMany(models.VideoShare, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'cascade' + }) } function afterDestroy (video: VideoInstance) { @@ -775,6 +785,54 @@ list = function () { return Video.findAll(query) } +listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, count: number) { + const queryVideo = 'SELECT "Video"."id" FROM "Videos" AS "Video" ' + + 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + + 'WHERE "VideoChannel"."accountId" = ' + accountId + const queryVideoShare = 'SELECT "Video"."id" FROM "VideoShares" AS "VideoShare" ' + + 'INNER JOIN "Videos" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + + 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + + 'WHERE "VideoShare"."accountId" = ' + accountId + const rawQuery = `(${queryVideo}) UNION (${queryVideoShare}) LIMIT ${count} OFFSET ${start}` + + const query = { + distinct: true, + offset: start, + limit: count, + order: [ getSort('createdAt'), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ], + where: { + id: { + [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')') + } + }, + include: [ + { + model: Video['sequelize'].models.VideoShare, + required: false + }, + { + model: Video['sequelize'].models.VideoChannel, + required: true, + include: [ + { + model: Video['sequelize'].models.Account, + required: true + } + ] + }, + Video['sequelize'].models.Tag, + Video['sequelize'].models.VideoFile + ] + } + + return Video.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) +} + listUserVideosForApi = function (userId: number, start: number, count: number, sort: string) { const query = { distinct: true, -- cgit v1.2.3