diff --git a/lib/shared/updates/delete-account-spec.js b/lib/shared/updates/delete-account-spec.js index d6a9447d2..5f9898614 100644 --- a/lib/shared/updates/delete-account-spec.js +++ b/lib/shared/updates/delete-account-spec.js @@ -1,103 +1,123 @@ // @flow import t from 'tcomb'; import type { UpdateSpec } from './update-spec.js'; -import type { LegacyRawThreadInfos } from '../../types/thread-types.js'; +import type { RawThreadInfos } from '../../types/thread-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { AccountDeletionRawUpdateInfo, AccountDeletionUpdateData, AccountDeletionUpdateInfo, } from '../../types/update-types.js'; import type { UserInfos } from '../../types/user-types.js'; import { tNumber, tShape } from '../../utils/validation-utils.js'; export const deleteAccountSpec: UpdateSpec< AccountDeletionUpdateInfo, AccountDeletionRawUpdateInfo, AccountDeletionUpdateData, > = Object.freeze({ generateOpsForThreadUpdates( - storeThreadInfos: LegacyRawThreadInfos, + storeThreadInfos: RawThreadInfos, update: AccountDeletionUpdateInfo, ) { const operations = []; for (const threadID in storeThreadInfos) { const threadInfo = storeThreadInfos[threadID]; - const newMembers = threadInfo.members.filter( - member => member.id !== update.deletedUserID, - ); - if (newMembers.length < threadInfo.members.length) { - const updatedThread = { - ...threadInfo, - members: newMembers, - }; - operations.push({ - type: 'replace', - payload: { - id: threadID, - threadInfo: updatedThread, - }, - }); + // TODO (atul): Try to get rid of this ridiculous branching. + if (threadInfo.minimallyEncoded) { + const newMembers = threadInfo.members.filter( + member => member.id !== update.deletedUserID, + ); + if (newMembers.length < threadInfo.members.length) { + const updatedThread = { + ...threadInfo, + members: newMembers, + }; + operations.push({ + type: 'replace', + payload: { + id: threadID, + threadInfo: updatedThread, + }, + }); + } + } else { + const newMembers = threadInfo.members.filter( + member => member.id !== update.deletedUserID, + ); + if (newMembers.length < threadInfo.members.length) { + const updatedThread = { + ...threadInfo, + members: newMembers, + }; + operations.push({ + type: 'replace', + payload: { + id: threadID, + threadInfo: updatedThread, + }, + }); + } } } return operations; }, reduceUserInfos(state: UserInfos, update: AccountDeletionUpdateInfo) { const { deletedUserID } = update; if (!state[deletedUserID]) { return state; } const { [deletedUserID]: deleted, ...rest } = state; return rest; }, rawUpdateInfoFromRow(row: Object) { const content = JSON.parse(row.content); return { type: updateTypes.DELETE_ACCOUNT, id: row.id.toString(), time: row.time, deletedUserID: content.deletedUserID, }; }, updateContentForServerDB(data: AccountDeletionUpdateData) { return JSON.stringify({ deletedUserID: data.deletedUserID }); }, rawInfoFromData(data: AccountDeletionUpdateData, id: string) { return { type: updateTypes.DELETE_ACCOUNT, id, time: data.time, deletedUserID: data.deletedUserID, }; }, updateInfoFromRawInfo(info: AccountDeletionRawUpdateInfo) { return { type: updateTypes.DELETE_ACCOUNT, id: info.id, time: info.time, deletedUserID: info.deletedUserID, }; }, deleteCondition: new Set([ updateTypes.DELETE_ACCOUNT, updateTypes.UPDATE_USER, ]), keyForUpdateData(data: AccountDeletionUpdateData) { return data.deletedUserID; }, keyForUpdateInfo(info: AccountDeletionUpdateInfo) { return info.deletedUserID; }, typesOfReplacedUpdatesForMatchingKey: 'all_types', generateOpsForUserInfoUpdates(update: AccountDeletionUpdateInfo) { return [{ type: 'remove_users', payload: { ids: [update.deletedUserID] } }]; }, infoValidator: tShape({ type: tNumber(updateTypes.DELETE_ACCOUNT), id: t.String, time: t.Number, deletedUserID: t.String, }), });