2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
import { join } from 'path';
|
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';
|
|
|
|
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';
|
2019-05-24 01:27:42 +00:00
|
|
|
import { getStickersPath, getTempPath } from './user';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const getSticker = (
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
packId: string,
|
|
|
|
stickerId: number,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickerPath: string,
|
|
|
|
tempPath: string
|
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';
|
|
|
|
|
|
|
|
return translateStickerFromDB(sticker, stickerPath, tempPath, isEphemeral);
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const translateStickerFromDB = (
|
|
|
|
sticker: StickerDBType,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickerPath: string,
|
|
|
|
tempPath: string,
|
|
|
|
isEphemeral: boolean
|
2019-05-16 22:32:11 +00:00
|
|
|
): StickerType => {
|
|
|
|
const { id, packId, emoji, path } = sticker;
|
2019-05-24 01:27:42 +00:00
|
|
|
const prefix = isEphemeral ? tempPath : stickerPath;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
packId,
|
|
|
|
emoji,
|
2019-05-24 01:27:42 +00:00
|
|
|
url: join(prefix, path),
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const translatePackFromDB = (
|
|
|
|
pack: StickerPackDBType,
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
blessedPacks: Dictionary<boolean>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
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 =>
|
2019-05-24 01:27:42 +00:00
|
|
|
translateStickerFromDB(sticker, stickersPath, tempPath, isEphemeral)
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...pack,
|
|
|
|
isBlessed: Boolean(blessedPacks[id]),
|
2019-05-24 01:27:42 +00:00
|
|
|
cover: getSticker(packs, id, coverStickerId, stickersPath, tempPath),
|
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,
|
2019-05-16 22:32:11 +00:00
|
|
|
blessedPacks: Dictionary<boolean>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
const list = filter(packs, packFilter);
|
|
|
|
const sorted = orderBy<StickerPackDBType>(list, packSort, ['desc']);
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
return sorted.map(pack =>
|
|
|
|
translatePackFromDB(pack, packs, blessedPacks, stickersPath, tempPath)
|
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,
|
|
|
|
getStickersPath,
|
2019-05-24 01:27:42 +00:00
|
|
|
getTempPath,
|
2019-05-16 22:32:11 +00:00
|
|
|
(
|
|
|
|
recents: Array<RecentStickerType>,
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
2019-05-16 22:32:11 +00:00
|
|
|
) => {
|
|
|
|
return compact(
|
|
|
|
recents.map(({ packId, stickerId }) => {
|
2019-05-24 01:27:42 +00:00
|
|
|
return getSticker(packs, packId, stickerId, stickersPath, tempPath);
|
2019-05-16 22:32:11 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getInstalledStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
getStickersPath,
|
2019-05-24 01:27:42 +00:00
|
|
|
getTempPath,
|
2019-05-16 22:32:11 +00:00
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
blessedPacks: Dictionary<boolean>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => pack.status === 'installed',
|
|
|
|
pack => pack.installedAt,
|
|
|
|
blessedPacks,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath,
|
|
|
|
tempPath
|
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,
|
|
|
|
getStickersPath,
|
2019-05-24 01:27:42 +00:00
|
|
|
getTempPath,
|
2019-05-16 22:32:11 +00:00
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
blessedPacks: Dictionary<boolean>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
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,
|
|
|
|
blessedPacks,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath,
|
|
|
|
tempPath
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getBlessedStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
getStickersPath,
|
2019-05-24 01:27:42 +00:00
|
|
|
getTempPath,
|
2019-05-16 22:32:11 +00:00
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
blessedPacks: Dictionary<boolean>,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
2019-05-16 22:32:11 +00:00
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => blessedPacks[pack.id] && pack.status !== 'installed',
|
|
|
|
pack => pack.createdAt,
|
|
|
|
blessedPacks,
|
2019-05-24 01:27:42 +00:00
|
|
|
stickersPath,
|
|
|
|
tempPath
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getKnownStickerPacks = createSelector(
|
|
|
|
getPacks,
|
|
|
|
getBlessedPacks,
|
|
|
|
getStickersPath,
|
|
|
|
getTempPath,
|
|
|
|
(
|
|
|
|
packs: Dictionary<StickerPackDBType>,
|
|
|
|
blessedPacks: Dictionary<boolean>,
|
|
|
|
stickersPath: string,
|
|
|
|
tempPath: string
|
|
|
|
): Array<StickerPackType> => {
|
|
|
|
return filterAndTransformPacks(
|
|
|
|
packs,
|
|
|
|
pack => !blessedPacks[pack.id] && pack.status === 'known',
|
|
|
|
pack => pack.createdAt,
|
|
|
|
blessedPacks,
|
|
|
|
stickersPath,
|
|
|
|
tempPath
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|