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 { compact, filter, map, orderBy, reject, sortBy, values } from 'lodash';
|
2019-05-16 22:32:11 +00:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
import type { RecentStickerType } from '../../types/Stickers';
|
2024-07-11 19:44:09 +00:00
|
|
|
import {
|
|
|
|
getLocalAttachmentUrl,
|
|
|
|
AttachmentDisposition,
|
|
|
|
} from '../../util/getLocalAttachmentUrl';
|
2021-07-09 19:36:10 +00:00
|
|
|
import type {
|
|
|
|
StickerType as StickerDBType,
|
|
|
|
StickerPackType as StickerPackDBType,
|
|
|
|
} from '../../sql/Interface';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType } from '../reducer';
|
|
|
|
import type {
|
2019-05-16 22:32:11 +00:00
|
|
|
StickersStateType,
|
2021-07-09 19:36:10 +00:00
|
|
|
StickerPackType,
|
2019-05-16 22:32:11 +00:00
|
|
|
StickerType,
|
|
|
|
} from '../ducks/stickers';
|
|
|
|
|
|
|
|
const getSticker = (
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
packId: string,
|
2024-07-11 19:44:09 +00:00
|
|
|
stickerId: number
|
2019-05-16 22:32:11 +00:00
|
|
|
): StickerType | undefined => {
|
|
|
|
const pack = packs[packId];
|
|
|
|
if (!pack) {
|
2020-09-14 21:56:35 +00:00
|
|
|
return undefined;
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sticker = pack.stickers[stickerId];
|
|
|
|
if (!sticker) {
|
2020-09-14 21:56:35 +00:00
|
|
|
return undefined;
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
const isEphemeral = pack.status === 'ephemeral';
|
|
|
|
|
2024-07-11 19:44:09 +00:00
|
|
|
return translateStickerFromDB(sticker, isEphemeral);
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const translateStickerFromDB = (
|
|
|
|
sticker: StickerDBType,
|
2019-05-24 01:27:42 +00:00
|
|
|
isEphemeral: boolean
|
2019-05-16 22:32:11 +00:00
|
|
|
): StickerType => {
|
2024-07-11 19:44:09 +00:00
|
|
|
const { id, packId, emoji } = sticker;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
packId,
|
|
|
|
emoji,
|
2024-07-11 19:44:09 +00:00
|
|
|
url: getLocalAttachmentUrl(sticker, {
|
|
|
|
disposition: isEphemeral
|
|
|
|
? AttachmentDisposition.Temporary
|
|
|
|
: AttachmentDisposition.Sticker,
|
|
|
|
}),
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const translatePackFromDB = (
|
|
|
|
pack: StickerPackDBType,
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2020-09-14 21:56:35 +00:00
|
|
|
): StickerPackType => {
|
2019-05-24 01:27:42 +00:00
|
|
|
const { id, stickers, status, coverStickerId } = pack;
|
|
|
|
const isEphemeral = status === 'ephemeral';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// Sometimes sticker packs have a cover which isn't included in their set of stickers.
|
|
|
|
// We don't want to show cover-only images when previewing or picking from a pack.
|
|
|
|
const filteredStickers = reject(
|
|
|
|
values(stickers),
|
|
|
|
sticker => sticker.isCoverOnly
|
|
|
|
);
|
|
|
|
const translatedStickers = map(filteredStickers, sticker =>
|
2024-07-11 19:44:09 +00:00
|
|
|
translateStickerFromDB(sticker, isEphemeral)
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...pack,
|
|
|
|
isBlessed: Boolean(blessedPacks[id]),
|
2024-07-11 19:44:09 +00:00
|
|
|
cover: getSticker(packs, id, coverStickerId),
|
2019-05-16 22:32:11 +00:00
|
|
|
stickers: sortBy(translatedStickers, sticker => sticker.id),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const filterAndTransformPacks = (
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
packFilter: (sticker: StickerPackDBType) => boolean,
|
2021-07-09 19:36:10 +00:00
|
|
|
packSort: (sticker: StickerPackDBType) => number | undefined,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
const list = filter(packs, packFilter);
|
|
|
|
const sorted = orderBy<StickerPackDBType>(list, packSort, ['desc']);
|
|
|
|
|
2024-07-11 19:44:09 +00:00
|
|
|
return sorted.map(pack => translatePackFromDB(pack, packs, blessedPacks));
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getStickers = (state: StateType) => state.stickers;
|
|
|
|
|
|
|
|
export const getPacks = createSelector(
|
|
|
|
getStickers,
|
|
|
|
(stickers: StickersStateType) => stickers.packs
|
|
|
|
);
|
|
|
|
|
|
|
|
const getRecents = createSelector(
|
|
|
|
getStickers,
|
|
|
|
(stickers: StickersStateType) => stickers.recentStickers
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getBlessedPacks = createSelector(
|
|
|
|
getStickers,
|
|
|
|
(stickers: StickersStateType) => stickers.blessedPacks
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getRecentStickers = createSelector(
|
|
|
|
getRecents,
|
|
|
|
getPacks,
|
|
|
|
(
|
2023-01-13 20:07:26 +00:00
|
|
|
recents: ReadonlyArray<RecentStickerType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
packs: Dictionary<StickerPackDBType>
|
2019-05-16 22:32:11 +00:00
|
|
|
) => {
|
|
|
|
return compact(
|
|
|
|
recents.map(({ packId, stickerId }) => {
|
2024-07-11 19:44:09 +00:00
|
|
|
return getSticker(packs, packId, stickerId);
|
2019-05-16 22:32:11 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getInstalledStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => pack.status === 'installed',
|
|
|
|
pack => pack.installedAt,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getRecentlyInstalledStickerPack = createSelector(
|
|
|
|
getInstalledStickerPacks,
|
|
|
|
getStickers,
|
|
|
|
(packs, { installedPack: packId }) => {
|
|
|
|
if (!packId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return packs.find(({ id }) => id === packId) || null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getReceivedStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack =>
|
2019-05-24 01:27:42 +00:00
|
|
|
(pack.status === 'downloaded' || pack.status === 'pending') &&
|
2019-05-16 22:32:11 +00:00
|
|
|
!blessedPacks[pack.id],
|
|
|
|
pack => pack.createdAt,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getBlessedStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => blessedPacks[pack.id] && pack.status !== 'installed',
|
|
|
|
pack => pack.createdAt,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks
|
2019-05-24 01:27:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getKnownStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks: Dictionary<boolean>
|
2019-05-24 01:27:42 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => !blessedPacks[pack.id] && pack.status === 'known',
|
|
|
|
pack => pack.createdAt,
|
2024-07-11 19:44:09 +00:00
|
|
|
blessedPacks
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|