Don't send messages to blocked group members

This commit is contained in:
Evan Hahn 2020-12-01 11:04:14 -06:00 committed by GitHub
parent c7290309d3
commit 688938b5a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 16 deletions

View file

@ -9,13 +9,29 @@
const BLOCKED_UUIDS_ID = 'blocked-uuids'; const BLOCKED_UUIDS_ID = 'blocked-uuids';
const BLOCKED_GROUPS_ID = 'blocked-groups'; const BLOCKED_GROUPS_ID = 'blocked-groups';
function getArray(key) {
const result = storage.get(key, []);
if (!Array.isArray(result)) {
window.log.error(
`Expected storage key ${JSON.stringify(
key
)} to contain an array or nothing`
);
return [];
}
return result;
}
storage.getBlockedNumbers = () => getArray(BLOCKED_NUMBERS_ID);
storage.isBlocked = number => { storage.isBlocked = number => {
const numbers = storage.get(BLOCKED_NUMBERS_ID, []); const numbers = storage.getBlockedNumbers();
return _.include(numbers, number); return _.include(numbers, number);
}; };
storage.addBlockedNumber = number => { storage.addBlockedNumber = number => {
const numbers = storage.get(BLOCKED_NUMBERS_ID, []); const numbers = storage.getBlockedNumbers();
if (_.include(numbers, number)) { if (_.include(numbers, number)) {
return; return;
} }
@ -24,7 +40,7 @@
storage.put(BLOCKED_NUMBERS_ID, numbers.concat(number)); storage.put(BLOCKED_NUMBERS_ID, numbers.concat(number));
}; };
storage.removeBlockedNumber = number => { storage.removeBlockedNumber = number => {
const numbers = storage.get(BLOCKED_NUMBERS_ID, []); const numbers = storage.getBlockedNumbers();
if (!_.include(numbers, number)) { if (!_.include(numbers, number)) {
return; return;
} }
@ -33,13 +49,14 @@
storage.put(BLOCKED_NUMBERS_ID, _.without(numbers, number)); storage.put(BLOCKED_NUMBERS_ID, _.without(numbers, number));
}; };
storage.getBlockedUuids = () => getArray(BLOCKED_UUIDS_ID);
storage.isUuidBlocked = uuid => { storage.isUuidBlocked = uuid => {
const uuids = storage.get(BLOCKED_UUIDS_ID, []); const uuids = storage.getBlockedUuids();
return _.include(uuids, uuid); return _.include(uuids, uuid);
}; };
storage.addBlockedUuid = uuid => { storage.addBlockedUuid = uuid => {
const uuids = storage.get(BLOCKED_UUIDS_ID, []); const uuids = storage.getBlockedUuids();
if (_.include(uuids, uuid)) { if (_.include(uuids, uuid)) {
return; return;
} }
@ -48,7 +65,7 @@
storage.put(BLOCKED_UUIDS_ID, uuids.concat(uuid)); storage.put(BLOCKED_UUIDS_ID, uuids.concat(uuid));
}; };
storage.removeBlockedUuid = uuid => { storage.removeBlockedUuid = uuid => {
const numbers = storage.get(BLOCKED_UUIDS_ID, []); const numbers = storage.getBlockedUuids();
if (!_.include(numbers, uuid)) { if (!_.include(numbers, uuid)) {
return; return;
} }
@ -57,13 +74,14 @@
storage.put(BLOCKED_UUIDS_ID, _.without(numbers, uuid)); storage.put(BLOCKED_UUIDS_ID, _.without(numbers, uuid));
}; };
storage.getBlockedGroups = () => getArray(BLOCKED_GROUPS_ID);
storage.isGroupBlocked = groupId => { storage.isGroupBlocked = groupId => {
const groupIds = storage.get(BLOCKED_GROUPS_ID, []); const groupIds = storage.getBlockedGroups();
return _.include(groupIds, groupId); return _.include(groupIds, groupId);
}; };
storage.addBlockedGroup = groupId => { storage.addBlockedGroup = groupId => {
const groupIds = storage.get(BLOCKED_GROUPS_ID, []); const groupIds = storage.getBlockedGroups();
if (_.include(groupIds, groupId)) { if (_.include(groupIds, groupId)) {
return; return;
} }
@ -72,7 +90,7 @@
storage.put(BLOCKED_GROUPS_ID, groupIds.concat(groupId)); storage.put(BLOCKED_GROUPS_ID, groupIds.concat(groupId));
}; };
storage.removeBlockedGroup = groupId => { storage.removeBlockedGroup = groupId => {
const groupIds = storage.get(BLOCKED_GROUPS_ID, []); const groupIds = storage.getBlockedGroups();
if (!_.include(groupIds, groupId)) { if (!_.include(groupIds, groupId)) {
return; return;
} }

View file

@ -1655,12 +1655,8 @@ export default class MessageSender {
const myE164 = window.textsecure.storage.user.getNumber(); const myE164 = window.textsecure.storage.user.getNumber();
const myUuid = window.textsecure.storage.user.getUuid(); const myUuid = window.textsecure.storage.user.getUuid();
// prettier-ignore
const recipients = groupV2 const groupMembers = groupV2?.members || groupV1?.members || [];
? groupV2.members
: groupV1
? groupV1.members
: [];
// We should always have a UUID but have this check just in case we don't. // We should always have a UUID but have this check just in case we don't.
let isNotMe: (recipient: string) => boolean; let isNotMe: (recipient: string) => boolean;
@ -1670,8 +1666,17 @@ export default class MessageSender {
isNotMe = r => r !== myE164; isNotMe = r => r !== myE164;
} }
const blockedIdentifiers = new Set([
...window.storage.getBlockedUuids(),
...window.storage.getBlockedNumbers(),
]);
const recipients = groupMembers.filter(
recipient => isNotMe(recipient) && !blockedIdentifiers.has(recipient)
);
const attrs = { const attrs = {
recipients: recipients.filter(isNotMe), recipients,
body: messageText, body: messageText,
timestamp, timestamp,
attachments, attachments,

3
ts/window.d.ts vendored
View file

@ -171,6 +171,9 @@ declare global {
<T = any>(key: string): T | undefined; <T = any>(key: string): T | undefined;
<T>(key: string, defaultValue: T): T; <T>(key: string, defaultValue: T): T;
}; };
getBlockedGroups: () => Array<string>;
getBlockedNumbers: () => Array<string>;
getBlockedUuids: () => Array<string>;
getItemsState: () => WhatIsThis; getItemsState: () => WhatIsThis;
isBlocked: (number: string) => boolean; isBlocked: (number: string) => boolean;
isGroupBlocked: (group: unknown) => boolean; isGroupBlocked: (group: unknown) => boolean;