Skip to content

Commit

Permalink
[lib] Merge consecutive membership robotext messages
Browse files Browse the repository at this point in the history
Summary:
This reduces a bunch of noise in community roots when people get auto-added to the communities.

Depends on D13688

Test Plan: {F2931679}

Reviewers: tomek

Reviewed By: tomek

Differential Revision: https://phab.comm.dev/D13689
  • Loading branch information
Ashoat committed Oct 14, 2024
1 parent 36459c7 commit 0387025
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 32 deletions.
62 changes: 53 additions & 9 deletions lib/shared/messages/add-members-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import type {
CreateMessageInfoParams,
MessageSpec,
NotificationTextsParams,
MergeRobotextMessageItemResult,
} from './message-spec.js';
import { joinResult } from './utils.js';
import type { RobotextChatMessageInfoItem } from '../../selectors/chat-selectors.js';
import { messageTypes } from '../../types/message-types-enum.js';
import type {
ClientDBMessageInfo,
Expand All @@ -30,6 +32,18 @@ import {
import { values } from '../../utils/objects.js';
import { notifRobotextForMessageInfo } from '../notif-utils.js';

function getAddMembersRobotext(messageInfo: AddMembersMessageInfo): EntityText {
const users = messageInfo.addedMembers;
invariant(users.length !== 0, 'added who??');

const creator = ET.user({ userInfo: messageInfo.creator });
const addedUsers = pluralizeEntityText(
users.map(user => ET`${ET.user({ userInfo: user })}`),
);

return ET`${creator} added ${addedUsers}`;
}

type AddMembersMessageSpec = MessageSpec<
AddMembersMessageData,
RawAddMembersMessageInfo,
Expand Down Expand Up @@ -111,15 +125,7 @@ export const addMembersMessageSpec: AddMembersMessageSpec = Object.freeze({
},

robotext(messageInfo: AddMembersMessageInfo): EntityText {
const users = messageInfo.addedMembers;
invariant(users.length !== 0, 'added who??');

const creator = ET.user({ userInfo: messageInfo.creator });
const addedUsers = pluralizeEntityText(
users.map(user => ET`${ET.user({ userInfo: user })}`),
);

return ET`${creator} added ${addedUsers}`;
return getAddMembersRobotext(messageInfo);
},

async notificationTexts(
Expand Down Expand Up @@ -180,4 +186,42 @@ export const addMembersMessageSpec: AddMembersMessageSpec = Object.freeze({
canBePinned: false,

validator: rawAddMembersMessageInfoValidator,

mergeIntoPrecedingRobotextMessageItem(
messageInfo: AddMembersMessageInfo,
precedingMessageInfoItem: RobotextChatMessageInfoItem,
): MergeRobotextMessageItemResult {
if (precedingMessageInfoItem.messageInfos.length === 0) {
return { shouldMerge: false };
}

const addedMembers = [];
const creatorID = messageInfo.creator.id;
for (const precedingMessageInfo of precedingMessageInfoItem.messageInfos) {
if (
precedingMessageInfo.type !== messageTypes.ADD_MEMBERS ||
precedingMessageInfo.creator.id !== creatorID
) {
return { shouldMerge: false };
}
for (const addedMember of precedingMessageInfo.addedMembers) {
addedMembers.push(addedMember);
}
}

const messageInfos = [
messageInfo,
...precedingMessageInfoItem.messageInfos,
];
const newRobotext = getAddMembersRobotext({
...messageInfo,
addedMembers,
});
const mergedItem = {
...precedingMessageInfoItem,
messageInfos,
robotext: newRobotext,
};
return { shouldMerge: true, item: mergedItem };
},
});
60 changes: 53 additions & 7 deletions lib/shared/messages/join-thread-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import invariant from 'invariant';

import type { MessageSpec, RobotextParams } from './message-spec.js';
import type {
MessageSpec,
RobotextParams,
MergeRobotextMessageItemResult,
} from './message-spec.js';
import { joinResult } from './utils.js';
import type { RobotextChatMessageInfoItem } from '../../selectors/chat-selectors.js';
import { messageTypes } from '../../types/message-types-enum.js';
import type {
ClientDBMessageInfo,
Expand All @@ -25,6 +30,19 @@ import {
} from '../../utils/entity-text.js';
import { values } from '../../utils/objects.js';

function getJoinThreadRobotext(
joinerString: EntityText,
threadID: string,
params: RobotextParams,
): EntityText {
return ET`${joinerString} joined ${ET.thread({
display: 'alwaysDisplayShortName',
threadID,
threadType: params.threadInfo?.type,
parentThreadID: params.threadInfo?.parentThreadID,
})}`;
}

export const joinThreadMessageSpec: MessageSpec<
JoinThreadMessageData,
RawJoinThreadMessageInfo,
Expand Down Expand Up @@ -79,12 +97,7 @@ export const joinThreadMessageSpec: MessageSpec<
params: RobotextParams,
): EntityText {
const creator = ET.user({ userInfo: messageInfo.creator });
return ET`${creator} joined ${ET.thread({
display: 'alwaysDisplayShortName',
threadID: messageInfo.threadID,
threadType: params.threadInfo?.type,
parentThreadID: params.threadInfo?.parentThreadID,
})}`;
return getJoinThreadRobotext(ET`${creator}`, messageInfo.threadID, params);
},

async notificationTexts(
Expand Down Expand Up @@ -124,4 +137,37 @@ export const joinThreadMessageSpec: MessageSpec<
canBePinned: false,

validator: rawJoinThreadMessageInfoValidator,

mergeIntoPrecedingRobotextMessageItem(
messageInfo: JoinThreadMessageInfo,
precedingMessageInfoItem: RobotextChatMessageInfoItem,
params: RobotextParams,
): MergeRobotextMessageItemResult {
if (precedingMessageInfoItem.messageInfos.length === 0) {
return { shouldMerge: false };
}
for (const precedingMessageInfo of precedingMessageInfoItem.messageInfos) {
if (precedingMessageInfo.type !== messageTypes.JOIN_THREAD) {
return { shouldMerge: false };
}
}
const messageInfos = [
messageInfo,
...precedingMessageInfoItem.messageInfos,
];
const joiningUsers = pluralizeEntityText(
messageInfos.map(info => ET`${ET.user({ userInfo: info.creator })}`),
);
const newRobotext = getJoinThreadRobotext(
joiningUsers,
messageInfo.threadID,
params,
);
const mergedItem = {
...precedingMessageInfoItem,
messageInfos,
robotext: newRobotext,
};
return { shouldMerge: true, item: mergedItem };
},
});
60 changes: 53 additions & 7 deletions lib/shared/messages/leave-thread-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import invariant from 'invariant';

import type { MessageSpec, RobotextParams } from './message-spec.js';
import type {
MessageSpec,
RobotextParams,
MergeRobotextMessageItemResult,
} from './message-spec.js';
import { joinResult } from './utils.js';
import type { RobotextChatMessageInfoItem } from '../../selectors/chat-selectors.js';
import { messageTypes } from '../../types/message-types-enum.js';
import type {
ClientDBMessageInfo,
Expand All @@ -25,6 +30,19 @@ import {
} from '../../utils/entity-text.js';
import { values } from '../../utils/objects.js';

function getLeaveThreadRobotext(
leaverString: EntityText,
threadID: string,
params: RobotextParams,
): EntityText {
return ET`${leaverString} left ${ET.thread({
display: 'alwaysDisplayShortName',
threadID,
threadType: params.threadInfo?.type,
parentThreadID: params.threadInfo?.parentThreadID,
})}`;
}

export const leaveThreadMessageSpec: MessageSpec<
LeaveThreadMessageData,
RawLeaveThreadMessageInfo,
Expand Down Expand Up @@ -79,12 +97,7 @@ export const leaveThreadMessageSpec: MessageSpec<
params: RobotextParams,
): EntityText {
const creator = ET.user({ userInfo: messageInfo.creator });
return ET`${creator} left ${ET.thread({
display: 'alwaysDisplayShortName',
threadID: messageInfo.threadID,
threadType: params.threadInfo?.type,
parentThreadID: params.threadInfo?.parentThreadID,
})}`;
return getLeaveThreadRobotext(ET`${creator}`, messageInfo.threadID, params);
},

async notificationTexts(
Expand Down Expand Up @@ -124,4 +137,37 @@ export const leaveThreadMessageSpec: MessageSpec<
canBePinned: false,

validator: rawLeaveThreadMessageInfoValidator,

mergeIntoPrecedingRobotextMessageItem(
messageInfo: LeaveThreadMessageInfo,
precedingMessageInfoItem: RobotextChatMessageInfoItem,
params: RobotextParams,
): MergeRobotextMessageItemResult {
if (precedingMessageInfoItem.messageInfos.length === 0) {
return { shouldMerge: false };
}
for (const precedingMessageInfo of precedingMessageInfoItem.messageInfos) {
if (precedingMessageInfo.type !== messageTypes.LEAVE_THREAD) {
return { shouldMerge: false };
}
}
const messageInfos = [
messageInfo,
...precedingMessageInfoItem.messageInfos,
];
const leavingUsers = pluralizeEntityText(
messageInfos.map(info => ET`${ET.user({ userInfo: info.creator })}`),
);
const newRobotext = getLeaveThreadRobotext(
leavingUsers,
messageInfo.threadID,
params,
);
const mergedItem = {
...precedingMessageInfoItem,
messageInfos,
robotext: newRobotext,
};
return { shouldMerge: true, item: mergedItem };
},
});
63 changes: 54 additions & 9 deletions lib/shared/messages/remove-members-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import type {
CreateMessageInfoParams,
MessageSpec,
NotificationTextsParams,
MergeRobotextMessageItemResult,
} from './message-spec.js';
import { joinResult } from './utils.js';
import type { RobotextChatMessageInfoItem } from '../../selectors/chat-selectors.js';
import { messageTypes } from '../../types/message-types-enum.js';
import type {
ClientDBMessageInfo,
Expand All @@ -30,6 +32,20 @@ import {
import { values } from '../../utils/objects.js';
import { notifRobotextForMessageInfo } from '../notif-utils.js';

function getRemoveMembersRobotext(
messageInfo: RemoveMembersMessageInfo,
): EntityText {
const users = messageInfo.removedMembers;
invariant(users.length !== 0, 'removed who??');

const creator = ET.user({ userInfo: messageInfo.creator });
const removedUsers = pluralizeEntityText(
users.map(user => ET`${ET.user({ userInfo: user })}`),
);

return ET`${creator} removed ${removedUsers}`;
}

type RemoveMembersMessageSpec = MessageSpec<
RemoveMembersMessageData,
RawRemoveMembersMessageInfo,
Expand Down Expand Up @@ -112,15 +128,7 @@ export const removeMembersMessageSpec: RemoveMembersMessageSpec = Object.freeze(
},

robotext(messageInfo: RemoveMembersMessageInfo): EntityText {
const users = messageInfo.removedMembers;
invariant(users.length !== 0, 'added who??');

const creator = ET.user({ userInfo: messageInfo.creator });
const removedUsers = pluralizeEntityText(
users.map(user => ET`${ET.user({ userInfo: user })}`),
);

return ET`${creator} removed ${removedUsers}`;
return getRemoveMembersRobotext(messageInfo);
},

async notificationTexts(
Expand Down Expand Up @@ -185,5 +193,42 @@ export const removeMembersMessageSpec: RemoveMembersMessageSpec = Object.freeze(
canBePinned: false,

validator: rawRemoveMembersMessageInfoValidator,

mergeIntoPrecedingRobotextMessageItem(
messageInfo: RemoveMembersMessageInfo,
precedingMessageInfoItem: RobotextChatMessageInfoItem,
): MergeRobotextMessageItemResult {
if (precedingMessageInfoItem.messageInfos.length === 0) {
return { shouldMerge: false };
}
const removedMembers = [];
const creatorID = messageInfo.creator.id;
for (const precedingMessageInfo of precedingMessageInfoItem.messageInfos) {
if (
precedingMessageInfo.type !== messageTypes.REMOVE_MEMBERS ||
precedingMessageInfo.creator.id !== creatorID
) {
return { shouldMerge: false };
}
for (const removedMember of precedingMessageInfo.removedMembers) {
removedMembers.push(removedMember);
}
}

const messageInfos = [
messageInfo,
...precedingMessageInfoItem.messageInfos,
];
const newRobotext = getRemoveMembersRobotext({
...messageInfo,
removedMembers,
});
const mergedItem = {
...precedingMessageInfoItem,
messageInfos,
robotext: newRobotext,
};
return { shouldMerge: true, item: mergedItem };
},
},
);

0 comments on commit 0387025

Please sign in to comment.