Skip to content

Commit

Permalink
Merge pull request Expensify#37687 from Expensify/ionatan_removesuppo…
Browse files Browse the repository at this point in the history
…rtoldpusherformat

Remove old pusher update format
  • Loading branch information
danieldoglas authored Mar 7, 2024
2 parents ffa731a + f1a085a commit 79d6cda
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 75 deletions.
8 changes: 3 additions & 5 deletions src/libs/Pusher/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {LiteralUnion, ValueOf} from 'type-fest';
import Log from '@libs/Log';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxUpdateEvent, OnyxUpdatesFromServer, ReportUserIsTyping} from '@src/types/onyx';
import type {OnyxUpdatesFromServer, ReportUserIsTyping} from '@src/types/onyx';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import TYPE from './EventType';
import Pusher from './library';
Expand All @@ -22,8 +22,6 @@ type Args = {
authEndpoint: string;
};

type PushJSON = OnyxUpdateEvent[] | OnyxUpdatesFromServer;

type UserIsTypingEvent = ReportUserIsTyping & {
userLogin?: string;
};
Expand All @@ -37,7 +35,7 @@ type PusherEventMap = {
[TYPE.USER_IS_LEAVING_ROOM]: UserIsLeavingRoomEvent;
};

type EventData<EventName extends string> = EventName extends keyof PusherEventMap ? PusherEventMap[EventName] : PushJSON;
type EventData<EventName extends string> = EventName extends keyof PusherEventMap ? PusherEventMap[EventName] : OnyxUpdatesFromServer;

type EventCallbackError = {type: ValueOf<typeof CONST.ERROR>; data: {code: number}};

Expand Down Expand Up @@ -413,4 +411,4 @@ export {
getPusherSocketID,
};

export type {EventCallbackError, States, PushJSON, UserIsTypingEvent, UserIsLeavingRoomEvent};
export type {EventCallbackError, States, UserIsTypingEvent, UserIsLeavingRoomEvent};
8 changes: 4 additions & 4 deletions src/libs/PusherUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {OnyxUpdate} from 'react-native-onyx';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import type {OnyxUpdatesFromServer} from '@src/types/onyx';
import Log from './Log';
import NetworkConnection from './NetworkConnection';
import * as Pusher from './Pusher/pusher';
import type {PushJSON} from './Pusher/pusher';

type Callback = (data: OnyxUpdate[]) => Promise<void>;

Expand All @@ -25,18 +25,18 @@ function triggerMultiEventHandler(eventType: string, data: OnyxUpdate[]): Promis
/**
* Abstraction around subscribing to private user channel events. Handles all logs and errors automatically.
*/
function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string, onEvent: (pushJSON: PushJSON) => void) {
function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string, onEvent: (pushJSON: OnyxUpdatesFromServer) => void) {
const pusherChannelName = `${CONST.PUSHER.PRIVATE_USER_CHANNEL_PREFIX}${accountID}${CONFIG.PUSHER.SUFFIX}` as const;

function logPusherEvent(pushJSON: PushJSON) {
function logPusherEvent(pushJSON: OnyxUpdatesFromServer) {
Log.info(`[Report] Handled ${eventName} event sent by Pusher`, false, pushJSON);
}

function onPusherResubscribeToPrivateUserChannel() {
NetworkConnection.triggerReconnectionCallbacks('Pusher re-subscribed to private user channel');
}

function onEventPush(pushJSON: PushJSON) {
function onEventPush(pushJSON: OnyxUpdatesFromServer) {
logPusherEvent(pushJSON);
onEvent(pushJSON);
}
Expand Down
17 changes: 2 additions & 15 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,21 +578,8 @@ function subscribeToUserEvents() {
// Handles the mega multipleEvents from Pusher which contains an array of single events.
// Each single event is passed to PusherUtils in order to trigger the callbacks for that event
PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.MULTIPLE_EVENTS, currentUserAccountID.toString(), (pushJSON) => {
// The data for this push event comes in two different formats:
// 1. Original format - this is what was sent before the RELIABLE_UPDATES project and will go away once RELIABLE_UPDATES is fully complete
// - The data is an array of objects, where each object is an onyx update
// Example: [{onyxMethod: 'whatever', key: 'foo', value: 'bar'}]
// 1. Reliable updates format - this is what was sent with the RELIABLE_UPDATES project and will be the format from now on
// - The data is an object, containing updateIDs from the server and an array of onyx updates (this array is the same format as the original format above)
// Example: {lastUpdateID: 1, previousUpdateID: 0, updates: [{onyxMethod: 'whatever', key: 'foo', value: 'bar'}]}
if (Array.isArray(pushJSON)) {
Log.warn('Received pusher event with array format');
pushJSON.forEach((multipleEvent) => {
PusherUtils.triggerMultiEventHandler(multipleEvent.eventType, multipleEvent.data);
});
return;
}

// The data for the update is an object, containing updateIDs from the server and an array of onyx updates (this array is the same format as the original format above)
// Example: {lastUpdateID: 1, previousUpdateID: 0, updates: [{onyxMethod: 'whatever', key: 'foo', value: 'bar'}]}
const updates = {
type: CONST.ONYX_UPDATE_TYPES.PUSHER,
lastUpdateID: Number(pushJSON.lastUpdateID || 0),
Expand Down
88 changes: 43 additions & 45 deletions tests/ui/UnreadIndicatorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as Pusher from '../../src/libs/Pusher/pusher';
import PusherConnectionManager from '../../src/libs/PusherConnectionManager';
import ONYXKEYS from '../../src/ONYXKEYS';
import appSetup from '../../src/setup';
import PusherHelper from '../utils/PusherHelper';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';
Expand Down Expand Up @@ -276,6 +277,9 @@ describe('Unread Indicators', () => {
afterEach(() => {
jest.clearAllMocks();
Onyx.clear();

// Unsubscribe to pusher channels
PusherHelper.teardown();
});

it('Display bold in the LHN for unread chat and new line indicator above the chat message when we navigate to it', () =>
Expand Down Expand Up @@ -375,54 +379,48 @@ describe('Unread Indicators', () => {
const NEW_REPORT_FIST_MESSAGE_CREATED_DATE = addSeconds(NEW_REPORT_CREATED_DATE, 1);
const createdReportActionID = NumberUtils.rand64();
const commentReportActionID = NumberUtils.rand64();
const channel = Pusher.getChannel(`${CONST.PUSHER.PRIVATE_USER_CHANNEL_PREFIX}${USER_A_ACCOUNT_ID}${CONFIG.PUSHER.SUFFIX}`);
channel.emit(Pusher.TYPE.MULTIPLE_EVENTS, [
PusherHelper.emitOnyxUpdate([
{
eventType: Pusher.TYPE.MULTIPLE_EVENT_TYPE.ONYX_API_UPDATE,
data: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${NEW_REPORT_ID}`,
value: {
reportID: NEW_REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastReadTime: '',
lastVisibleActionCreated: DateUtils.getDBTime(utcToZonedTime(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, 'UTC').valueOf()),
lastMessageText: 'Comment 1',
lastActorAccountID: USER_C_ACCOUNT_ID,
participantAccountIDs: [USER_C_ACCOUNT_ID],
type: CONST.REPORT.TYPE.CHAT,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${NEW_REPORT_ID}`,
value: {
[createdReportActionID]: {
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
automatic: false,
created: format(NEW_REPORT_CREATED_DATE, CONST.DATE.FNS_DB_FORMAT_STRING),
reportActionID: createdReportActionID,
},
[commentReportActionID]: {
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
actorAccountID: USER_C_ACCOUNT_ID,
person: [{type: 'TEXT', style: 'strong', text: 'User C'}],
created: format(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, CONST.DATE.FNS_DB_FORMAT_STRING),
message: [{type: 'COMMENT', html: 'Comment 1', text: 'Comment 1'}],
reportActionID: commentReportActionID,
},
},
shouldNotify: true,
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${NEW_REPORT_ID}`,
value: {
reportID: NEW_REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastReadTime: '',
lastVisibleActionCreated: DateUtils.getDBTime(utcToZonedTime(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, 'UTC').valueOf()),
lastMessageText: 'Comment 1',
lastActorAccountID: USER_C_ACCOUNT_ID,
participantAccountIDs: [USER_C_ACCOUNT_ID],
type: CONST.REPORT.TYPE.CHAT,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${NEW_REPORT_ID}`,
value: {
[createdReportActionID]: {
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
automatic: false,
created: format(NEW_REPORT_CREATED_DATE, CONST.DATE.FNS_DB_FORMAT_STRING),
reportActionID: createdReportActionID,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[USER_C_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_C_EMAIL, USER_C_ACCOUNT_ID, 'C'),
},
[commentReportActionID]: {
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
actorAccountID: USER_C_ACCOUNT_ID,
person: [{type: 'TEXT', style: 'strong', text: 'User C'}],
created: format(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, CONST.DATE.FNS_DB_FORMAT_STRING),
message: [{type: 'COMMENT', html: 'Comment 1', text: 'Comment 1'}],
reportActionID: commentReportActionID,
},
],
},
shouldNotify: true,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[USER_C_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_C_EMAIL, USER_C_ACCOUNT_ID, 'C'),
},
},
]);
return waitForBatchedUpdates();
Expand Down
17 changes: 11 additions & 6 deletions tests/utils/PusherHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ function setup() {

function emitOnyxUpdate(args: OnyxUpdate[]) {
const channel = Pusher.getChannel(CHANNEL_NAME);
channel?.emit(Pusher.TYPE.MULTIPLE_EVENTS, [
{
eventType: Pusher.TYPE.MULTIPLE_EVENT_TYPE.ONYX_API_UPDATE,
data: args,
},
]);
channel?.emit(Pusher.TYPE.MULTIPLE_EVENTS, {
type: 'pusher',
lastUpdateID: null,
previousUpdateID: null,
updates: [
{
eventType: Pusher.TYPE.MULTIPLE_EVENT_TYPE.ONYX_API_UPDATE,
data: args,
},
],
});
}

function teardown() {
Expand Down

0 comments on commit 79d6cda

Please sign in to comment.