From 87966634bae38d974aed655728d8f528f8a2bd92 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 7 Aug 2020 11:54:27 -0300 Subject: [PATCH] [FIX] Sending notifications from senders without a name (#18479) * Save error and skip from queue --- .github/workflows/codeql-analysis.yml | 2 +- app/lib/server/functions/notifications/mobile.js | 5 +---- app/models/server/models/NotificationQueue.js | 1 + app/models/server/raw/NotificationQueue.ts | 15 +++++++++++++++ .../server/NotificationQueue.ts | 2 +- definition/INotification.ts | 1 + 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a88b2a63f81..da9570060ed 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -24,7 +24,7 @@ jobs: # the head of the pull request instead of the merge commit. - run: git checkout HEAD^2 if: ${{ github.event_name == 'pull_request' }} - + # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v1 diff --git a/app/lib/server/functions/notifications/mobile.js b/app/lib/server/functions/notifications/mobile.js index 02e5cb4d4f8..4d1b7482b12 100644 --- a/app/lib/server/functions/notifications/mobile.js +++ b/app/lib/server/functions/notifications/mobile.js @@ -42,10 +42,7 @@ function enableNotificationReplyButton(room, username) { } export async function getPushData({ room, message, userId, senderUsername, senderName, notificationMessage, receiver, shouldOmitMessage = true }) { - let username = ''; - if (settings.get('Push_show_username_room')) { - username = settings.get('UI_Use_Real_Name') === true ? senderName : senderUsername; - } + const username = (settings.get('Push_show_username_room') && settings.get('UI_Use_Real_Name') && senderName) || senderUsername; const lng = receiver.language || settings.get('Language') || 'en'; diff --git a/app/models/server/models/NotificationQueue.js b/app/models/server/models/NotificationQueue.js index 1210f5e7312..32eb7524c2c 100644 --- a/app/models/server/models/NotificationQueue.js +++ b/app/models/server/models/NotificationQueue.js @@ -7,6 +7,7 @@ export class NotificationQueue extends Base { this.tryEnsureIndex({ ts: 1 }, { expireAfterSeconds: 2 * 60 * 60 }); this.tryEnsureIndex({ schedule: 1 }, { sparse: true }); this.tryEnsureIndex({ sending: 1 }, { sparse: true }); + this.tryEnsureIndex({ error: 1 }, { sparse: true }); } } diff --git a/app/models/server/raw/NotificationQueue.ts b/app/models/server/raw/NotificationQueue.ts index 136671fba37..44a34a21be7 100644 --- a/app/models/server/raw/NotificationQueue.ts +++ b/app/models/server/raw/NotificationQueue.ts @@ -18,6 +18,19 @@ export class NotificationQueueRaw extends BaseRaw { }); } + setErrorById(_id: string, error: any) { + return this.col.updateOne({ + _id, + }, { + $set: { + error, + }, + $unset: { + sending: 1, + }, + }); + } + removeById(_id: string) { return this.col.deleteOne({ _id }); } @@ -55,6 +68,8 @@ export class NotificationQueueRaw extends BaseRaw { { schedule: { $exists: false } }, { schedule: { $lte: now } }, ], + }, { + error: { $exists: false }, }], }, { $set: { diff --git a/app/notification-queue/server/NotificationQueue.ts b/app/notification-queue/server/NotificationQueue.ts index 1927d888c22..7665a588c8e 100644 --- a/app/notification-queue/server/NotificationQueue.ts +++ b/app/notification-queue/server/NotificationQueue.ts @@ -75,7 +75,7 @@ class NotificationClass { NotificationQueue.removeById(notification._id); } catch (e) { console.error(e); - await NotificationQueue.unsetSendingById(notification._id); + await NotificationQueue.setErrorById(notification._id, e.message); } if (counter >= this.maxBatchSize) { diff --git a/definition/INotification.ts b/definition/INotification.ts index 6359f33dd61..d8153de9427 100644 --- a/definition/INotification.ts +++ b/definition/INotification.ts @@ -40,5 +40,6 @@ export interface INotification { ts: Date; schedule?: Date; sending?: Date; + error?: string; items: NotificationItem[]; }