X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Fuser-notification.ts;h=30985bb0f19cb318f485578353bc250c8eb15a8b;hb=26d6bf6533023326fa017812cf31bbe20c752d36;hp=9b13a83763eb572f688665429c639b1d2003abbb;hpb=5c5e587307a27e173333789b5b5167d35f468b01;p=oweals%2Fpeertube.git diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 9b13a8376..30985bb0f 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -16,7 +16,7 @@ import { ActorModel } from '../activitypub/actor' import { ActorFollowModel } from '../activitypub/actor-follow' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' -import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/typings/models/user' +import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/types/models/user' enum ScopeNames { WITH_ALL = 'WITH_ALL' @@ -135,13 +135,18 @@ function buildAccountInclude (required: boolean, withActor = false) { ] }, { - attributes: [ 'preferredUsername' ], + attributes: [ 'preferredUsername', 'type' ], model: ActorModel.unscoped(), required: true, as: 'ActorFollowing', include: [ buildChannelInclude(false), - buildAccountInclude(false) + buildAccountInclude(false), + { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: false + } ] } ] @@ -332,25 +337,25 @@ export class UserNotificationModel extends Model { ActorFollow: ActorFollowModel static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) { + const where = { userId } + const query: FindOptions = { offset: start, limit: count, order: getSort(sort), - where: { - userId - } + where } if (unread !== undefined) query.where['read'] = !unread - return UserNotificationModel.scope(ScopeNames.WITH_ALL) - .findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + UserNotificationModel.count({ where }) + .then(count => count || 0), + + count === 0 + ? [] + : UserNotificationModel.scope(ScopeNames.WITH_ALL).findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } static markAsRead (userId: number, notificationIds: number[]) { @@ -358,7 +363,7 @@ export class UserNotificationModel extends Model { where: { userId, id: { - [Op.in]: notificationIds // FIXME: sequelize ANY seems broken + [Op.in]: notificationIds } } } @@ -374,7 +379,7 @@ export class UserNotificationModel extends Model { toFormattedJSON (this: UserNotificationModelForApi): UserNotification { const video = this.Video - ? Object.assign(this.formatVideo(this.Video),{ channel: this.formatActor(this.Video.VideoChannel) }) + ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) : undefined const videoImport = this.VideoImport ? { @@ -404,6 +409,11 @@ export class UserNotificationModel extends Model { const account = this.Account ? this.formatActor(this.Account) : undefined + const actorFollowingType = { + Application: 'instance' as 'instance', + Group: 'channel' as 'channel', + Person: 'account' as 'account' + } const actorFollow = this.ActorFollow ? { id: this.ActorFollow.id, state: this.ActorFollow.state, @@ -415,9 +425,10 @@ export class UserNotificationModel extends Model { host: this.ActorFollow.ActorFollower.getHost() }, following: { - type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account', + type: actorFollowingType[this.ActorFollow.ActorFollowing.type], displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(), - name: this.ActorFollow.ActorFollowing.preferredUsername + name: this.ActorFollow.ActorFollowing.preferredUsername, + host: this.ActorFollow.ActorFollowing.getHost() } } : undefined