signal-desktop/ts/state/ducks/stories.ts

1000 lines
25 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-07-06 19:06:20 +00:00
import type { ThunkAction, ThunkDispatch } from 'redux-thunk';
2022-07-01 00:52:03 +00:00
import { isEqual, pick } from 'lodash';
2022-03-04 21:14:52 +00:00
import type { AttachmentType } from '../../types/Attachment';
import type { BodyRangeType } from '../../types/Util';
import type { MessageAttributesType } from '../../model-types.d';
2022-04-15 00:08:46 +00:00
import type {
MessageChangedActionType,
MessageDeletedActionType,
} from './conversations';
2022-03-04 21:14:52 +00:00
import type { NoopActionType } from './noop';
import type { StateType as RootStateType } from '../reducer';
2022-07-01 00:52:03 +00:00
import type { StoryViewType } from '../../types/Stories';
2022-03-04 21:14:52 +00:00
import type { SyncType } from '../../jobs/helpers/syncHelpers';
import * as log from '../../logging/log';
2022-03-29 01:10:08 +00:00
import dataInterface from '../../sql/Client';
2022-07-01 00:52:03 +00:00
import { DAY } from '../../util/durations';
2022-03-04 21:14:52 +00:00
import { ReadStatus } from '../../messages/MessageReadStatus';
2022-07-06 19:06:20 +00:00
import { StoryViewDirectionType, StoryViewModeType } from '../../types/Stories';
2022-03-04 21:14:52 +00:00
import { ToastReactionFailed } from '../../components/ToastReactionFailed';
import { enqueueReactionForSend } from '../../reactions/enqueueReactionForSend';
import { getMessageById } from '../../messages/getMessageById';
import { markViewed } from '../../services/MessageUpdater';
2022-03-29 01:10:08 +00:00
import { queueAttachmentDownloads } from '../../util/queueAttachmentDownloads';
import { replaceIndex } from '../../util/replaceIndex';
2022-07-01 00:52:03 +00:00
import { sendDeleteForEveryoneMessage } from '../../util/sendDeleteForEveryoneMessage';
2022-03-04 21:14:52 +00:00
import { showToast } from '../../util/showToast';
import {
hasNotResolved,
isDownloaded,
isDownloading,
} from '../../types/Attachment';
2022-07-06 19:06:20 +00:00
import { getConversationSelector } from '../selectors/conversations';
import { getStories } from '../selectors/stories';
import { isGroup } from '../../util/whatTypeOfConversation';
2022-03-04 21:14:52 +00:00
import { useBoundActions } from '../../hooks/useBoundActions';
import { viewSyncJobQueue } from '../../jobs/viewSyncJobQueue';
import { viewedReceiptsJobQueue } from '../../jobs/viewedReceiptsJobQueue';
export type StoryDataType = {
attachment?: AttachmentType;
messageId: string;
} & Pick<
MessageAttributesType,
2022-07-01 00:52:03 +00:00
| 'canReplyToStory'
2022-04-15 00:08:46 +00:00
| 'conversationId'
| 'deletedForEveryone'
2022-04-28 22:06:28 +00:00
| 'reactions'
2022-04-15 00:08:46 +00:00
| 'readStatus'
| 'sendStateByConversationId'
| 'source'
| 'sourceUuid'
2022-07-01 00:52:03 +00:00
| 'storyDistributionListId'
2022-04-15 00:08:46 +00:00
| 'timestamp'
| 'type'
2022-03-04 21:14:52 +00:00
>;
2022-07-06 19:06:20 +00:00
export type SelectedStoryDataType = {
currentIndex: number;
numStories: number;
story: StoryDataType;
};
2022-03-04 21:14:52 +00:00
// State
export type StoriesStateType = {
readonly isShowingStoriesView: boolean;
2022-04-15 00:08:46 +00:00
readonly replyState?: {
messageId: string;
replies: Array<MessageAttributesType>;
};
2022-07-06 19:06:20 +00:00
readonly selectedStoryData?: SelectedStoryDataType;
2022-03-04 21:14:52 +00:00
readonly stories: Array<StoryDataType>;
2022-07-06 19:06:20 +00:00
readonly storyViewMode?: StoryViewModeType;
2022-03-04 21:14:52 +00:00
};
// Actions
2022-07-01 00:52:03 +00:00
const DOE_STORY = 'stories/DOE';
2022-04-15 00:08:46 +00:00
const LOAD_STORY_REPLIES = 'stories/LOAD_STORY_REPLIES';
2022-03-29 01:10:08 +00:00
const MARK_STORY_READ = 'stories/MARK_STORY_READ';
2022-04-15 00:08:46 +00:00
const REPLY_TO_STORY = 'stories/REPLY_TO_STORY';
export const RESOLVE_ATTACHMENT_URL = 'stories/RESOLVE_ATTACHMENT_URL';
2022-03-29 01:10:08 +00:00
const STORY_CHANGED = 'stories/STORY_CHANGED';
2022-03-04 21:14:52 +00:00
const TOGGLE_VIEW = 'stories/TOGGLE_VIEW';
2022-07-06 19:06:20 +00:00
const VIEW_STORY = 'stories/VIEW_STORY';
2022-03-04 21:14:52 +00:00
2022-07-01 00:52:03 +00:00
type DOEStoryActionType = {
type: typeof DOE_STORY;
payload: string;
};
2022-04-15 00:08:46 +00:00
type LoadStoryRepliesActionType = {
type: typeof LOAD_STORY_REPLIES;
payload: {
messageId: string;
replies: Array<MessageAttributesType>;
};
};
2022-03-29 01:10:08 +00:00
type MarkStoryReadActionType = {
type: typeof MARK_STORY_READ;
payload: string;
2022-03-04 21:14:52 +00:00
};
2022-04-15 00:08:46 +00:00
type ReplyToStoryActionType = {
type: typeof REPLY_TO_STORY;
payload: MessageAttributesType;
};
type ResolveAttachmentUrlActionType = {
type: typeof RESOLVE_ATTACHMENT_URL;
payload: {
messageId: string;
attachmentUrl: string;
};
};
2022-03-29 01:10:08 +00:00
type StoryChangedActionType = {
type: typeof STORY_CHANGED;
payload: StoryDataType;
};
2022-03-04 21:14:52 +00:00
type ToggleViewActionType = {
type: typeof TOGGLE_VIEW;
};
2022-07-06 19:06:20 +00:00
type ViewStoryActionType = {
type: typeof VIEW_STORY;
payload:
| {
selectedStoryData: SelectedStoryDataType;
storyViewMode: StoryViewModeType;
}
| undefined;
};
2022-03-04 21:14:52 +00:00
export type StoriesActionType =
2022-07-01 00:52:03 +00:00
| DOEStoryActionType
2022-04-15 00:08:46 +00:00
| LoadStoryRepliesActionType
2022-03-29 01:10:08 +00:00
| MarkStoryReadActionType
2022-04-15 00:08:46 +00:00
| MessageChangedActionType
2022-03-04 21:14:52 +00:00
| MessageDeletedActionType
2022-04-15 00:08:46 +00:00
| ReplyToStoryActionType
| ResolveAttachmentUrlActionType
2022-03-29 01:10:08 +00:00
| StoryChangedActionType
2022-07-06 19:06:20 +00:00
| ToggleViewActionType
| ViewStoryActionType;
2022-03-04 21:14:52 +00:00
// Action Creators
2022-07-01 00:52:03 +00:00
function deleteStoryForEveryone(
story: StoryViewType
): ThunkAction<void, RootStateType, unknown, DOEStoryActionType> {
return (dispatch, getState) => {
if (!story.sendState) {
return;
}
const conversationIds = new Set(
story.sendState.map(({ recipient }) => recipient.id)
);
// Find stories that were sent to other distribution lists so that we don't
// send a DOE request to the members of those lists.
const { stories } = getState().stories;
stories.forEach(item => {
if (item.timestamp !== story.timestamp) {
return;
}
if (!item.sendStateByConversationId) {
return;
}
Object.keys(item.sendStateByConversationId).forEach(conversationId => {
conversationIds.delete(conversationId);
});
});
conversationIds.forEach(cid => {
const conversation = window.ConversationController.get(cid);
if (!conversation) {
return;
}
sendDeleteForEveryoneMessage(conversation.attributes, {
deleteForEveryoneDuration: DAY,
id: story.messageId,
timestamp: story.timestamp,
});
});
dispatch({
type: DOE_STORY,
payload: story.messageId,
});
};
}
2022-04-15 00:08:46 +00:00
function loadStoryReplies(
conversationId: string,
messageId: string
): ThunkAction<void, RootStateType, unknown, LoadStoryRepliesActionType> {
return async (dispatch, getState) => {
const conversation = getConversationSelector(getState())(conversationId);
2022-04-15 00:08:46 +00:00
const replies = await dataInterface.getOlderMessagesByConversation(
conversationId,
{ limit: 9000, storyId: messageId, isGroup: isGroup(conversation) }
2022-04-15 00:08:46 +00:00
);
dispatch({
type: LOAD_STORY_REPLIES,
payload: {
messageId,
replies,
},
});
};
}
2022-03-04 21:14:52 +00:00
function markStoryRead(
messageId: string
2022-03-29 01:10:08 +00:00
): ThunkAction<void, RootStateType, unknown, MarkStoryReadActionType> {
2022-03-04 21:14:52 +00:00
return async (dispatch, getState) => {
const { stories } = getState().stories;
const matchingStory = stories.find(story => story.messageId === messageId);
if (!matchingStory) {
log.warn(`markStoryRead: no matching story found: ${messageId}`);
return;
}
2022-04-08 15:40:15 +00:00
if (!isDownloaded(matchingStory.attachment)) {
return;
}
2022-03-04 21:14:52 +00:00
if (matchingStory.readStatus !== ReadStatus.Unread) {
return;
}
const message = await getMessageById(messageId);
if (!message) {
return;
}
2022-03-29 01:10:08 +00:00
const storyReadDate = Date.now();
markViewed(message.attributes, storyReadDate);
2022-03-04 21:14:52 +00:00
const viewedReceipt = {
messageId,
senderE164: message.attributes.source,
senderUuid: message.attributes.sourceUuid,
timestamp: message.attributes.sent_at,
};
const viewSyncs: Array<SyncType> = [viewedReceipt];
if (!window.ConversationController.areWePrimaryDevice()) {
viewSyncJobQueue.add({ viewSyncs });
}
viewedReceiptsJobQueue.add({ viewedReceipt });
2022-03-29 01:10:08 +00:00
await dataInterface.addNewStoryRead({
authorId: message.attributes.sourceUuid,
conversationId: message.attributes.conversationId,
2022-07-08 20:46:25 +00:00
storyId: messageId,
2022-03-29 01:10:08 +00:00
storyReadDate,
});
dispatch({
type: MARK_STORY_READ,
payload: messageId,
});
};
}
function queueStoryDownload(
storyId: string
): ThunkAction<
void,
RootStateType,
unknown,
NoopActionType | ResolveAttachmentUrlActionType
> {
2022-07-01 00:52:03 +00:00
return async (dispatch, getState) => {
const { stories } = getState().stories;
const story = stories.find(item => item.messageId === storyId);
2022-03-29 01:10:08 +00:00
if (!story) {
return;
}
2022-07-01 00:52:03 +00:00
const { attachment } = story;
2022-03-29 01:10:08 +00:00
if (!attachment) {
log.warn('queueStoryDownload: No attachment found for story', {
storyId,
});
return;
}
if (isDownloaded(attachment)) {
if (!attachment.path) {
return;
}
// This function also resolves the attachment's URL in case we've already
// downloaded the attachment but haven't pointed its path to an absolute
// location on disk.
if (hasNotResolved(attachment)) {
dispatch({
type: RESOLVE_ATTACHMENT_URL,
payload: {
messageId: storyId,
attachmentUrl: window.Signal.Migrations.getAbsoluteAttachmentPath(
attachment.path
),
},
});
}
2022-03-29 01:10:08 +00:00
return;
}
if (isDownloading(attachment)) {
return;
}
2022-07-01 00:52:03 +00:00
const message = await getMessageById(storyId);
if (message) {
// We want to ensure that we re-hydrate the story reply context with the
// completed attachment download.
message.set({ storyReplyContext: undefined });
2022-03-29 01:10:08 +00:00
2022-07-01 00:52:03 +00:00
await queueAttachmentDownloads(message.attributes);
}
2022-03-29 01:10:08 +00:00
2022-03-04 21:14:52 +00:00
dispatch({
type: 'NOOP',
payload: null,
});
};
}
function reactToStory(
nextReaction: string,
2022-04-28 22:06:28 +00:00
messageId: string
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
2022-03-04 21:14:52 +00:00
return async dispatch => {
try {
await enqueueReactionForSend({
messageId,
emoji: nextReaction,
2022-04-28 22:06:28 +00:00
remove: false,
2022-03-04 21:14:52 +00:00
});
} catch (error) {
log.error('Error enqueuing reaction', error, messageId, nextReaction);
showToast(ToastReactionFailed);
}
2022-04-28 22:06:28 +00:00
dispatch({
type: 'NOOP',
payload: null,
});
2022-03-04 21:14:52 +00:00
};
}
function replyToStory(
conversationId: string,
messageBody: string,
2022-03-04 21:14:52 +00:00
mentions: Array<BodyRangeType>,
timestamp: number,
story: StoryViewType
2022-04-15 00:08:46 +00:00
): ThunkAction<void, RootStateType, unknown, ReplyToStoryActionType> {
return async dispatch => {
const conversation = window.ConversationController.get(conversationId);
if (!conversation) {
log.error('replyToStory: conversation does not exist', conversationId);
return;
}
2022-03-04 21:14:52 +00:00
2022-04-15 00:08:46 +00:00
const messageAttributes = await conversation.enqueueMessageForSend(
{
body: messageBody,
attachments: [],
mentions,
},
2022-03-04 21:14:52 +00:00
{
storyId: story.messageId,
timestamp,
}
);
2022-04-15 00:08:46 +00:00
if (messageAttributes) {
dispatch({
type: REPLY_TO_STORY,
payload: messageAttributes,
});
}
2022-03-04 21:14:52 +00:00
};
}
2022-03-29 01:10:08 +00:00
function storyChanged(story: StoryDataType): StoryChangedActionType {
return {
type: STORY_CHANGED,
payload: story,
};
}
2022-03-04 21:14:52 +00:00
function toggleStoriesView(): ToggleViewActionType {
return {
type: TOGGLE_VIEW,
};
}
2022-07-06 19:06:20 +00:00
const getSelectedStoryDataForConversationId = (
dispatch: ThunkDispatch<
RootStateType,
unknown,
NoopActionType | ResolveAttachmentUrlActionType
>,
getState: () => RootStateType,
conversationId: string,
selectedStoryId?: string
): {
currentIndex: number;
hasUnread: boolean;
numStories: number;
storiesByConversationId: Array<StoryDataType>;
} => {
const state = getState();
const { stories } = state.stories;
const storiesByConversationId = stories.filter(
item => item.conversationId === conversationId
);
// Find the index of the storyId provided, or if none provided then find the
// oldest unread story from the user. If all stories are read then we can
// start at the first story.
let currentIndex = 0;
let hasUnread = false;
storiesByConversationId.forEach((item, index) => {
if (selectedStoryId && item.messageId === selectedStoryId) {
currentIndex = index;
}
if (
!selectedStoryId &&
!currentIndex &&
item.readStatus === ReadStatus.Unread
) {
hasUnread = true;
currentIndex = index;
}
});
const numStories = storiesByConversationId.length;
// Queue all undownloaded stories once we're viewing someone's stories
storiesByConversationId.forEach(item => {
if (isDownloaded(item.attachment) || isDownloading(item.attachment)) {
return;
}
queueStoryDownload(item.messageId)(dispatch, getState, null);
});
return {
currentIndex,
hasUnread,
numStories,
storiesByConversationId,
};
};
function viewUserStories(
conversationId: string
): ThunkAction<void, RootStateType, unknown, ViewStoryActionType> {
return (dispatch, getState) => {
const { currentIndex, hasUnread, numStories, storiesByConversationId } =
getSelectedStoryDataForConversationId(dispatch, getState, conversationId);
const story = storiesByConversationId[currentIndex];
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex,
numStories,
story,
},
storyViewMode: hasUnread
? StoryViewModeType.Unread
: StoryViewModeType.All,
},
});
};
}
export type ViewStoryActionCreatorType = (
storyId?: string,
storyViewMode?: StoryViewModeType,
viewDirection?: StoryViewDirectionType
) => unknown;
const viewStory: ViewStoryActionCreatorType = (
storyId,
storyViewMode,
viewDirection
): ThunkAction<void, RootStateType, unknown, ViewStoryActionType> => {
return (dispatch, getState) => {
if (!storyId || !storyViewMode) {
dispatch({
type: VIEW_STORY,
payload: undefined,
});
return;
}
const state = getState();
const { stories } = state.stories;
// Spec:
// When opening the story viewer you should always be taken to the oldest
// un viewed story of the user you tapped on
// If all stories from a user are viewed, opening the viewer should take
// you to their oldest story
const story = stories.find(item => item.messageId === storyId);
if (!story) {
return;
}
const { currentIndex, numStories, storiesByConversationId } =
getSelectedStoryDataForConversationId(
dispatch,
getState,
story.conversationId,
storyId
);
// Go directly to the storyId selected
if (!viewDirection) {
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex,
numStories,
story,
},
storyViewMode,
},
});
return;
}
// Next story within the same user's stories
if (
viewDirection === StoryViewDirectionType.Next &&
currentIndex < numStories - 1
) {
const nextIndex = currentIndex + 1;
const nextStory = storiesByConversationId[nextIndex];
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex: nextIndex,
numStories,
story: nextStory,
},
storyViewMode,
},
});
return;
}
// Prev story within the same user's stories
if (viewDirection === StoryViewDirectionType.Previous && currentIndex > 0) {
const nextIndex = currentIndex - 1;
const nextStory = storiesByConversationId[nextIndex];
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex: nextIndex,
numStories,
story: nextStory,
},
storyViewMode,
},
});
return;
}
// Are there any unviewed stories left? If so we should play the unviewed
// stories first. But only if we're going "next"
if (viewDirection === StoryViewDirectionType.Next) {
const unreadStory = stories.find(
item => item.readStatus === ReadStatus.Unread
);
if (unreadStory) {
const nextSelectedStoryData = getSelectedStoryDataForConversationId(
dispatch,
getState,
unreadStory.conversationId,
unreadStory.messageId
);
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex: nextSelectedStoryData.currentIndex,
numStories: nextSelectedStoryData.numStories,
story: unreadStory,
},
storyViewMode,
},
});
return;
}
}
const conversationStories = getStories(state).stories;
const conversationStoryIndex = conversationStories.findIndex(
item => item.conversationId === story.conversationId
);
if (conversationStoryIndex < 0) {
return;
}
// Find the next user's stories
if (
viewDirection === StoryViewDirectionType.Next &&
conversationStoryIndex < conversationStories.length - 1
) {
// Spec:
// Tapping right advances you to the next un viewed story
// If all stories are viewed, advance to the next viewed story
// When you reach the newest story from a user, tapping right again
// should take you to the next user's oldest un viewed story or oldest
// story if all stories for the next user are viewed.
// When you reach the newest story from the last user in the story list,
// tapping right should close the viewer
// Touch area for tapping right should be 80% of width of the screen
const nextConversationStoryIndex = conversationStoryIndex + 1;
const conversationStory = conversationStories[nextConversationStoryIndex];
const nextSelectedStoryData = getSelectedStoryDataForConversationId(
dispatch,
getState,
conversationStory.conversationId
);
// Close the viewer if we were viewing unread stories only and we've
// reached the last unread story.
if (
!nextSelectedStoryData.hasUnread &&
storyViewMode === StoryViewModeType.Unread
) {
dispatch({
type: VIEW_STORY,
payload: undefined,
});
return;
}
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex: 0,
numStories: nextSelectedStoryData.numStories,
story: nextSelectedStoryData.storiesByConversationId[0],
},
storyViewMode,
},
});
return;
}
// Find the previous user's stories
if (
viewDirection === StoryViewDirectionType.Previous &&
conversationStoryIndex > 0
) {
// Spec:
// Tapping left takes you back to the previous story
// When you reach the oldest story from a user, tapping left again takes
// you to the previous users oldest un viewed story or newest viewed
// story if all stories are viewed
// If you tap left on the oldest story from the first user in the story
// list, it should re-start playback on that story
// Touch area for tapping left should be 20% of width of the screen
const nextConversationStoryIndex = conversationStoryIndex - 1;
const conversationStory = conversationStories[nextConversationStoryIndex];
const nextSelectedStoryData = getSelectedStoryDataForConversationId(
dispatch,
getState,
conversationStory.conversationId
);
dispatch({
type: VIEW_STORY,
payload: {
selectedStoryData: {
currentIndex: 0,
numStories: nextSelectedStoryData.numStories,
story: nextSelectedStoryData.storiesByConversationId[0],
},
storyViewMode,
},
});
return;
}
// Could not meet any criteria, close the viewer
dispatch({
type: VIEW_STORY,
payload: undefined,
});
};
};
export const actions = {
deleteStoryForEveryone,
loadStoryReplies,
markStoryRead,
queueStoryDownload,
reactToStory,
replyToStory,
storyChanged,
toggleStoriesView,
viewUserStories,
viewStory,
};
export const useStoriesActions = (): typeof actions => useBoundActions(actions);
2022-03-04 21:14:52 +00:00
// Reducer
export function getEmptyState(
overrideState: Partial<StoriesStateType> = {}
): StoriesStateType {
return {
isShowingStoriesView: false,
stories: [],
...overrideState,
};
}
export function reducer(
state: Readonly<StoriesStateType> = getEmptyState(),
action: Readonly<StoriesActionType>
): StoriesStateType {
if (action.type === TOGGLE_VIEW) {
return {
...state,
isShowingStoriesView: !state.isShowingStoriesView,
};
}
if (action.type === 'MESSAGE_DELETED') {
2022-04-15 00:08:46 +00:00
const nextStories = state.stories.filter(
story => story.messageId !== action.payload.id
);
if (nextStories.length === state.stories.length) {
return state;
}
2022-03-04 21:14:52 +00:00
return {
...state,
2022-04-15 00:08:46 +00:00
stories: nextStories,
2022-03-04 21:14:52 +00:00
};
}
2022-03-29 01:10:08 +00:00
if (action.type === STORY_CHANGED) {
2022-03-04 21:14:52 +00:00
const newStory = pick(action.payload, [
'attachment',
2022-07-01 00:52:03 +00:00
'canReplyToStory',
2022-03-04 21:14:52 +00:00
'conversationId',
2022-04-15 00:08:46 +00:00
'deletedForEveryone',
2022-03-04 21:14:52 +00:00
'messageId',
2022-04-28 22:06:28 +00:00
'reactions',
2022-03-04 21:14:52 +00:00
'readStatus',
2022-04-15 00:08:46 +00:00
'sendStateByConversationId',
2022-03-04 21:14:52 +00:00
'source',
'sourceUuid',
2022-07-01 00:52:03 +00:00
'storyDistributionListId',
2022-03-04 21:14:52 +00:00
'timestamp',
2022-04-15 00:08:46 +00:00
'type',
2022-03-04 21:14:52 +00:00
]);
2022-04-21 00:29:37 +00:00
const prevStoryIndex = state.stories.findIndex(
2022-03-04 21:14:52 +00:00
existingStory => existingStory.messageId === newStory.messageId
);
2022-04-21 00:29:37 +00:00
if (prevStoryIndex >= 0) {
const prevStory = state.stories[prevStoryIndex];
2022-03-29 01:10:08 +00:00
2022-04-21 00:29:37 +00:00
// Stories rarely need to change, here are the following exceptions:
const isDownloadingAttachment = isDownloading(newStory.attachment);
const hasAttachmentDownloaded =
!isDownloaded(prevStory.attachment) &&
isDownloaded(newStory.attachment);
const readStatusChanged = prevStory.readStatus !== newStory.readStatus;
2022-04-28 22:06:28 +00:00
const reactionsChanged =
prevStory.reactions?.length !== newStory.reactions?.length;
2022-07-01 00:52:03 +00:00
const hasBeenDeleted =
!prevStory.deletedForEveryone && newStory.deletedForEveryone;
const hasSendStateChanged = !isEqual(
prevStory.sendStateByConversationId,
newStory.sendStateByConversationId
);
2022-03-29 01:10:08 +00:00
2022-04-21 00:29:37 +00:00
const shouldReplace =
2022-04-28 22:06:28 +00:00
isDownloadingAttachment ||
hasAttachmentDownloaded ||
2022-07-01 00:52:03 +00:00
hasBeenDeleted ||
hasSendStateChanged ||
2022-04-28 22:06:28 +00:00
readStatusChanged ||
reactionsChanged;
2022-04-21 00:29:37 +00:00
if (!shouldReplace) {
2022-04-15 00:08:46 +00:00
return state;
}
2022-03-29 01:10:08 +00:00
return {
...state,
2022-04-21 00:29:37 +00:00
stories: replaceIndex(state.stories, prevStoryIndex, newStory),
2022-03-29 01:10:08 +00:00
};
2022-03-04 21:14:52 +00:00
}
2022-04-21 00:29:37 +00:00
// Adding a new story
2022-03-29 01:10:08 +00:00
const stories = [...state.stories, newStory].sort((a, b) =>
a.timestamp > b.timestamp ? 1 : -1
2022-03-04 21:14:52 +00:00
);
return {
...state,
stories,
};
}
2022-03-29 01:10:08 +00:00
if (action.type === MARK_STORY_READ) {
return {
...state,
stories: state.stories.map(story => {
if (story.messageId === action.payload) {
return {
...story,
readStatus: ReadStatus.Viewed,
};
}
return story;
}),
};
}
2022-04-15 00:08:46 +00:00
if (action.type === LOAD_STORY_REPLIES) {
return {
...state,
replyState: action.payload,
};
}
// For live updating of the story replies
if (
action.type === 'MESSAGE_CHANGED' &&
state.replyState &&
state.replyState.messageId === action.payload.data.storyId
) {
const { replyState } = state;
const messageIndex = replyState.replies.findIndex(
reply => reply.id === action.payload.id
);
// New message
if (messageIndex < 0) {
return {
...state,
replyState: {
messageId: replyState.messageId,
replies: [...replyState.replies, action.payload.data],
},
};
}
// Changed message, also handles DOE
return {
...state,
replyState: {
messageId: replyState.messageId,
replies: replaceIndex(
replyState.replies,
messageIndex,
action.payload.data
),
},
};
}
if (action.type === REPLY_TO_STORY) {
const { replyState } = state;
if (!replyState) {
return state;
}
return {
...state,
replyState: {
messageId: replyState.messageId,
replies: [...replyState.replies, action.payload],
},
};
}
if (action.type === RESOLVE_ATTACHMENT_URL) {
const { messageId, attachmentUrl } = action.payload;
const storyIndex = state.stories.findIndex(
existingStory => existingStory.messageId === messageId
);
if (storyIndex < 0) {
return state;
}
const story = state.stories[storyIndex];
if (!story.attachment) {
return state;
}
const storyWithResolvedAttachment = {
...story,
attachment: {
...story.attachment,
url: attachmentUrl,
},
};
return {
...state,
stories: replaceIndex(
state.stories,
storyIndex,
storyWithResolvedAttachment
),
};
}
2022-07-01 00:52:03 +00:00
if (action.type === DOE_STORY) {
const prevStoryIndex = state.stories.findIndex(
existingStory => existingStory.messageId === action.payload
);
if (prevStoryIndex < 0) {
return state;
}
return {
...state,
stories: replaceIndex(state.stories, prevStoryIndex, {
...state.stories[prevStoryIndex],
deletedForEveryone: true,
}),
};
}
2022-07-06 19:06:20 +00:00
if (action.type === VIEW_STORY) {
const { selectedStoryData, storyViewMode } = action.payload || {};
return {
...state,
selectedStoryData,
storyViewMode,
};
}
2022-03-04 21:14:52 +00:00
return state;
}