aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/actor/actor.ts61
-rw-r--r--server/models/video/video-share.ts25
2 files changed, 31 insertions, 55 deletions
diff --git a/server/models/actor/actor.ts b/server/models/actor/actor.ts
index 93145b8ae..943b7364f 100644
--- a/server/models/actor/actor.ts
+++ b/server/models/actor/actor.ts
@@ -1,5 +1,5 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import { literal, Op, Transaction } from 'sequelize' 2import { literal, Op, QueryTypes, Transaction } from 'sequelize'
3import { 3import {
4 AllowNull, 4 AllowNull,
5 BelongsTo, 5 BelongsTo,
@@ -43,15 +43,18 @@ import {
43 MActorAccountChannelId, 43 MActorAccountChannelId,
44 MActorAPAccount, 44 MActorAPAccount,
45 MActorAPChannel, 45 MActorAPChannel,
46 MActorFollowersUrl,
46 MActorFormattable, 47 MActorFormattable,
47 MActorFull, 48 MActorFull,
48 MActorHost, 49 MActorHost,
50 MActorId,
49 MActorServer, 51 MActorServer,
50 MActorSummaryFormattable, 52 MActorSummaryFormattable,
51 MActorUrl, 53 MActorUrl,
52 MActorWithInboxes 54 MActorWithInboxes
53} from '../../types/models' 55} from '../../types/models'
54import { AccountModel } from '../account/account' 56import { AccountModel } from '../account/account'
57import { getServerActor } from '../application/application'
55import { ServerModel } from '../server/server' 58import { ServerModel } from '../server/server'
56import { isOutdated, throwIfNotValid } from '../utils' 59import { isOutdated, throwIfNotValid } from '../utils'
57import { VideoModel } from '../video/video' 60import { VideoModel } from '../video/video'
@@ -304,7 +307,10 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> {
304 }) 307 })
305 VideoChannel: VideoChannelModel 308 VideoChannel: VideoChannelModel
306 309
307 static load (id: number): Promise<MActor> { 310 static async load (id: number): Promise<MActor> {
311 const actorServer = await getServerActor()
312 if (id === actorServer.id) return actorServer
313
308 return ActorModel.unscoped().findByPk(id) 314 return ActorModel.unscoped().findByPk(id)
309 } 315 }
310 316
@@ -312,48 +318,21 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> {
312 return ActorModel.scope(ScopeNames.FULL).findByPk(id) 318 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
313 } 319 }
314 320
315 static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise<MActor> { 321 static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) {
316 const query = { 322 const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
317 include: [ 323 `FROM "actor" ` +
318 { 324 `INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` +
319 attributes: [ 'id' ], 325 `INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` +
320 model: AccountModel.unscoped(), 326 `INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId`
321 required: true, 327
322 include: [ 328 const options = {
323 { 329 type: QueryTypes.SELECT as QueryTypes.SELECT,
324 attributes: [ 'id' ], 330 replacements: { videoId },
325 model: VideoChannelModel.unscoped(), 331 plain: true as true,
326 required: true,
327 include: [
328 {
329 attributes: [ 'id' ],
330 model: VideoModel.unscoped(),
331 required: true,
332 where: {
333 id: videoId
334 }
335 }
336 ]
337 }
338 ]
339 }
340 ],
341 transaction 332 transaction
342 } 333 }
343 334
344 return ActorModel.unscoped().findOne(query) 335 return ActorModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
345 }
346
347 static isActorUrlExist (url: string) {
348 const query = {
349 raw: true,
350 where: {
351 url
352 }
353 }
354
355 return ActorModel.unscoped().findOne(query)
356 .then(a => !!a)
357 } 336 }
358 337
359 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> { 338 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts
index ad95dec6e..ca63bb2d9 100644
--- a/server/models/video/video-share.ts
+++ b/server/models/video/video-share.ts
@@ -3,7 +3,7 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Mode
3import { AttributesOnly } from '@shared/typescript-utils' 3import { AttributesOnly } from '@shared/typescript-utils'
4import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 4import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
5import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 5import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
6import { MActorDefault } from '../../types/models' 6import { MActorDefault, MActorFollowersUrl, MActorId } from '../../types/models'
7import { MVideoShareActor, MVideoShareFull } from '../../types/models/video' 7import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
8import { ActorModel } from '../actor/actor' 8import { ActorModel } from '../actor/actor'
9import { buildLocalActorIdsIn, throwIfNotValid } from '../utils' 9import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
@@ -107,22 +107,19 @@ export class VideoShareModel extends Model<Partial<AttributesOnly<VideoShareMode
107 }) 107 })
108 } 108 }
109 109
110 static loadActorsByShare (videoId: number, t: Transaction): Promise<MActorDefault[]> { 110 static listActorIdsAndFollowerUrlsByShare (videoId: number, t: Transaction) {
111 const query = { 111 const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
112 where: { 112 `FROM "videoShare" ` +
113 videoId 113 `INNER JOIN "actor" ON "actor"."id" = "videoShare"."actorId" ` +
114 }, 114 `WHERE "videoShare"."videoId" = :videoId`
115 include: [ 115
116 { 116 const options = {
117 model: ActorModel, 117 type: QueryTypes.SELECT as QueryTypes.SELECT,
118 required: true 118 replacements: { videoId },
119 }
120 ],
121 transaction: t 119 transaction: t
122 } 120 }
123 121
124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query) 122 return VideoShareModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
125 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
126 } 123 }
127 124
128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> { 125 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {