Clean up "can reply" logic, add tests

This commit is contained in:
Evan Hahn 2021-07-01 18:57:10 -05:00 committed by GitHub
parent 7dac480df5
commit 56933192ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 8 deletions

View file

@ -1070,11 +1070,14 @@ function processQuoteAttachment(
} }
export function canReply( export function canReply(
message: MessageAttributesType, message: Pick<
MessageAttributesType,
'conversationId' | 'deletedForEveryone' | 'sent_to' | 'type'
>,
conversationSelector: GetConversationByIdType conversationSelector: GetConversationByIdType
): boolean { ): boolean {
const conversation = getConversation(message, conversationSelector); const conversation = getConversation(message, conversationSelector);
const { delivered, errors } = message; const { deletedForEveryone, sent_to: sentTo } = message;
if (!conversation) { if (!conversation) {
return false; return false;
@ -1097,17 +1100,17 @@ export function canReply(
} }
// We cannot reply if this message is deleted for everyone // We cannot reply if this message is deleted for everyone
if (message.deletedForEveryone) { if (deletedForEveryone) {
return false; return false;
} }
// We can reply if this is outgoing and delivered to at least one recipient // We can reply if this is outgoing and sent to at least one recipient
if (isOutgoing(message) && delivered && delivered > 0) { if (isOutgoing(message)) {
return true; return (sentTo || []).length > 0;
} }
// We can reply if there are no errors // We can reply to incoming messages
if (!errors || (errors && errors.length === 0)) { if (isIncoming(message)) {
return true; return true;
} }

View file

@ -2,8 +2,11 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai'; import { assert } from 'chai';
import { v4 as uuid } from 'uuid';
import { ConversationType } from '../../../state/ducks/conversations';
import { import {
canReply,
isEndSession, isEndSession,
isGroupUpdate, isGroupUpdate,
isIncoming, isIncoming,
@ -11,6 +14,80 @@ import {
} from '../../../state/selectors/message'; } from '../../../state/selectors/message';
describe('state/selectors/messages', () => { describe('state/selectors/messages', () => {
describe('canReply', () => {
const defaultConversation: ConversationType = {
id: uuid(),
type: 'direct',
title: 'Test conversation',
isMe: false,
sharedGroupNames: [],
acceptedMessageRequest: true,
};
it('returns false for disabled v1 groups', () => {
const message = {
conversationId: 'fake-conversation-id',
type: 'incoming' as const,
};
const getConversationById = () => ({
...defaultConversation,
type: 'group' as const,
isGroupV1AndDisabled: true,
});
assert.isFalse(canReply(message, getConversationById));
});
// NOTE: This is missing a test for mandatory profile sharing.
it('returns false if the message was deleted for everyone', () => {
const message = {
conversationId: 'fake-conversation-id',
type: 'incoming' as const,
deletedForEveryone: true,
};
const getConversationById = () => defaultConversation;
assert.isFalse(canReply(message, getConversationById));
});
it('returns false for outgoing messages that have not been sent', () => {
const message = {
conversationId: 'fake-conversation-id',
type: 'outgoing' as const,
sent_to: [],
};
const getConversationById = () => defaultConversation;
assert.isFalse(canReply(message, getConversationById));
});
it('returns true for outgoing messages that have been delivered to at least one person', () => {
const message = {
conversationId: 'fake-conversation-id',
type: 'outgoing' as const,
receipients: [uuid(), uuid()],
sent_to: [uuid()],
};
const getConversationById = () => ({
...defaultConversation,
type: 'group' as const,
});
assert.isTrue(canReply(message, getConversationById));
});
it('returns true for incoming messages', () => {
const message = {
conversationId: 'fake-conversation-id',
type: 'incoming' as const,
};
const getConversationById = () => defaultConversation;
assert.isTrue(canReply(message, getConversationById));
});
});
describe('isEndSession', () => { describe('isEndSession', () => {
it('checks if it is end of the session', () => { it('checks if it is end of the session', () => {
assert.isFalse(isEndSession({})); assert.isFalse(isEndSession({}));