Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/

import {CONVERSATION_TYPING} from '@wireapp/api-client/lib/conversation/data/';
import {ConversationTypingEvent, CONVERSATION_EVENT} from '@wireapp/api-client/lib/event/';
import {
ConversationMemberJoinEvent,
ConversationOtrMessageAddEvent,
ConversationTypingEvent,
CONVERSATION_EVENT,
} from '@wireapp/api-client/lib/event/';

import {EventSource} from './EventSource';
import {EventValidation} from './EventValidation';
Expand All @@ -40,5 +45,37 @@ describe('EventValidator', () => {

expect(result).toBe(EventValidation.VALID);
});

it('ignores duplicate-risk events replayed on the notification stream', () => {
const eventTime = '2026-05-28T06:37:31.673Z';

const event: ConversationMemberJoinEvent = {
conversation: '939d0410-a17e-499e-804b-e7a8503415ae',
data: {user_ids: ['30c6d863-6d9d-41ba-882d-fbcdc32b75a8']},
from: '30c6d863-6d9d-41ba-882d-fbcdc32b75a8',
time: eventTime,
type: CONVERSATION_EVENT.MEMBER_JOIN,
};

const result = validateEvent(event, EventSource.NOTIFICATION_STREAM, eventTime);

expect(result).toBe(EventValidation.OUTDATED_TIMESTAMP);
});

it('allows OTR messages with the same timestamp as lastEventDate on the notification stream', () => {
const eventTime = '2026-05-28T06:37:31.673Z';

const event: ConversationOtrMessageAddEvent = {
conversation: '939d0410-a17e-499e-804b-e7a8503415ae',
data: {recipient: 'a66c7dc1e8ffa326', sender: 'c9878bbba7a1f9ab', text: 'encrypted'},
from: '30c6d863-6d9d-41ba-882d-fbcdc32b75a8',
time: eventTime,
type: CONVERSATION_EVENT.OTR_MESSAGE_ADD,
};

const result = validateEvent(event, EventSource.NOTIFICATION_STREAM, eventTime);

expect(result).toBe(EventValidation.VALID);
});
});
});
15 changes: 5 additions & 10 deletions apps/webapp/src/script/repositories/event/EventValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
*/

import {CONVERSATION_EVENT, USER_EVENT} from '@wireapp/api-client/lib/event/';
import {isOutdatedNotificationStreamEvent} from '@wireapp/core/lib/notification';

import {EventSource} from './EventSource';
import {EventValidation} from './EventValidation';

export function validateEvent(
event: {time: string; type: CONVERSATION_EVENT | USER_EVENT},
source: EventSource,
lastEventDate?: string,
lastEventDateString?: string,
): EventValidation {
const eventTime = event.time;
const isFromNotificationStream = source === EventSource.NOTIFICATION_STREAM;
const shouldCheckEventDate = !!eventTime && isFromNotificationStream && lastEventDate;
const lastEventDate = lastEventDateString ? new Date(lastEventDateString) : undefined;

if (shouldCheckEventDate) {
/** This check prevents duplicated "You joined" system messages. */
const isOutdated = new Date(lastEventDate).getTime() >= new Date(eventTime).getTime();
if (isOutdated) {
return EventValidation.OUTDATED_TIMESTAMP;
}
if (isOutdatedNotificationStreamEvent(event, source, lastEventDate)) {
return EventValidation.OUTDATED_TIMESTAMP;
}

return EventValidation.VALID;
Expand Down
1 change: 1 addition & 0 deletions libraries/core/src/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export * from './notificationService';
export * from './notificationSource.types';
export * from './outdatedNotificationStreamEventTypes';
13 changes: 3 additions & 10 deletions libraries/core/src/notification/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {CRUDEngine, error as StoreEngineError} from '@wireapp/store-engine';
import {NotificationBackendRepository} from './notificationBackendRepository';
import {NotificationDatabaseRepository} from './notificationDatabaseRepository';
import {NotificationSource} from './notificationSource.types';
import {isOutdatedNotificationStreamEvent} from './outdatedNotificationStreamEventTypes';

import {ConversationService} from '../conversation';
import {CoreError, NotificationError} from '../coreError';
Expand Down Expand Up @@ -195,16 +196,8 @@ export class NotificationService extends TypedEventEmitter<Events> {
* @param source
* @param lastEventDate?
*/
private isOutdatedEvent(event: {time: string}, source: NotificationSource, lastEventDate?: Date) {
const isFromNotificationStream = source === NotificationSource.NOTIFICATION_STREAM;
const shouldCheckEventDate = event.time.length > 0 && isFromNotificationStream && lastEventDate !== undefined;

if (shouldCheckEventDate) {
/** This check prevents duplicated "You joined" system messages. */
const isOutdated = lastEventDate.getTime() >= new Date(event.time).getTime();
return isOutdated;
}
return false;
private isOutdatedEvent(event: {time: string; type: string}, source: NotificationSource, lastEventDate?: Date) {
return isOutdatedNotificationStreamEvent(event, source, lastEventDate);
}

public async *handleNotification(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

import {CONVERSATION_EVENT} from '@wireapp/api-client/lib/event';

import {NotificationSource} from './notificationSource.types';
import {isOutdatedNotificationStreamEvent} from './outdatedNotificationStreamEventTypes';

describe('isOutdatedNotificationStreamEvent', () => {
const eventTime = '2026-05-28T06:37:31.673Z';
const lastEventDate = new Date(eventTime);

it('marks duplicate-risk events with an equal timestamp as outdated on the notification stream', () => {
expect(
isOutdatedNotificationStreamEvent(
{time: eventTime, type: CONVERSATION_EVENT.MEMBER_JOIN},
NotificationSource.NOTIFICATION_STREAM,
lastEventDate,
),
).toBe(true);
});

it('does not mark OTR message events with an equal timestamp as outdated', () => {
expect(
isOutdatedNotificationStreamEvent(
{time: eventTime, type: CONVERSATION_EVENT.OTR_MESSAGE_ADD},
NotificationSource.NOTIFICATION_STREAM,
lastEventDate,
),
).toBe(false);
});

it('does not apply the outdated check outside the notification stream', () => {
expect(
isOutdatedNotificationStreamEvent(
{time: eventTime, type: CONVERSATION_EVENT.MEMBER_JOIN},
NotificationSource.WEBSOCKET,
lastEventDate,
),
).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

import is from '@sindresorhus/is';
import {CONVERSATION_EVENT} from '@wireapp/api-client/lib/event';

import {NotificationSource} from './notificationSource.types';

/**
* Event types that can be handled locally (API response or pre-app init) and then
* replayed on the notification stream with the same timestamp.
*
* @see ConversationJoin.tsx (member-join via guest link)
* @see ConversationRepository injectEvent(..., BACKEND_RESPONSE)
*/
export const NOTIFICATION_STREAM_DUPLICATE_RISK_EVENT_TYPES: ReadonlySet<string> = new Set([
CONVERSATION_EVENT.MEMBER_JOIN,
CONVERSATION_EVENT.MEMBER_LEAVE,
CONVERSATION_EVENT.CREATE,
CONVERSATION_EVENT.RENAME,
CONVERSATION_EVENT.PROTOCOL_UPDATE,
CONVERSATION_EVENT.MESSAGE_TIMER_UPDATE,
CONVERSATION_EVENT.RECEIPT_MODE_UPDATE,
CONVERSATION_EVENT.ADD_PERMISSION_UPDATE,
]);

export function isOutdatedNotificationStreamEvent(
event: {time?: string; type: string},
notificationSource: string,
lastEventDate?: Date,
): boolean {
if (notificationSource !== NotificationSource.NOTIFICATION_STREAM) {
return false;
}

if (!is.nonEmptyString(event.time) || is.nullOrUndefined(lastEventDate)) {
return false;
}

if (!NOTIFICATION_STREAM_DUPLICATE_RISK_EVENT_TYPES.has(event.type)) {
return false;
}

return lastEventDate.getTime() >= new Date(event.time).getTime();
}
Loading