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'
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[]) {
where: {
userId,
id: {
- [Op.in]: notificationIds // FIXME: sequelize ANY seems broken
+ [Op.in]: notificationIds
}
}
}
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 ? {