Sticker Picker: Support current chat sticker pack (#1880)

This commit is contained in:
Alexander Zinchuk 2022-05-16 13:34:41 +02:00
parent 348ecfdcac
commit e908a3bd17
5 changed files with 53 additions and 21 deletions

View File

@ -44,6 +44,15 @@ import {
import { addEntitiesWithPhotosToLocalDb, addMessageToLocalDb, addPhotoToLocalDb } from '../helpers'; import { addEntitiesWithPhotosToLocalDb, addMessageToLocalDb, addPhotoToLocalDb } from '../helpers';
import { buildApiPeerId, getApiChatIdFromMtpPeer } from '../apiBuilders/peers'; import { buildApiPeerId, getApiChatIdFromMtpPeer } from '../apiBuilders/peers';
import { buildApiPhoto } from '../apiBuilders/common'; import { buildApiPhoto } from '../apiBuilders/common';
import { buildStickerSet } from '../apiBuilders/symbols';
type FullChatData = {
fullInfo: ApiChatFullInfo;
users?: ApiUser[];
userStatusesById: { [userId: string]: ApiUserStatus };
groupCall?: Partial<ApiGroupCall>;
membersCount?: number;
};
const MAX_INT_32 = 2 ** 31 - 1; const MAX_INT_32 = 2 ** 31 - 1;
let onUpdate: OnApiUpdate; let onUpdate: OnApiUpdate;
@ -336,13 +345,7 @@ export function clearDraft(chat: ApiChat) {
})); }));
} }
async function getFullChatInfo(chatId: string): Promise<{ async function getFullChatInfo(chatId: string): Promise<FullChatData | undefined> {
fullInfo: ApiChatFullInfo;
users?: ApiUser[];
userStatusesById?: { [userId: string]: ApiUserStatus };
groupCall?: Partial<ApiGroupCall>;
membersCount?: number;
} | undefined> {
const result = await invokeRequest(new GramJs.messages.GetFullChat({ const result = await invokeRequest(new GramJs.messages.GetFullChat({
chatId: buildInputEntity(chatId) as BigInt.BigInteger, chatId: buildInputEntity(chatId) as BigInt.BigInteger,
})); }));
@ -404,13 +407,7 @@ async function getFullChannelInfo(
id: string, id: string,
accessHash: string, accessHash: string,
adminRights?: ApiChatAdminRights, adminRights?: ApiChatAdminRights,
): Promise<{ ): Promise<FullChatData | undefined> {
fullInfo: ApiChatFullInfo;
users?: ApiUser[];
userStatusesById: { [userId: string]: ApiUserStatus };
groupCall?: Partial<ApiGroupCall>;
membersCount?: number;
} | undefined> {
const result = await invokeRequest(new GramJs.channels.GetFullChannel({ const result = await invokeRequest(new GramJs.channels.GetFullChannel({
channel: buildInputEntity(id, accessHash) as GramJs.InputChannel, channel: buildInputEntity(id, accessHash) as GramJs.InputChannel,
})); }));
@ -439,6 +436,7 @@ async function getFullChannelInfo(
recentRequesters, recentRequesters,
statsDc, statsDc,
participantsCount, participantsCount,
stickerset,
} = result.fullChat; } = result.fullChat;
const inviteLink = exportedInvite instanceof GramJs.ChatInviteExported const inviteLink = exportedInvite instanceof GramJs.ChatInviteExported
@ -501,6 +499,7 @@ async function getFullChannelInfo(
requestsPending, requestsPending,
recentRequesterIds: recentRequesters?.map((userId) => buildApiPeerId(userId, 'user')), recentRequesterIds: recentRequesters?.map((userId) => buildApiPeerId(userId, 'user')),
statisticsDcId: statsDc, statisticsDcId: statsDc,
stickerSet: stickerset ? buildStickerSet(stickerset) : undefined,
}, },
users: [...(users || []), ...(bannedUsers || []), ...(adminUsers || [])], users: [...(users || []), ...(bannedUsers || []), ...(adminUsers || [])],
userStatusesById: statusesById, userStatusesById: statusesById,

View File

@ -1,4 +1,4 @@
import { ApiMessage, ApiPhoto } from './messages'; import { ApiMessage, ApiPhoto, ApiStickerSet } from './messages';
import { ApiBotCommand } from './bots'; import { ApiBotCommand } from './bots';
import { ApiChatInviteImporter } from './misc'; import { ApiChatInviteImporter } from './misc';
import { ApiFakeType } from './users'; import { ApiFakeType } from './users';
@ -104,6 +104,7 @@ export interface ApiChatFullInfo {
recentRequesterIds?: string[]; recentRequesterIds?: string[];
requestsPending?: number; requestsPending?: number;
statisticsDcId?: number; statisticsDcId?: number;
stickerSet?: ApiStickerSet;
} }
export interface ApiChatMember { export interface ApiChatMember {

View File

@ -3,10 +3,11 @@ import React, {
} from '../../../lib/teact/teact'; } from '../../../lib/teact/teact';
import { getActions, withGlobal } from '../../../global'; import { getActions, withGlobal } from '../../../global';
import { ApiStickerSet, ApiSticker } from '../../../api/types'; import { ApiStickerSet, ApiSticker, ApiChat } from '../../../api/types';
import { StickerSetOrRecent } from '../../../types'; import { StickerSetOrRecent } from '../../../types';
import { import {
CHAT_STICKER_SET_ID,
FAVORITE_SYMBOL_SET_ID, RECENT_SYMBOL_SET_ID, SLIDE_TRANSITION_DURATION, STICKER_SIZE_PICKER_HEADER, FAVORITE_SYMBOL_SET_ID, RECENT_SYMBOL_SET_ID, SLIDE_TRANSITION_DURATION, STICKER_SIZE_PICKER_HEADER,
} from '../../../config'; } from '../../../config';
import { IS_TOUCH_ENV } from '../../../util/environment'; import { IS_TOUCH_ENV } from '../../../util/environment';
@ -14,7 +15,8 @@ import { MEMO_EMPTY_ARRAY } from '../../../util/memo';
import fastSmoothScroll from '../../../util/fastSmoothScroll'; import fastSmoothScroll from '../../../util/fastSmoothScroll';
import buildClassName from '../../../util/buildClassName'; import buildClassName from '../../../util/buildClassName';
import fastSmoothScrollHorizontal from '../../../util/fastSmoothScrollHorizontal'; import fastSmoothScrollHorizontal from '../../../util/fastSmoothScrollHorizontal';
import { selectIsChatWithSelf } from '../../../global/selectors'; import { pickTruthy } from '../../../util/iteratees';
import { selectChat, selectIsChatWithSelf } from '../../../global/selectors';
import useAsyncRendering from '../../right/hooks/useAsyncRendering'; import useAsyncRendering from '../../right/hooks/useAsyncRendering';
import { useIntersectionObserver } from '../../../hooks/useIntersectionObserver'; import { useIntersectionObserver } from '../../../hooks/useIntersectionObserver';
@ -22,6 +24,7 @@ import useHorizontalScroll from '../../../hooks/useHorizontalScroll';
import useLang from '../../../hooks/useLang'; import useLang from '../../../hooks/useLang';
import useSendMessageAction from '../../../hooks/useSendMessageAction'; import useSendMessageAction from '../../../hooks/useSendMessageAction';
import Avatar from '../../common/Avatar';
import Loading from '../../ui/Loading'; import Loading from '../../ui/Loading';
import Button from '../../ui/Button'; import Button from '../../ui/Button';
import StickerButton from '../../common/StickerButton'; import StickerButton from '../../common/StickerButton';
@ -41,6 +44,7 @@ type OwnProps = {
}; };
type StateProps = { type StateProps = {
chat?: ApiChat;
recentStickers: ApiSticker[]; recentStickers: ApiSticker[];
favoriteStickers: ApiSticker[]; favoriteStickers: ApiSticker[];
stickerSetsById: Record<string, ApiStickerSet>; stickerSetsById: Record<string, ApiStickerSet>;
@ -56,7 +60,7 @@ const STICKER_INTERSECTION_THROTTLE = 200;
const stickerSetIntersections: boolean[] = []; const stickerSetIntersections: boolean[] = [];
const StickerPicker: FC<OwnProps & StateProps> = ({ const StickerPicker: FC<OwnProps & StateProps> = ({
chatId, chat,
threadId, threadId,
className, className,
loadAndPlay, loadAndPlay,
@ -82,7 +86,8 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
const headerRef = useRef<HTMLDivElement>(null); const headerRef = useRef<HTMLDivElement>(null);
const [activeSetIndex, setActiveSetIndex] = useState<number>(0); const [activeSetIndex, setActiveSetIndex] = useState<number>(0);
const sendMessageAction = useSendMessageAction(chatId, threadId);
const sendMessageAction = useSendMessageAction(chat!.id, threadId);
const { observe: observeIntersection } = useIntersectionObserver({ const { observe: observeIntersection } = useIntersectionObserver({
rootRef: containerRef, rootRef: containerRef,
@ -139,11 +144,23 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
}); });
} }
if (chat?.fullInfo?.stickerSet) {
const fullSet = stickerSetsById[chat.fullInfo.stickerSet.id];
if (fullSet) {
defaultSets.push({
id: CHAT_STICKER_SET_ID,
title: lang('GroupStickers'),
stickers: fullSet.stickers,
count: fullSet.stickers!.length,
});
}
}
return [ return [
...defaultSets, ...defaultSets,
...addedSetIds.map((id) => stickerSetsById[id]).filter(Boolean), ...Object.values(pickTruthy(stickerSetsById, addedSetIds)),
]; ];
}, [addedSetIds, lang, recentStickers, favoriteStickers, stickerSetsById]); }, [addedSetIds, favoriteStickers, recentStickers, chat, lang, stickerSetsById]);
const noPopulatedSets = useMemo(() => ( const noPopulatedSets = useMemo(() => (
areAddedLoaded areAddedLoaded
@ -213,6 +230,7 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
if (stickerSet.id === RECENT_SYMBOL_SET_ID if (stickerSet.id === RECENT_SYMBOL_SET_ID
|| stickerSet.id === FAVORITE_SYMBOL_SET_ID || stickerSet.id === FAVORITE_SYMBOL_SET_ID
|| stickerSet.id === CHAT_STICKER_SET_ID
|| stickerSet.hasThumbnail || stickerSet.hasThumbnail
|| !firstSticker) { || !firstSticker) {
return ( return (
@ -230,6 +248,8 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
<i className="icon-recent" /> <i className="icon-recent" />
) : stickerSet.id === FAVORITE_SYMBOL_SET_ID ? ( ) : stickerSet.id === FAVORITE_SYMBOL_SET_ID ? (
<i className="icon-favorite" /> <i className="icon-favorite" />
) : stickerSet.id === CHAT_STICKER_SET_ID ? (
<Avatar chat={chat} size="small" />
) : stickerSet.isLottie ? ( ) : stickerSet.isLottie ? (
<StickerSetCoverAnimated <StickerSetCoverAnimated
stickerSet={stickerSet as ApiStickerSet} stickerSet={stickerSet as ApiStickerSet}
@ -320,8 +340,10 @@ export default memo(withGlobal<OwnProps>(
} = global.stickers; } = global.stickers;
const isSavedMessages = selectIsChatWithSelf(global, chatId); const isSavedMessages = selectIsChatWithSelf(global, chatId);
const chat = selectChat(global, chatId);
return { return {
chat,
recentStickers: recent.stickers, recentStickers: recent.stickers,
favoriteStickers: favorite.stickers, favoriteStickers: favorite.stickers,
stickerSetsById: setsById, stickerSetsById: setsById,

View File

@ -141,6 +141,7 @@ export const RECENT_STICKERS_LIMIT = 20;
export const NO_STICKER_SET_ID = 'NO_STICKER_SET'; export const NO_STICKER_SET_ID = 'NO_STICKER_SET';
export const RECENT_SYMBOL_SET_ID = 'recent'; export const RECENT_SYMBOL_SET_ID = 'recent';
export const FAVORITE_SYMBOL_SET_ID = 'favorite'; export const FAVORITE_SYMBOL_SET_ID = 'favorite';
export const CHAT_STICKER_SET_ID = 'chatStickers';
export const BASE_EMOJI_KEYWORD_LANG = 'en'; export const BASE_EMOJI_KEYWORD_LANG = 'en';

View File

@ -1135,6 +1135,15 @@ export async function loadFullChat(chat: ApiChat) {
setGlobal(global); setGlobal(global);
const stickerSet = fullInfo.stickerSet;
if (stickerSet) {
getActions().loadStickers({
stickerSetId: stickerSet.id,
stickerSetAccessHash: stickerSet.accessHash,
stickerSetShortName: stickerSet.shortName,
});
}
return result; return result;
} }