signal-desktop/ts/test-both/helpers/getFakeStory.tsx

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-07-01 00:52:03 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-07-20 21:29:09 +00:00
import casual from 'casual';
import { v4 as generateUuid } from 'uuid';
2022-07-01 00:52:03 +00:00
import type { AttachmentType } from '../../types/Attachment';
import type { ConversationType } from '../../state/ducks/conversations';
import type {
ConversationStoryType,
MyStoryType,
StoryViewType,
} from '../../types/Stories';
import * as durations from '../../util/durations';
import { getDefaultConversation } from './getDefaultConversation';
import { fakeAttachment, fakeThumbnail } from './fakeAttachment';
2022-11-16 22:10:11 +00:00
import { MY_STORY_ID, ResolvedSendStatus } from '../../types/Stories';
2022-07-01 00:52:03 +00:00
function getAttachmentWithThumbnail(url: string): AttachmentType {
return fakeAttachment({
2022-11-16 22:10:11 +00:00
path: url,
2022-07-01 00:52:03 +00:00
thumbnail: fakeThumbnail(url),
2022-11-16 22:10:11 +00:00
url,
2022-07-01 00:52:03 +00:00
});
}
export function getFakeMyStory(id?: string, name?: string): MyStoryType {
2022-07-20 21:29:09 +00:00
const storyCount = casual.integer(2, 6);
2022-07-01 00:52:03 +00:00
return {
id: id || generateUuid(),
name: name || id === MY_STORY_ID ? 'My Stories' : casual.catch_phrase,
2022-11-16 22:10:11 +00:00
reducedSendStatus: ResolvedSendStatus.Sent,
2022-07-01 00:52:03 +00:00
stories: Array.from(Array(storyCount), () => ({
...getFakeStoryView(),
sendState: [],
2022-07-20 21:29:09 +00:00
views: casual.integer(1, 20),
2022-07-01 00:52:03 +00:00
})),
};
}
export function getFakeStoryView(
attachmentUrl?: string,
timestamp?: number
): StoryViewType {
const sender = getDefaultConversation();
const messageId = generateUuid();
2022-07-01 00:52:03 +00:00
return {
attachment: getAttachmentWithThumbnail(
attachmentUrl || '/fixtures/tina-rolf-269345-unsplash.jpg'
),
2022-07-20 21:29:09 +00:00
isUnread: Boolean(casual.coin_flip),
messageId,
messageIdForLogging: `${messageId} (for logging)`,
2022-07-01 00:52:03 +00:00
sender,
timestamp: timestamp || Date.now() - 2 * durations.MINUTE,
expirationTimestamp: undefined,
2022-07-01 00:52:03 +00:00
};
}
export function getFakeStory({
attachmentUrl,
group,
timestamp,
}: {
attachmentUrl?: string;
group?: ConversationType;
timestamp?: number;
}): ConversationStoryType {
const storyView = getFakeStoryView(attachmentUrl, timestamp);
const hasReplies = group ? Boolean(casual.coin_flip) : false;
const hasRepliesFromSelf =
group && hasReplies ? Boolean(casual.coin_flip) : false;
2022-07-01 00:52:03 +00:00
return {
conversationId: storyView.sender.id,
hasReplies,
hasRepliesFromSelf,
2022-07-01 00:52:03 +00:00
group,
2022-07-06 19:06:20 +00:00
storyView,
2022-07-01 00:52:03 +00:00
};
}