Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Optimize notification endpoint

Chocobozzz119b16e5acff
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts
index ccb81b891f..a05f30175c 100644
--- a/server/models/account/user-notification.ts
+++ b/server/models/account/user-notification.ts
@@ -337,25 +337,25 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
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[]) {