Clean up MessageModel group_update

This commit is contained in:
Evan Hahn 2021-06-23 11:14:11 -05:00 committed by GitHub
parent 0a4eef29db
commit b990204bce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 28 deletions

14
ts/model-types.d.ts vendored
View file

@ -71,6 +71,13 @@ export type RetryOptions = Readonly<{
now: number; now: number;
}>; }>;
export type GroupV1Update = {
avatarUpdated?: boolean;
joined?: Array<string>;
left?: string | 'You';
name?: string;
};
export type MessageAttributesType = { export type MessageAttributesType = {
bodyPending?: boolean; bodyPending?: boolean;
bodyRanges?: BodyRangesType; bodyRanges?: BodyRangesType;
@ -86,12 +93,7 @@ export type MessageAttributesType = {
expirationStartTimestamp?: number | null; expirationStartTimestamp?: number | null;
expireTimer?: number; expireTimer?: number;
groupMigration?: GroupMigrationType; groupMigration?: GroupMigrationType;
group_update?: { group_update?: GroupV1Update;
avatarUpdated: boolean;
joined: Array<string>;
left: string | 'You';
name: string;
};
hasAttachments?: boolean; hasAttachments?: boolean;
hasFileAttachments?: boolean; hasFileAttachments?: boolean;
hasVisualMediaAttachments?: boolean; hasVisualMediaAttachments?: boolean;

View file

@ -1,8 +1,10 @@
// Copyright 2020-2021 Signal Messenger, LLC // Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { isEmpty } from 'lodash';
import { import {
CustomError, CustomError,
GroupV1Update,
MessageAttributesType, MessageAttributesType,
RetryOptions, RetryOptions,
ReactionAttributesType, ReactionAttributesType,
@ -10,7 +12,8 @@ import {
QuotedMessageType, QuotedMessageType,
WhatIsThis, WhatIsThis,
} from '../model-types.d'; } from '../model-types.d';
import { find } from '../util/iterables'; import { map, filter, find } from '../util/iterables';
import { isNotNil } from '../util/isNotNil';
import { DataMessageClass } from '../textsecure.d'; import { DataMessageClass } from '../textsecure.d';
import { ConversationModel } from './conversations'; import { ConversationModel } from './conversations';
import { MessageStatusType } from '../components/conversation/Message'; import { MessageStatusType } from '../components/conversation/Message';
@ -2687,7 +2690,8 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
// GroupV1 // GroupV1
if (!hasGroupV2Prop && dataMessage.group) { if (!hasGroupV2Prop && dataMessage.group) {
const pendingGroupUpdate = []; const pendingGroupUpdate: GroupV1Update = {};
const memberConversations: Array<ConversationModel> = await Promise.all( const memberConversations: Array<ConversationModel> = await Promise.all(
dataMessage.group.membersE164.map((e164: string) => dataMessage.group.membersE164.map((e164: string) =>
window.ConversationController.getOrCreateAndWait( window.ConversationController.getOrCreateAndWait(
@ -2710,7 +2714,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
}; };
if (dataMessage.group.name !== conversation.get('name')) { if (dataMessage.group.name !== conversation.get('name')) {
pendingGroupUpdate.push(['name', dataMessage.group.name]); pendingGroupUpdate.name = dataMessage.group.name;
} }
const avatarAttachment = dataMessage.group.avatar; const avatarAttachment = dataMessage.group.avatar;
@ -2773,7 +2777,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
attributes.avatar = avatar; attributes.avatar = avatar;
pendingGroupUpdate.push(['avatarUpdated', true]); pendingGroupUpdate.avatarUpdated = true;
} else { } else {
window.log.info( window.log.info(
'handleDataMessage: Group avatar hash matched; not replacing group avatar' 'handleDataMessage: Group avatar hash matched; not replacing group avatar'
@ -2787,11 +2791,11 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
); );
if (difference.length > 0) { if (difference.length > 0) {
// Because GroupV1 groups are based on e164 only // Because GroupV1 groups are based on e164 only
const e164s = difference.map(id => { const maybeE164s = map(difference, id =>
const c = window.ConversationController.get(id); window.ConversationController.get(id)?.get('e164')
return c ? c.get('e164') : null; );
}); const e164s = filter(maybeE164s, isNotNil);
pendingGroupUpdate.push(['joined', e164s]); pendingGroupUpdate.joined = [...e164s];
} }
if (conversation.get('left')) { if (conversation.get('left')) {
window.log.warn('re-added to a left group'); window.log.warn('re-added to a left group');
@ -2815,9 +2819,9 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
if (isMe(sender.attributes)) { if (isMe(sender.attributes)) {
attributes.left = true; attributes.left = true;
pendingGroupUpdate.push(['left', 'You']); pendingGroupUpdate.left = 'You';
} else { } else {
pendingGroupUpdate.push(['left', sender.get('id')]); pendingGroupUpdate.left = sender.get('id');
} }
attributes.members = _.without( attributes.members = _.without(
conversation.get('members'), conversation.get('members'),
@ -2825,15 +2829,8 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
); );
} }
if (pendingGroupUpdate.length) { if (!isEmpty(pendingGroupUpdate)) {
const groupUpdate = pendingGroupUpdate.reduce( message.set('group_update', pendingGroupUpdate);
(acc, [key, value]) => {
acc[key] = value;
return acc;
},
{} as typeof window.WhatIsThis
);
message.set({ group_update: groupUpdate });
} }
} }

View file

@ -671,7 +671,7 @@ function getPropsForGroupNotification(
}); });
} }
if (groupUpdate.joined) { if (groupUpdate.joined?.length) {
changes.push({ changes.push({
type: 'add' as ChangeType, type: 'add' as ChangeType,
contacts: map( contacts: map(

View file

@ -150,7 +150,7 @@ describe('Message', () => {
let message = messages.add(attributes); let message = messages.add(attributes);
assert.notOk(isGroupUpdate(message.attributes)); assert.notOk(isGroupUpdate(message.attributes));
message = messages.add({ group_update: true }); message = messages.add({ group_update: { left: 'You' } });
assert.ok(isGroupUpdate(message.attributes)); assert.ok(isGroupUpdate(message.attributes));
}); });
}); });