session-desktop/ts/state/selectors/conversations.ts

319 lines
9.4 KiB
TypeScript
Raw Normal View History

2019-01-14 22:49:58 +01:00
import { createSelector } from 'reselect';
import { StateType } from '../reducer';
import {
ConversationLookupType,
ConversationsStateType,
MessagePropsDetails,
ReduxConversationType,
SortedMessageModelProps,
2019-01-14 22:49:58 +01:00
} from '../ducks/conversations';
2021-01-27 23:46:15 +01:00
import { getIntl, getOurNumber } from './user';
import { BlockedNumberController } from '../../util';
import { ConversationTypeEnum } from '../../models/conversation';
import { LocalizerType } from '../../types/Util';
import {
ConversationHeaderProps,
ConversationHeaderTitleProps,
} from '../../components/conversation/ConversationHeader';
2021-07-14 05:48:59 +02:00
import { LightBoxOptions } from '../../components/session/conversation/SessionConversation';
import { ReplyingToMessageProps } from '../../components/session/conversation/SessionCompositionBox';
2021-07-16 01:34:32 +02:00
import { createSlice } from '@reduxjs/toolkit';
2019-01-14 22:49:58 +01:00
2021-04-22 10:03:58 +02:00
export const getConversations = (state: StateType): ConversationsStateType => state.conversations;
2019-01-14 22:49:58 +01:00
export const getConversationLookup = createSelector(
getConversations,
(state: ConversationsStateType): ConversationLookupType => {
return state.conversationLookup;
}
);
export const getSelectedConversationKey = createSelector(
2020-02-27 00:01:18 +01:00
getConversations,
2019-01-14 22:49:58 +01:00
(state: ConversationsStateType): string | undefined => {
return state.selectedConversation;
}
);
export const getSelectedConversation = createSelector(
getConversations,
(state: ConversationsStateType): ReduxConversationType | undefined => {
return state.selectedConversation
? state.conversationLookup[state.selectedConversation]
: undefined;
}
);
export const getOurPrimaryConversation = createSelector(
getConversations,
(state: ConversationsStateType): ReduxConversationType =>
state.conversationLookup[window.storage.get('primaryDevicePubKey')]
);
export const getMessagesOfSelectedConversation = createSelector(
getConversations,
(state: ConversationsStateType): Array<SortedMessageModelProps> => state.messages
);
function getConversationTitle(
conversation: ReduxConversationType,
testingi18n?: LocalizerType
): string {
2019-01-14 22:49:58 +01:00
if (conversation.name) {
return conversation.name;
}
if (conversation.type === 'group') {
return (testingi18n || window.i18n)('unknown');
2019-01-14 22:49:58 +01:00
}
2021-01-27 23:46:15 +01:00
return conversation.id;
2019-01-14 22:49:58 +01:00
}
const collator = new Intl.Collator();
export const _getConversationComparator = (testingi18n?: LocalizerType) => {
return (left: ReduxConversationType, right: ReduxConversationType): number => {
2021-07-06 09:16:05 +02:00
// Pin is the first criteria to check
if (left.isPinned && !right.isPinned) {
return -1;
}
if (!left.isPinned && right.isPinned) {
return 1;
}
// Then if none is pinned, check other criteria
const leftActiveAt = left.activeAt;
const rightActiveAt = right.activeAt;
if (leftActiveAt && !rightActiveAt) {
2019-01-14 22:49:58 +01:00
return -1;
}
if (rightActiveAt && !leftActiveAt) {
2019-01-14 22:49:58 +01:00
return 1;
}
if (leftActiveAt && rightActiveAt && leftActiveAt !== rightActiveAt) {
return rightActiveAt - leftActiveAt;
2019-01-14 22:49:58 +01:00
}
const leftTitle = getConversationTitle(left, testingi18n).toLowerCase();
const rightTitle = getConversationTitle(right, testingi18n).toLowerCase();
2019-01-14 22:49:58 +01:00
return collator.compare(leftTitle, rightTitle);
};
};
2021-04-22 10:03:58 +02:00
export const getConversationComparator = createSelector(getIntl, _getConversationComparator);
2019-01-14 22:49:58 +01:00
2021-03-16 07:22:46 +01:00
// export only because we use it in some of our tests
2019-03-12 01:20:16 +01:00
export const _getLeftPaneLists = (
2019-01-14 22:49:58 +01:00
lookup: ConversationLookupType,
comparator: (left: ReduxConversationType, right: ReduxConversationType) => number,
2019-01-14 22:49:58 +01:00
selectedConversation?: string
2019-03-12 01:20:16 +01:00
): {
conversations: Array<ReduxConversationType>;
contacts: Array<ReduxConversationType>;
unreadCount: number;
2019-03-12 01:20:16 +01:00
} => {
2019-01-14 22:49:58 +01:00
const values = Object.values(lookup);
2019-03-12 01:20:16 +01:00
const sorted = values.sort(comparator);
const conversations: Array<ReduxConversationType> = [];
const directConversations: Array<ReduxConversationType> = [];
2019-03-12 01:20:16 +01:00
2021-01-29 01:29:24 +01:00
let index = 0;
let unreadCount = 0;
for (let conversation of sorted) {
2019-03-12 01:20:16 +01:00
if (selectedConversation === conversation.id) {
conversation = {
...conversation,
isSelected: true,
};
}
const isBlocked =
BlockedNumberController.isBlocked(conversation.id) ||
BlockedNumberController.isGroupBlocked(conversation.id);
2019-03-12 01:20:16 +01:00
if (isBlocked) {
conversation = {
...conversation,
isBlocked: true,
};
}
2019-03-12 01:20:16 +01:00
2020-07-08 06:29:22 +02:00
// Add Open Group to list as soon as the name has been set
2021-04-22 10:03:58 +02:00
if (conversation.isPublic && (!conversation.name || conversation.name === 'Unknown group')) {
2020-07-08 06:29:22 +02:00
continue;
}
// Show loading icon while fetching messages
if (conversation.isPublic && !conversation.activeAt) {
2020-07-08 06:29:22 +02:00
conversation.lastMessage = {
status: 'sending',
text: '',
};
}
2020-07-08 07:25:18 +02:00
// Remove all invalid conversations and conversatons of devices associated
// with cancelled attempted links
if (!conversation.isPublic && !conversation.activeAt) {
2020-07-06 08:31:33 +02:00
continue;
}
if (conversation.activeAt !== undefined && conversation.type === ConversationTypeEnum.PRIVATE) {
2021-03-16 07:22:46 +01:00
directConversations.push(conversation);
2020-01-07 03:24:44 +01:00
}
2020-06-16 02:00:37 +02:00
if (unreadCount < 9 && conversation.unreadCount > 0) {
unreadCount += conversation.unreadCount;
2020-01-07 03:24:44 +01:00
}
2021-01-20 06:58:59 +01:00
conversations.push(conversation);
2021-01-29 01:29:24 +01:00
index++;
2019-03-12 01:20:16 +01:00
}
2020-01-07 03:24:44 +01:00
return {
conversations,
2021-03-16 07:22:46 +01:00
contacts: directConversations,
unreadCount,
2020-01-07 03:24:44 +01:00
};
2019-01-14 22:49:58 +01:00
};
2019-03-12 01:20:16 +01:00
export const getLeftPaneLists = createSelector(
2019-01-14 22:49:58 +01:00
getConversationLookup,
getConversationComparator,
getSelectedConversationKey,
2019-03-12 01:20:16 +01:00
_getLeftPaneLists
2019-01-14 22:49:58 +01:00
);
export const getMe = createSelector(
2021-01-20 06:58:59 +01:00
[getConversationLookup, getOurNumber],
(lookup: ConversationLookupType, ourNumber: string): ReduxConversationType => {
2019-01-14 22:49:58 +01:00
return lookup[ourNumber];
}
);
2021-03-16 07:22:46 +01:00
export const getDirectContacts = createSelector(
getLeftPaneLists,
(state: {
conversations: Array<ReduxConversationType>;
contacts: Array<ReduxConversationType>;
unreadCount: number;
}) => state.contacts
);
2021-04-22 10:03:58 +02:00
export const getUnreadMessageCount = createSelector(getLeftPaneLists, (state): number => {
return state.unreadCount;
});
export const getConversationHeaderTitleProps = createSelector(getSelectedConversation, (state):
| ConversationHeaderTitleProps
| undefined => {
if (!state) {
return undefined;
}
return {
isKickedFromGroup: state.isKickedFromGroup,
phoneNumber: state.phoneNumber,
isMe: state.isMe,
members: state.members || [],
isPublic: state.isPublic,
profileName: state.profileName,
name: state.name,
subscriberCount: state.subscriberCount,
isGroup: state.type === 'group',
};
});
export const getConversationHeaderProps = createSelector(getSelectedConversation, (state):
| ConversationHeaderProps
| undefined => {
if (!state) {
return undefined;
}
const expirationSettingName = state.expireTimer
? window.Whisper.ExpirationTimerOptions.getName(state.expireTimer || 0)
: null;
return {
id: state.id,
isPrivate: state.isPrivate,
notificationForConvo: state.notificationForConvo,
currentNotificationSetting: state.currentNotificationSetting,
isBlocked: state.isBlocked,
left: state.left,
avatarPath: state.avatarPath,
expirationSettingName: expirationSettingName,
hasNickname: state.hasNickname,
weAreAdmin: state.weAreAdmin,
isKickedFromGroup: state.isKickedFromGroup,
phoneNumber: state.phoneNumber,
isMe: state.isMe,
members: state.members || [],
isPublic: state.isPublic,
profileName: state.profileName,
name: state.name,
subscriberCount: state.subscriberCount,
isGroup: state.isGroup,
};
});
export const getNumberOfPinnedConversations = createSelector(getConversations, (state): number => {
2021-07-07 01:29:04 +02:00
const values = Object.values(state.conversationLookup);
return values.filter(conversation => conversation.isPinned).length;
});
export const isMessageDetailView = createSelector(
getConversations,
(state: ConversationsStateType): boolean => state.messageDetailProps !== undefined
);
export const getMessageDetailsViewProps = createSelector(
getConversations,
(state: ConversationsStateType): MessagePropsDetails | undefined => state.messageDetailProps
);
export const isRightPanelShowing = createSelector(
getConversations,
(state: ConversationsStateType): boolean => state.showRightPanel
);
export const isMessageSelectionMode = createSelector(
getConversations,
(state: ConversationsStateType): boolean => state.selectedMessageIds.length > 0
);
export const getSelectedMessageIds = createSelector(
getConversations,
(state: ConversationsStateType): Array<string> => state.selectedMessageIds
);
2021-07-14 05:48:59 +02:00
export const getLightBoxOptions = createSelector(
getConversations,
(state: ConversationsStateType): LightBoxOptions | undefined => state.lightBox
);
export const getQuotedMessage = createSelector(
getConversations,
(state: ConversationsStateType): ReplyingToMessageProps | undefined => state.quotedMessage
);
2021-07-16 01:34:32 +02:00
2021-07-20 08:58:51 +02:00
export const areMoreMessagesBeingFetched = createSelector(
getConversations,
(state: ConversationsStateType): boolean => state.areMoreMessagesBeingFetched || false
);
export const getShowScrollButton = createSelector(
getConversations,
(state: ConversationsStateType): boolean => state.showScrollButton || false
);
2021-07-16 01:34:32 +02:00
2021-07-20 08:58:51 +02:00
export const getQuotedMessageToAnimate = createSelector(
getConversations,
(state: ConversationsStateType): string | undefined => state.animateQuotedMessageId || undefined
);
export const getNextMessageToPlayIndex = createSelector(
getConversations,
(state: ConversationsStateType): number | undefined => state.nextMessageToPlay || undefined
);