2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Dictionary } from 'lodash';
|
|
|
|
import { omit, reject } from 'lodash';
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-07-09 19:36:10 +00:00
|
|
|
import type {
|
|
|
|
StickerPackStatusType,
|
|
|
|
StickerType as StickerDBType,
|
|
|
|
StickerPackType as StickerPackDBType,
|
|
|
|
} from '../../sql/Interface';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../../sql/Client';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { RecentStickerType } from '../../types/Stickers';
|
2019-05-24 01:27:42 +00:00
|
|
|
import {
|
|
|
|
downloadStickerPack as externalDownloadStickerPack,
|
|
|
|
maybeDeletePack,
|
2021-07-09 19:36:10 +00:00
|
|
|
} from '../../types/Stickers';
|
2022-08-03 17:10:49 +00:00
|
|
|
import { storageServiceUploadJob } from '../../services/storage';
|
2019-05-16 22:32:11 +00:00
|
|
|
import { sendStickerPackSync } from '../../shims/textsecure';
|
|
|
|
import { trigger } from '../../shims/events';
|
2023-08-17 23:35:41 +00:00
|
|
|
import { ERASE_STORAGE_SERVICE } from './user';
|
|
|
|
import type { EraseStorageServiceStateAction } from './user';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { NoopActionType } from './noop';
|
2024-03-12 16:29:31 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2019-05-24 01:27:42 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const { getRecentStickers } = DataReader;
|
|
|
|
const { updateStickerLastUsed } = DataWriter;
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StickersStateType = ReadonlyDeep<{
|
|
|
|
installedPack: string | null;
|
|
|
|
packs: Dictionary<StickerPackDBType>;
|
|
|
|
recentStickers: Array<RecentStickerType>;
|
|
|
|
blessedPacks: Dictionary<boolean>;
|
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// These are for the React components
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StickerType = ReadonlyDeep<{
|
|
|
|
id: number;
|
|
|
|
packId: string;
|
|
|
|
emoji?: string;
|
|
|
|
url: string;
|
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StickerPackType = ReadonlyDeep<{
|
2021-07-09 19:36:10 +00:00
|
|
|
id: string;
|
|
|
|
key: string;
|
|
|
|
title: string;
|
|
|
|
author: string;
|
|
|
|
isBlessed: boolean;
|
|
|
|
cover?: StickerType;
|
|
|
|
lastUsed?: number;
|
|
|
|
attemptedStatus?: 'downloaded' | 'installed' | 'ephemeral';
|
|
|
|
status: StickerPackStatusType;
|
|
|
|
stickers: Array<StickerType>;
|
|
|
|
stickerCount: number;
|
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type StickerPackAddedAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/STICKER_PACK_ADDED';
|
|
|
|
payload: StickerPackDBType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type StickerAddedAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/STICKER_ADDED';
|
|
|
|
payload: StickerDBType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type InstallStickerPackPayloadType = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
packId: string;
|
2020-01-09 14:35:33 +00:00
|
|
|
fromSync: boolean;
|
2019-05-16 22:32:11 +00:00
|
|
|
status: 'installed';
|
|
|
|
installedAt: number;
|
|
|
|
recentStickers: Array<RecentStickerType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type InstallStickerPackAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/INSTALL_STICKER_PACK';
|
|
|
|
payload: Promise<InstallStickerPackPayloadType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type InstallStickerPackFulfilledAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/INSTALL_STICKER_PACK_FULFILLED';
|
|
|
|
payload: InstallStickerPackPayloadType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type ClearInstalledStickerPackAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/CLEAR_INSTALLED_STICKER_PACK';
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type UninstallStickerPackPayloadType = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
packId: string;
|
2020-01-09 14:35:33 +00:00
|
|
|
fromSync: boolean;
|
2019-05-24 01:27:42 +00:00
|
|
|
status: 'downloaded';
|
2021-07-09 19:36:10 +00:00
|
|
|
installedAt?: undefined;
|
2019-05-16 22:32:11 +00:00
|
|
|
recentStickers: Array<RecentStickerType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type UninstallStickerPackAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/UNINSTALL_STICKER_PACK';
|
|
|
|
payload: Promise<UninstallStickerPackPayloadType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type UninstallStickerPackFulfilledAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/UNINSTALL_STICKER_PACK_FULFILLED';
|
|
|
|
payload: UninstallStickerPackPayloadType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type StickerPackUpdatedAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/STICKER_PACK_UPDATED';
|
|
|
|
payload: { packId: string; patch: Partial<StickerPackDBType> };
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type StickerPackRemovedAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/REMOVE_STICKER_PACK';
|
|
|
|
payload: string;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type UseStickerPayloadType = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
packId: string;
|
|
|
|
stickerId: number;
|
|
|
|
time: number;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type UseStickerAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/USE_STICKER';
|
|
|
|
payload: Promise<UseStickerPayloadType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type UseStickerFulfilledAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'stickers/USE_STICKER_FULFILLED';
|
|
|
|
payload: UseStickerPayloadType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StickersActionType = ReadonlyDeep<
|
2019-05-16 22:32:11 +00:00
|
|
|
| ClearInstalledStickerPackAction
|
2023-08-17 23:35:41 +00:00
|
|
|
| InstallStickerPackFulfilledAction
|
|
|
|
| NoopActionType
|
2019-05-16 22:32:11 +00:00
|
|
|
| StickerAddedAction
|
|
|
|
| StickerPackAddedAction
|
|
|
|
| StickerPackRemovedAction
|
2023-08-17 23:35:41 +00:00
|
|
|
| StickerPackUpdatedAction
|
|
|
|
| UninstallStickerPackFulfilledAction
|
2019-05-24 01:27:42 +00:00
|
|
|
| UseStickerFulfilledAction
|
2023-01-13 20:07:26 +00:00
|
|
|
>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
clearInstalledStickerPack,
|
2023-08-17 23:35:41 +00:00
|
|
|
downloadStickerPack,
|
|
|
|
installStickerPack,
|
2019-05-16 22:32:11 +00:00
|
|
|
removeStickerPack,
|
|
|
|
stickerAdded,
|
|
|
|
stickerPackAdded,
|
|
|
|
stickerPackUpdated,
|
2023-08-17 23:35:41 +00:00
|
|
|
uninstallStickerPack,
|
2019-05-16 22:32:11 +00:00
|
|
|
useSticker,
|
|
|
|
};
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
export const useStickersActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function removeStickerPack(id: string): StickerPackRemovedAction {
|
|
|
|
return {
|
|
|
|
type: 'stickers/REMOVE_STICKER_PACK',
|
|
|
|
payload: id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function stickerAdded(payload: StickerDBType): StickerAddedAction {
|
|
|
|
return {
|
|
|
|
type: 'stickers/STICKER_ADDED',
|
|
|
|
payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:33:48 +00:00
|
|
|
function stickerPackAdded(
|
|
|
|
payload: StickerPackDBType,
|
|
|
|
options?: { suppressError?: boolean }
|
|
|
|
): StickerPackAddedAction {
|
2019-05-16 22:32:11 +00:00
|
|
|
const { status, attemptedStatus } = payload;
|
|
|
|
|
|
|
|
// We do this to trigger a toast, which is still done via Backbone
|
2022-03-23 21:33:48 +00:00
|
|
|
if (
|
|
|
|
status === 'error' &&
|
|
|
|
attemptedStatus === 'installed' &&
|
|
|
|
!options?.suppressError
|
|
|
|
) {
|
2019-05-16 22:32:11 +00:00
|
|
|
trigger('pack-install-failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'stickers/STICKER_PACK_ADDED',
|
|
|
|
payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
function downloadStickerPack(
|
|
|
|
packId: string,
|
|
|
|
packKey: string,
|
|
|
|
options?: { finalStatus?: 'installed' | 'downloaded' }
|
|
|
|
): NoopActionType {
|
|
|
|
const { finalStatus } = options || { finalStatus: undefined };
|
|
|
|
|
|
|
|
// We're just kicking this off, since it will generate more redux events
|
2022-12-21 18:41:48 +00:00
|
|
|
void externalDownloadStickerPack(packId, packKey, { finalStatus });
|
2019-05-24 01:27:42 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'NOOP',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function installStickerPack(
|
|
|
|
packId: string,
|
|
|
|
packKey: string,
|
2024-06-12 20:47:56 +00:00
|
|
|
options: {
|
|
|
|
fromSync?: boolean;
|
|
|
|
fromStorageService?: boolean;
|
|
|
|
fromBackup?: boolean;
|
|
|
|
} = {}
|
2019-05-16 22:32:11 +00:00
|
|
|
): InstallStickerPackAction {
|
|
|
|
return {
|
|
|
|
type: 'stickers/INSTALL_STICKER_PACK',
|
|
|
|
payload: doInstallStickerPack(packId, packKey, options),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
async function doInstallStickerPack(
|
|
|
|
packId: string,
|
|
|
|
packKey: string,
|
2024-06-12 20:47:56 +00:00
|
|
|
options: {
|
|
|
|
fromSync?: boolean;
|
|
|
|
fromStorageService?: boolean;
|
|
|
|
fromBackup?: boolean;
|
|
|
|
} = {}
|
2019-05-16 22:32:11 +00:00
|
|
|
): Promise<InstallStickerPackPayloadType> {
|
2024-06-12 20:47:56 +00:00
|
|
|
const {
|
|
|
|
fromSync = false,
|
|
|
|
fromStorageService = false,
|
|
|
|
fromBackup = false,
|
|
|
|
} = options;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const timestamp = Date.now();
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.installStickerPack(packId, timestamp);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2024-06-12 20:47:56 +00:00
|
|
|
if (!fromSync && !fromStorageService && !fromBackup) {
|
2019-05-16 22:32:11 +00:00
|
|
|
// Kick this off, but don't wait for it
|
2022-12-21 18:41:48 +00:00
|
|
|
void sendStickerPackSync(packId, packKey, true);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 20:47:56 +00:00
|
|
|
if (!fromStorageService && !fromBackup) {
|
2022-08-03 17:10:49 +00:00
|
|
|
storageServiceUploadJob();
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
const recentStickers = await getRecentStickers();
|
|
|
|
|
|
|
|
return {
|
|
|
|
packId,
|
2020-01-09 14:35:33 +00:00
|
|
|
fromSync,
|
2022-08-03 17:10:49 +00:00
|
|
|
status: 'installed',
|
2020-01-09 14:35:33 +00:00
|
|
|
installedAt: timestamp,
|
2019-05-16 22:32:11 +00:00
|
|
|
recentStickers: recentStickers.map(item => ({
|
|
|
|
packId: item.packId,
|
|
|
|
stickerId: item.id,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function uninstallStickerPack(
|
|
|
|
packId: string,
|
|
|
|
packKey: string,
|
2022-08-03 17:10:49 +00:00
|
|
|
options: { fromSync?: boolean; fromStorageService?: boolean } = {}
|
2019-05-16 22:32:11 +00:00
|
|
|
): UninstallStickerPackAction {
|
|
|
|
return {
|
|
|
|
type: 'stickers/UNINSTALL_STICKER_PACK',
|
|
|
|
payload: doUninstallStickerPack(packId, packKey, options),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
async function doUninstallStickerPack(
|
|
|
|
packId: string,
|
|
|
|
packKey: string,
|
2022-08-03 17:10:49 +00:00
|
|
|
options: { fromSync?: boolean; fromStorageService?: boolean } = {}
|
2019-05-16 22:32:11 +00:00
|
|
|
): Promise<UninstallStickerPackPayloadType> {
|
2022-08-03 17:10:49 +00:00
|
|
|
const { fromSync = false, fromStorageService = false } = options;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
const timestamp = Date.now();
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.uninstallStickerPack(packId, timestamp);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// If there are no more references, it should be removed
|
|
|
|
await maybeDeletePack(packId);
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
if (!fromSync && !fromStorageService) {
|
2019-05-16 22:32:11 +00:00
|
|
|
// Kick this off, but don't wait for it
|
2022-12-21 18:41:48 +00:00
|
|
|
void sendStickerPackSync(packId, packKey, false);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
if (!fromStorageService) {
|
|
|
|
storageServiceUploadJob();
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
const recentStickers = await getRecentStickers();
|
|
|
|
|
|
|
|
return {
|
|
|
|
packId,
|
2020-01-09 14:35:33 +00:00
|
|
|
fromSync,
|
2022-08-03 17:10:49 +00:00
|
|
|
status: 'downloaded',
|
2021-07-09 19:36:10 +00:00
|
|
|
installedAt: undefined,
|
2019-05-16 22:32:11 +00:00
|
|
|
recentStickers: recentStickers.map(item => ({
|
|
|
|
packId: item.packId,
|
|
|
|
stickerId: item.id,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function clearInstalledStickerPack(): ClearInstalledStickerPackAction {
|
|
|
|
return { type: 'stickers/CLEAR_INSTALLED_STICKER_PACK' };
|
|
|
|
}
|
|
|
|
|
|
|
|
function stickerPackUpdated(
|
|
|
|
packId: string,
|
2022-03-23 21:33:48 +00:00
|
|
|
patch: Partial<StickerPackDBType>,
|
|
|
|
options?: { suppressError?: boolean }
|
2019-05-16 22:32:11 +00:00
|
|
|
): StickerPackUpdatedAction {
|
2019-05-24 01:27:42 +00:00
|
|
|
const { status, attemptedStatus } = patch;
|
|
|
|
|
|
|
|
// We do this to trigger a toast, which is still done via Backbone
|
2022-03-23 21:33:48 +00:00
|
|
|
if (
|
|
|
|
status === 'error' &&
|
|
|
|
attemptedStatus === 'installed' &&
|
|
|
|
!options?.suppressError
|
|
|
|
) {
|
2019-05-24 01:27:42 +00:00
|
|
|
trigger('pack-install-failed');
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
type: 'stickers/STICKER_PACK_UPDATED',
|
|
|
|
payload: {
|
|
|
|
packId,
|
|
|
|
patch,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function useSticker(
|
|
|
|
packId: string,
|
|
|
|
stickerId: number,
|
2022-08-03 17:10:49 +00:00
|
|
|
time?: number
|
2019-05-16 22:32:11 +00:00
|
|
|
): UseStickerAction {
|
|
|
|
return {
|
|
|
|
type: 'stickers/USE_STICKER',
|
|
|
|
payload: doUseSticker(packId, stickerId, time),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
async function doUseSticker(
|
|
|
|
packId: string,
|
|
|
|
stickerId: number,
|
|
|
|
time = Date.now()
|
|
|
|
): Promise<UseStickerPayloadType> {
|
|
|
|
await updateStickerLastUsed(packId, stickerId, time);
|
|
|
|
|
|
|
|
return {
|
|
|
|
packId,
|
|
|
|
stickerId,
|
|
|
|
time,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): StickersStateType {
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
installedPack: null,
|
|
|
|
packs: {},
|
|
|
|
recentStickers: [],
|
|
|
|
blessedPacks: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<StickersStateType> = getEmptyState(),
|
2023-08-17 23:35:41 +00:00
|
|
|
action: Readonly<StickersActionType | EraseStorageServiceStateAction>
|
2019-05-16 22:32:11 +00:00
|
|
|
): StickersStateType {
|
|
|
|
if (action.type === 'stickers/STICKER_PACK_ADDED') {
|
2020-09-14 21:56:35 +00:00
|
|
|
// ts complains due to `stickers: {}` being overridden by the payload
|
|
|
|
// but without full confidence that that's the case, `any` and ignore
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const { payload } = action as any;
|
2019-05-16 22:32:11 +00:00
|
|
|
const newPack = {
|
|
|
|
stickers: {},
|
|
|
|
...payload,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
packs: {
|
|
|
|
...state.packs,
|
|
|
|
[payload.id]: newPack,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'stickers/STICKER_ADDED') {
|
|
|
|
const { payload } = action;
|
|
|
|
const packToUpdate = state.packs[payload.packId];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
packs: {
|
|
|
|
...state.packs,
|
|
|
|
[packToUpdate.id]: {
|
|
|
|
...packToUpdate,
|
|
|
|
stickers: {
|
|
|
|
...packToUpdate.stickers,
|
|
|
|
[payload.id]: payload,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'stickers/STICKER_PACK_UPDATED') {
|
|
|
|
const { payload } = action;
|
|
|
|
const packToUpdate = state.packs[payload.packId];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
packs: {
|
|
|
|
...state.packs,
|
|
|
|
[packToUpdate.id]: {
|
|
|
|
...packToUpdate,
|
|
|
|
...payload.patch,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
action.type === 'stickers/INSTALL_STICKER_PACK_FULFILLED' ||
|
|
|
|
action.type === 'stickers/UNINSTALL_STICKER_PACK_FULFILLED'
|
|
|
|
) {
|
|
|
|
const { payload } = action;
|
2020-01-09 14:35:33 +00:00
|
|
|
const { fromSync, installedAt, packId, status, recentStickers } = payload;
|
2019-05-16 22:32:11 +00:00
|
|
|
const { packs } = state;
|
|
|
|
const existingPack = packs[packId];
|
|
|
|
|
|
|
|
// A pack might be deleted as part of the uninstall process
|
|
|
|
if (!existingPack) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
installedPack:
|
|
|
|
state.installedPack === packId ? null : state.installedPack,
|
|
|
|
recentStickers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:35:33 +00:00
|
|
|
const isBlessed = state.blessedPacks[packId];
|
|
|
|
const installedPack = !fromSync && !isBlessed ? packId : null;
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2020-01-09 14:35:33 +00:00
|
|
|
installedPack,
|
2019-05-16 22:32:11 +00:00
|
|
|
packs: {
|
|
|
|
...packs,
|
|
|
|
[packId]: {
|
|
|
|
...packs[packId],
|
|
|
|
status,
|
|
|
|
installedAt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
recentStickers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'stickers/CLEAR_INSTALLED_STICKER_PACK') {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
installedPack: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'stickers/REMOVE_STICKER_PACK') {
|
|
|
|
const { payload } = action;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
packs: omit(state.packs, payload),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'stickers/USE_STICKER_FULFILLED') {
|
|
|
|
const { payload } = action;
|
|
|
|
const { packId, stickerId, time } = payload;
|
|
|
|
const { recentStickers, packs } = state;
|
|
|
|
|
|
|
|
const filteredRecents = reject(
|
|
|
|
recentStickers,
|
|
|
|
item => item.packId === packId && item.stickerId === stickerId
|
|
|
|
);
|
|
|
|
const pack = packs[packId];
|
|
|
|
const sticker = pack.stickers[stickerId];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
recentStickers: [payload, ...filteredRecents],
|
|
|
|
packs: {
|
|
|
|
...state.packs,
|
|
|
|
[packId]: {
|
|
|
|
...pack,
|
|
|
|
lastUsed: time,
|
|
|
|
stickers: {
|
|
|
|
...pack.stickers,
|
|
|
|
[stickerId]: {
|
|
|
|
...sticker,
|
|
|
|
lastUsed: time,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:35:41 +00:00
|
|
|
if (action.type === ERASE_STORAGE_SERVICE) {
|
|
|
|
const { packs } = state;
|
|
|
|
|
|
|
|
const entries = Object.entries(packs).map(([id, pack]) => {
|
|
|
|
return [
|
|
|
|
id,
|
|
|
|
omit(pack, [
|
|
|
|
'storageID',
|
|
|
|
'storageVersion',
|
|
|
|
'storageUnknownFields',
|
|
|
|
'storageNeedsSync',
|
|
|
|
]),
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
packs: Object.fromEntries(entries),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return state;
|
|
|
|
}
|