aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/account.ts6
-rw-r--r--server/models/account/user.ts8
-rw-r--r--server/models/activitypub/actor.ts37
-rw-r--r--server/models/avatar/avatar.ts30
-rw-r--r--server/models/video/video-comment.ts2
5 files changed, 68 insertions, 15 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index 1ee232537..d3503aaa3 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -13,6 +13,7 @@ import {
13 Table, 13 Table,
14 UpdatedAt 14 UpdatedAt
15} from 'sequelize-typescript' 15} from 'sequelize-typescript'
16import { Account } from '../../../shared/models/actors'
16import { isUserUsernameValid } from '../../helpers/custom-validators/users' 17import { isUserUsernameValid } from '../../helpers/custom-validators/users'
17import { sendDeleteActor } from '../../lib/activitypub/send' 18import { sendDeleteActor } from '../../lib/activitypub/send'
18import { ActorModel } from '../activitypub/actor' 19import { ActorModel } from '../activitypub/actor'
@@ -165,11 +166,12 @@ export class AccountModel extends Model<AccountModel> {
165 return AccountModel.findOne(query) 166 return AccountModel.findOne(query)
166 } 167 }
167 168
168 toFormattedJSON () { 169 toFormattedJSON (): Account {
169 const actor = this.Actor.toFormattedJSON() 170 const actor = this.Actor.toFormattedJSON()
170 const account = { 171 const account = {
171 id: this.id, 172 id: this.id,
172 name: this.name, 173 name: this.Actor.preferredUsername,
174 displayName: this.name,
173 createdAt: this.createdAt, 175 createdAt: this.createdAt,
174 updatedAt: this.updatedAt 176 updatedAt: this.updatedAt
175 } 177 }
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index d7e09e328..4226bcb35 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -4,6 +4,7 @@ import {
4 Scopes, Table, UpdatedAt 4 Scopes, Table, UpdatedAt
5} from 'sequelize-typescript' 5} from 'sequelize-typescript'
6import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' 6import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared'
7import { User } from '../../../shared/models/users'
7import { 8import {
8 isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, 9 isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
9 isUserVideoQuotaValid 10 isUserVideoQuotaValid
@@ -210,7 +211,7 @@ export class UserModel extends Model<UserModel> {
210 return comparePassword(password, this.password) 211 return comparePassword(password, this.password)
211 } 212 }
212 213
213 toFormattedJSON () { 214 toFormattedJSON (): User {
214 const json = { 215 const json = {
215 id: this.id, 216 id: this.id,
216 username: this.username, 217 username: this.username,
@@ -221,11 +222,12 @@ export class UserModel extends Model<UserModel> {
221 roleLabel: USER_ROLE_LABELS[ this.role ], 222 roleLabel: USER_ROLE_LABELS[ this.role ],
222 videoQuota: this.videoQuota, 223 videoQuota: this.videoQuota,
223 createdAt: this.createdAt, 224 createdAt: this.createdAt,
224 account: this.Account.toFormattedJSON() 225 account: this.Account.toFormattedJSON(),
226 videoChannels: []
225 } 227 }
226 228
227 if (Array.isArray(this.Account.VideoChannels) === true) { 229 if (Array.isArray(this.Account.VideoChannels) === true) {
228 json['videoChannels'] = this.Account.VideoChannels 230 json.videoChannels = this.Account.VideoChannels
229 .map(c => c.toFormattedJSON()) 231 .map(c => c.toFormattedJSON())
230 .sort((v1, v2) => { 232 .sort((v1, v2) => {
231 if (v1.createdAt < v2.createdAt) return -1 233 if (v1.createdAt < v2.createdAt) return -1
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts
index 3d96b3706..8422653df 100644
--- a/server/models/activitypub/actor.ts
+++ b/server/models/activitypub/actor.ts
@@ -1,5 +1,5 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import { join } from 'path' 2import { extname, join } from 'path'
3import * as Sequelize from 'sequelize' 3import * as Sequelize from 'sequelize'
4import { 4import {
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes, 5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
@@ -30,6 +30,10 @@ enum ScopeNames {
30 { 30 {
31 model: () => ServerModel, 31 model: () => ServerModel,
32 required: false 32 required: false
33 },
34 {
35 model: () => AvatarModel,
36 required: false
33 } 37 }
34 ] 38 ]
35}) 39})
@@ -47,6 +51,10 @@ enum ScopeNames {
47 { 51 {
48 model: () => ServerModel, 52 model: () => ServerModel,
49 required: false 53 required: false
54 },
55 {
56 model: () => AvatarModel,
57 required: false
50 } 58 }
51 ] 59 ]
52 } 60 }
@@ -141,7 +149,7 @@ export class ActorModel extends Model<ActorModel> {
141 foreignKey: { 149 foreignKey: {
142 allowNull: true 150 allowNull: true
143 }, 151 },
144 onDelete: 'cascade' 152 onDelete: 'set null'
145 }) 153 })
146 Avatar: AvatarModel 154 Avatar: AvatarModel
147 155
@@ -253,11 +261,7 @@ export class ActorModel extends Model<ActorModel> {
253 toFormattedJSON () { 261 toFormattedJSON () {
254 let avatar: Avatar = null 262 let avatar: Avatar = null
255 if (this.Avatar) { 263 if (this.Avatar) {
256 avatar = { 264 avatar = this.Avatar.toFormattedJSON()
257 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
258 createdAt: this.Avatar.createdAt,
259 updatedAt: this.Avatar.updatedAt
260 }
261 } 265 }
262 266
263 let score: number 267 let score: number
@@ -286,6 +290,16 @@ export class ActorModel extends Model<ActorModel> {
286 activityPubType = 'Group' as 'Group' 290 activityPubType = 'Group' as 'Group'
287 } 291 }
288 292
293 let icon = undefined
294 if (this.avatarId) {
295 const extension = extname(this.Avatar.filename)
296 icon = {
297 type: 'Image',
298 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
299 url: this.getAvatarUrl()
300 }
301 }
302
289 const json = { 303 const json = {
290 type: activityPubType, 304 type: activityPubType,
291 id: this.url, 305 id: this.url,
@@ -304,7 +318,8 @@ export class ActorModel extends Model<ActorModel> {
304 id: this.getPublicKeyUrl(), 318 id: this.getPublicKeyUrl(),
305 owner: this.url, 319 owner: this.url,
306 publicKeyPem: this.publicKey 320 publicKeyPem: this.publicKey
307 } 321 },
322 icon
308 } 323 }
309 324
310 return activityPubContextify(json) 325 return activityPubContextify(json)
@@ -353,4 +368,10 @@ export class ActorModel extends Model<ActorModel> {
353 getHost () { 368 getHost () {
354 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST 369 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
355 } 370 }
371
372 getAvatarUrl () {
373 if (!this.avatarId) return undefined
374
375 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath
376 }
356} 377}
diff --git a/server/models/avatar/avatar.ts b/server/models/avatar/avatar.ts
index 2e7a8ae2c..7493c3d75 100644
--- a/server/models/avatar/avatar.ts
+++ b/server/models/avatar/avatar.ts
@@ -1,4 +1,10 @@
1import { AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' 1import { join } from 'path'
2import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { Avatar } from '../../../shared/models/avatars/avatar.model'
4import { unlinkPromise } from '../../helpers/core-utils'
5import { logger } from '../../helpers/logger'
6import { CONFIG, STATIC_PATHS } from '../../initializers'
7import { sendDeleteVideo } from '../../lib/activitypub/send'
2 8
3@Table({ 9@Table({
4 tableName: 'avatar' 10 tableName: 'avatar'
@@ -14,4 +20,26 @@ export class AvatarModel extends Model<AvatarModel> {
14 20
15 @UpdatedAt 21 @UpdatedAt
16 updatedAt: Date 22 updatedAt: Date
23
24 @AfterDestroy
25 static removeFilesAndSendDelete (instance: AvatarModel) {
26 return instance.removeAvatar()
27 }
28
29 toFormattedJSON (): Avatar {
30 return {
31 path: this.getWebserverPath(),
32 createdAt: this.createdAt,
33 updatedAt: this.updatedAt
34 }
35 }
36
37 getWebserverPath () {
38 return join(STATIC_PATHS.AVATARS, this.filename)
39 }
40
41 removeAvatar () {
42 const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
43 return unlinkPromise(avatarPath)
44 }
17} 45}
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts
index d381ccafa..829022a51 100644
--- a/server/models/video/video-comment.ts
+++ b/server/models/video/video-comment.ts
@@ -214,7 +214,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
214 214
215 static listThreadCommentsForApi (videoId: number, threadId: number) { 215 static listThreadCommentsForApi (videoId: number, threadId: number) {
216 const query = { 216 const query = {
217 order: [ [ 'id', 'ASC' ] ], 217 order: [ [ 'createdAt', 'DESC' ] ],
218 where: { 218 where: {
219 videoId, 219 videoId,
220 [ Sequelize.Op.or ]: [ 220 [ Sequelize.Op.or ]: [