Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5716 lines
196 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2 years ago
import type {AppImManager, ChatSavedPosition} from '../../lib/appManagers/appImManager';
import type {HistoryResult, MyMessage} from '../../lib/appManagers/appMessagesManager';
import type {MyDocument} from '../../lib/appManagers/appDocsManager';
import type Chat from './chat';
import {CHAT_ANIMATION_GROUP} from '../../lib/appManagers/appImManager';
import IS_TOUCH_SUPPORTED from '../../environment/touchSupport';
import {logger} from '../../lib/logger';
import rootScope from '../../lib/rootScope';
import BubbleGroups from './bubbleGroups';
import PopupDatePicker from '../popups/datePicker';
import PopupForward from '../popups/forward';
import PopupStickers from '../popups/stickers';
import ProgressivePreloader from '../preloader';
import Scrollable, {SliceSides} from '../scrollable';
import StickyIntersector from '../stickyIntersector';
import animationIntersector from '../animationIntersector';
import mediaSizes from '../../helpers/mediaSizes';
import {IS_ANDROID, IS_APPLE, IS_MOBILE, IS_SAFARI} from '../../environment/userAgent';
import I18n, {FormatterArguments, i18n, langPack, LangPackKey, UNSUPPORTED_LANG_PACK_KEY, _i18n} from '../../lib/langPack';
import AvatarElement from '../avatar';
import ripple from '../ripple';
import {wrapAlbum, wrapPhoto, wrapVideo, wrapDocument, wrapSticker, wrapPoll, wrapGroupedDocuments} from '../wrappers';
import {MessageRender} from './messageRender';
import LazyLoadQueue from '../lazyLoadQueue';
import ListenerSetter from '../../helpers/listenerSetter';
import PollElement from '../poll';
import AudioElement from '../audio';
import {ChatInvite, Document, Message, MessageEntity, MessageMedia, MessageReplyHeader, Photo, PhotoSize, ReactionCount, ReplyMarkup, SponsoredMessage, Update, User, WebPage} from '../../layer';
import {BOT_START_PARAM, NULL_PEER_ID, REPLIES_PEER_ID} from '../../lib/mtproto/mtproto_config';
import {FocusDirection, ScrollStartCallbackDimensions} from '../../helpers/fastSmoothScroll';
import useHeavyAnimationCheck, {getHeavyAnimationPromise, dispatchHeavyAnimationEvent, interruptHeavyAnimation} from '../../hooks/useHeavyAnimationCheck';
import {fastRaf, fastRafPromise} from '../../helpers/schedulers';
import deferredPromise from '../../helpers/cancellablePromise';
import RepliesElement from './replies';
import DEBUG from '../../config/debug';
import {SliceEnd} from '../../helpers/slicedArray';
import PeerTitle from '../peerTitle';
import findUpClassName from '../../helpers/dom/findUpClassName';
import findUpTag from '../../helpers/dom/findUpTag';
import {toast, toastNew} from '../toast';
import {getElementByPoint} from '../../helpers/dom/getElementByPoint';
import {getMiddleware} from '../../helpers/middleware';
import cancelEvent from '../../helpers/dom/cancelEvent';
import {attachClickEvent, simulateClickEvent} from '../../helpers/dom/clickEvent';
import htmlToDocumentFragment from '../../helpers/dom/htmlToDocumentFragment';
import reflowScrollableElement from '../../helpers/dom/reflowScrollableElement';
import replaceContent from '../../helpers/dom/replaceContent';
import setInnerHTML from '../../helpers/dom/setInnerHTML';
import whichChild from '../../helpers/dom/whichChild';
import {cancelAnimationByKey} from '../../helpers/animation';
import assumeType from '../../helpers/assumeType';
import debounce, {DebounceReturnType} from '../../helpers/schedulers/debounce';
import {SEND_WHEN_ONLINE_TIMESTAMP} from '../../lib/mtproto/constants';
import windowSize from '../../helpers/windowSize';
import {formatPhoneNumber} from '../../helpers/formatPhoneNumber';
import AppMediaViewer from '../appMediaViewer';
import SetTransition from '../singleTransition';
import handleHorizontalSwipe from '../../helpers/dom/handleHorizontalSwipe';
import findUpAttribute from '../../helpers/dom/findUpAttribute';
import findUpAsChild from '../../helpers/dom/findUpAsChild';
import formatCallDuration from '../../helpers/formatCallDuration';
import IS_CALL_SUPPORTED from '../../environment/callSupport';
import Button from '../button';
import {CallType} from '../../lib/calls/types';
import getVisibleRect from '../../helpers/dom/getVisibleRect';
import PopupJoinChatInvite from '../popups/joinChatInvite';
import {InternalLink, INTERNAL_LINK_TYPE} from '../../lib/appManagers/internalLink';
import ReactionsElement, {REACTIONS_ELEMENTS} from './reactions';
import type ReactionElement from './reaction';
import RLottiePlayer from '../../lib/rlottie/rlottiePlayer';
import pause from '../../helpers/schedulers/pause';
import ScrollSaver from '../../helpers/scrollSaver';
import getObjectKeysAndSort from '../../helpers/object/getObjectKeysAndSort';
import forEachReverse from '../../helpers/array/forEachReverse';
import formatNumber from '../../helpers/number/formatNumber';
import getViewportSlice from '../../helpers/dom/getViewportSlice';
import SuperIntersectionObserver from '../../helpers/dom/superIntersectionObserver';
import generateFakeIcon from '../generateFakeIcon';
import copyFromElement from '../../helpers/dom/copyFromElement';
import PopupElement from '../popups';
import setAttachmentSize from '../../helpers/setAttachmentSize';
import wrapWebPageDescription from '../wrappers/webPageDescription';
import wrapWebPageTitle from '../wrappers/webPageTitle';
import wrapEmojiText from '../../lib/richTextProcessor/wrapEmojiText';
import wrapRichText from '../../lib/richTextProcessor/wrapRichText';
import wrapMessageActionTextNew from '../wrappers/messageActionTextNew';
import isMentionUnread from '../../lib/appManagers/utils/messages/isMentionUnread';
import getMediaFromMessage from '../../lib/appManagers/utils/messages/getMediaFromMessage';
import getPeerColorById from '../../lib/appManagers/utils/peers/getPeerColorById';
import getPeerId from '../../lib/appManagers/utils/peers/getPeerId';
import getServerMessageId from '../../lib/appManagers/utils/messageId/getServerMessageId';
import generateMessageId from '../../lib/appManagers/utils/messageId/generateMessageId';
import {AppManagers} from '../../lib/appManagers/managers';
import {Awaited} from '../../types';
import idleController from '../../helpers/idleController';
import overlayCounter from '../../helpers/overlayCounter';
import {cancelContextMenuOpening} from '../../helpers/dom/attachContextMenuListener';
import contextMenuController from '../../helpers/contextMenuController';
import {AckedResult} from '../../lib/mtproto/superMessagePort';
import middlewarePromise from '../../helpers/middlewarePromise';
import {EmoticonsDropdown} from '../emoticonsDropdown';
import indexOfAndSplice from '../../helpers/array/indexOfAndSplice';
import noop from '../../helpers/noop';
import getAlbumText from '../../lib/appManagers/utils/messages/getAlbumText';
import paymentsWrapCurrencyAmount from '../../helpers/paymentsWrapCurrencyAmount';
import PopupPayment from '../popups/payment';
3 years ago
const USE_MEDIA_TAILS = false;
const IGNORE_ACTIONS: Set<Message.messageService['action']['_']> = new Set([
'messageActionHistoryClear',
'messageActionChatCreate'/* ,
'messageActionChannelMigrateFrom' */
]);
export const SERVICE_AS_REGULAR: Set<Message.messageService['action']['_']> = new Set();
if(IS_CALL_SUPPORTED) {
SERVICE_AS_REGULAR.add('messageActionPhoneCall');
}
3 years ago
const TEST_SCROLL_TIMES: number = undefined;
let TEST_SCROLL = TEST_SCROLL_TIMES;
let queueId = 0;
type GenerateLocalMessageType<IsService> = IsService extends true ? Message.messageService : Message.message;
const SPONSORED_MESSAGE_ID_OFFSET = 1;
export const STICKY_OFFSET = 3;
const SCROLLED_DOWN_THRESHOLD = 300;
const PEER_CHANGED_ERROR = new Error('peer changed');
const DO_NOT_SLICE_VIEWPORT = false;
const DO_NOT_SLICE_VIEWPORT_ON_RENDER = false;
const DO_NOT_UPDATE_MESSAGE_VIEWS = false;
const DO_NOT_UPDATE_MESSAGE_REACTIONS = false;
const DO_NOT_UPDATE_MESSAGE_REPLY = false;
type Bubble = {
2 years ago
bubble: HTMLElement,
mids: Set<number>,
groupedId?: string
};
type MyHistoryResult = HistoryResult | {history: number[]};
function getMainMidForGrouped(mids: number[]) {
return Math.max(...mids);
}
3 years ago
export default class ChatBubbles {
public container: HTMLDivElement;
public chatInner: HTMLDivElement;
3 years ago
public scrollable: Scrollable;
private getHistoryTopPromise: Promise<boolean>;
private getHistoryBottomPromise: Promise<boolean>;
2 years ago
// public messagesCount: number = -1;
3 years ago
private unreadOut = new Set<number>();
3 years ago
public needUpdate: {replyToPeerId: PeerId, replyMid: number, mid: number}[] = []; // if need wrapSingleMessage
3 years ago
public bubbles: {[mid: string]: HTMLElement} = {};
3 years ago
public skippedMids: Set<number> = new Set();
public bubblesNewByGroupedId: {[groupId: string]: Bubble} = {};
public bubblesNew: {[mid: string]: Bubble} = {};
2 years ago
private dateMessages: {[timestamp: number]: {
div: HTMLElement,
firstTimestamp: number,
container: HTMLElement,
2 years ago
timeout?: number
3 years ago
}} = {};
private scrolledDown = true;
private isScrollingTimeout = 0;
private stickyIntersector: StickyIntersector;
private unreaded: Map<HTMLElement, number> = new Map();
private unreadedSeen: Set<number> = new Set();
private readPromise: Promise<void>;
private bubbleGroups: BubbleGroups;
private preloader: ProgressivePreloader = null;
public messagesQueuePromise: Promise<void> = null;
private messagesQueue: ReturnType<ChatBubbles['safeRenderMessage']>[] = [];
// private messagesQueueOnRender: () => void = null;
3 years ago
private messagesQueueOnRenderAdditional: () => void = null;
private firstUnreadBubble: HTMLElement = null;
3 years ago
private attachedUnreadBubble: boolean;
public lazyLoadQueue: LazyLoadQueue;
private middleware = getMiddleware();
private log: ReturnType<typeof logger>;
public listenerSetter: ListenerSetter;
private replyFollowHistory: number[] = [];
private isHeavyAnimationInProgress = false;
3 years ago
private scrollingToBubble: HTMLElement;
3 years ago
private isFirstLoad = true;
private needReflowScroll: boolean;
private fetchNewPromise: Promise<void>;
private passEntities: Partial<{
[_ in MessageEntity['_']]: boolean
}> = {};
private onAnimateLadder: () => Promise<any> | void;
// private ladderDeferred: CancellablePromise<void>;
private resolveLadderAnimation: () => Promise<any>;
private emptyPlaceholderBubble: HTMLElement;
3 years ago
private viewsMids: Set<number> = new Set();
private sendViewCountersDebounced: () => Promise<void>;
private isTopPaddingSet = false;
private getSponsoredMessagePromise: Promise<void>;
private previousStickyDate: HTMLElement;
private sponsoredMessage: SponsoredMessage.sponsoredMessage;
2 years ago
2 years ago
private hoverBubble: HTMLElement;
private hoverReaction: HTMLElement;
private sliceViewportDebounced: DebounceReturnType<ChatBubbles['sliceViewport']>;
private resizeObserver: ResizeObserver;
private willScrollOnLoad: boolean;
private observer: SuperIntersectionObserver;
2 years ago
private renderingMessages: Set<number> = new Set();
private setPeerCached: boolean;
private attachPlaceholderOnRender: () => void;
private bubblesToEject: Set<HTMLElement> = new Set();
private bubblesToReplace: Map<HTMLElement, HTMLElement> = new Map(); // TO -> FROM
private updatePlaceholderPosition: () => void;
private setPeerOptions: {lastMsgId: number; topMessage: number;};
private setPeerTempId: number = 0;
private renderNewPromises: Set<Promise<any>> = new Set();
2 years ago
// private reactions: Map<number, ReactionsElement>;
3 years ago
constructor(
2 years ago
private chat: Chat,
private managers: AppManagers
3 years ago
) {
this.log = this.chat.log;
2 years ago
// this.chat.log.error('Bubbles construction');
3 years ago
this.listenerSetter = new ListenerSetter();
this.constructBubbles();
3 years ago
// * constructor end
this.bubbleGroups = new BubbleGroups(this.chat);
this.preloader = new ProgressivePreloader({
cancelable: false
});
this.lazyLoadQueue = new LazyLoadQueue();
this.lazyLoadQueue.queueId = ++queueId;
2 years ago
// this.reactions = new Map();
3 years ago
// * events
// will call when sent for update pos
this.listenerSetter.add(rootScope)('history_update', async({storageKey, sequential, message}) => {
if(this.chat.messagesStorageKey !== storageKey || this.chat.type === 'scheduled') {
return;
}
3 years ago
const {mid} = message;
const log = false ? this.log.bindPrefix('history_update-' + mid) : undefined;
log && log('start');
const bubble = this.bubbles[mid];
if(!bubble) return;
if(this.renderNewPromises.size) {
log && log.error('will await new messages render');
await Promise.all(Array.from(this.renderNewPromises));
}
if(this.messagesQueuePromise) {
log && log.error('messages render in process');
await this.messagesQueuePromise;
}
if(this.bubbles[mid] !== bubble) return;
// await getHeavyAnimationPromise();
2 years ago
const item = this.bubbleGroups.getItemByBubble(bubble);
if(!item) { // probably a group item
log && log.error('no item by bubble', bubble);
return;
} else if(item.mid === mid) {
log && log.warn('wow what', item, mid);
return;
}
if(sequential) {
const group = item.group;
const newItem = this.bubbleGroups.createItem(bubble, message);
// newItem.mid = item.mid;
const _items = this.bubbleGroups.itemsArr.slice();
indexOfAndSplice(_items, item);
const foundItem = this.bubbleGroups.findGroupSiblingByItem(newItem, _items);
if(
2 years ago
group === foundItem?.group ||
(group === this.bubbleGroups.getLastGroup() && group.items.length === 1 && newItem.dateTimestamp === item.dateTimestamp) ||
(this.peerId === rootScope.myId && sequential && newItem.dateTimestamp === item.dateTimestamp)
) {
log && log('item has correct position', item);
this.bubbleGroups.changeBubbleMid(bubble, mid);
return;
}
}
// return;
// await fastRafPromise();
// if(this.bubbles[mid] !== bubble) return;
3 years ago
// const groupIndex = this.bubbleGroups.groups.indexOf(group);
this.bubbleGroups.removeAndUnmountBubble(bubble);
// if(!group.items.length) { // group has collapsed, next message can have higher mid so have to reposition them too
// log && log('group has collapsed', item);
// const siblingGroups = this.bubbleGroups.groups.slice(0, groupIndex + 1);
// for(let length = siblingGroups.length, i = length - 2; i >= 0; --i) {
// const siblingGroup = siblingGroups[i];
// const siblingItems = siblingGroup.items;
// const nextGroup = siblingGroups[i + 1];
// const nextItems = nextGroup.items;
// let _break = false, moved = false;
// for(let j = siblingItems.length - 1; j >= 0; --j) {
// const siblingItem = siblingItems[j];
// const foundItem = this.bubbleGroups.findGroupSiblingByItem(siblingItem, nextItems);
// if(!foundItem) {
// _break = true;
// break;
// }
// log('will move item', siblingItem, nextGroup);
// this.bubbleGroups.removeAndUnmountBubble(siblingItem.bubble);
// this.bubbleGroups.addItemToGroup(siblingItem, nextGroup);
// moved = true;
// }
// if(moved) {
// nextGroup.mount();
// }
// if(_break) {
// break;
// }
// }
// }
3 years ago
const {groups} = this.groupBubbles([{bubble, message}]);
this.bubbleGroups.mountUnmountGroups(groups);
if(this.scrollingToBubble) {
this.scrollToEnd();
}
log && log('end');
// this.bubbleGroups.findIncorrentPositions();
3 years ago
});
this.listenerSetter.add(rootScope)('dialog_flush', ({peerId}) => {
3 years ago
if(this.peerId === peerId) {
this.deleteMessagesByIds(Object.keys(this.bubbles).map((m) => +m));
3 years ago
}
});
// Calls when message successfully sent and we have an id
this.listenerSetter.add(rootScope)('message_sent', async(e) => {
const {storageKey, tempId, tempMessage, mid, message} = e;
3 years ago
// ! can't use peerId to validate here, because id can be the same in 'scheduled' and 'chat' types
if(this.chat.messagesStorageKey !== storageKey) {
3 years ago
return;
}
const bubbles = this.bubbles;
const _bubble = bubbles[tempId];
if(_bubble) {
const bubble = bubbles[tempId];
bubbles[mid] = bubble;
bubble.dataset.mid = '' + mid;
delete bubbles[tempId];
fastRaf(() => {
const mid = +bubble.dataset.mid;
if(bubbles[mid] === bubble && bubble.classList.contains('is-outgoing')) {
bubble.classList.remove('is-sending', 'is-outgoing');
bubble.classList.add((this.peerId === rootScope.myId && this.chat.type !== 'scheduled') || !this.unreadOut.has(mid) ? 'is-read' : 'is-sent');
}
});
}
if(this.unreadOut.has(tempId)) {
this.unreadOut.delete(tempId);
this.unreadOut.add(mid);
}
// * check timing of scheduled message
if(this.chat.type === 'scheduled') {
const timestamp = Date.now() / 1000 | 0;
const maxTimestamp = tempMessage.date - 10;
if(timestamp >= maxTimestamp) {
this.deleteMessagesByIds([mid]);
}
}
if(!_bubble) {
return;
}
let messages: (Message.message | Message.messageService)[], tempIds: number[];
const groupedId = (message as Message.message).grouped_id;
if(groupedId) {
messages = await this.managers.appMessagesManager.getMessagesByAlbum(groupedId);
const mids = messages.map(({mid}) => mid);
if(!mids.length || getMainMidForGrouped(mids) !== mid || bubbles[mid] !== _bubble) {
return;
}
2 years ago
if(bubbles[mid] !== _bubble) {
return;
}
tempIds = (Array.from(_bubble.querySelectorAll('.grouped-item')) as HTMLElement[]).map((el) => +el.dataset.mid);
} else {
messages = [message];
tempIds = [tempId];
}
const reactionsElements = Array.from(_bubble.querySelectorAll('reactions-element')) as ReactionsElement[];
if(reactionsElements.length) {
reactionsElements.forEach((reactionsElement) => {
reactionsElement.changeMessage(message as Message.message);
});
}
(messages as Message.message[]).forEach((message, idx) => {
if(!message) {
return;
}
const tempId = tempIds[idx];
const mid = message.mid;
const bubble: HTMLElement = _bubble.querySelector(`.document-container[data-mid="${mid}"]`) || _bubble;
if(message._ !== 'message') {
return;
2 years ago
}
3 years ago
if(message.replies) {
const repliesElement = _bubble.querySelector('replies-element') as RepliesElement;
3 years ago
if(repliesElement) {
repliesElement.message = message;
repliesElement.init();
}
}
const media = message.media ?? {} as MessageMedia.messageMediaEmpty;
const doc = (media as MessageMedia.messageMediaDocument).document as Document.document;
const poll = (media as MessageMedia.messageMediaPoll).poll;
const webPage = (media as MessageMedia.messageMediaWebPage).webpage as WebPage.webPage;
if(doc) {
3 years ago
const div = bubble.querySelector(`.document-container[data-mid="${tempId}"] .document`);
if(div) {
3 years ago
const container = findUpClassName(div, 'document-container');
if(!tempMessage.media?.document?.thumbs?.length && doc.thumbs?.length) {
getHeavyAnimationPromise().then(async() => {
const timeSpan = div.querySelector('.time');
const newDiv = await wrapDocument({message});
div.replaceWith(newDiv);
2 years ago
if(timeSpan) {
newDiv.querySelector('.document').append(timeSpan);
}
});
3 years ago
}
if(container) {
container.dataset.mid = '' + mid;
}
3 years ago
}
const element = bubble.querySelector(`audio-element[data-mid="${tempId}"], .document[data-doc-id="${tempId}"], .media-round[data-mid="${tempId}"]`) as HTMLElement;
3 years ago
if(element) {
if(element instanceof AudioElement || element.classList.contains('media-round')) {
element.dataset.mid = '' + message.mid;
delete element.dataset.isOutgoing;
(element as any).message = message;
(element as any).onLoad(true);
3 years ago
} else {
element.dataset.docId = '' + doc.id;
(element as any).doc = doc;
3 years ago
}
}
} else if(poll) {
const pollElement = bubble.querySelector('poll-element') as PollElement;
if(pollElement) {
pollElement.message = message;
pollElement.setAttribute('poll-id', '' + poll.id);
pollElement.setAttribute('message-id', '' + mid);
}
} else if(webPage && !bubble.querySelector('.web')) {
3 years ago
getHeavyAnimationPromise().then(() => {
this.safeRenderMessage(message, true, bubble);
3 years ago
this.scrollToBubbleIfLast(bubble);
});
}
2 years ago
// set new mids to album items for mediaViewer
if(groupedId) {
const item = (bubble.querySelector(`.grouped-item[data-mid="${tempId}"]`) as HTMLElement) || bubble; // * it can be .document-container
if(item) {
item.dataset.mid = '' + mid;
}
}
});
});
3 years ago
2 years ago
this.listenerSetter.add(rootScope)('message_edit', async({storageKey, message}) => {
if(storageKey !== this.chat.messagesStorageKey) return;
3 years ago
const bubble = this.bubbles[message.mid];
if(!bubble) return;
3 years ago
2 years ago
await getHeavyAnimationPromise();
if(this.bubbles[message.mid] !== bubble) return;
this.safeRenderMessage(message, true, bubble);
});
3 years ago
this.listenerSetter.add(rootScope)('album_edit', ({peerId, messages, deletedMids}) => {
if(peerId !== this.peerId) return;
2 years ago
const mids = messages.map(({mid}) => mid);
const oldMids = mids.concat(Array.from(deletedMids));
const wasMainMid = getMainMidForGrouped(oldMids);
const bubble = this.bubbles[wasMainMid];
if(!bubble) {
return;
3 years ago
}
const mainMid = getMainMidForGrouped(mids);
const message = messages.find((message) => message.mid === mainMid);
this.safeRenderMessage(message, true, bubble);
3 years ago
});
this.listenerSetter.add(rootScope)('peer_title_edit', async(peerId) => {
if(peerId.isUser()) {
const middleware = this.getMiddleware();
const user = await this.managers.appUsersManager.getUser(peerId.toUserId());
if(!middleware()) return;
const isPremium = user?.pFlags?.premium;
const groups = this.bubbleGroups.groups.filter((group) => group.avatar?.peerId === peerId);
groups.forEach((group) => {
group.avatar.classList.toggle('is-premium', isPremium);
group.avatar.classList.toggle('tgico-star', isPremium);
});
}
});
if(this.chat.type !== 'scheduled' && !DO_NOT_UPDATE_MESSAGE_REACTIONS/* && false */) {
this.listenerSetter.add(rootScope)('messages_reactions', async(arr) => {
let scrollSaver: ScrollSaver;
const a = arr.map(async({message, changedResults}) => {
if(this.peerId !== message.peerId) {
return;
}
const result = await this.getMountedBubble(message.mid, message);
if(!result) {
return;
}
return {bubble: result.bubble, message, changedResults};
});
let top: number;
(await Promise.all(a)).filter(Boolean).forEach(({bubble, message, changedResults}) => {
if(!scrollSaver) {
scrollSaver = this.createScrollSaver(false);
scrollSaver.save();
}
2 years ago
const key = message.peerId + '_' + message.mid;
const set = REACTIONS_ELEMENTS.get(key);
if(set) {
for(const element of set) {
element.update(message, changedResults);
}
} else if(!message.reactions || !message.reactions.results.length) {
return;
} else {
this.appendReactionsElementToBubble(bubble, message, message, changedResults);
}
});
if(scrollSaver) {
scrollSaver.restore();
}
});
}
!DO_NOT_UPDATE_MESSAGE_REPLY && this.listenerSetter.add(rootScope)('messages_downloaded', async({peerId, mids}) => {
const middleware = this.getMiddleware();
await getHeavyAnimationPromise();
if(!middleware()) return;
3 years ago
(mids as number[]).forEach((mid) => {
const needUpdate = this.needUpdate;
const filtered: typeof needUpdate[0][] = [];
forEachReverse(this.needUpdate, (obj, idx) => {
if(obj.replyMid === mid && obj.replyToPeerId === peerId) {
this.needUpdate.splice(idx, 1)[0];
filtered.push(obj);
}
});
filtered.forEach(async({mid, replyMid, replyToPeerId}) => {
const bubble = this.bubbles[mid];
if(!bubble) return;
const message = (await this.chat.getMessage(mid)) as Message.message;
2 years ago
MessageRender.setReply({
chat: this.chat,
bubble,
message
3 years ago
});
});
});
3 years ago
});
attachClickEvent(this.scrollable.container, this.onBubblesClick, {listenerSetter: this.listenerSetter});
// this.listenerSetter.add(this.bubblesContainer)('click', this.onBubblesClick/* , {capture: true, passive: false} */);
2 years ago
this.listenerSetter.add(this.scrollable.container)('mousedown', (e) => {
if(e.button !== 0) return;
2 years ago
const code: HTMLElement = findUpTag(e.target, 'CODE');
if(code) {
cancelEvent(e);
copyFromElement(code);
toastNew({langPackKey: 'TextCopied'});
return;
}
});
2 years ago
/* if(false) */this.stickyIntersector = new StickyIntersector(this.scrollable.container, (stuck, target) => {
for(const timestamp in this.dateMessages) {
const dateMessage = this.dateMessages[timestamp];
if(dateMessage.container === target) {
const dateBubble = dateMessage.div;
// dateMessage.container.classList.add('has-sticky-dates');
// SetTransition(dateBubble, 'kek', stuck, this.previousStickyDate ? 300 : 0);
// if(this.previousStickyDate) {
2 years ago
// dateBubble.classList.add('kek');
// }
dateBubble.classList.toggle('is-sticky', stuck);
if(stuck) {
this.previousStickyDate = dateBubble;
}
break;
}
}
if(this.previousStickyDate) {
// fastRaf(() => {
2 years ago
// this.bubblesContainer.classList.add('has-sticky-dates');
// });
}
});
if(!IS_SAFARI) {
this.sliceViewportDebounced = debounce(this.sliceViewport.bind(this), 3000, false, true);
2 years ago
}
let middleware: ReturnType<ChatBubbles['getMiddleware']>;
useHeavyAnimationCheck(() => {
this.isHeavyAnimationInProgress = true;
this.lazyLoadQueue.lock();
middleware = this.getMiddleware();
3 years ago
// if(this.sliceViewportDebounced) {
// this.sliceViewportDebounced.clearTimeout();
// }
}, () => {
this.isHeavyAnimationInProgress = false;
3 years ago
if(middleware && middleware()) {
this.lazyLoadQueue.unlock();
this.lazyLoadQueue.refresh();
3 years ago
// if(this.sliceViewportDebounced) {
// this.sliceViewportDebounced();
// }
}
3 years ago
middleware = null;
}, this.listenerSetter);
}
private constructBubbles() {
const container = this.container = document.createElement('div');
container.classList.add('bubbles', 'scrolled-down');
const chatInner = this.chatInner = document.createElement('div');
chatInner.classList.add('bubbles-inner');
this.setScroll();
container.append(this.scrollable.container);
}
public attachContainerListeners() {
const container = this.container;
this.chat.contextMenu.attachTo(container);
this.chat.selection.attachListeners(container, new ListenerSetter());
if(DEBUG) {
this.listenerSetter.add(container)('dblclick', async(e) => {
const bubble = findUpClassName(e.target, 'grouped-item') || findUpClassName(e.target, 'bubble');
if(bubble) {
const mid = +bubble.dataset.mid
this.log('debug message:', await this.chat.getMessage(mid));
this.highlightBubble(bubble);
}
3 years ago
});
}
if(this.chat.type !== 'pinned' && this.chat.type !== 'scheduled') {
if(!IS_MOBILE) {
this.listenerSetter.add(container)('dblclick', async(e) => {
2 years ago
if(this.chat.selection.isSelecting ||
!(await this.chat.canSend())) {
return;
}
2 years ago
const target = e.target as HTMLElement;
2 years ago
const bubble = target.classList.contains('bubble') ?
target :
(target.classList.contains('document-selection') ? target.parentElement : null);
if(bubble && !bubble.classList.contains('bubble-first')) {
const mid = +bubble.dataset.mid;
const message = await this.chat.getMessage(mid);
if(message.pFlags.is_outgoing) {
return;
3 years ago
}
2 years ago
this.chat.input.initMessageReply(mid);
3 years ago
}
});
} else if(IS_TOUCH_SUPPORTED) {
const className = 'is-gesturing-reply';
const MAX = 64;
const replyAfter = MAX * .75;
let shouldReply = false;
let target: HTMLElement;
let icon: HTMLElement;
handleHorizontalSwipe({
element: container,
verifyTouchTarget: async(e) => {
if(this.chat.selection.isSelecting || !(await this.chat.canSend())) {
return false;
3 years ago
}
2 years ago
// cancelEvent(e);
target = findUpClassName(e.target, 'bubble');
if(target) {
SetTransition(target, className, true, 250);
void target.offsetLeft; // reflow
2 years ago
if(!icon) {
icon = document.createElement('span');
icon.classList.add('tgico-reply_filled', 'bubble-gesture-reply-icon');
} else {
icon.classList.remove('is-visible');
icon.style.opacity = '';
}
2 years ago
target/* .querySelector('.bubble-content') */.append(icon);
3 years ago
}
2 years ago
return !!target;
},
onSwipe: (xDiff, yDiff) => {
shouldReply = xDiff >= replyAfter;
2 years ago
if(shouldReply && !icon.classList.contains('is-visible')) {
icon.classList.add('is-visible');
}
icon.style.opacity = '' + Math.min(1, xDiff / replyAfter);
2 years ago
const x = -Math.max(0, Math.min(MAX, xDiff));
target.style.transform = `translateX(${x}px)`;
cancelContextMenuOpening();
},
onReset: () => {
const _target = target;
SetTransition(_target, className, false, 250, () => {
if(icon.parentElement === _target) {
icon.classList.remove('is-visible');
icon.remove();
}
});
2 years ago
fastRaf(() => {
_target.style.transform = ``;
2 years ago
if(shouldReply) {
const {mid} = _target.dataset;
this.chat.input.initMessageReply(+mid);
shouldReply = false;
}
});
},
listenerOptions: {capture: true}
});
}
3 years ago
}
3 years ago
}
public constructPeerHelpers() {
// will call when message is sent (only 1)
this.listenerSetter.add(rootScope)('history_append', async({storageKey, message}) => {
if(storageKey !== this.chat.messagesStorageKey) return;
3 years ago
if(!this.scrollable.loadedAll.bottom) {
this.chat.setMessageId();
} else {
this.renderNewMessage(message, true);
3 years ago
}
if(rootScope.settings.animationsEnabled) {
const gradientRenderer = this.chat.gradientRenderer;
if(gradientRenderer) {
gradientRenderer.toNextPosition();
}
}
3 years ago
});
this.listenerSetter.add(rootScope)('history_multiappend', (message) => {
if(this.peerId !== message.peerId) return;
this.renderNewMessage(message);
3 years ago
});
2 years ago
this.listenerSetter.add(rootScope)('history_delete', ({peerId, msgs}) => {
3 years ago
if(peerId === this.peerId) {
3 years ago
this.deleteMessagesByIds(Array.from(msgs));
3 years ago
}
});
this.listenerSetter.add(rootScope)('dialog_unread', ({peerId}) => {
if(peerId === this.peerId) {
3 years ago
this.chat.input.setUnreadCount();
getHeavyAnimationPromise().then(() => {
this.updateUnreadByDialog();
});
3 years ago
}
});
this.listenerSetter.add(rootScope)('dialogs_multiupdate', (dialogs) => {
3 years ago
if(dialogs[this.peerId]) {
this.chat.input.setUnreadCount();
}
});
this.listenerSetter.add(rootScope)('dialog_notify_settings', (dialog) => {
if(this.peerId === dialog.peerId) {
this.chat.input.setUnreadCount();
}
});
this.listenerSetter.add(rootScope)('chat_update', async(chatId) => {
3 years ago
if(this.peerId === chatId.toPeerId(true)) {
3 years ago
const hadRights = this.chatInner.classList.contains('has-rights');
const hasRights = await this.chat.canSend();
3 years ago
if(hadRights !== hasRights) {
const callbacks = await Promise.all([
this.finishPeerChange(),
2 years ago
this.chat.input.finishPeerChange()
]);
callbacks.forEach((callback) => callback());
3 years ago
}
}
});
this.listenerSetter.add(rootScope)('settings_updated', async({key}) => {
if(key === 'settings.emoji.big') {
const middleware = this.getMiddleware();
3 years ago
const mids = getObjectKeysAndSort(this.bubbles, 'desc');
const m = mids.map(async(mid) => {
3 years ago
const bubble = this.bubbles[mid];
if(bubble.classList.contains('can-have-big-emoji')) {
return {bubble, message: await this.chat.getMessage(mid)};
3 years ago
}
});
const awaited = await Promise.all(m);
if(!middleware()) {
return;
3 years ago
}
awaited.forEach(({bubble, message}) => {
if(this.bubbles[message.mid] !== bubble) {
return;
}
2 years ago
this.safeRenderMessage(message, true, bubble);
});
3 years ago
}
});
!DO_NOT_UPDATE_MESSAGE_VIEWS && this.listenerSetter.add(rootScope)('messages_views', (arr) => {
fastRaf(() => {
let scrollSaver: ScrollSaver;
for(const {peerId, views, mid} of arr) {
if(this.peerId !== peerId) continue;
const bubble = this.bubbles[mid];
if(!bubble) continue;
2 years ago
const postViewsElements = Array.from(bubble.querySelectorAll('.post-views')) as HTMLElement[];
if(!postViewsElements.length) continue;
const str = formatNumber(views, 1);
let different = false;
postViewsElements.forEach((postViews) => {
if(different || postViews.textContent !== str) {
if(!scrollSaver) {
scrollSaver = this.createScrollSaver(true);
scrollSaver.save();
}
different = true;
postViews.textContent = str;
}
});
}
if(scrollSaver) {
scrollSaver.restore();
}
});
});
this.observer = new SuperIntersectionObserver({root: this.scrollable.container});
3 years ago
this.listenerSetter.add(this.chat.appImManager)('chat_changing', ({to}) => {
const freeze = to !== this.chat;
const cb = () => {
this.observer.toggleObservingNew(freeze);
};
if(!freeze) {
setTimeout(() => {
cb();
}, 400);
} else {
cb();
}
});
this.sendViewCountersDebounced = debounce(() => {
const mids = [...this.viewsMids];
this.viewsMids.clear();
this.managers.appMessagesManager.incrementMessageViews(this.peerId, mids);
}, 1000, false, true);
}
private get peerId() {
return this.chat.peerId;
}
private createScrollSaver(reverse = true) {
const scrollSaver = new ScrollSaver(this.scrollable, '.bubble:not(.is-date)', reverse);
return scrollSaver;
}
private unreadedObserverCallback = (entry: IntersectionObserverEntry) => {
if(entry.isIntersecting) {
const target = entry.target as HTMLElement;
const mid = this.unreaded.get(target as HTMLElement);
this.onUnreadedInViewport(target, mid);
}
};
private viewsObserverCallback = (entry: IntersectionObserverEntry) => {
if(entry.isIntersecting) {
const mid = +(entry.target as HTMLElement).dataset.mid;
this.observer.unobserve(entry.target, this.viewsObserverCallback);
if(mid) {
this.viewsMids.add(mid);
this.sendViewCountersDebounced();
} else {
const {sponsoredMessage} = this;
if(sponsoredMessage && sponsoredMessage.random_id) {
delete sponsoredMessage.random_id;
this.managers.appChatsManager.viewSponsoredMessage(this.peerId.toChatId(), sponsoredMessage.random_id);
}
}
}
};
private createResizeObserver() {
if(!('ResizeObserver' in window) || this.resizeObserver) {
return;
}
3 years ago
const container = this.scrollable.container;
let wasHeight = 0/* container.offsetHeight */;
let resizing = false;
let skip = false;
let scrolled = 0;
let part = 0;
let rAF = 0;
// let skipNext = true;
3 years ago
const onResizeEnd = () => {
const height = container.offsetHeight;
const isScrolledDown = this.scrollable.isScrolledDown;
if(height !== wasHeight && (!skip || !isScrolledDown)) { // * fix opening keyboard while ESG is active, offsetHeight will change right between 'start' and this first frame
part += wasHeight - height;
}
3 years ago
/* if(DEBUG) {
this.log('resize end', scrolled, part, this.scrollable.scrollTop, height, wasHeight, this.scrollable.isScrolledDown);
} */
3 years ago
if(part) {
this.scrollable.setScrollTopSilently(this.scrollable.scrollTop + Math.round(part));
}
3 years ago
wasHeight = height;
scrolled = 0;
rAF = 0;
part = 0;
resizing = false;
skip = false;
};
3 years ago
const setEndRAF = (single: boolean) => {
if(rAF) window.cancelAnimationFrame(rAF);
rAF = window.requestAnimationFrame(single ? onResizeEnd : () => {
rAF = window.requestAnimationFrame(onResizeEnd);
2 years ago
// this.log('resize after RAF', part);
});
};
3 years ago
const processEntries: ResizeObserverCallback = (entries) => {
/* if(skipNext) {
skipNext = false;
return;
} */
3 years ago
if(skip) {
setEndRAF(false);
return;
}
3 years ago
const entry = entries[0];
const height = entry.contentRect.height;/* Math.ceil(entry.contentRect.height); */
2 years ago
if(!wasHeight) {
wasHeight = height;
return;
}
3 years ago
const realDiff = wasHeight - height;
let diff = realDiff + part;
const _part = diff % 1;
diff -= _part;
3 years ago
if(!resizing) {
resizing = true;
3 years ago
/* if(DEBUG) {
this.log('resize start', realDiff, this.scrollable.scrollTop, this.scrollable.container.offsetHeight, this.scrollable.isScrolledDown);
3 years ago
} */
if(realDiff < 0 && this.scrollable.isScrolledDown) {
2 years ago
// if(isSafari) { // * fix opening keyboard while ESG is active
part = -realDiff;
// }
skip = true;
setEndRAF(false);
return;
3 years ago
}
}
3 years ago
scrolled += diff;
3 years ago
/* if(DEBUG) {
this.log('resize', wasHeight - height, diff, this.scrollable.container.offsetHeight, this.scrollable.isScrolledDown, height, wasHeight);
} */
if(diff) {
const needScrollTop = this.scrollable.scrollTop + diff;
this.scrollable.setScrollTopSilently(needScrollTop);
}
2 years ago
setEndRAF(false);
part = _part;
wasHeight = height;
};
const resizeObserver = this.resizeObserver = new ResizeObserver(processEntries);
resizeObserver.observe(container);
}
private destroyResizeObserver() {
const resizeObserver = this.resizeObserver;
if(!resizeObserver) {
return;
3 years ago
}
resizeObserver.disconnect();
this.resizeObserver = undefined;
3 years ago
}
private onBubblesMouseMove = async(e: MouseEvent) => {
2 years ago
const content = findUpClassName(e.target, 'bubble-content');
if(content && !this.chat.selection.isSelecting) {
const bubble = findUpClassName(content, 'bubble');
if(!this.chat.selection.canSelectBubble(bubble)) {
this.unhoverPrevious();
return;
}
let {hoverBubble, hoverReaction} = this;
if(bubble === hoverBubble) {
return;
}
this.unhoverPrevious();
hoverBubble = this.hoverBubble = bubble;
hoverReaction = this.hoverReaction;
// hoverReaction = contentWrapper.querySelector('.bubble-hover-reaction');
if(!hoverReaction) {
hoverReaction = this.hoverReaction = document.createElement('div');
hoverReaction.classList.add('bubble-hover-reaction');
const stickerWrapper = document.createElement('div');
stickerWrapper.classList.add('bubble-hover-reaction-sticker');
hoverReaction.append(stickerWrapper);
content.append(hoverReaction);
let message = (await this.chat.getMessage(+bubble.dataset.mid)) as Message.message;
message = await this.managers.appMessagesManager.getGroupsFirstMessage(message);
2 years ago
const middleware = this.getMiddleware(() => this.hoverReaction === hoverReaction);
Promise.all([
this.managers.appReactionsManager.getAvailableReactionsByMessage(message),
2 years ago
pause(400)
]).then(([availableReactions]) => {
const availableReaction = availableReactions[0];
if(!availableReaction) {
hoverReaction.remove();
2 years ago
return;
}
wrapSticker({
div: stickerWrapper,
doc: availableReaction.select_animation,
width: 18,
height: 18,
needUpscale: true,
middleware,
group: CHAT_ANIMATION_GROUP,
withThumb: false,
needFadeIn: false
}).then(({render}) => render).then((player) => {
2 years ago
assumeType<RLottiePlayer>(player);
if(!middleware()) {
return;
}
player.addEventListener('firstFrame', () => {
if(!middleware()) {
// debugger;
return;
}
hoverReaction.dataset.loaded = '1';
this.setHoverVisible(hoverReaction, true);
}, {once: true});
attachClickEvent(hoverReaction, (e) => {
cancelEvent(e); // cancel triggering selection
this.managers.appReactionsManager.sendReaction(message, availableReaction.reaction);
2 years ago
this.unhoverPrevious();
}, {listenerSetter: this.listenerSetter});
});
});
} else if(hoverReaction.dataset.loaded) {
this.setHoverVisible(hoverReaction, true);
}
} else {
this.unhoverPrevious();
}
};
public setReactionsHoverListeners() {
this.listenerSetter.add(contextMenuController)('toggle', this.unhoverPrevious);
this.listenerSetter.add(overlayCounter)('change', this.unhoverPrevious);
2 years ago
this.listenerSetter.add(this.chat.selection)('toggle', this.unhoverPrevious);
this.listenerSetter.add(this.container)('mousemove', this.onBubblesMouseMove);
2 years ago
}
private setHoverVisible(hoverReaction: HTMLElement, visible: boolean) {
SetTransition(hoverReaction, 'is-visible', visible, 200, visible ? undefined : () => {
hoverReaction.remove();
}, 2);
}
private unhoverPrevious = () => {
const {hoverBubble, hoverReaction} = this;
if(hoverBubble) {
this.setHoverVisible(hoverReaction, false);
this.hoverBubble = undefined;
this.hoverReaction = undefined;
}
};
public setStickyDateManually() {
return;
const timestamps = Object.keys(this.dateMessages).map((k) => +k).sort((a, b) => b - a);
let lastVisible: HTMLElement;
// if(this.chatInner.classList.contains('is-scrolling')) {
2 years ago
const {scrollTop} = this.scrollable.container;
const isOverflown = scrollTop > 0;
if(isOverflown) {
for(const timestamp of timestamps) {
const dateMessage = this.dateMessages[timestamp];
const visibleRect = getVisibleRect(dateMessage.container, this.scrollable.container);
if(visibleRect && visibleRect.overflow.top) {
lastVisible = dateMessage.div;
} else if(lastVisible) {
break;
}
}
2 years ago
}
// }
if(lastVisible === this.previousStickyDate) {
return;
}
if(lastVisible) {
const needReflow = /* !!this.chat.setPeerPromise || */!this.previousStickyDate;
if(needReflow) {
lastVisible.classList.add('no-transition');
}
lastVisible.classList.add('is-sticky');
if(needReflow) {
void lastVisible.offsetLeft; // reflow
lastVisible.classList.remove('no-transition');
}
}
if(this.previousStickyDate && this.previousStickyDate !== lastVisible) {
this.previousStickyDate.classList.remove('is-sticky');
}
this.previousStickyDate = lastVisible;
}
3 years ago
public getRenderedLength() {
return Object.keys(this.bubbles).length - this.skippedMids.size;
}
private onUnreadedInViewport(target: HTMLElement, mid: number) {
this.unreadedSeen.add(mid);
this.observer.unobserve(target, this.unreadedObserverCallback);
3 years ago
this.unreaded.delete(target);
this.readUnreaded();
}
private readUnreaded() {
if(this.readPromise) return;
const middleware = this.getMiddleware();
this.readPromise = idleController.getFocusPromise().then(async() => {
3 years ago
if(!middleware()) return;
let maxId = Math.max(...Array.from(this.unreadedSeen));
// ? if message with maxId is not rendered ?
if(this.scrollable.loadedAll.bottom) {
const bubblesMaxId = Math.max(...Object.keys(this.bubbles).map((i) => +i));
3 years ago
if(maxId >= bubblesMaxId) {
maxId = Math.max((await this.chat.getHistoryMaxId()) || 0, maxId);
3 years ago
}
}
this.unreaded.forEach((mid, target) => {
if(mid <= maxId) {
this.onUnreadedInViewport(target, mid);
}
});
3 years ago
const readContents: number[] = [];
for(const mid of this.unreadedSeen) {
const message: MyMessage = await this.chat.getMessage(mid);
2 years ago
if(isMentionUnread(message)) {
3 years ago
readContents.push(mid);
}
}
this.managers.appMessagesManager.readMessages(this.peerId, readContents);
3 years ago
3 years ago
this.unreadedSeen.clear();
if(DEBUG) {
this.log('will readHistory by maxId:', maxId);
}
// return;
2 years ago
return this.managers.appMessagesManager.readHistory(this.peerId, maxId, this.chat.threadId).catch((err: any) => {
3 years ago
this.log.error('readHistory err:', err);
this.managers.appMessagesManager.readHistory(this.peerId, maxId, this.chat.threadId);
3 years ago
}).finally(() => {
if(!middleware()) return;
this.readPromise = undefined;
if(this.unreadedSeen.size) {
this.readUnreaded();
}
});
});
}
public constructPinnedHelpers() {
this.listenerSetter.add(rootScope)('peer_pinned_messages', (e) => {
const {peerId, mids, pinned} = e;
if(peerId !== this.peerId) return;
if(mids) {
if(!pinned) {
this.deleteMessagesByIds(mids);
}
}
});
}
public constructScheduledHelpers() {
const onUpdate = async() => {
this.chat.topbar.setTitle((await this.managers.appMessagesManager.getScheduledMessagesStorage(this.peerId)).size);
3 years ago
};
this.listenerSetter.add(rootScope)('scheduled_new', (message) => {
if(message.peerId !== this.peerId) return;
3 years ago
this.renderNewMessage(message);
3 years ago
onUpdate();
});
this.listenerSetter.add(rootScope)('scheduled_delete', ({peerId, mids}) => {
3 years ago
if(peerId !== this.peerId) return;
this.deleteMessagesByIds(mids);
onUpdate();
});
}
public onBubblesClick = async(e: Event) => {
3 years ago
let target = e.target as HTMLElement;
let bubble: HTMLElement = null;
try {
bubble = findUpClassName(target, 'bubble');
} catch(err) {}
2 years ago
if(!bubble && !this.chat.selection.isSelecting) {
const avatar = findUpClassName(target, 'user-avatar');
if(!avatar) {
return;
}
const peerId = avatar.dataset.peerId.toPeerId();
if(peerId !== NULL_PEER_ID) {
this.chat.appImManager.setInnerPeer({peerId});
} else {
toast(I18n.format('HidAccount', true));
}
return;
}
3 years ago
if(bubble.classList.contains('is-date') && findUpClassName(target, 'bubble-content')) {
if(bubble.classList.contains('is-sticky') && !this.chatInner.classList.contains('is-scrolling')) {
return;
}
for(const timestamp in this.dateMessages) {
const d = this.dateMessages[timestamp];
if(d.div === bubble) {
2 years ago
PopupElement.createPopup(PopupDatePicker, new Date(+timestamp), this.onDatePick).show();
3 years ago
break;
}
}
return;
}
if(!IS_TOUCH_SUPPORTED && findUpClassName(target, 'time')) {
this.chat.selection.toggleByElement(bubble);
return;
}
3 years ago
// ! Trusted - due to audio autoclick
if(this.chat.selection.isSelecting && e.isTrusted) {
if(bubble.classList.contains('service') && bubble.dataset.mid === undefined) {
return;
}
cancelEvent(e);
2 years ago
// console.log('bubble click', e);
3 years ago
if(IS_TOUCH_SUPPORTED && this.chat.selection.selectedText) {
3 years ago
this.chat.selection.selectedText = undefined;
return;
}
2 years ago
// this.chatSelection.toggleByBubble(bubble);
this.chat.selection.toggleByElement(findUpClassName(target, 'grouped-item') || bubble);
3 years ago
return;
}
const contactDiv: HTMLElement = findUpClassName(target, 'contact');
if(contactDiv) {
this.chat.appImManager.setInnerPeer({
peerId: contactDiv.dataset.peerId.toPeerId()
});
3 years ago
return;
}
const callDiv: HTMLElement = findUpClassName(target, 'bubble-call');
if(callDiv) {
this.chat.appImManager.callUser(this.peerId.toUserId(), callDiv.dataset.type as any);
return;
}
2 years ago
const buyButton: HTMLElement = findUpClassName(target, 'is-buy');
if(buyButton) {
2 years ago
cancelEvent(e);
2 years ago
2 years ago
const message = await this.chat.getMessage(+bubble.dataset.mid);
if(!message) {
return;
}
new PopupPayment(
2 years ago
message as Message.message,
await this.managers.appPaymentsManager.getInputInvoiceByPeerId(message.peerId, message.mid)
);
2 years ago
return;
}
2 years ago
const spoiler: HTMLElement = findUpClassName(target, 'spoiler');
if(spoiler) {
const messageDiv = findUpClassName(spoiler, 'message');
const className = 'is-spoiler-visible';
const isVisible = messageDiv.classList.contains(className);
if(!isVisible) {
cancelEvent(e);
}
const duration = 400 / 2;
const showDuration = 5000;
const useRafs = !isVisible ? 2 : 0;
2 years ago
if(useRafs) {
messageDiv.classList.add('will-change');
}
const spoilerTimeout = messageDiv.dataset.spoilerTimeout;
if(spoilerTimeout !== null) {
clearTimeout(+spoilerTimeout);
delete messageDiv.dataset.spoilerTimeout;
}
SetTransition(messageDiv, className, true, duration, () => {
messageDiv.dataset.spoilerTimeout = '' + window.setTimeout(() => {
SetTransition(messageDiv, className, false, duration, () => {
messageDiv.classList.remove('will-change');
delete messageDiv.dataset.spoilerTimeout;
});
}, showDuration);
2 years ago
}, useRafs);
return;
}
2 years ago
const reactionElement = findUpTag(target, 'REACTION-ELEMENT') as ReactionElement;
if(reactionElement) {
cancelEvent(e);
if(reactionElement.classList.contains('is-inactive')) {
return;
}
2 years ago
const reactionsElement = reactionElement.parentElement as ReactionsElement;
const reactionCount = reactionsElement.getReactionCount(reactionElement);
const message = reactionsElement.getMessage();
this.managers.appReactionsManager.sendReaction(message, reactionCount.reaction);
2 years ago
return;
}
3 years ago
const commentsDiv: HTMLElement = findUpClassName(target, 'replies');
if(commentsDiv) {
const bubbleMid = +bubble.dataset.mid;
if(this.peerId === REPLIES_PEER_ID) {
const message = await this.chat.getMessage(bubbleMid) as Message.message;
2 years ago
const peerId = getPeerId(message.reply_to.reply_to_peer_id);
3 years ago
const threadId = message.reply_to.reply_to_top_id;
const lastMsgId = message.fwd_from.saved_from_msg_id;
this.chat.appImManager.openThread(peerId, lastMsgId, threadId);
3 years ago
} else {
const message1 = await this.chat.getMessage(bubbleMid);
const message = await this.managers.appMessagesManager.getMessageWithReplies(message1 as Message.message);
3 years ago
const replies = message.replies;
if(replies) {
this.managers.appMessagesManager.getDiscussionMessage(this.peerId, message.mid).then((message) => {
this.chat.appImManager.setInnerPeer({
peerId: replies.channel_id.toPeerId(true),
2 years ago
type: 'discussion',
threadId: (message as MyMessage).mid
});
3 years ago
});
}
}
return;
}
const via = findUpClassName(target, 'is-via');
if(via) {
const el = via.querySelector('.peer-title') as HTMLElement;
if(target === el || findUpAsChild(target, el)) {
const message = el.innerText + ' ';
this.managers.appDraftsManager.setDraft(this.peerId, this.chat.threadId, message);
cancelEvent(e);
2 years ago
return;
}
}
3 years ago
const nameDiv = findUpClassName(target, 'peer-title') || findUpTag(target, 'AVATAR-ELEMENT') || findUpAttribute(target, 'data-saved-from');
if(nameDiv && nameDiv !== bubble) {
target = nameDiv || target;
2 years ago
const peerIdStr = target.dataset.peerId || target.getAttribute('peer') || (target as AvatarElement).peerId;
const savedFrom = target.dataset.savedFrom;
if(typeof(peerIdStr) === 'string' || savedFrom) {
if(savedFrom) {
const [peerId, mid] = savedFrom.split('_');
2 years ago
this.chat.appImManager.setInnerPeer({
2 years ago
peerId: peerId.toPeerId(),
lastMsgId: +mid
});
3 years ago
} else {
const peerId = peerIdStr.toPeerId();
if(peerId !== NULL_PEER_ID) {
this.chat.appImManager.setInnerPeer({peerId});
} else {
toast(I18n.format('HidAccount', true));
}
3 years ago
}
}
return;
}
2 years ago
// this.log('chatInner click:', target);
// const isVideoComponentElement = target.tagName === 'SPAN' && findUpClassName(target, 'media-container');
3 years ago
/* if(isVideoComponentElement) {
const video = target.parentElement.querySelector('video') as HTMLElement;
if(video) {
video.click(); // hot-fix for time and play button
return;
}
} */
if(bubble.classList.contains('sticker') && target.parentElement.classList.contains('attachment')) {
const messageId = +bubble.dataset.mid;
const message = await this.chat.getMessage(messageId);
3 years ago
const doc = ((message as Message.message).media as MessageMedia.messageMediaDocument)?.document as Document.document;
3 years ago
if(doc?.stickerSetInput) {
new PopupStickers(doc.stickerSetInput).show();
}
return;
}
const documentDiv = findUpClassName(target, 'document-with-thumb');
2 years ago
if((target.tagName === 'IMG' && !target.classList.contains('emoji') && !target.classList.contains('document-thumb')) ||
target.classList.contains('album-item') ||
// || isVideoComponentElement
2 years ago
(target.tagName === 'VIDEO' && !bubble.classList.contains('round')) ||
(documentDiv && !documentDiv.querySelector('.preloader-container')) ||
target.classList.contains('canvas-thumbnail')) {
const groupedItem = findUpClassName(target, 'album-item') || findUpClassName(target, 'document-container');
const preloader = (groupedItem || bubble).querySelector<HTMLElement>('.preloader-container');
if(preloader) {
simulateClickEvent(preloader);
cancelEvent(e);
return;
}
2 years ago
cancelEvent(e);
const messageId = +(groupedItem || bubble).dataset.mid;
const message = await this.chat.getMessage(messageId);
if(!message) {
this.log.warn('no message by messageId:', messageId);
return;
}
const SINGLE_MEDIA_CLASSNAME = 'webpage';
const isSingleMedia = bubble.classList.contains(SINGLE_MEDIA_CLASSNAME);
const f = documentDiv ? (media: any) => {
return AppMediaViewer.isMediaCompatibleForDocumentViewer(media);
} : (media: any) => {
return media._ === 'photo' || ['video', 'gif'].includes(media.type);
};
3 years ago
const targets: {element: HTMLElement, mid: number, peerId: PeerId}[] = [];
const ids = isSingleMedia ? [messageId] : (await Promise.all(Object.keys(this.bubbles).map((k) => +k).map(async(mid) => {
/* if(isSingleMedia && !this.bubbles[id].classList.contains(SINGLE_MEDIA_CLASSNAME)) {
return false;
} */
2 years ago
// if(!this.scrollable.visibleElements.find((e) => e.element === this.bubbles[id])) return false;
3 years ago
const message = await this.chat.getMessage(mid);
2 years ago
const media = getMediaFromMessage(message);
2 years ago
return media && f(media) && mid;
}))).filter(Boolean).sort((a, b) => a - b);
3 years ago
ids.forEach((id) => {
let selector: string;
if(documentDiv) {
selector = '.document-container';
3 years ago
} else {
const withTail = this.bubbles[id].classList.contains('with-media-tail');
selector = '.album-item video, .album-item img, .preview video, .preview img, ';
if(withTail) {
selector += '.bubble__media-container';
} else {
selector += '.attachment video, .attachment img';
}
3 years ago
}
const elements = Array.from(this.bubbles[id].querySelectorAll(selector)) as HTMLElement[];
3 years ago
const parents: Set<HTMLElement> = new Set();
if(documentDiv) {
elements.forEach((element) => {
targets.push({
element: element.querySelector('.document-ico'),
mid: +element.dataset.mid,
peerId: this.peerId
});
3 years ago
});
} else {
const hasAspecter = !!this.bubbles[id].querySelector('.media-container-aspecter');
elements.forEach((element) => {
if(hasAspecter && !findUpClassName(element, 'media-container-aspecter')) return;
2 years ago
const albumItem = findUpClassName(element, 'album-item');
const parent = albumItem || element.parentElement;
if(parents.has(parent)) return;
parents.add(parent);
targets.push({
element,
mid: albumItem ? +albumItem.dataset.mid : id,
peerId: this.peerId
});
});
}
3 years ago
});
targets.sort((a, b) => a.mid - b.mid);
2 years ago
const idx = targets.findIndex((t) => t.mid === messageId);
3 years ago
if(DEBUG) {
this.log('open mediaViewer single with ids:', ids, idx, targets);
}
if(!targets[idx]) {
this.log('no target for media viewer!', target);
return;
}
new AppMediaViewer()
.setSearchContext({
threadId: this.chat.threadId,
peerId: this.peerId,
inputFilter: {_: documentDiv ? 'inputMessagesFilterDocument' : 'inputMessagesFilterPhotoVideo'},
useSearch: this.chat.type !== 'scheduled' && !isSingleMedia,
isScheduled: this.chat.type === 'scheduled'
3 years ago
})
.openMedia(message, targets[idx].element, 0, true, targets.slice(0, idx), targets.slice(idx + 1));
2 years ago
// appMediaViewer.openMedia(message, target as HTMLImageElement);
3 years ago
return;
}
2 years ago
3 years ago
if(['IMG', 'DIV', 'SPAN'/* , 'A' */].indexOf(target.tagName) === -1) target = findUpTag(target, 'DIV');
2 years ago
3 years ago
if(['DIV', 'SPAN'].indexOf(target.tagName) !== -1/* || target.tagName === 'A' */) {
if(target.classList.contains('goto-original')) {
const savedFrom = bubble.dataset.savedFrom;
3 years ago
const [peerId, mid] = savedFrom.split('_');
2 years ago
// //this.log('savedFrom', peerId, msgID);
this.chat.appImManager.setInnerPeer({
2 years ago
peerId: peerId.toPeerId(),
lastMsgId: +mid
});
3 years ago
return;
} else if(target.classList.contains('forward')) {
const mid = +bubble.dataset.mid;
const message = await this.managers.appMessagesManager.getMessageByPeer(this.peerId, mid);
new PopupForward({
[this.peerId]: await this.managers.appMessagesManager.getMidsByMessage(message)
});
2 years ago
// appSidebarRight.forwardTab.open([mid]);
3 years ago
return;
}
2 years ago
3 years ago
let isReplyClick = false;
2 years ago
3 years ago
try {
isReplyClick = !!findUpClassName(e.target, 'reply');
} catch(err) {}
2 years ago
3 years ago
if(isReplyClick && bubble.classList.contains('is-reply')/* || bubble.classList.contains('forwarded') */) {
const bubbleMid = +bubble.dataset.mid;
this.replyFollowHistory.push(bubbleMid);
const message = (await this.chat.getMessage(bubbleMid)) as Message.message;
3 years ago
2 years ago
const replyToPeerId = message.reply_to.reply_to_peer_id ? getPeerId(message.reply_to.reply_to_peer_id) : this.peerId;
3 years ago
const replyToMid = message.reply_to.reply_to_msg_id;
this.chat.appImManager.setInnerPeer({
2 years ago
peerId: replyToPeerId,
lastMsgId: replyToMid,
type: this.chat.type,
threadId: this.chat.threadId
});
3 years ago
/* if(this.chat.type === 'discussion') {
this.chat.appImManager.setMessageId(, originalMessageId);
} else {
this.chat.appImManager.setInnerPeer(this.peerId, originalMessageId);
} */
2 years ago
// this.chat.setMessageId(, originalMessageId);
3 years ago
}
}
2 years ago
// console.log('chatInner click', e);
3 years ago
};
public async onGoDownClick() {
if(!this.replyFollowHistory.length) {
3 years ago
this.chat.setMessageId(/* , dialog.top_message */);
// const dialog = this.appMessagesManager.getDialogByPeerId(this.peerId)[0];
2 years ago
3 years ago
// if(dialog) {
// this.chat.setPeer(this.peerId/* , dialog.top_message */);
// } else {
// this.log('will scroll down 3');
// this.scroll.scrollTop = this.scroll.scrollHeight;
// }
return;
3 years ago
}
const middleware = this.getMiddleware();
const slice = this.replyFollowHistory.slice();
const messages = await Promise.all(slice.map((mid) => this.chat.getMessage(mid)));
if(!middleware()) return;
slice.forEach((mid, idx) => {
const message = messages[idx];
const bubble = this.bubbles[mid];
let bad = true;
if(bubble) {
const rect = bubble.getBoundingClientRect();
bad = (windowSize.height / 2) > rect.top;
} else if(message) {
bad = false;
}
if(bad) {
this.replyFollowHistory.splice(this.replyFollowHistory.indexOf(mid), 1);
}
});
this.replyFollowHistory.sort((a, b) => b - a);
const mid = this.replyFollowHistory.pop();
this.chat.setMessageId(mid);
3 years ago
}
public getBubbleByPoint(verticalSide: 'top' | 'bottom') {
let element = getElementByPoint(this.scrollable.container, verticalSide, 'center');
/* if(element) {
if(element.classList.contains('bubbles-date-group')) {
const children = Array.from(element.children) as HTMLElement[];
if(verticalSide === 'top') {
element = children[this.stickyIntersector ? 2 : 1];
} else {
element = children[children.length - 1];
}
} else {
element = findUpClassName(element, 'bubble');
if(element && element.classList.contains('is-date')) {
element = element.nextElementSibling as HTMLElement;
}
}
} */
if(element) element = findUpClassName(element, 'bubble');
return element;
}
public async getGroupedBubble(groupId: string) {
const mids = await this.managers.appMessagesManager.getMidsByAlbum(groupId);
for(const mid of mids) {
if(this.bubbles[mid] && !this.skippedMids.has(mid)) {
// const maxId = Math.max(...mids); // * because in scheduled album can be rendered by lowest mid during sending
3 years ago
return {
2 years ago
bubble: this.bubbles[mid],
mid: mid
// message: await this.chat.getMessage(maxId) as Message.message
3 years ago
};
}
}
2 years ago
}
3 years ago
public getBubbleGroupedItems(bubble: HTMLElement) {
return Array.from(bubble.querySelectorAll('.grouped-item')) as HTMLElement[];
}
public async getMountedBubble(mid: number, message?: Message.message | Message.messageService) {
if(message === undefined) {
message = await this.chat.getMessage(mid);
}
if(!message) {
return;
}
const groupedId = (message as Message.message).grouped_id;
if(groupedId) {
const a = await this.getGroupedBubble(groupedId);
3 years ago
if(a) {
a.bubble = a.bubble.querySelector(`.document-container[data-mid="${mid}"]`) || a.bubble;
return a;
}
}
const bubble = this.bubbles[mid];
if(!bubble) return;
return {bubble, mid};
3 years ago
}
private findNextMountedBubbleByMsgId(mid: number, prev?: boolean) {
const mids = getObjectKeysAndSort(this.bubbles, prev ? 'desc' : 'asc');
let filterCallback: (_mid: number) => boolean;
if(prev) filterCallback = (_mid) => _mid < mid;
else filterCallback = (_mid) => mid < _mid;
const foundMid = mids.find((_mid) => {
if(!filterCallback(_mid)) return false;
return !!this.bubbles[_mid]?.parentElement;
});
return this.bubbles[foundMid];
3 years ago
}
public loadMoreHistory(top: boolean, justLoad = false) {
2 years ago
// this.log('loadMoreHistory', top);
if(
2 years ago
!this.peerId ||
/* TEST_SCROLL || */
this.chat.setPeerPromise ||
this.isHeavyAnimationInProgress ||
(top && (this.getHistoryTopPromise || this.scrollable.loadedAll.top)) ||
3 years ago
(!top && (this.getHistoryBottomPromise || this.scrollable.loadedAll.bottom))
) {
return;
}
// warning, если иды только отрицательные то вниз не попадёт (хотя мб и так не попадёт)
// some messages can have negative id (such as sponsored message)
const history = Object.keys(this.bubbles)
.map((id) => +id)
.filter((id) => id > 0 && !this.skippedMids.has(id))
.sort((a, b) => a - b);
3 years ago
if(!history.length) return;
2 years ago
3 years ago
if(top) {
if(DEBUG) {
this.log('Will load more (up) history by id:', history[0], 'maxId:', history[history.length - 1], justLoad/* , history */);
}
this.getHistory1(history[0], true, undefined, undefined, justLoad);
3 years ago
} else {
2 years ago
// let dialog = this.appMessagesManager.getDialogByPeerId(this.peerId)[0];
// const historyMaxId = await this.chat.getHistoryMaxId();
2 years ago
// // if scroll down after search
// if(history.indexOf(historyMaxId) !== -1) {
// this.setLoaded('bottom', true);
// return;
// }
3 years ago
if(DEBUG) {
this.log('Will load more (down) history by id:', history[history.length - 1], justLoad/* , history */);
}
this.getHistory1(history[history.length - 1], false, true, undefined, justLoad);
3 years ago
}
}
public onScroll = (ignoreHeavyAnimation?: boolean, scrollDimensions?: ScrollStartCallbackDimensions) => {
2 years ago
// return;
if(this.isHeavyAnimationInProgress) {
if(this.sliceViewportDebounced) {
this.sliceViewportDebounced.clearTimeout();
}
// * В таком случае, кнопка не будет моргать если чат в самом низу, и правильно отработает случай написания нового сообщения и проскролла вниз
if(this.scrolledDown && !ignoreHeavyAnimation) {
return;
}
} else {
if(this.chat.topbar.pinnedMessage) {
this.chat.topbar.pinnedMessage.setCorrectIndexThrottled(this.scrollable.lastScrollDirection);
}
2 years ago
if(this.sliceViewportDebounced) {
this.sliceViewportDebounced();
}
2 years ago
this.setStickyDateManually();
}
2 years ago
// lottieLoader.checkAnimations(false, 'chat');
if(scrollDimensions && scrollDimensions.distanceToEnd < SCROLLED_DOWN_THRESHOLD && this.scrolledDown) {
return;
}
const distanceToEnd = scrollDimensions?.distanceToEnd ?? this.scrollable.getDistanceToEnd();
if(/* !IS_TOUCH_SUPPORTED && */(this.scrollable.lastScrollDirection !== 0 && distanceToEnd > 0) || scrollDimensions) {
// if(/* !IS_TOUCH_SUPPORTED && */(this.scrollable.lastScrollDirection !== 0 || scrollDimensions) && distanceToEnd > 0) {
3 years ago
if(this.isScrollingTimeout) {
clearTimeout(this.isScrollingTimeout);
} else if(!this.chatInner.classList.contains('is-scrolling')) {
this.chatInner.classList.add('is-scrolling');
}
2 years ago
3 years ago
this.isScrollingTimeout = window.setTimeout(() => {
this.chatInner.classList.remove('is-scrolling');
this.isScrollingTimeout = 0;
}, 1350 + (scrollDimensions?.duration ?? 0));
3 years ago
}
2 years ago
if(distanceToEnd < SCROLLED_DOWN_THRESHOLD && (this.scrollable.loadedAll.bottom || this.chat.setPeerPromise || !this.peerId)) {
this.container.classList.add('scrolled-down');
3 years ago
this.scrolledDown = true;
} else if(this.container.classList.contains('scrolled-down')) {
this.container.classList.remove('scrolled-down');
3 years ago
this.scrolledDown = false;
}
};
public setScroll() {
if(this.scrollable) {
this.destroyScrollable();
}
3 years ago
this.scrollable = new Scrollable(null, 'IM', /* 10300 */300);
this.setLoaded('top', false, false);
this.setLoaded('bottom', false, false);
3 years ago
this.scrollable.container.append(this.chatInner);
/* const getScrollOffset = () => {
//return Math.round(Math.max(300, appPhotosManager.windowH / 1.5));
2 years ago
return 300;
3 years ago
};
window.addEventListener('resize', () => {
this.scrollable.onScrollOffset = getScrollOffset();
});
this.scrollable = new Scrollable(this.bubblesContainer, 'y', 'IM', this.chatInner, getScrollOffset()); */
this.scrollable.onAdditionalScroll = this.onScroll;
this.scrollable.onScrolledTop = () => this.loadMoreHistory(true);
this.scrollable.onScrolledBottom = () => this.loadMoreHistory(false);
2 years ago
// this.scrollable.attachSentinels(undefined, 300);
3 years ago
if(IS_TOUCH_SUPPORTED && false) {
3 years ago
this.scrollable.container.addEventListener('touchmove', () => {
if(this.isScrollingTimeout) {
clearTimeout(this.isScrollingTimeout);
} else if(!this.chatInner.classList.contains('is-scrolling')) {
this.chatInner.classList.add('is-scrolling');
}
}, {passive: true});
this.scrollable.container.addEventListener('touchend', () => {
if(!this.chatInner.classList.contains('is-scrolling')) {
return;
}
if(this.isScrollingTimeout) {
clearTimeout(this.isScrollingTimeout);
}
this.isScrollingTimeout = window.setTimeout(() => {
this.chatInner.classList.remove('is-scrolling');
this.isScrollingTimeout = 0;
}, 1350);
}, {passive: true});
}
}
public async updateUnreadByDialog() {
const historyStorage = await this.chat.getHistoryStorage();
3 years ago
const maxId = this.peerId === rootScope.myId ? historyStorage.readMaxId : historyStorage.readOutboxMaxId;
2 years ago
// /////this.log('updateUnreadByDialog', maxId, dialog, this.unreadOut);
3 years ago
for(const msgId of this.unreadOut) {
if(msgId > 0 && msgId <= maxId) {
const bubble = this.bubbles[msgId];
if(bubble) {
this.unreadOut.delete(msgId);
if(bubble.classList.contains('is-outgoing')) {
continue;
}
2 years ago
bubble.classList.remove('is-sent', 'is-sending', 'is-outgoing'); // is-sending can be when there are bulk of updates (e.g. sending command to Stickers bot)
3 years ago
bubble.classList.add('is-read');
}
}
}
}
2 years ago
public deleteMessagesByIds(mids: number[], permanent = true, ignoreOnScroll?: boolean) {
let deleted = false;
mids.forEach((mid) => {
const bubble = this.bubbles[mid];
if(!bubble) return;
2 years ago
deleted = true;
3 years ago
/* const mounted = this.getMountedBubble(mid);
if(!mounted) return; */
delete this.bubbles[mid];
this.skippedMids.delete(mid);
if(this.firstUnreadBubble === bubble) {
this.firstUnreadBubble = null;
}
this.bubbleGroups.removeAndUnmountBubble(bubble);
if(this.observer) {
this.observer.unobserve(bubble, this.unreadedObserverCallback);
3 years ago
this.unreaded.delete(bubble);
this.observer.unobserve(bubble, this.viewsObserverCallback);
this.viewsMids.delete(mid);
}
3 years ago
if(this.emptyPlaceholderBubble === bubble) {
this.emptyPlaceholderBubble = undefined;
3 years ago
}
2 years ago
// this.reactions.delete(mid);
3 years ago
});
if(!deleted) {
return;
}
this.scrollable.ignoreNextScrollEvent();
3 years ago
if(permanent && this.chat.selection.isSelecting) {
this.chat.selection.deleteSelectedMids(this.peerId, mids);
3 years ago
}
2 years ago
3 years ago
animationIntersector.checkAnimations(false, CHAT_ANIMATION_GROUP);
this.deleteEmptyDateGroups();
if(!ignoreOnScroll) {
this.scrollable.onScroll();
// this.onScroll();
}
3 years ago
}
private setTopPadding(middleware = this.getMiddleware()) {
let isPaddingNeeded = false;
let setPaddingTo: HTMLElement;
if(!this.isTopPaddingSet && this.chat.type !== 'scheduled') {
const {clientHeight, scrollHeight} = this.scrollable.container;
isPaddingNeeded = clientHeight === scrollHeight;
/* const firstEl = this.chatInner.firstElementChild as HTMLElement;
if(this.chatInner.firstElementChild) {
const visibleRect = getVisibleRect(firstEl, this.scrollable.container);
isPaddingNeeded = !visibleRect.overflow.top && (visibleRect.rect.top - firstEl.offsetTop) !== this.scrollable.container.getBoundingClientRect().top;
} else {
isPaddingNeeded = true;
} */
if(isPaddingNeeded) {
/* const add = clientHeight - scrollHeight;
this.chatInner.style.paddingTop = add + 'px';
this.scrollable.scrollTop += add; */
setPaddingTo = this.chatInner;
setPaddingTo.style.paddingTop = clientHeight + 'px';
this.scrollable.setScrollTopSilently(scrollHeight);
this.isTopPaddingSet = true;
}
}
return {
isPaddingNeeded,
unsetPadding: isPaddingNeeded ? () => {
if(middleware() && isPaddingNeeded) {
setPaddingTo.style.paddingTop = '';
this.isTopPaddingSet = false;
}
} : undefined
};
}
private renderNewMessage(message: MyMessage, scrolledDown?: boolean) {
const promise = this._renderNewMessage(message, scrolledDown);
this.renderNewPromises.add(promise);
promise.catch(noop).finally(() => {
this.renderNewPromises.delete(promise);
});
return promise;
}
2 years ago
private async _renderNewMessage(message: MyMessage, scrolledDown?: boolean) {
3 years ago
if(!this.scrollable.loadedAll.bottom) { // seems search active or sliced
2 years ago
// this.log('renderNewMessagesByIds: seems search is active, skipping render:', mids);
const setPeerPromise = this.chat.setPeerPromise;
if(setPeerPromise) {
const middleware = this.getMiddleware();
setPeerPromise.then(async() => {
if(!middleware()) return;
const newMessage = await this.chat.getMessage(message.mid);
if(!middleware()) return;
this.renderNewMessage(newMessage);
});
}
3 years ago
return;
}
if(this.chat.threadId) {
const replyTo = message?.reply_to as MessageReplyHeader;
if(!(replyTo && (replyTo.reply_to_top_id || replyTo.reply_to_msg_id) === this.chat.threadId)) {
return;
}
3 years ago
}
if(this.bubbles[message.mid]) {
return;
}
3 years ago
// ! should scroll even without new messages
/* if(!mids.length) {
return;
} */
3 years ago
if(!scrolledDown) {
scrolledDown = this.scrolledDown && (
2 years ago
!this.scrollingToBubble ||
this.scrollingToBubble === this.getLastBubble() ||
this.scrollingToBubble === this.chatInner
);
3 years ago
}
const middleware = this.getMiddleware();
const {isPaddingNeeded, unsetPadding} = this.setTopPadding(middleware);
const promise = this.performHistoryResult({history: [message]}, false);
3 years ago
if(scrolledDown) {
promise.then(() => {
if(!middleware()) return;
2 years ago
// this.log('renderNewMessagesByIDs: messagesQueuePromise after', this.scrollable.isScrolledDown);
// this.scrollable.scrollTo(this.scrollable.scrollHeight, 'top', true, true, 5000);
// const bubble = this.bubbles[Math.max(...mids)];
let bubble: HTMLElement;
if(this.chat.type === 'scheduled') {
bubble = this.bubbles[message.mid];
}
const promise = bubble ? this.scrollToBubbleEnd(bubble) : this.scrollToEnd();
if(isPaddingNeeded) {
// it will be called only once even if was set multiple times (that won't happen)
promise.then(unsetPadding);
}
3 years ago
2 years ago
// this.scrollable.scrollIntoViewNew(this.chatInner, 'end');
3 years ago
/* setTimeout(() => {
this.log('messagesQueuePromise afterafter:', this.chatInner.childElementCount, this.scrollable.scrollHeight);
}, 10); */
});
}
return promise;
3 years ago
}
3 years ago
public getLastBubble() {
const group = this.bubbleGroups.getLastGroup();
return group?.lastItem?.bubble;
3 years ago
}
3 years ago
public scrollToBubble(
2 years ago
element: HTMLElement,
3 years ago
position: ScrollLogicalPosition,
forceDirection?: FocusDirection,
3 years ago
forceDuration?: number
3 years ago
) {
const bubble = findUpClassName(element, 'bubble');
if(!element.parentElement) {
this.log.error('element is not connected', bubble);
}
let fallbackToElementStartWhenCentering: HTMLElement;
3 years ago
// * if it's a start, then scroll to start of the group
if(bubble && position !== 'end') {
const item = this.bubbleGroups.getItemByBubble(bubble);
if(item.group.firstItem === item && whichChild(item.group.container) === (this.stickyIntersector ? STICKY_OFFSET : 1)) {
const dateGroup = item.group.container.parentElement;
// if(whichChild(dateGroup) === 0) {
2 years ago
fallbackToElementStartWhenCentering = dateGroup;
// position = 'start';
// element = dateGroup;
// }
}
3 years ago
}
// const isLastBubble = this.getLastBubble() === bubble;
/* if(isLastBubble) {
element = this.getLastDateGroup();
} */
2 years ago
const margin = 4; // * 4 = .25rem
/* if(isLastBubble && this.chat.type === 'chat' && this.bubblesContainer.classList.contains('is-chat-input-hidden')) {
margin = 20;
} */
const isChangingHeight = (this.chat.input.messageInput && this.chat.input.messageInput.classList.contains('is-changing-height')) || this.chat.container.classList.contains('is-toggling-helper');
const promise = this.scrollable.scrollIntoViewNew({
2 years ago
element,
position,
margin,
forceDirection,
forceDuration,
axis: 'y',
getNormalSize: isChangingHeight ? ({rect}) => {
// return rect.height;
2 years ago
let height = windowSize.height;
// height -= this.chat.topbar.container.getBoundingClientRect().height;
height -= this.container.offsetTop;
2 years ago
height -= mediaSizes.isMobile || windowSize.height < 570 ? 58 : 78;
return height;
/* const rowsWrapperHeight = this.chat.input.rowsWrapper.getBoundingClientRect().height;
const diff = rowsWrapperHeight - 54;
return rect.height + diff; */
} : undefined,
fallbackToElementStartWhenCentering,
startCallback: (dimensions) => {
// this.onScroll(true, this.scrolledDown && dimensions.distanceToEnd <= SCROLLED_DOWN_THRESHOLD ? undefined : dimensions);
this.onScroll(true, dimensions);
}
});
// fix flickering date when opening unread chat and focusing message
if(forceDirection === FocusDirection.Static) {
this.scrollable.lastScrollPosition = this.scrollable.scrollTop;
}
2 years ago
return promise;
3 years ago
}
public scrollToEnd() {
return this.scrollToBubbleEnd(this.chatInner);
}
public async scrollToBubbleEnd(bubble: HTMLElement) {
3 years ago
/* if(DEBUG) {
this.log('scrollToNewLastBubble: will scroll into view:', bubble);
} */
if(bubble) {
3 years ago
this.scrollingToBubble = bubble;
const middleware = this.getMiddleware();
await this.scrollToBubble(bubble, 'end', undefined, undefined);
if(!middleware()) return;
this.scrollingToBubble = undefined;
3 years ago
}
}
// ! can't get it by chatInner.lastElementChild because placeholder can be the last...
// private getLastDateGroup() {
// let lastTime = 0, lastElem: HTMLElement;
// for(const i in this.dateMessages) {
// const dateMessage = this.dateMessages[i];
// if(dateMessage.firstTimestamp > lastTime) {
// lastElem = dateMessage.container;
// lastTime = dateMessage.firstTimestamp;
// }
// }
// return lastElem;
// }
3 years ago
public async scrollToBubbleIfLast(bubble: HTMLElement) {
if(this.getLastBubble() === bubble) {
// return this.scrollToBubbleEnd(bubble);
return this.scrollToEnd();
3 years ago
}
}
public highlightBubble(element: HTMLElement) {
const datasetKey = 'highlightTimeout';
if(element.dataset[datasetKey]) {
clearTimeout(+element.dataset[datasetKey]);
element.classList.remove('is-highlighted');
void element.offsetWidth; // reflow
}
element.classList.add('is-highlighted');
element.dataset[datasetKey] = '' + setTimeout(() => {
element.classList.remove('is-highlighted');
delete element.dataset[datasetKey];
}, 2000);
}
private createDateBubble(timestamp: number, date: Date = new Date(timestamp * 1000)) {
let dateElement: HTMLElement;
2 years ago
const today = new Date();
today.setHours(0, 0, 0, 0);
3 years ago
const isScheduled = this.chat.type === 'scheduled';
2 years ago
if(today.getTime() === date.getTime()) {
dateElement = i18n(isScheduled ? 'Chat.Date.ScheduledForToday' : 'Date.Today');
} else if(isScheduled && timestamp === SEND_WHEN_ONLINE_TIMESTAMP) {
dateElement = i18n('MessageScheduledUntilOnline');
} else {
const options: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'long'
};
3 years ago
if(date.getFullYear() !== today.getFullYear()) {
options.year = 'numeric';
}
3 years ago
dateElement = new I18n.IntlDateElement({
date,
options
}).element;
3 years ago
if(isScheduled) {
dateElement = i18n('Chat.Date.ScheduledFor', [dateElement]);
3 years ago
}
}
2 years ago
const bubble = document.createElement('div');
bubble.className = 'bubble service is-date';
const bubbleContent = document.createElement('div');
bubbleContent.classList.add('bubble-content');
const serviceMsg = document.createElement('div');
serviceMsg.classList.add('service-msg');
serviceMsg.append(dateElement);
bubbleContent.append(serviceMsg);
bubble.append(bubbleContent);
3 years ago
return bubble;
}
3 years ago
public getDateForDateContainer(timestamp: number) {
const date = new Date(timestamp * 1000);
date.setHours(0, 0, 0);
return {date, dateTimestamp: date.getTime()};
}
public getDateContainerByTimestamp(timestamp: number) {
const {date, dateTimestamp} = this.getDateForDateContainer(timestamp);
if(!this.dateMessages[dateTimestamp]) {
const bubble = this.createDateBubble(timestamp, date);
// bubble.classList.add('is-sticky');
const fakeBubble = this.createDateBubble(timestamp, date);
fakeBubble.classList.add('is-fake');
3 years ago
const container = document.createElement('section');
3 years ago
container.className = 'bubbles-date-group';
container.append(bubble, fakeBubble);
3 years ago
this.dateMessages[dateTimestamp] = {
div: bubble,
container,
firstTimestamp: date.getTime()
};
3 years ago
const haveTimestamps = getObjectKeysAndSort(this.dateMessages, 'asc');
2 years ago
const length = haveTimestamps.length;
let i = 0, insertBefore: HTMLElement; // there can be 'first bubble' (e.g. bot description) so can't insert by index
3 years ago
for(; i < haveTimestamps.length; ++i) {
const t = haveTimestamps[i];
3 years ago
insertBefore = this.dateMessages[t].container;
3 years ago
if(dateTimestamp < t) {
break;
}
}
3 years ago
if(i === length && insertBefore) {
insertBefore = insertBefore.nextElementSibling as HTMLElement;
}
3 years ago
3 years ago
if(!insertBefore) {
3 years ago
this.chatInner.append(container);
3 years ago
} else {
this.chatInner.insertBefore(container, insertBefore);
}
3 years ago
if(this.stickyIntersector) {
this.stickyIntersector.observeStickyHeaderChanges(container);
}
if(this.chatInner.parentElement) {
this.container.classList.add('has-groups');
}
3 years ago
}
return this.dateMessages[dateTimestamp];
}
private destroyScrollable() {
this.scrollable.removeListeners();
this.scrollable.onScrolledTop = this.scrollable.onScrolledBottom = this.scrollable.onAdditionalScroll = null;
}
3 years ago
public destroy() {
2 years ago
// this.chat.log.error('Bubbles destroying');
3 years ago
this.destroyScrollable();
3 years ago
this.listenerSetter.removeAll();
this.lazyLoadQueue.clear();
this.observer && this.observer.disconnect();
3 years ago
this.stickyIntersector && this.stickyIntersector.disconnect();
delete this.lazyLoadQueue;
this.observer && delete this.observer;
3 years ago
this.stickyIntersector && delete this.stickyIntersector;
}
public cleanup(bubblesToo = false) {
this.bubbles = {}; // clean it before so sponsored message won't be deleted faster on peer changing
2 years ago
// //console.time('appImManager cleanup');
this.setLoaded('top', false, false);
this.setLoaded('bottom', false, false);
3 years ago
// cancel scroll
cancelAnimationByKey(this.scrollable.container);
// do not wait ending of previous scale animation
interruptHeavyAnimation();
if(TEST_SCROLL !== undefined) {
TEST_SCROLL = TEST_SCROLL_TIMES;
}
this.skippedMids.clear();
this.dateMessages = {};
this.bubbleGroups.cleanup();
this.unreadOut.clear();
this.needUpdate.length = 0;
this.lazyLoadQueue.clear();
this.renderNewPromises.clear();
2 years ago
3 years ago
// clear messages
if(bubblesToo) {
this.scrollable.container.textContent = '';
this.cleanupPlaceholders();
3 years ago
}
2 years ago
3 years ago
this.firstUnreadBubble = null;
this.attachedUnreadBubble = false;
2 years ago
3 years ago
this.messagesQueue.length = 0;
this.messagesQueuePromise = null;
2 years ago
3 years ago
this.getHistoryTopPromise = this.getHistoryBottomPromise = undefined;
this.fetchNewPromise = undefined;
this.getSponsoredMessagePromise = undefined;
2 years ago
3 years ago
if(this.stickyIntersector) {
this.stickyIntersector.disconnect();
}
2 years ago
if(this.observer) {
this.observer.disconnect();
3 years ago
this.unreaded.clear();
this.unreadedSeen.clear();
this.readPromise = undefined;
this.viewsMids.clear();
}
3 years ago
this.middleware.clean();
2 years ago
3 years ago
this.onAnimateLadder = undefined;
this.resolveLadderAnimation = undefined;
this.attachPlaceholderOnRender = undefined;
this.emptyPlaceholderBubble = undefined;
this.sponsoredMessage = undefined;
this.previousStickyDate = undefined;
3 years ago
this.scrollingToBubble = undefined;
2 years ago
// //console.timeEnd('appImManager cleanup');
this.isTopPaddingSet = false;
this.renderingMessages.clear();
this.bubblesToEject.clear();
this.bubblesToReplace.clear();
2 years ago
// this.reactions.clear();
if(this.isScrollingTimeout) {
clearTimeout(this.isScrollingTimeout);
this.isScrollingTimeout = 0;
}
this.container.classList.remove('has-sticky-dates');
this.scrollable.cancelMeasure();
3 years ago
}
private cleanupPlaceholders(bubble = this.emptyPlaceholderBubble) {
if(bubble) {
bubble.remove();
if(this.emptyPlaceholderBubble === bubble) {
this.emptyPlaceholderBubble = undefined;
}
}
}
public async setPeer(samePeer: boolean, peerId: PeerId, lastMsgId?: number, startParam?: string): Promise<{cached?: boolean, promise: Chat['setPeerPromise']}> {
const tempId = ++this.setPeerTempId;
3 years ago
if(!peerId) {
this.cleanup(true);
2 years ago
this.preloader.detach();
3 years ago
return null;
}
const perf = performance.now();
const log = this.log.bindPrefix('setPeer');
log.warn('start');
3 years ago
const middleware = () => {
return this.setPeerTempId === tempId;
};
const m = middlewarePromise(middleware, PEER_CHANGED_ERROR);
if(!samePeer) {
await m(this.chat.onChangePeer(m));
}
3 years ago
/* if(samePeer && this.chat.setPeerPromise) {
return {cached: true, promise: this.chat.setPeerPromise};
} */
const chatType = this.chat.type;
if(chatType === 'scheduled' || this.chat.isRestricted) {
lastMsgId = 0;
}
const historyStorage = await m(this.chat.getHistoryStorage());
2 years ago
const topMessage = chatType === 'pinned' ? await m(this.managers.appMessagesManager.getPinnedMessagesMaxId(peerId)) : historyStorage.maxId ?? 0;
3 years ago
const isTarget = lastMsgId !== undefined;
// * this one will fix topMessage for null message in history (e.g. channel comments with only 1 comment and it is a topMessage)
/* if(chatType !== 'pinned' && topMessage && !historyStorage.history.slice.includes(topMessage)) {
3 years ago
topMessage = 0;
} */
let followingUnread: boolean;
let readMaxId = 0, savedPosition: ReturnType<AppImManager['getChatSavedPosition']>, overrideAdditionMsgId: number;
3 years ago
if(!isTarget) {
if(!samePeer) {
savedPosition = this.chat.appImManager.getChatSavedPosition(this.chat);
}
if(savedPosition) {
2 years ago
3 years ago
} else if(topMessage) {
readMaxId = await m(this.managers.appMessagesManager.getReadMaxIdIfUnread(peerId, this.chat.threadId));
const dialog = await m(this.managers.appMessagesManager.getDialogOnly(peerId));
if(/* dialog.unread_count */readMaxId && !samePeer && (!dialog || dialog.unread_count !== 1)) {
const foundSlice = historyStorage.history.findSliceOffset(readMaxId);
if(foundSlice && foundSlice.slice.isEnd(SliceEnd.Bottom)) {
overrideAdditionMsgId = foundSlice.slice[foundSlice.offset - 25] || foundSlice.slice[0] || readMaxId;
}
followingUnread = !isTarget;
3 years ago
lastMsgId = readMaxId;
} else {
lastMsgId = topMessage;
2 years ago
// lastMsgID = topMessage;
3 years ago
}
}
}
const isJump = lastMsgId !== topMessage/* && overrideAdditionMsgId === undefined */;
if(startParam === undefined && await m(this.chat.isStartButtonNeeded())) {
startParam = BOT_START_PARAM;
}
3 years ago
if(samePeer) {
const mounted = await m(this.getMountedBubble(lastMsgId));
3 years ago
if(mounted) {
if(isTarget) {
this.scrollToBubble(mounted.bubble, 'center');
this.highlightBubble(mounted.bubble);
this.chat.dispatchEvent('setPeer', lastMsgId, false);
} else if(topMessage && !isJump) {
2 years ago
// log('will scroll down', this.scroll.scrollTop, this.scroll.scrollHeight);
// scrollable.setScrollTopSilently(scrollable.scrollHeight);
this.scrollToEnd();
3 years ago
this.chat.dispatchEvent('setPeer', lastMsgId, true);
}
if(startParam !== undefined) {
this.chat.input.setStartParam(startParam);
}
2 years ago
3 years ago
return null;
}
} else {
if(this.peerId) { // * set new queue id if new peer (setting not from 0)
this.lazyLoadQueue.queueId = ++queueId;
this.managers.apiFileManager.setQueueId(this.chat.bubbles.lazyLoadQueue.queueId);
3 years ago
}
this.replyFollowHistory.length = 0;
this.passEntities = {
messageEntityBotCommand: await m(this.managers.appPeersManager.isAnyGroup(peerId)) || await m(this.managers.appUsersManager.isBot(peerId))
3 years ago
};
}
if(DEBUG) {
log('setPeer peerId:', peerId, historyStorage, lastMsgId, topMessage);
3 years ago
}
// add last message, bc in getHistory will load < max_id
const additionMsgId = overrideAdditionMsgId ?? (isJump || chatType === 'scheduled' || this.chat.isRestricted ? 0 : topMessage);
3 years ago
let maxBubbleId = 0;
if(samePeer) {
2 years ago
const el = this.getBubbleByPoint('bottom'); // ! this may not work if being called when chat is hidden
// this.chat.log('[PM]: setCorrectIndex: get last element perf:', performance.now() - perf, el);
3 years ago
if(el) {
maxBubbleId = +el.dataset.mid;
}
if(maxBubbleId <= 0) {
maxBubbleId = Math.max(...Object.keys(this.bubbles).map((mid) => +mid));
3 years ago
}
} else {
this.isFirstLoad = true;
this.destroyResizeObserver();
3 years ago
}
const oldChatInner = this.chatInner;
const oldPlaceholderBubble = this.emptyPlaceholderBubble;
3 years ago
this.cleanup();
const chatInner = this.chatInner = document.createElement('div');
3 years ago
if(samePeer) {
chatInner.className = oldChatInner.className;
chatInner.classList.remove('disable-hover', 'is-scrolling');
3 years ago
} else {
chatInner.classList.add('bubbles-inner');
3 years ago
}
this.lazyLoadQueue.lock();
// const haveToScrollToBubble = (topMessage && (isJump || samePeer)) || isTarget;
const haveToScrollToBubble = samePeer || (topMessage && isJump) || isTarget;
const fromUp = maxBubbleId > 0 && (!lastMsgId || maxBubbleId < lastMsgId || lastMsgId < 0);
const scrollFromDown = !fromUp && samePeer;
const scrollFromUp = !scrollFromDown && fromUp/* && (samePeer || forwardingUnread) */;
this.willScrollOnLoad = scrollFromDown || scrollFromUp;
this.setPeerOptions = {
lastMsgId,
topMessage
};
let result: Awaited<ReturnType<ChatBubbles['getHistory']>>;
3 years ago
if(!savedPosition) {
result = await m(this.getHistory1(lastMsgId, true, isJump, additionMsgId));
3 years ago
} else {
result = {
promise: getHeavyAnimationPromise().then(() => {
return this.performHistoryResult({history: savedPosition.mids}, true);
3 years ago
}) as any,
cached: true,
waitPromise: Promise.resolve()
3 years ago
};
}
this.setPeerCached = result.cached;
log.warn('got history');// warning
3 years ago
const {promise, cached} = result;
if(!cached && !samePeer) {
await m(this.chat.finishPeerChange(isTarget, isJump, lastMsgId, startParam));
this.scrollable.container.textContent = '';
// oldContainer.textContent = '';
2 years ago
// oldChatInner.remove();
this.preloader.attach(this.container);
3 years ago
}
/* this.ladderDeferred && this.ladderDeferred.resolve();
this.ladderDeferred = deferredPromise<void>(); */
3 years ago
animationIntersector.lockGroup(CHAT_ANIMATION_GROUP);
const setPeerPromise = m(promise).then(async() => {
log.warn('promise fulfilled');
3 years ago
2 years ago
const mountedByLastMsgId = haveToScrollToBubble ? await m(lastMsgId ? this.getMountedBubble(lastMsgId) : {bubble: this.getLastBubble()}) : undefined;
if(cached && !samePeer) {
log.warn('finishing peer change');
await m(this.chat.finishPeerChange(isTarget, isJump, lastMsgId, startParam)); // * костыль
log.warn('finished peer change');
3 years ago
}
this.preloader.detach();
3 years ago
if(this.resolveLadderAnimation) {
this.resolveLadderAnimation();
this.resolveLadderAnimation = undefined;
}
this.setPeerCached = undefined;
3 years ago
// this.ladderDeferred.resolve();
const scrollable = this.scrollable;
scrollable.lastScrollDirection = 0;
2 years ago
scrollable.lastScrollPosition = 0;
replaceContent(scrollable.container, chatInner);
// this.chat.topbar.container.nextElementSibling.replaceWith(container);
if(oldPlaceholderBubble) {
this.cleanupPlaceholders(oldPlaceholderBubble);
}
if(this.attachPlaceholderOnRender) {
this.attachPlaceholderOnRender();
}
if(!isTarget && this.chat.type === 'chat') {
this.chat.topbar.pinnedMessage.setCorrectIndex(0);
}
this.container.classList.toggle('has-groups', !!Object.keys(this.dateMessages).length);
log.warn('mounted chat', this.chatInner === chatInner, this.chatInner.parentElement, performance.now() - perf);
3 years ago
animationIntersector.unlockGroup(CHAT_ANIMATION_GROUP);
animationIntersector.checkAnimations(false, CHAT_ANIMATION_GROUP/* , true */);
2 years ago
// fastRaf(() => {
this.lazyLoadQueue.unlock();
// });
3 years ago
2 years ago
// if(dialog && lastMsgID && lastMsgID !== topMessage && (this.bubbles[lastMsgID] || this.firstUnreadBubble)) {
3 years ago
if(savedPosition) {
scrollable.setScrollTopSilently(savedPosition.top);
3 years ago
/* const mountedByLastMsgId = this.getMountedBubble(lastMsgId);
let bubble: HTMLElement = mountedByLastMsgId?.bubble;
if(!bubble?.parentElement) {
bubble = this.findNextMountedBubbleByMsgId(lastMsgId);
}
if(bubble) {
const top = bubble.getBoundingClientRect().top;
const distance = savedPosition.top - top;
scrollable.scrollTop += distance;
3 years ago
} */
} else if(haveToScrollToBubble) {
let unsetPadding: () => void;
if(scrollFromDown) {
scrollable.setScrollTopSilently(99999);
} else if(scrollFromUp) {
const set = this.setTopPadding();
if(set.isPaddingNeeded) {
unsetPadding = set.unsetPadding;
}
scrollable.setScrollTopSilently(0);
3 years ago
}
// const mountedByLastMsgId = lastMsgId ? this.getMountedBubble(lastMsgId) : {bubble: this.getLastBubble()};
3 years ago
let bubble: HTMLElement = (followingUnread && this.firstUnreadBubble) || mountedByLastMsgId?.bubble;
if(!bubble?.parentElement) {
bubble = this.findNextMountedBubbleByMsgId(lastMsgId, false) || this.findNextMountedBubbleByMsgId(lastMsgId, true);
3 years ago
}
2 years ago
let promise: Promise<void>;
3 years ago
// ! sometimes there can be no bubble
if(bubble) {
const lastBubble = this.getLastBubble();
const position: ScrollLogicalPosition = followingUnread ? 'start' : (!isJump && !isTarget && lastBubble === bubble ? 'end' : 'center');
if(position === 'end' && lastBubble === bubble && samePeer) {
promise = this.scrollToEnd();
} else {
promise = this.scrollToBubble(bubble, position, !samePeer ? FocusDirection.Static : undefined);
}
if(!followingUnread && isTarget) {
3 years ago
this.highlightBubble(bubble);
}
}
if(unsetPadding) {
(promise || Promise.resolve()).then(() => {
unsetPadding();
});
}
3 years ago
} else {
scrollable.setScrollTopSilently(99999);
3 years ago
}
// if(!cached) {
2 years ago
this.onRenderScrollSet();
// }
3 years ago
this.onScroll();
const afterSetPromise = Promise.all([setPeerPromise, getHeavyAnimationPromise()]);
afterSetPromise.then(() => { // check whether list isn't full
scrollable.checkForTriggers();
// if(cached) {
2 years ago
// this.onRenderScrollSet();
// }
3 years ago
});
this.chat.dispatchEvent('setPeer', lastMsgId, !isJump);
Promise.all([
this.setFetchReactionsInterval(afterSetPromise),
this.setFetchHistoryInterval({
afterSetPromise,
lastMsgId,
samePeer,
savedPosition,
topMessage
2 years ago
})
]).then(() => {
log('scrolledAllDown:', scrollable.loadedAll.bottom);
2 years ago
// if(!this.unreaded.length && dialog) { // lol
if(scrollable.loadedAll.bottom && topMessage && !this.unreaded.size) { // lol
this.onScrolledAllDown();
}
});
3 years ago
if(chatType === 'chat') {
const dialog = await m(this.managers.appMessagesManager.getDialogOnly(peerId));
3 years ago
if(dialog?.pFlags.unread_mark) {
this.managers.appMessagesManager.markDialogUnread(peerId, true);
3 years ago
}
}
2 years ago
// this.chatInner.classList.remove('disable-hover', 'is-scrolling'); // warning, performance!
}).catch((err) => {
log.error('getHistory promise error:', err);
if(!middleware()) {
this.preloader.detach();
}
3 years ago
throw err;
});
return {cached, promise: setPeerPromise};
}
private async setFetchReactionsInterval(afterSetPromise: Promise<any>) {
const middleware = this.getMiddleware();
const needReactionsInterval = await this.managers.appPeersManager.isChannel(this.peerId);
if(needReactionsInterval) {
const fetchReactions = async() => {
if(!middleware()) return;
const mids: number[] = [];
for(const mid in this.bubbles) {
let message = await this.chat.getMessage(+mid);
if(message?._ !== 'message') {
continue;
}
message = await this.managers.appMessagesManager.getGroupsFirstMessage(message);
mids.push(message.mid);
}
const promise = mids.length ? this.managers.appReactionsManager.getMessagesReactions(this.peerId, mids) : Promise.resolve();
promise.then(() => {
setTimeout(fetchReactions, 10e3);
});
};
Promise.all([afterSetPromise, getHeavyAnimationPromise(), pause(500)]).then(() => {
fetchReactions();
});
3 years ago
}
}
private async setFetchHistoryInterval({
lastMsgId,
topMessage,
afterSetPromise,
savedPosition,
samePeer
}: {
lastMsgId: number,
topMessage: number,
afterSetPromise: Promise<any>,
savedPosition: ChatSavedPosition,
samePeer: boolean
}) {
const middleware = this.getMiddleware();
const peerId = this.peerId;
const needFetchInterval = await this.managers.appMessagesManager.isFetchIntervalNeeded(peerId);
const needFetchNew = savedPosition || needFetchInterval;
if(!needFetchNew) {
return;
}
await afterSetPromise;
if(!middleware()) {
return;
}
this.setLoaded('bottom', false);
this.scrollable.checkForTriggers();
if(!needFetchInterval) {
return;
}
const f = () => {
this.fetchNewPromise = new Promise<void>(async(resolve) => {
if(!middleware() || !(await this.managers.appMessagesManager.isFetchIntervalNeeded(peerId))) {
resolve();
return;
}
this.managers.appMessagesManager.getNewHistory(peerId, this.chat.threadId).then((result) => {
if(!middleware() || !result) {
resolve();
return;
}
3 years ago
const {isBottomEnd} = result;
if(this.scrollable.loadedAll.bottom && this.scrollable.loadedAll.bottom !== isBottomEnd) {
this.setLoaded('bottom', isBottomEnd);
this.onScroll();
}
setTimeout(f, 30e3);
resolve();
});
}).finally(() => {
this.fetchNewPromise = undefined;
});
};
2 years ago
if(samePeer) {
setTimeout(f, 30e3);
} else {
f();
}
3 years ago
}
public async onScrolledAllDown() {
if(this.chat.type === 'chat' || this.chat.type === 'discussion') {
const historyMaxId = await this.chat.getHistoryMaxId();
this.managers.appMessagesManager.readHistory(this.peerId, historyMaxId, this.chat.threadId, true);
}
}
3 years ago
public async finishPeerChange() {
const [isChannel, canWrite, isAnyGroup] = await Promise.all([
this.managers.appPeersManager.isChannel(this.peerId),
this.chat.canSend(),
this.chat.isAnyGroup
]);
3 years ago
return () => {
this.chatInner.classList.toggle('has-rights', canWrite);
this.container.classList.toggle('is-chat-input-hidden', !canWrite);
2 years ago
this.chatInner.classList.toggle('is-chat', isAnyGroup);
this.chatInner.classList.toggle('is-channel', isChannel);
2 years ago
this.createResizeObserver();
};
}
3 years ago
public renderMessagesQueue(options: ChatBubbles['messagesQueue'][0]) {
this.messagesQueue.push(options);
2 years ago
return this.setMessagesQueuePromise();
3 years ago
}
public setMessagesQueuePromise() {
if(!this.messagesQueue.length) return Promise.resolve();
3 years ago
if(this.messagesQueuePromise) {
return this.messagesQueuePromise;
}
2 years ago
const middleware = this.getMiddleware();
const log = this.log.bindPrefix('queue');
const possibleError = PEER_CHANGED_ERROR;
const m = middlewarePromise(middleware, possibleError);
const processQueue = async(): Promise<void> => {
log('start');
2 years ago
// if(!this.chat.setPeerPromise) {
// await pause(10000000);
// }
const renderQueue = this.messagesQueue.slice();
this.messagesQueue.length = 0;
const renderQueuePromises = renderQueue.map((promise) => {
const perf = performance.now();
promise.then((details) => {
log('render message time', performance.now() - perf, details);
});
3 years ago
return promise;
});
let loadQueue = await m(Promise.all(renderQueuePromises));
const filterQueue = (queue: typeof loadQueue) => {
return queue.filter((details) => {
// message can be deleted during rendering
return details && this.bubbles[details.bubble.dataset.mid] === details.bubble;
});
};
3 years ago
loadQueue = filterQueue(loadQueue);
3 years ago
log('messages rendered');
const reverse = loadQueue[0]?.reverse;
const {groups, avatarPromises} = this.groupBubbles(loadQueue.filter((details) => details.updatePosition));
// if(groups.length > 2 && loadQueue.length === 1) {
// debugger;
// }
2 years ago
const promises = loadQueue.reduce((acc, details) => {
const perf = performance.now();
3 years ago
const promises = details.promises.slice();
const timePromises = promises.map(async(promise) => (await promise, performance.now() - perf));
Promise.all(timePromises).then((times) => {
log.groupCollapsed('media message time', performance.now() - perf, details, times);
times.forEach((time, idx) => {
log('media message time', time, idx, promises[idx]);
});
log.groupEnd();
});
3 years ago
// if(details.updatePosition) {
// if(res) {
// groups.add(res.group);
// if(details.needAvatar) {
// details.promises.push(res.group.createAvatar(details.message));
// }
// }
// }
3 years ago
acc.push(...details.promises);
return acc;
}, [] as Promise<any>[]);
3 years ago
promises.push(...avatarPromises);
// promises.push(pause(200));
// * это нужно для того, чтобы если захочет подгрузить reply или какое-либо сообщение, то скролл не прервался
// * если добавить этот промис - в таком случае нужно сделать, чтобы скроллило к последнему сообщению после рендера
// promises.push(getHeavyAnimationPromise());
log('media promises to call', promises, loadQueue, this.isHeavyAnimationInProgress);
await m(Promise.all([...promises, this.setUnreadDelimiter()])); // не нашёл места лучше
await m(fastRafPromise()); // have to be the last
log('media promises end');
loadQueue = filterQueue(loadQueue);
3 years ago
const {restoreScroll, scrollSaver} = this.prepareToSaveScroll(reverse);
// if(this.messagesQueueOnRender) {
2 years ago
// this.messagesQueueOnRender();
// }
if(this.messagesQueueOnRenderAdditional) {
this.messagesQueueOnRenderAdditional();
}
3 years ago
this.ejectBubbles();
for(const [bubble, oldBubble] of this.bubblesToReplace) {
if(scrollSaver) {
scrollSaver.replaceSaved(oldBubble, bubble);
}
2 years ago
if(!loadQueue.find((details) => details.bubble === bubble)) {
continue;
}
const item = this.bubbleGroups.getItemByBubble(bubble);
item.mounted = false;
if(!groups.includes(item.group)) {
groups.push(item.group);
}
this.bubblesToReplace.delete(bubble);
}
2 years ago
if(this.chat.selection.isSelecting) {
loadQueue.forEach(({bubble}) => {
this.chat.selection.toggleElementCheckbox(bubble, true);
});
}
3 years ago
loadQueue.forEach(({message, bubble, updatePosition}) => {
if(message.pFlags.local && updatePosition) {
this.chatInner[(message as Message.message).pFlags.sponsored ? 'append' : 'prepend'](bubble);
return;
}
});
this.bubbleGroups.mountUnmountGroups(groups);
// this.bubbleGroups.findIncorrentPositions();
if(this.updatePlaceholderPosition) {
this.updatePlaceholderPosition();
}
if(restoreScroll) {
restoreScroll();
}
// this.setStickyDateManually();
2 years ago
if(this.messagesQueue.length) {
log('have new messages to render');
return processQueue();
} else {
log('end');
}
};
log('setting pause');
const promise = this.messagesQueuePromise = m(pause(0)).then(processQueue).finally(() => {
if(this.messagesQueuePromise === promise) {
this.messagesQueuePromise = null;
}
3 years ago
});
return promise;
}
private ejectBubbles() {
for(const bubble of this.bubblesToEject) {
bubble.remove();
// this.bubbleGroups.removeAndUnmountBubble(bubble);
}
this.bubblesToEject.clear();
3 years ago
}
public groupBubbles(items: Array<{
2 years ago
// Awaited<ReturnType<ChatBubbles['safeRenderMessage']>> &
bubble: HTMLElement,
message: Message.message | Message.messageService
}/* & {
unmountIfFound?: boolean
} */>) {
let modifiedGroups: typeof groups;
if(this.chat.type === 'scheduled') {
modifiedGroups = new Set();
items.forEach(({bubble, message}) => {
const item = this.bubbleGroups.getItemByBubble(bubble);
const group = item?.group;
if(group && item.message.date !== message.date) {
this.bubbleGroups.removeItem(item);
modifiedGroups.add(group);
}
});
}
2 years ago
items.forEach(({bubble, message}) => {
this.bubbleGroups.prepareForGrouping(bubble, message);
});
3 years ago
const groups = this.bubbleGroups.groupUngrouped();
const avatarPromises = Array.from(groups).map((group) => {
if(group.avatar) return;
const firstItem = group.firstItem;
if(firstItem && this.chat.isAvatarNeeded(firstItem.message)) {
return group.createAvatar(firstItem.message);
}
}).filter(Boolean);
if(modifiedGroups) {
for(const group of modifiedGroups) {
groups.add(group);
}
}
return {
2 years ago
groups: [...groups],
avatarPromises
};
}
public getMiddleware(additionalCallback?: () => boolean) {
return this.middleware.get(additionalCallback);
}
private async safeRenderMessage(
2 years ago
message: Message.message | Message.messageService,
reverse?: boolean,
bubble?: HTMLElement,
updatePosition = true,
processResult?: (result: ReturnType<ChatBubbles['renderMessage']>, bubble: HTMLElement) => typeof result
) {
if(!message || this.renderingMessages.has(message.mid) || (this.bubbles[message.mid] && !bubble)) {
return;
}
const middleware = this.getMiddleware();
let result: Awaited<ReturnType<ChatBubbles['renderMessage']>> & {updatePosition: typeof updatePosition};
try {
this.renderingMessages.add(message.mid);
// const groupedId = (message as Message.message).grouped_id;
const newBubble = document.createElement('div');
newBubble.dataset.mid = '' + message.mid;
newBubble.dataset.peerId = '' + message.peerId;
newBubble.dataset.timestamp = '' + message.date;
// const bubbleNew: Bubble = this.bubblesNew[message.mid] ??= {
2 years ago
// bubble: newBubble,
// mids: new Set(),
// groupedId
// };
// bubbleNew.mids.add(message.mid);
if(bubble) {
this.skippedMids.delete(message.mid);
this.bubblesToEject.add(bubble);
this.bubblesToReplace.delete(bubble);
this.bubblesToReplace.set(newBubble, bubble);
this.bubbleGroups.changeBubbleByBubble(bubble, newBubble);
3 years ago
}
bubble = this.bubbles[message.mid] = newBubble;
let originalPromise = this.renderMessage(message, reverse, bubble);
if(processResult) {
originalPromise = processResult(originalPromise, bubble);
}
2 years ago
const promise = originalPromise.then((r) => ((r && middleware() ? {...r, updatePosition} : undefined) as typeof result));
this.renderMessagesQueue(promise.catch(() => undefined));
result = await promise;
if(!middleware()) {
return;
3 years ago
}
if(!result) {
this.skippedMids.add(+message.mid);
3 years ago
}
} catch(err) {
this.log.error('renderMessage error:', err);
3 years ago
}
if(!middleware()) {
return;
}
3 years ago
this.renderingMessages.delete(message.mid);
return result;
3 years ago
}
2 years ago
3 years ago
// reverse means top
private async renderMessage(
2 years ago
message: Message.message | Message.messageService,
reverse = false,
bubble: HTMLElement
) {
// if(DEBUG) {
// this.log('message to render:', message);
// }
// if(!bubble && this.bubbles[message.mid]) {
// return;
// }
3 years ago
// await pause(1000);
const isMessage = message._ === 'message';
const groupedId = isMessage && message.grouped_id;
let albumMids: number[], reactionsMessage: Message.message;
const albumMessages = groupedId ? await this.managers.appMessagesManager.getMessagesByAlbum(groupedId) : undefined;
3 years ago
const albumMustBeRenderedFull = this.chat.type !== 'pinned';
if(groupedId && albumMustBeRenderedFull) { // will render only last album's message
albumMids = albumMessages.map((message) => message.mid);
const mainMid = getMainMidForGrouped(albumMids);
if(message.mid !== mainMid) {
3 years ago
return;
}
}
if(isMessage) {
reactionsMessage = groupedId ? albumMessages[0] : message;
}
2 years ago
3 years ago
// * can't use 'message.pFlags.out' here because this check will be used to define side of message (left-right)
const our = this.chat.isOurMessage(message);
2 years ago
3 years ago
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
2 years ago
const contentWrapper = document.createElement('div');
contentWrapper.classList.add('bubble-content-wrapper');
2 years ago
const bubbleContainer = document.createElement('div');
bubbleContainer.classList.add('bubble-content');
2 years ago
bubble.classList.add('bubble');
contentWrapper.append(bubbleContainer);
bubble.append(contentWrapper);
3 years ago
if(!our && !message.pFlags.out && this.observer) {
2 years ago
// this.log('not our message', message, message.pFlags.unread);
const isUnread = message.pFlags.unread ||
isMentionUnread(message)/* ||
(this.historyStorage.readMaxId !== undefined && this.historyStorage.readMaxId < message.mid) */;
if(isUnread) {
this.observer.observe(bubble, this.unreadedObserverCallback);
this.unreaded.set(bubble, message.mid);
}
}
3 years ago
const loadPromises: Promise<any>[] = [];
const ret = {
bubble,
promises: loadPromises,
message,
reverse
};
3 years ago
if(message._ === 'messageService' && (!message.action || !SERVICE_AS_REGULAR.has(message.action._))) {
3 years ago
const action = message.action;
if(action) {
const _ = action._;
if(IGNORE_ACTIONS.has(_) || (langPack.hasOwnProperty(_) && !langPack[_])) {
return;
3 years ago
}
}
bubble.className = 'bubble service';
bubbleContainer.innerHTML = '';
const s = document.createElement('div');
s.classList.add('service-msg');
if(action) {
let promise: Promise<any>;
3 years ago
if(action._ === 'messageActionChannelMigrateFrom') {
const peerTitle = new PeerTitle();
promise = peerTitle.update({peerId: action.chat_id.toPeerId(true)});
s.append(i18n('ChatMigration.From', [peerTitle.element]));
3 years ago
} else if(action._ === 'messageActionChatMigrateTo') {
const peerTitle = new PeerTitle();
promise = peerTitle.update({peerId: action.channel_id.toPeerId(true)});
s.append(i18n('ChatMigration.To', [peerTitle.element]));
3 years ago
} else {
s.append(await wrapMessageActionTextNew(message));
3 years ago
}
}
bubbleContainer.append(s);
if(message.pFlags.is_single) { // * Ignore 'Discussion started'
bubble.classList.add('is-group-last');
3 years ago
}
return ret;
3 years ago
}
let messageMedia: MessageMedia = isMessage && message.media;
3 years ago
let messageMessage: string, totalEntities: MessageEntity[];
if(isMessage) {
2 years ago
if((messageMedia as MessageMedia.messageMediaDocument)?.document &&
!['video', 'gif'].includes(((messageMedia as MessageMedia.messageMediaDocument).document as MyDocument).type)) {
// * just filter these cases for documents caption
} else if(groupedId && albumMustBeRenderedFull) {
const t = getAlbumText(albumMessages);
messageMessage = t.message;
2 years ago
// totalEntities = t.entities;
totalEntities = t.totalEntities;
} else if(((messageMedia as MessageMedia.messageMediaDocument)?.document as MyDocument)?.type !== 'sticker') {
messageMessage = message.message;
2 years ago
// totalEntities = message.entities;
totalEntities = message.totalEntities;
}
} else {
if(message.action._ === 'messageActionPhoneCall') {
messageMedia = {
_: 'messageMediaCall',
action: message.action
};
}
3 years ago
}
2 years ago
2 years ago
/* let richText = wrapRichText(messageMessage, {
3 years ago
entities: totalEntities
}); */
2 years ago
const richText = wrapRichText(messageMessage, {
3 years ago
entities: totalEntities,
passEntities: this.passEntities
});
let canHaveTail = true;
let isStandaloneMedia = false;
3 years ago
let needToSetHTML = true;
if(totalEntities && !messageMedia) {
2 years ago
const emojiEntities = totalEntities.filter((e) => e._ === 'messageEntityEmoji');
const strLength = messageMessage.length;
const emojiStrLength = emojiEntities.reduce((acc, curr) => acc + curr.length, 0);
if(emojiStrLength === strLength && emojiEntities.length <= 3 && totalEntities.length === emojiEntities.length) {
3 years ago
if(rootScope.settings.emoji.big) {
2 years ago
const sticker = await this.managers.appStickersManager.getAnimatedEmojiSticker(messageMessage);
3 years ago
if(emojiEntities.length === 1 && !messageMedia && sticker) {
messageMedia = {
_: 'messageMediaDocument',
document: sticker
};
} else {
2 years ago
const attachmentDiv = document.createElement('div');
3 years ago
attachmentDiv.classList.add('attachment');
2 years ago
setInnerHTML(attachmentDiv, richText);
2 years ago
3 years ago
bubble.classList.add('emoji-' + emojiEntities.length + 'x');
2 years ago
3 years ago
bubbleContainer.append(attachmentDiv);
}
bubble.classList.add('is-message-empty', 'emoji-big');
isStandaloneMedia = true;
3 years ago
canHaveTail = false;
needToSetHTML = false;
}
2 years ago
3 years ago
bubble.classList.add('can-have-big-emoji');
}
2 years ago
3 years ago
/* if(strLength === emojiStrLength) {
messageDiv.classList.add('emoji-only');
messageDiv.classList.add('message-empty');
} */
}
if(needToSetHTML) {
setInnerHTML(messageDiv, richText);
}
2 years ago
const timeSpan = MessageRender.setTime({
2 years ago
chatType: this.chat.type,
message,
reactionsMessage
2 years ago
});
messageDiv.append(timeSpan);
3 years ago
bubbleContainer.prepend(messageDiv);
2 years ago
// bubble.prepend(timeSpan, messageDiv); // that's bad
3 years ago
if(isMessage && message.views) {
2 years ago
bubble.classList.add('channel-post');
if(!message.fwd_from?.saved_from_msg_id && this.chat.type !== 'pinned') {
const forward = document.createElement('div');
forward.classList.add('bubble-beside-button', 'forward', 'tgico-forward_filled');
bubbleContainer.prepend(forward);
bubble.classList.add('with-beside-button');
}
2 years ago
if(!message.pFlags.is_outgoing && this.observer) {
this.observer.observe(bubble, this.viewsObserverCallback);
}
}
const replyMarkup = isMessage && message.reply_markup;
if(replyMarkup && replyMarkup._ === 'replyInlineMarkup' && replyMarkup.rows && replyMarkup.rows.length) {
const rows = replyMarkup.rows;
3 years ago
const containerDiv = document.createElement('div');
containerDiv.classList.add('reply-markup');
rows.forEach((row) => {
const buttons = row.buttons;
if(!buttons || !buttons.length) return;
const rowDiv = document.createElement('div');
rowDiv.classList.add('reply-markup-row');
buttons.forEach((button) => {
let text: DocumentFragment | HTMLElement | string = wrapRichText(button.text, {noLinks: true, noLinebreaks: true});
3 years ago
let buttonEl: HTMLButtonElement | HTMLAnchorElement;
2 years ago
3 years ago
switch(button._) {
case 'keyboardButtonUrl': {
2 years ago
const r = wrapRichText(' ', {
3 years ago
entities: [{
_: 'messageEntityTextUrl',
length: 1,
offset: 0,
url: button.url
}]
});
buttonEl = htmlToDocumentFragment(r).firstElementChild as HTMLAnchorElement;
buttonEl.classList.add('is-link');
3 years ago
break;
}
case 'keyboardButtonSwitchInline': {
buttonEl = document.createElement('button');
buttonEl.classList.add('is-switch-inline');
3 years ago
attachClickEvent(buttonEl, (e) => {
cancelEvent(e);
const botId = message.viaBotId || message.fromId;
3 years ago
let promise: Promise<PeerId>;
3 years ago
if(button.pFlags.same_peer) promise = Promise.resolve(this.peerId);
else promise = this.managers.appInlineBotsManager.checkSwitchReturn(botId).then((peerId) => {
3 years ago
if(peerId) {
return peerId;
}
2 years ago
3 years ago
return new Promise<PeerId>((resolve, reject) => {
const popup = new PopupForward({
[this.peerId]: []
}, (peerId) => {
3 years ago
resolve(peerId);
}, true);
popup.addEventListener('close', () => {
reject();
});
3 years ago
});
});
2 years ago
promise.then((peerId) => {
3 years ago
const threadId = this.peerId === peerId ? this.chat.threadId : undefined;
this.chat.appImManager.setInnerPeer({peerId});
this.managers.appInlineBotsManager.switchInlineQuery(peerId, threadId, botId, button.query);
3 years ago
});
});
break;
}
case 'keyboardButtonBuy': {
buttonEl = document.createElement('button');
buttonEl.classList.add('is-buy');
if(messageMedia?._ === 'messageMediaInvoice') {
if(messageMedia.receipt_msg_id) {
text = i18n('Message.ReplyActionButtonShowReceipt');
}
}
break;
}
3 years ago
default: {
buttonEl = document.createElement('button');
break;
}
}
2 years ago
buttonEl.classList.add('reply-markup-button', 'rp', 'tgico');
if(typeof(text) === 'string') {
buttonEl.insertAdjacentHTML('beforeend', text);
} else {
buttonEl.append(text);
}
3 years ago
ripple(buttonEl);
rowDiv.append(buttonEl);
});
containerDiv.append(rowDiv);
});
attachClickEvent(containerDiv, (e) => {
let target = e.target as HTMLElement;
2 years ago
3 years ago
if(!target.classList.contains('reply-markup-button')) target = findUpClassName(target, 'reply-markup-button');
2 years ago
if(
2 years ago
!target ||
target.classList.contains('is-link') ||
target.classList.contains('is-switch-inline') ||
target.classList.contains('is-buy')
2 years ago
) return;
3 years ago
cancelEvent(e);
const column = whichChild(target);
const row = rows[whichChild(target.parentElement)];
if(!row.buttons || !row.buttons[column]) {
this.log.warn('no such button', row, column, message);
return;
}
const button = row.buttons[column];
this.managers.appInlineBotsManager.callbackButtonClick(this.peerId, message.mid, button).then((callbackAnswer) => {
2 years ago
if(typeof callbackAnswer.message === 'string' && callbackAnswer.message.length) {
toast(wrapRichText(callbackAnswer.message, {noLinks: true, noLinebreaks: true}));
}
2 years ago
// console.log('callbackButtonClick callbackAnswer:', callbackAnswer);
2 years ago
});
3 years ago
});
canHaveTail = false;
bubble.classList.add('with-reply-markup');
contentWrapper.append(containerDiv);
}
2 years ago
3 years ago
const isOutgoing = message.pFlags.is_outgoing/* && this.peerId !== rootScope.myId */;
if(our) {
if(message.pFlags.unread || isOutgoing) this.unreadOut.add(message.mid);
let status = '';
if(isOutgoing) status = 'is-sending';
else status = message.pFlags.unread || (message as Message.message).pFlags.is_scheduled ? 'is-sent' : 'is-read';
3 years ago
bubble.classList.add(status);
}
if(isOutgoing) {
bubble.classList.add('is-outgoing');
}
const messageWithReplies = isMessage && await this.managers.appMessagesManager.getMessageWithCommentReplies(message);
3 years ago
const withReplies = !!messageWithReplies && message.mid > 0;
if(withReplies) {
bubble.classList.add('with-replies');
}
const fwdFrom = isMessage && message.fwd_from;
const fwdFromId = isMessage && message.fwdFromId;
const isOut = this.chat.isOutMessage(message);
3 years ago
let nameContainer: HTMLElement = bubbleContainer;
const canHideNameIfMedia = !message.viaBotId && (message.fromId === rootScope.myId || !message.pFlags.out);
3 years ago
// media
if(messageMedia/* && messageMedia._ === 'messageMediaPhoto' */) {
let attachmentDiv = document.createElement('div');
attachmentDiv.classList.add('attachment');
2 years ago
3 years ago
if(!messageMessage) {
bubble.classList.add('is-message-empty');
}
2 years ago
3 years ago
let processingWebPage = false;
2 years ago
/* if(isMessage) */switch(messageMedia._) {
3 years ago
case 'messageMediaPhoto': {
const photo = messageMedia.photo;
2 years ago
// //////this.log('messageMediaPhoto', photo);
3 years ago
if(!messageMessage) {
canHaveTail = false;
}
2 years ago
if(canHideNameIfMedia) {
2 years ago
bubble.classList.add('hide-name');
}
bubble.classList.add('photo');
2 years ago
if(albumMustBeRenderedFull && groupedId && albumMids.length !== 1) {
3 years ago
bubble.classList.add('is-album', 'is-grouped');
wrapAlbum({
messages: albumMessages,
3 years ago
attachmentDiv,
middleware: this.getMiddleware(),
isOut: our,
lazyLoadQueue: this.lazyLoadQueue,
chat: this.chat,
loadPromises,
2 years ago
autoDownload: this.chat.autoDownload
3 years ago
});
3 years ago
break;
}
2 years ago
const withTail = !IS_ANDROID && canHaveTail && !withReplies && USE_MEDIA_TAILS;
3 years ago
if(withTail) bubble.classList.add('with-media-tail');
wrapPhoto({
2 years ago
photo: photo as Photo.photo,
3 years ago
message,
container: attachmentDiv,
2 years ago
withTail,
isOut,
3 years ago
lazyLoadQueue: this.lazyLoadQueue,
middleware: this.getMiddleware(),
loadPromises,
2 years ago
autoDownloadSize: this.chat.autoDownload.photo
3 years ago
});
break;
}
2 years ago
3 years ago
case 'messageMediaWebPage': {
processingWebPage = true;
2 years ago
const webPage: WebPage = messageMedia.webpage;
// //////this.log('messageMediaWebPage', webpage);
if(webPage._ !== 'webPage') {
3 years ago
break;
2 years ago
}
3 years ago
bubble.classList.add('webpage');
2 years ago
const box = document.createElement('div');
3 years ago
box.classList.add('web');
2 years ago
const quote = document.createElement('div');
3 years ago
quote.classList.add('quote');
let previewResizer: HTMLDivElement, preview: HTMLDivElement;
const photo: Photo.photo = webPage.photo as any;
if(photo || webPage.document) {
3 years ago
previewResizer = document.createElement('div');
previewResizer.classList.add('preview-resizer');
preview = document.createElement('div');
preview.classList.add('preview');
previewResizer.append(preview);
}
2 years ago
const quoteTextDiv = document.createElement('div');
quoteTextDiv.classList.add('quote-text');
2 years ago
const doc = webPage.document as MyDocument;
if(doc) {
if(doc.type === 'gif' || doc.type === 'video' || doc.type === 'round') {
2 years ago
// if(doc.size <= 20e6) {
const mediaSize = doc.type === 'round' ? mediaSizes.active.round : mediaSizes.active.webpage;
if(doc.type === 'round') {
bubble.classList.add('round');
preview.classList.add('is-round');
} else {
bubble.classList.add('video');
}
3 years ago
wrapVideo({
2 years ago
doc,
container: preview,
message: message as Message.message,
boxWidth: mediaSize.width,
boxHeight: mediaSize.height,
3 years ago
lazyLoadQueue: this.lazyLoadQueue,
middleware: this.getMiddleware(),
isOut,
group: CHAT_ANIMATION_GROUP,
loadPromises,
autoDownload: this.chat.autoDownload,
noInfo: message.mid < 0
3 years ago
});
2 years ago
// }
3 years ago
} else {
const docDiv = await wrapDocument({
message: message as Message.message,
autoDownloadSize: this.chat.autoDownload.file,
lazyLoadQueue: this.lazyLoadQueue,
loadPromises,
sizeType: 'documentName',
searchContext: {
useSearch: false,
peerId: this.peerId,
inputFilter: {
_: 'inputMessagesFilterEmpty'
}
}
3 years ago
});
preview.append(docDiv);
preview.classList.add('preview-with-document');
quoteTextDiv.classList.add('has-document');
2 years ago
// messageDiv.classList.add((webpage.type || 'document') + '-message');
// doc = null;
3 years ago
}
}
2 years ago
if(previewResizer) {
quoteTextDiv.append(previewResizer);
}
let t: HTMLElement;
if(webPage.site_name) {
const html = wrapRichText(webPage.url);
const a: HTMLAnchorElement = htmlToDocumentFragment(html).firstElementChild as any;
a.classList.add('webpage-name');
const strong = document.createElement('strong');
setInnerHTML(strong, wrapEmojiText(webPage.site_name));
a.textContent = '';
a.append(strong);
quoteTextDiv.append(a);
t = a;
3 years ago
}
const title = wrapWebPageTitle(webPage);
if(title.textContent) {
2 years ago
const titleDiv = document.createElement('div');
3 years ago
titleDiv.classList.add('title');
const strong = document.createElement('strong');
setInnerHTML(strong, title);
titleDiv.append(strong);
3 years ago
quoteTextDiv.append(titleDiv);
t = titleDiv;
3 years ago
}
const description = wrapWebPageDescription(webPage);
if(description.textContent) {
2 years ago
const textDiv = document.createElement('div');
3 years ago
textDiv.classList.add('text');
setInnerHTML(textDiv, description);
3 years ago
quoteTextDiv.append(textDiv);
t = textDiv;
}
/* if(t) {
t.append(timeSpan);
} else {
box.classList.add('no-text');
} */
3 years ago
quote.append(quoteTextDiv);
if(photo && !doc) {
3 years ago
bubble.classList.add('photo');
const size: PhotoSize.photoSize = photo.sizes[photo.sizes.length - 1] as any;
3 years ago
let isSquare = false;
if(size.w === size.h && t) {
3 years ago
bubble.classList.add('is-square-photo');
isSquare = true;
2 years ago
setAttachmentSize(photo, preview, 48, 48, false);
/* if(t) {
t.append(timeSpan);
} */
3 years ago
} else if(size.h > size.w) {
bubble.classList.add('is-vertical-photo');
}
wrapPhoto({
2 years ago
photo,
message,
container: preview,
boxWidth: isSquare ? 0 : mediaSizes.active.webpage.width,
boxHeight: isSquare ? 0 : mediaSizes.active.webpage.height,
isOut,
lazyLoadQueue: this.lazyLoadQueue,
3 years ago
middleware: this.getMiddleware(),
loadPromises,
withoutPreloader: isSquare,
2 years ago
autoDownloadSize: this.chat.autoDownload.photo
3 years ago
});
}
2 years ago
3 years ago
box.append(quote);
2 years ago
// bubble.prepend(box);
// if(timeSpan.parentElement === messageDiv) {
2 years ago
messageDiv.insertBefore(box, timeSpan);
// } else {
// messageDiv.append(box);
// }
2 years ago
// this.log('night running', bubble.scrollHeight);
3 years ago
break;
}
2 years ago
3 years ago
case 'messageMediaDocument': {
const doc = messageMedia.document as MyDocument;
3 years ago
2 years ago
// this.log('messageMediaDocument', doc, bubble);
3 years ago
if(doc.sticker/* && doc.size <= 1e6 */) {
bubble.classList.add('sticker');
canHaveTail = false;
isStandaloneMedia = true;
2 years ago
3 years ago
if(doc.animated) {
bubble.classList.add('sticker-animated');
}
2 years ago
3 years ago
const sizes = mediaSizes.active;
const size = bubble.classList.contains('emoji-big') ? sizes.emojiSticker : (doc.animated ? sizes.animatedSticker : sizes.staticSticker);
2 years ago
setAttachmentSize(doc, attachmentDiv, size.width, size.height);
2 years ago
// let preloader = new ProgressivePreloader(attachmentDiv, false);
2 years ago
bubbleContainer.style.minWidth = attachmentDiv.style.width;
bubbleContainer.style.minHeight = attachmentDiv.style.height;
2 years ago
// appPhotosManager.setAttachmentSize(doc, bubble);
3 years ago
wrapSticker({
2 years ago
doc,
3 years ago
div: attachmentDiv,
middleware: this.getMiddleware(),
lazyLoadQueue: this.lazyLoadQueue,
group: CHAT_ANIMATION_GROUP,
2 years ago
// play: !!message.pending || !multipleRender,
3 years ago
play: true,
loop: true,
emoji: bubble.classList.contains('emoji-big') ? messageMessage : undefined,
withThumb: true,
loadPromises
});
} else if(doc.type === 'video' || doc.type === 'gif' || doc.type === 'round'/* && doc.size <= 20e6 */) {
2 years ago
// this.log('never get free 2', doc);
3 years ago
const isRound = doc.type === 'round';
if(isRound) {
isStandaloneMedia = true;
}
if(isRound || !messageMessage) {
3 years ago
canHaveTail = false;
}
if(canHideNameIfMedia) {
bubble.classList.add('hide-name');
}
2 years ago
bubble.classList.add(isRound ? 'round' : 'video');
if(albumMustBeRenderedFull && groupedId && albumMids.length !== 1) {
3 years ago
bubble.classList.add('is-album', 'is-grouped');
2 years ago
wrapAlbum({
messages: albumMessages,
3 years ago
attachmentDiv,
middleware: this.getMiddleware(),
isOut: our,
lazyLoadQueue: this.lazyLoadQueue,
chat: this.chat,
loadPromises,
2 years ago
autoDownload: this.chat.autoDownload
3 years ago
});
} else {
const withTail = !IS_ANDROID && !IS_APPLE && !isRound && canHaveTail && !withReplies && USE_MEDIA_TAILS;
3 years ago
if(withTail) bubble.classList.add('with-media-tail');
wrapVideo({
2 years ago
doc,
container: attachmentDiv,
message: message as Message.message,
3 years ago
boxWidth: mediaSizes.active.regular.width,
2 years ago
boxHeight: mediaSizes.active.regular.height,
withTail,
3 years ago
isOut,
lazyLoadQueue: this.lazyLoadQueue,
middleware: this.getMiddleware(),
group: CHAT_ANIMATION_GROUP,
loadPromises,
autoDownload: this.chat.autoDownload,
searchContext: isRound ? {
peerId: this.peerId,
inputFilter: {_: 'inputMessagesFilterRoundVoice'},
threadId: this.chat.threadId,
useSearch: !(message as Message.message).pFlags.is_scheduled,
isScheduled: (message as Message.message).pFlags.is_scheduled
} : undefined,
noInfo: message.mid < 0
3 years ago
});
}
} else {
const newNameContainer = await wrapGroupedDocuments({
3 years ago
albumMustBeRenderedFull,
message,
bubble,
messageDiv,
chat: this.chat,
loadPromises,
autoDownloadSize: this.chat.autoDownload.file,
lazyLoadQueue: this.lazyLoadQueue,
searchContext: doc.type === 'voice' || doc.type === 'audio' ? {
peerId: this.peerId,
inputFilter: {_: doc.type === 'voice' ? 'inputMessagesFilterRoundVoice' : 'inputMessagesFilterMusic'},
threadId: this.chat.threadId,
useSearch: !(message as Message.message).pFlags.is_scheduled,
isScheduled: (message as Message.message).pFlags.is_scheduled
} : undefined,
sizeType: 'documentName'
3 years ago
});
if(newNameContainer) {
nameContainer = newNameContainer;
}
const lastContainer = messageDiv.lastElementChild.querySelector('.document-message, .document, .audio');
2 years ago
// lastContainer && lastContainer.append(timeSpan.cloneNode(true));
lastContainer && lastContainer.append(timeSpan);
3 years ago
bubble.classList.remove('is-message-empty');
messageDiv.classList.add((!(['photo', 'pdf'] as MyDocument['type'][]).includes(doc.type) ? doc.type || 'document' : 'document') + '-message');
processingWebPage = true;
}
break;
}
case 'messageMediaCall': {
const action = messageMedia.action;
const div = document.createElement('div');
div.classList.add('bubble-call', action.pFlags.video ? 'tgico-videocamera' : 'tgico-phone');
const type: CallType = action.pFlags.video ? 'video' : 'voice';
div.dataset.type = type;
const title = document.createElement('div');
title.classList.add('bubble-call-title');
2 years ago
_i18n(title, isOut ?
(action.pFlags.video ? 'CallMessageVideoOutgoing' : 'CallMessageOutgoing') :
(action.pFlags.video ? 'CallMessageVideoIncoming' : 'CallMessageIncoming'));
const subtitle = document.createElement('div');
subtitle.classList.add('bubble-call-subtitle');
if(action.duration !== undefined) {
subtitle.append(formatCallDuration(action.duration));
} else {
let langPackKey: LangPackKey;
switch(action.reason._) {
case 'phoneCallDiscardReasonBusy':
langPackKey = 'Call.StatusBusy';
break;
case 'phoneCallDiscardReasonMissed':
langPackKey = 'Chat.Service.Call.Missed';
break;
// case 'phoneCallDiscardReasonHangup':
default:
langPackKey = 'Chat.Service.Call.Cancelled';
break;
}
subtitle.classList.add('is-reason');
_i18n(subtitle, langPackKey);
}
subtitle.classList.add('tgico', 'arrow-' + (action.duration !== undefined ? 'green' : 'red'));
div.append(title, subtitle);
processingWebPage = true;
bubble.classList.remove('is-message-empty');
messageDiv.classList.add('call-message');
messageDiv.append(div);
break;
}
3 years ago
case 'messageMediaContact': {
2 years ago
// this.log('wrapping contact', message);
3 years ago
const contact = messageMedia;
3 years ago
const contactDiv = document.createElement('div');
contactDiv.classList.add('contact');
contactDiv.dataset.peerId = '' + contact.user_id;
3 years ago
processingWebPage = true;
const contactDetails = document.createElement('div');
contactDetails.className = 'contact-details';
const contactNameDiv = document.createElement('div');
contactNameDiv.className = 'contact-name';
contactNameDiv.append(
2 years ago
wrapEmojiText([
contact.first_name,
contact.last_name
].filter(Boolean).join(' '))
);
const contactNumberDiv = document.createElement('div');
contactNumberDiv.className = 'contact-number';
contactNumberDiv.textContent = contact.phone_number ? '+' + formatPhoneNumber(contact.phone_number).formatted : 'Unknown phone number';
3 years ago
contactDiv.append(contactDetails);
contactDetails.append(contactNameDiv, contactNumberDiv);
3 years ago
const avatarElem = new AvatarElement();
2 years ago
avatarElem.updateWithOptions({
lazyLoadQueue: this.lazyLoadQueue,
peerId: contact.user_id.toPeerId()
});
3 years ago
avatarElem.classList.add('contact-avatar', 'avatar-54');
contactDiv.prepend(avatarElem);
bubble.classList.remove('is-message-empty');
messageDiv.classList.add('contact-message');
messageDiv.append(contactDiv);
break;
}
case 'messageMediaPoll': {
bubble.classList.remove('is-message-empty');
2 years ago
3 years ago
const pollElement = wrapPoll(message);
messageDiv.prepend(pollElement);
messageDiv.classList.add('poll-message');
break;
}
case 'messageMediaInvoice': {
const isTest = messageMedia.pFlags.test;
const photo = messageMedia.photo;
const priceEl = document.createElement(photo ? 'span' : 'div');
const f = document.createDocumentFragment();
const l = i18n(messageMedia.receipt_msg_id ? 'PaymentReceipt' : (isTest ? 'PaymentTestInvoice' : 'PaymentInvoice'));
l.classList.add('text-uppercase');
const joiner = ' ';
const p = document.createElement('span');
p.classList.add('text-bold');
p.textContent = paymentsWrapCurrencyAmount(messageMedia.total_amount, messageMedia.currency) + joiner;
f.append(p, l);
if(isTest && messageMedia.receipt_msg_id) {
const a = document.createElement('span');
a.classList.add('text-uppercase', 'pre-wrap');
a.append(joiner + '(Test)');
f.append(a);
}
setInnerHTML(priceEl, f);
if(photo) {
const mediaSize = mediaSizes.active.invoice;
wrapPhoto({
2 years ago
photo,
container: attachmentDiv,
2 years ago
withTail: false,
isOut,
lazyLoadQueue: this.lazyLoadQueue,
middleware: this.getMiddleware(),
loadPromises,
boxWidth: mediaSize.width,
boxHeight: mediaSize.height
});
bubble.classList.add('photo');
priceEl.classList.add('video-time');
attachmentDiv.append(priceEl);
} else {
attachmentDiv = undefined;
}
const titleDiv = document.createElement('div');
titleDiv.classList.add('bubble-primary-color');
2 years ago
setInnerHTML(titleDiv, wrapEmojiText(messageMedia.title));
2 years ago
2 years ago
const richText = wrapEmojiText(messageMedia.description);
messageDiv.prepend(...[titleDiv, !photo && priceEl, richText].filter(Boolean));
bubble.classList.remove('is-message-empty');
2 years ago
bubble.classList.add('is-invoice');
break;
}
2 years ago
3 years ago
default:
attachmentDiv = undefined;
3 years ago
bubble.classList.remove('is-message-empty');
messageDiv.append(i18n(UNSUPPORTED_LANG_PACK_KEY), timeSpan);
this.log.warn('unrecognized media type:', messageMedia._, message);
3 years ago
break;
}
2 years ago
if(!processingWebPage && attachmentDiv) {
3 years ago
bubbleContainer.append(attachmentDiv);
}
/* if(bubble.classList.contains('is-message-empty') && (bubble.classList.contains('photo') || bubble.classList.contains('video'))) {
bubble.classList.add('no-tail');
if(!bubble.classList.contains('with-media-tail')) {
bubble.classList.add('use-border-radius');
}
} */
}
if(isStandaloneMedia) {
bubble.classList.add('just-media');
}
3 years ago
let savedFrom = '';
2 years ago
3 years ago
// const needName = ((peerId.isAnyChat() && (peerId !== message.fromId || our)) && message.fromId !== rootScope.myId) || message.viaBotId;
const needName = (message.fromId !== rootScope.myId && this.chat.isAnyGroup) || message.viaBotId || (message as Message.message).pFlags.sponsored;
if(needName || fwdFrom || message.reply_to_mid) { // chat
let title: HTMLElement | DocumentFragment;
let titleVia: typeof title;
3 years ago
const isForwardFromChannel = message.from_id && message.from_id._ === 'peerChannel' && message.fromId === fwdFromId;
2 years ago
const isHidden = fwdFrom && !fwdFrom.from_id;
3 years ago
if(message.viaBotId) {
titleVia = document.createElement('span');
titleVia.innerText = '@' + (await this.managers.appUsersManager.getUser(message.viaBotId)).username;
titleVia.classList.add('peer-title');
bubble.classList.add('must-have-name');
}
2 years ago
if(isHidden) {
2 years ago
// /////this.log('message to render hidden', message);
3 years ago
title = document.createElement('span');
2 years ago
setInnerHTML(title, wrapEmojiText(fwdFrom.from_name));
3 years ago
title.classList.add('peer-title');
2 years ago
// title = fwdFrom.from_name;
3 years ago
bubble.classList.add('hidden-profile');
} else {
title = new PeerTitle({peerId: fwdFromId || message.fromId}).element;
3 years ago
}
if(message.reply_to_mid && message.reply_to_mid !== this.chat.threadId && isMessage) {
await MessageRender.setReply({
chat: this.chat,
bubble,
bubbleContainer,
message
});
}
2 years ago
// this.log(title);
let nameDiv: HTMLElement;
if((fwdFromId || fwdFrom)) {
3 years ago
if(this.peerId !== rootScope.myId && !isForwardFromChannel) {
bubble.classList.add('forwarded');
}
2 years ago
3 years ago
if(message.savedFrom) {
savedFrom = message.savedFrom;
title.dataset.savedFrom = savedFrom;
3 years ago
}
2 years ago
nameDiv = document.createElement('div');
title.dataset.peerId = '' + fwdFromId;
3 years ago
if((this.peerId === rootScope.myId || this.peerId === REPLIES_PEER_ID || isForwardFromChannel) && !isStandaloneMedia) {
2 years ago
nameDiv.style.color = getPeerColorById(fwdFromId, false);
nameDiv.append(title);
} else {
/* const fromTitle = message.fromId === this.myID || appPeersManager.isBroadcast(fwdFromId || message.fromId) ? '' : `<div class="name" data-peer-id="${message.fromId}" style="color: ${appPeersManager.getPeerColorByID(message.fromId, false)};">${appPeersManager.getPeerTitle(message.fromId)}</div>`;
nameDiv.innerHTML = fromTitle + 'Forwarded from ' + title; */
const args: FormatterArguments = [title];
if(isStandaloneMedia) {
args.unshift(document.createElement('br'));
3 years ago
}
nameDiv.append(i18n('ForwardedFrom', [args]));
}
} else if(!message.viaBotId) {
if(!isStandaloneMedia && needName) {
nameDiv = document.createElement('div');
3 years ago
nameDiv.append(title);
const peer = await this.managers.appPeersManager.getPeer(message.fromId);
const pFlags = (peer as User.user)?.pFlags;
if(pFlags && (pFlags.scam || pFlags.fake)) {
nameDiv.append(generateFakeIcon(pFlags.scam));
}
3 years ago
if(!our) {
2 years ago
nameDiv.style.color = getPeerColorById(message.fromId, false);
3 years ago
}
nameDiv.dataset.peerId = '' + message.fromId;
3 years ago
} else /* if(!message.reply_to_mid) */ {
bubble.classList.add('hide-name');
}
}
if(message.viaBotId) {
if(!nameDiv) {
nameDiv = document.createElement('div');
} else {
nameDiv.append(' ');
}
const span = document.createElement('span');
span.append(i18n('ViaBot'), ' ', titleVia);
span.classList.add('is-via');
nameDiv.append(span);
}
if(nameDiv) {
nameDiv.classList.add('name');
nameContainer.append(nameDiv);
}
3 years ago
} else {
bubble.classList.add('hide-name');
}
if(this.chat.type === 'pinned') {
savedFrom = `${this.chat.peerId}_${message.mid}`;
}
const isThreadStarter = messageWithReplies && messageWithReplies.mid === this.chat.threadId;
if(isThreadStarter) {
bubble.classList.add('is-thread-starter', 'is-group-last');
}
if(savedFrom && (this.chat.type === 'pinned' || fwdFrom.saved_from_msg_id) && this.peerId !== REPLIES_PEER_ID) {
3 years ago
const goto = document.createElement('div');
goto.classList.add('bubble-beside-button', 'goto-original', 'tgico-arrow_next');
bubbleContainer.append(goto);
bubble.dataset.savedFrom = savedFrom;
bubble.classList.add('with-beside-button');
}
2 years ago
3 years ago
bubble.classList.add(isOut ? 'is-out' : 'is-in');
if(withReplies) {
const isFooter = MessageRender.renderReplies({
bubble,
bubbleContainer,
message: messageWithReplies,
messageDiv,
loadPromises,
lazyLoadQueue: this.lazyLoadQueue
});
if(isFooter) {
canHaveTail = true;
}
}
2 years ago
if(isMessage) {
this.appendReactionsElementToBubble(bubble, message, reactionsMessage);
2 years ago
}
/* if(isMessage) {
const reactionHover = document.createElement('div');
reactionHover.classList.add('bubble-reaction-hover');
contentWrapper.append(reactionHover);
} */
3 years ago
if(canHaveTail) {
bubble.classList.add('can-have-tail');
bubbleContainer.append(generateTail());
}
return ret;
3 years ago
}
private appendReactionsElementToBubble(bubble: HTMLElement, message: Message.message, reactionsMessage: Message.message, changedResults?: ReactionCount[]) {
if(this.peerId.isUser()/* || true */) {
2 years ago
return;
}
if(!reactionsMessage?.reactions || !reactionsMessage.reactions.results.length) {
2 years ago
return;
}
// message = this.appMessagesManager.getMessageWithReactions(message);
const reactionsElement = new ReactionsElement();
reactionsElement.init(reactionsMessage, 'block');
reactionsElement.render(changedResults);
if(bubble.classList.contains('is-message-empty')) {
bubble.querySelector('.bubble-content-wrapper').append(reactionsElement);
} else {
const messageDiv = bubble.querySelector('.message');
if(bubble.classList.contains('is-multiple-documents')) {
const documentContainer = messageDiv.lastElementChild as HTMLElement;
let documentMessageDiv = documentContainer.querySelector('.document-message');
let timeSpan: HTMLElement = documentMessageDiv && documentMessageDiv.querySelector('.time');
if(!timeSpan) {
timeSpan = MessageRender.setTime({
2 years ago
chatType: this.chat.type,
message,
reactionsMessage
2 years ago
});
}
2 years ago
2 years ago
reactionsElement.append(timeSpan);
if(!documentMessageDiv) {
documentMessageDiv = document.createElement('div');
documentMessageDiv.classList.add('document-message');
documentContainer.querySelector('.document-wrapper').prepend(documentMessageDiv);
}
documentMessageDiv.append(reactionsElement);
} else {
const timeSpan = Array.from(bubble.querySelectorAll('.time')).pop();
reactionsElement.append(timeSpan);
2 years ago
2 years ago
messageDiv.append(reactionsElement);
}
}
}
private prepareToSaveScroll(reverse?: boolean) {
const isMounted = !!this.chatInner.parentElement;
if(!isMounted) {
return {};
}
3 years ago
const log = this.log.bindPrefix('prepareToSaveScroll');
log('save');
const scrollSaver = this.createScrollSaver(reverse);
scrollSaver.save(); // * let's save scroll position by point before the slicing, not after
2 years ago
if(this.getRenderedLength() && !this.chat.setPeerPromise) {
const viewportSlice = this.getViewportSlice();
this.deleteViewportSlice(viewportSlice, true);
3 years ago
}
2 years ago
// scrollSaver.save(); // ! slicing will corrupt scroll position
// const saved = scrollSaver.getSaved();
// const hadScroll = saved.scrollHeight !== saved.clientHeight;
3 years ago
return {
restoreScroll: () => {
log('restore');
// scrollSaver.restore(_history.length === 1 && !reverse ? false : true);
scrollSaver.restore(reverse);
this.onRenderScrollSet(scrollSaver.getSaved());
},
scrollSaver
};
}
3 years ago
public async performHistoryResult(historyResult: HistoryResult | {history: (Message.message | Message.messageService | number)[]}, reverse: boolean) {
const log = false ? this.log.bindPrefix('perform-' + (Math.random() * 1000 | 0)) : undefined;
log && log('start', this.chatInner.parentElement);
3 years ago
let history = historyResult.history;
history = history.slice(); // need
3 years ago
if(this.needReflowScroll) {
reflowScrollableElement(this.scrollable.container);
this.needReflowScroll = false;
}
const cb = (message: Message.message | Message.messageService) => {
if(!message) {
return;
} else if(message.pFlags.local) {
return this.processLocalMessageRender(message);
} else {
return this.safeRenderMessage(message, reverse);
3 years ago
}
};
const messages = await Promise.all(history.map((mid) => {
return typeof(mid) === 'number' ? this.chat.getMessage(mid) : mid;
}));
const setLoadedPromises: Promise<any>[] = [];
if(!this.scrollable.loadedAll['bottom'] || !this.scrollable.loadedAll['top']) {
let isEnd = (historyResult as HistoryResult).isEnd;
if(!isEnd) {
const historyStorage = await this.chat.getHistoryStorage();
const firstSlice = historyStorage.history.first;
const lastSlice = historyStorage.history.last;
isEnd = {top: false, bottom: false, both: false};
if(firstSlice.isEnd(SliceEnd.Bottom) && (!firstSlice.length || history.includes(firstSlice[0]))) {
isEnd.bottom = true;
}
2 years ago
if(lastSlice.isEnd(SliceEnd.Top) && (!lastSlice.length || history.includes(lastSlice[lastSlice.length - 1]))) {
isEnd.top = true;
}
3 years ago
}
if(!isEnd.bottom && this.setPeerOptions) {
const {lastMsgId, topMessage} = this.setPeerOptions;
this.setPeerOptions = undefined;
if(!lastMsgId || this.bubbles[topMessage] || lastMsgId === topMessage) {
isEnd.bottom = true;
}
3 years ago
}
if(isEnd.top) setLoadedPromises.push(this.setLoaded('top', true));
if(isEnd.bottom) setLoadedPromises.push(this.setLoaded('bottom', true));
3 years ago
}
await Promise.all(setLoadedPromises);
// ! it is important to insert bubbles to group reversed way
// const length = history.length, promises: Promise<any>[] = [];
// if(reverse) for(let i = 0; i < length; ++i) promises.push(cb(messages[i]));
// else for(let i = length - 1; i >= 0; --i) promises.push(cb(messages[i]));
const promises = messages.map(cb);
// cannot combine them into one promise
await Promise.all(promises);
await this.messagesQueuePromise;
3 years ago
if(this.scrollable.loadedAll.top && this.messagesQueueOnRenderAdditional) {
this.messagesQueueOnRenderAdditional();
if(this.messagesQueueOnRenderAdditional) {
this.messagesQueueOnRenderAdditional();
}
}
log && log('performHistoryResult end');
}
private onRenderScrollSet(state?: {scrollHeight: number, clientHeight: number}) {
const className = 'has-sticky-dates';
if(!this.container.classList.contains(className)) {
const isLoading = !this.preloader.detached;
2 years ago
if(isLoading ||
(
state ??= {
scrollHeight: this.scrollable.scrollHeight,
clientHeight: this.scrollable.container.clientHeight
2 years ago
},
state.scrollHeight !== state.clientHeight
)
) {
/* for(const timestamp in this.dateMessages) {
const dateMessage = this.dateMessages[timestamp];
dateMessage.div.classList.add('is-sticky');
} */
2 years ago
const middleware = this.getMiddleware();
const callback = () => {
if(!middleware()) return;
this.container.classList.add(className);
};
if(this.willScrollOnLoad) {
callback();
} else {
setTimeout(callback, 600);
}
return;
}
3 years ago
}
2 years ago
this.willScrollOnLoad = undefined;
3 years ago
}
public onDatePick = (timestamp: number) => {
3 years ago
const peerId = this.peerId;
this.managers.appMessagesManager.requestHistory(peerId, 0, 2, -1, timestamp, this.chat.threadId).then((history) => {
3 years ago
if(!history?.messages?.length) {
this.log.error('no history!');
return;
} else if(this.peerId !== peerId) {
return;
}
this.chat.setMessageId((history.messages[0] as MyMessage).mid);
2 years ago
// console.log('got history date:', history);
3 years ago
});
};
public requestHistory(maxId: number, loadCount: number, backLimit: number) {
2 years ago
// const middleware = this.getMiddleware();
3 years ago
if(this.chat.type === 'chat' || this.chat.type === 'discussion') {
return this.managers.acknowledged.appMessagesManager.getHistory(this.peerId, maxId, loadCount, backLimit, this.chat.threadId);
3 years ago
} else if(this.chat.type === 'pinned') {
return this.managers.acknowledged.appMessagesManager.getSearch({
2 years ago
peerId: this.peerId,
inputFilter: {_: 'inputMessagesFilterPinned'},
maxId,
limit: loadCount,
3 years ago
backLimit
}).then((ackedResult) => {
return {
cached: ackedResult.cached,
result: Promise.resolve(ackedResult.result).then((value) => {
return {history: value.history.map((m) => m.mid)};
})
};
});
3 years ago
} else if(this.chat.type === 'scheduled') {
return this.managers.acknowledged.appMessagesManager.getScheduledMessages(this.peerId).then((ackedResult) => {
3 years ago
// this.setLoaded('top', true);
// this.setLoaded('bottom', true);
return {
cached: ackedResult.cached,
result: Promise.resolve(ackedResult.result).then((mids) => ({history: mids.slice().reverse()}))
};
3 years ago
});
}
}
private async animateAsLadder(additionMsgId: number, additionMsgIds: number[], isAdditionRender: boolean, backLimit: number, maxId: number) {
/* const middleware = this.getMiddleware();
await this.ladderDeferred; */
const log = this.log.bindPrefix('ladder');
3 years ago
if(this.chat.setPeerPromise && !this.resolveLadderAnimation) {
log.warn('will be delayed');
3 years ago
// @ts-ignore
this.resolveLadderAnimation = this.animateAsLadder.bind(this, additionMsgId, additionMsgIds, isAdditionRender, backLimit, maxId);
return;
}
/* if(!middleware()) {
return;
} */
if(!Object.keys(this.bubbles).length) {
log.warn('no bubbles');
3 years ago
return;
}
let sortedMids = getObjectKeysAndSort(this.bubbles, 'desc');
if(isAdditionRender && additionMsgIds.length) {
sortedMids = sortedMids.filter((mid) => !additionMsgIds.includes(mid));
3 years ago
}
let targetMid: number;
if(backLimit) {
targetMid = maxId || Math.max(...sortedMids); // * on discussion enter
} else {
if(additionMsgId) {
targetMid = additionMsgId;
} else { // * if maxId === 0
targetMid = Math.max(...sortedMids);
}
}
const topIds = sortedMids.slice(sortedMids.findIndex((mid) => targetMid > mid));
3 years ago
const middleIds = isAdditionRender ? [] : [targetMid];
const bottomIds = isAdditionRender ? [] : sortedMids.slice(0, sortedMids.findIndex((mid) => targetMid >= mid)).reverse();
2 years ago
3 years ago
if(DEBUG) {
2 years ago
log('targeting mid:', targetMid, maxId, additionMsgId,
topIds.map((m) => getServerMessageId(m)),
bottomIds.map((m) => getServerMessageId(m)));
3 years ago
}
const setBubbles: HTMLElement[] = [];
this.chatInner.classList.add('zoom-fading');
const delay = isAdditionRender ? 10 : 40;
const offsetIndex = isAdditionRender ? 0 : 1;
const animateAsLadder = (mids: number[], offsetIndex = 0) => {
const animationPromise = deferredPromise<void>();
let lastMsDelay = 0;
mids.forEach((mid, idx) => {
const bubble = this.bubbles[mid];
if(!bubble || this.skippedMids.has(mid)) {
log.warn('no bubble by mid:', mid);
3 years ago
return;
}
lastMsDelay = ((idx + offsetIndex) || 0.1) * delay;
2 years ago
// lastMsDelay = (idx + offsetIndex) * delay;
// lastMsDelay = (idx || 0.1) * 1000;
3 years ago
const contentWrapper = bubble.lastElementChild as HTMLElement;
const elementsToAnimate: HTMLElement[] = [contentWrapper];
const item = this.bubbleGroups.getItemByBubble(bubble);
if(item && item.group.avatar && item.group.lastItem === item) {
elementsToAnimate.push(item.group.avatar);
}
elementsToAnimate.forEach((element) => {
element.classList.add('zoom-fade', 'can-zoom-fade');
element.style.transitionDelay = lastMsDelay + 'ms';
});
2 years ago
3 years ago
if(idx === (mids.length - 1)) {
const onTransitionEnd = (e: TransitionEvent) => {
if(e.target !== contentWrapper) {
return;
}
animationPromise.resolve();
contentWrapper.removeEventListener('transitionend', onTransitionEnd);
};
contentWrapper.addEventListener('transitionend', onTransitionEnd);
}
setBubbles.push(...elementsToAnimate);
3 years ago
});
if(!mids.length) {
animationPromise.resolve();
}
return {lastMsDelay, animationPromise};
};
const topRes = animateAsLadder(topIds, offsetIndex);
const middleRes = animateAsLadder(middleIds);
const bottomRes = animateAsLadder(bottomIds, offsetIndex);
const promises = [topRes.animationPromise, middleRes.animationPromise, bottomRes.animationPromise];
const delays: number[] = [topRes.lastMsDelay, middleRes.lastMsDelay, bottomRes.lastMsDelay];
if(this.onAnimateLadder) {
await this.onAnimateLadder();
}
fastRaf(() => {
this.setStickyDateManually(); // ! maybe it's not efficient
setBubbles.forEach((element) => {
element.classList.remove('zoom-fade');
3 years ago
});
});
let promise: Promise<any>;
if(topIds.length || middleIds.length || bottomIds.length) {
promise = Promise.all(promises);
dispatchHeavyAnimationEvent(promise, Math.max(...delays) + 200) // * 200 - transition time
2 years ago
.then(() => {
3 years ago
fastRaf(() => {
setBubbles.forEach((element) => {
element.style.transitionDelay = '';
element.classList.remove('can-zoom-fade');
3 years ago
});
this.chatInner.classList.remove('zoom-fading');
});
2 years ago
// ! в хроме, каким-то образом из-за zoom-fade класса начинает прыгать скролл при подгрузке сообщений вверх,
3 years ago
// ! т.е. скролл не ставится, так же, как в сафари при translateZ на блок выше scrollable
// if(!IS_SAFARI) {
// this.needReflowScroll = true;
// }
3 years ago
});
}
return promise;
}
private async renderEmptyPlaceholder(
2 years ago
type: 'group' | 'saved' | 'noMessages' | 'noScheduledMessages' | 'greeting' | 'restricted',
bubble: HTMLElement,
message: any,
elements: (Node | string)[]
) {
3 years ago
const BASE_CLASS = 'empty-bubble-placeholder';
bubble.classList.add(BASE_CLASS, BASE_CLASS + '-' + type);
2 years ago
let title: HTMLElement;
3 years ago
if(type === 'group') title = i18n('GroupEmptyTitle1');
else if(type === 'saved') title = i18n('ChatYourSelfTitle');
else if(type === 'noMessages' || type === 'greeting') title = i18n('NoMessages');
else if(type === 'noScheduledMessages') title = i18n('NoScheduledMessages');
else if(type === 'restricted') {
title = document.createElement('span');
title.innerText = await this.managers.appPeersManager.getRestrictionReasonText(this.peerId);
}
3 years ago
title.classList.add('center', BASE_CLASS + '-title');
elements.push(title);
let listElements: HTMLElement[];
if(type === 'group') {
elements.push(i18n('GroupEmptyTitle2'));
listElements = [
i18n('GroupDescription1'),
i18n('GroupDescription2'),
i18n('GroupDescription3'),
i18n('GroupDescription4')
];
} else if(type === 'saved') {
listElements = [
i18n('ChatYourSelfDescription1'),
i18n('ChatYourSelfDescription2'),
i18n('ChatYourSelfDescription3'),
i18n('ChatYourSelfDescription4')
];
} else if(type === 'greeting') {
const subtitle = i18n('NoMessagesGreetingsDescription');
subtitle.classList.add('center', BASE_CLASS + '-subtitle');
// findAndSplice(this.messagesQueue, q => q.bubble === bubble);
3 years ago
const stickerDiv = document.createElement('div');
stickerDiv.classList.add(BASE_CLASS + '-sticker');
const middleware = this.getMiddleware();
2 years ago
await this.managers.appStickersManager.getGreetingSticker().then(async(doc) => {
3 years ago
if(!middleware()) return;
const loadPromises: Promise<any>[] = [];
await wrapSticker({
2 years ago
doc,
3 years ago
// doc: appDocsManager.getDoc("5431607541660389336"), // cubigator mockup
div: stickerDiv,
middleware,
lazyLoadQueue: this.lazyLoadQueue,
group: CHAT_ANIMATION_GROUP,
2 years ago
// play: !!message.pending || !multipleRender,
3 years ago
play: true,
loop: true,
withThumb: true,
loadPromises
});
attachClickEvent(stickerDiv, (e) => {
cancelEvent(e);
EmoticonsDropdown.onMediaClick({target: e.target});
});
return Promise.all(loadPromises);
});
// this.renderMessagesQueue({
2 years ago
// message,
// bubble,
// reverse: false,
// promises: [loadPromise]
// });
3 years ago
elements.push(subtitle, stickerDiv);
}
if(listElements) {
elements.push(
...listElements.map((elem) => {
3 years ago
const span = document.createElement('span');
span.classList.add(BASE_CLASS + '-list-item');
span.append(elem);
return span;
})
);
2 years ago
3 years ago
if(type === 'group') {
listElements.forEach((elem) => {
3 years ago
const i = document.createElement('span');
i.classList.add('tgico-check');
elem.prepend(i);
});
} else if(type === 'saved') {
listElements.forEach((elem) => {
3 years ago
const i = document.createElement('span');
i.classList.add(BASE_CLASS + '-list-bullet');
i.innerText = '•';
elem.prepend(i);
});
}
}
if(elements.length > 1) {
bubble.classList.add('has-description');
}
elements.forEach((element: any) => element.classList.add(BASE_CLASS + '-line'));
}
private async processLocalMessageRender(message: Message.message | Message.messageService, animate?: boolean) {
const isSponsored = !!(message as Message.message).pFlags.sponsored;
const middleware = this.getMiddleware();
const m = middlewarePromise(middleware);
2 years ago
return this.safeRenderMessage(message, isSponsored ? false : true, undefined, isSponsored, async(result) => {
const {bubble} = await m(result);
if(!bubble) {
return result;
}
bubble.classList.add('is-group-last', 'is-group-first');
const updatePosition = () => {
if(this.updatePlaceholderPosition === updatePosition) {
this.updatePlaceholderPosition = undefined;
}
appendTo[method](bubble);
};
if(!isSponsored) {
bubble.classList.add('bubble-first');
bubble.classList.remove('can-have-tail', 'is-in');
}
const elements: (Node | string)[] = [];
const isBot = await m(this.managers.appPeersManager.isBot(this.peerId));
let renderPromise: Promise<any>, appendTo = this.container, method: 'append' | 'prepend' = 'append';
if(this.chat.isRestricted) {
renderPromise = this.renderEmptyPlaceholder('restricted', bubble, message, elements);
} else if(isSponsored) {
let text: LangPackKey, mid: number, startParam: string, callback: () => void;
bubble.classList.add('avoid-selection');
const sponsoredMessage = this.sponsoredMessage = (message as Message.message).sponsoredMessage;
const peerId = getPeerId(sponsoredMessage.from_id);
// const peer = this.appPeersManager.getPeer(peerId);
if(sponsoredMessage.channel_post) {
text = 'OpenChannelPost';
mid = generateMessageId(sponsoredMessage.channel_post);
} else if(sponsoredMessage.start_param || isBot) {
text = 'Chat.Message.ViewBot';
startParam = sponsoredMessage.start_param;
} else {
text = await this.managers.appPeersManager.isAnyGroup(peerId) ? 'Chat.Message.ViewGroup' : 'Chat.Message.ViewChannel';
}
if(sponsoredMessage.chat_invite) {
callback = () => {
new PopupJoinChatInvite(sponsoredMessage.chat_invite_hash, sponsoredMessage.chat_invite as ChatInvite.chatInvite);
};
} else if(sponsoredMessage.chat_invite_hash) {
callback = () => {
const link: InternalLink = {
_: INTERNAL_LINK_TYPE.JOIN_CHAT,
invite: sponsoredMessage.chat_invite_hash
};
2 years ago
this.chat.appImManager.processInternalLink(link);
};
} else {
callback = () => {
this.chat.appImManager.setInnerPeer({
peerId,
lastMsgId: mid,
startParam
});
};
}
const button = Button('btn-primary btn-primary-transparent bubble-view-button', {
text
});
this.observer.observe(button, this.viewsObserverCallback);
if(callback) {
attachClickEvent(button, callback);
}
bubble.querySelector('.bubble-content').prepend(button);
return result;
} else if(isBot && message._ === 'message') {
const b = document.createElement('b');
b.append(i18n('BotInfoTitle'));
elements.push(b, '\n\n');
appendTo = this.chatInner;
method = 'prepend';
} else if(await m(this.managers.appPeersManager.isAnyGroup(this.peerId)) && (await m(this.managers.appPeersManager.getPeer(this.peerId))).pFlags.creator) {
renderPromise = this.renderEmptyPlaceholder('group', bubble, message, elements);
} else if(this.chat.type === 'scheduled') {
renderPromise = this.renderEmptyPlaceholder('noScheduledMessages', bubble, message, elements);
} else if(rootScope.myId === this.peerId) {
renderPromise = this.renderEmptyPlaceholder('saved', bubble, message, elements);
} else if(this.peerId.isUser() && !isBot && await m(this.chat.canSend()) && this.chat.type === 'chat') {
renderPromise = this.renderEmptyPlaceholder('greeting', bubble, message, elements);
} else {
renderPromise = this.renderEmptyPlaceholder('noMessages', bubble, message, elements);
}
if(renderPromise) {
await renderPromise;
}
if(elements.length) {
const messageDiv = bubble.querySelector('.message, .service-msg');
messageDiv.prepend(...elements);
}
const isWaitingForAnimation = !!this.messagesQueueOnRenderAdditional;
const noTransition = this.setPeerCached && !isWaitingForAnimation;
if(noTransition) {
const setOn = bubble.firstElementChild;
setOn.classList.add('no-transition');
2 years ago
if(this.chat.setPeerPromise) {
this.chat.setPeerPromise.catch(noop).finally(() => {
setOn.classList.remove('no-transition');
});
}
}
if(animate === undefined && !noTransition) {
animate = true;
}
if(isWaitingForAnimation || animate) {
this.updatePlaceholderPosition = updatePosition;
3 years ago
this.onAnimateLadder = () => {
// appendTo[method](bubble);
this.onAnimateLadder = undefined;
3 years ago
// need raf here because animation won't fire if this message is single
if(!this.messagesQueuePromise) {
return fastRafPromise();
}
};
} else if(this.chat.setPeerPromise) {
this.attachPlaceholderOnRender = () => {
this.attachPlaceholderOnRender = undefined;
updatePosition();
// appendTo[method](bubble);
};
} else {
this.updatePlaceholderPosition = updatePosition;
// appendTo[method](bubble);
}
if(!isWaitingForAnimation && animate) {
await m(getHeavyAnimationPromise());
const additionMsgIds = getObjectKeysAndSort(this.bubbles);
indexOfAndSplice(additionMsgIds, message.mid);
this.animateAsLadder(message.mid, additionMsgIds, false, 0, 0);
}
3 years ago
// if(!isSponsored) {
2 years ago
this.emptyPlaceholderBubble = bubble;
// }
3 years ago
return result;
});
3 years ago
}
private generateLocalMessageId(addOffset = 0) {
// const INCREMENT = 0x10;
2 years ago
const offset = (this.chat.type === 'scheduled' ? -1 : 0) + addOffset;
// offset = generateMessageId(offset);
// id: -Math.abs(+this.peerId * INCREMENT + offset),
const id = -Math.abs(offset);
const mid = -Math.abs(generateMessageId(id));
return {id, mid};
}
3 years ago
private async generateLocalFirstMessage<T extends boolean>(service?: T, fill?: (message: GenerateLocalMessageType<T>) => void, addOffset = 0): Promise<GenerateLocalMessageType<T>> {
const {id, mid} = this.generateLocalMessageId(addOffset);
let message: Omit<Message.message | Message.messageService, 'message'> & {message?: string} = {
3 years ago
_: service ? 'messageService' : 'message',
date: 0,
id,
mid,
peer_id: await this.managers.appPeersManager.getOutputPeer(this.peerId),
pFlags: {
local: true
}
3 years ago
};
if(!service) {
message.message = '';
}/* else {
(message as Message.messageService).action = {} as any;
} */
3 years ago
assumeType<GenerateLocalMessageType<T>>(message);
fill && fill(message);
const savedMessages = await this.managers.appMessagesManager.saveMessages([message], {storage: new Map() as any});
message = savedMessages[0];
message.mid = mid;
return message as any;
3 years ago
}
public getViewportSlice() {
// this.log.trace('viewport slice');
return getViewportSlice({
2 years ago
overflowElement: this.scrollable.container,
selector: '.bubbles-date-group .bubble:not(.is-date)',
extraSize: Math.max(700, windowSize.height) * 2
});
}
public deleteViewportSlice(slice: ReturnType<ChatBubbles['getViewportSlice']>, ignoreScrollSaving?: boolean) {
if(DO_NOT_SLICE_VIEWPORT_ON_RENDER) {
return;
}
const {invisibleTop, invisibleBottom} = slice;
const invisible = invisibleTop.concat(invisibleBottom);
if(!invisible.length) {
return;
}
if(invisibleTop.length) {
this.setLoaded('top', false);
this.getHistoryTopPromise = undefined;
}
if(invisibleBottom.length) {
this.setLoaded('bottom', false);
this.getHistoryBottomPromise = undefined;
}
const mids = invisible.map(({element}) => +element.dataset.mid);
let scrollSaver: ScrollSaver;
if(!!invisibleTop.length !== !!invisibleBottom.length && !ignoreScrollSaving) {
scrollSaver = this.createScrollSaver(!!invisibleTop.length);
scrollSaver.save();
}
2 years ago
this.deleteMessagesByIds(mids, false, true);
if(scrollSaver) {
scrollSaver.restore();
} else if(invisibleTop.length) {
this.scrollable.lastScrollPosition = this.scrollable.scrollTop;
}
}
public sliceViewport(ignoreHeavyAnimation?: boolean) {
// Safari cannot reset the scroll.
if(IS_SAFARI || (this.isHeavyAnimationInProgress && !ignoreHeavyAnimation) || DO_NOT_SLICE_VIEWPORT) {
return;
}
2 years ago
// const scrollSaver = new ScrollSaver(this.scrollable, true);
// scrollSaver.save();
const slice = this.getViewportSlice();
// if(IS_SAFARI) slice.invisibleTop = [];
this.deleteViewportSlice(slice);
// scrollSaver.restore();
}
private async setLoaded(side: SliceSides, value: boolean, checkPlaceholders = true) {
3 years ago
const willChange = this.scrollable.loadedAll[side] !== value;
if(!willChange) {
return;
}
const log = this.log.bindPrefix('setLoaded');
log('change', side, value);
3 years ago
this.scrollable.loadedAll[side] = value;
// return;
if(!checkPlaceholders) {
3 years ago
return;
}
3 years ago
if(!this.chat.isRestricted) {
if(side === 'bottom' && await this.managers.appPeersManager.isBroadcast(this.peerId)/* && false */) {
this.toggleSponsoredMessage(value);
}
2 years ago
if(side === 'top' && value && await this.managers.appPeersManager.isBot(this.peerId)) {
return this.renderBotPlaceholder();
}
}
return this.checkIfEmptyPlaceholderNeeded();
}
private async toggleSponsoredMessage(value: boolean) {
const _log = this.log.bindPrefix('sponsored');
_log('checking');
const {mid} = this.generateLocalMessageId(SPONSORED_MESSAGE_ID_OFFSET);
if(value) {
const middleware = this.getMiddleware(() => {
return this.scrollable.loadedAll.bottom && !this.bubbles[mid] && this.getSponsoredMessagePromise === promise;
});
const promise = this.getSponsoredMessagePromise = this.managers.appChatsManager.getSponsoredMessage(this.peerId.toChatId())
.then(async(sponsoredMessages) => {
const sponsoredMessage = sponsoredMessages.messages[0];
if(!sponsoredMessage) {
_log('no message');
return;
}
const messagePromise = this.generateLocalFirstMessage(false, (message) => {
message.message = sponsoredMessage.message;
message.from_id = sponsoredMessage.from_id;
message.entities = sponsoredMessage.entities;
message.pFlags.sponsored = true;
message.sponsoredMessage = sponsoredMessage;
}, SPONSORED_MESSAGE_ID_OFFSET);
2 years ago
return Promise.all([
messagePromise,
this.getHistoryTopPromise, // wait for top load and execute rendering after or with it
this.messagesQueuePromise
]).then(([message]) => {
if(!middleware()) return;
// this.processLocalMessageRender(message);
_log('rendering', message);
const promise = this.performHistoryResult({history: [message]}, false);
});
}).finally(() => {
this.getSponsoredMessagePromise = undefined;
});
} else {
_log('clearing rendered', mid);
this.deleteMessagesByIds([mid]);
this.getSponsoredMessagePromise = undefined;
}
}
private async renderBotPlaceholder() {
const _log = this.log.bindPrefix('bot placeholder');
2 years ago
const middleware = this.getMiddleware();
const result = await this.managers.acknowledged.appProfileManager.getProfile(this.peerId.toUserId());
_log('getting profile, cached:', result.cached);
const processPromise = result.result.then(async(userFull) => {
if(!middleware()) {
return;
}
3 years ago
if(!userFull.bot_info?.description) {
_log.warn('no description');
return this.checkIfEmptyPlaceholderNeeded();
}
3 years ago
const message = await this.generateLocalFirstMessage(false, (message) => {
const botInfo = userFull.bot_info;
message.message = botInfo.description;
if(botInfo.description_document) message.media = {_: 'messageMediaDocument', document: botInfo.description_document};
if(botInfo.description_photo) message.media = {_: 'messageMediaPhoto', photo: botInfo.description_photo};
});
3 years ago
if(!middleware()) {
return;
}
3 years ago
_log('rendering');
const renderPromise = this.processLocalMessageRender(message, !result.cached).then(() => {
_log('done');
3 years ago
});
return {renderPromise};
});
if(!result.cached) {
return;
3 years ago
}
return processPromise;
3 years ago
}
public async checkIfEmptyPlaceholderNeeded() {
2 years ago
if(this.scrollable.loadedAll.top &&
this.scrollable.loadedAll.bottom &&
this.emptyPlaceholderBubble === undefined &&
3 years ago
(
2 years ago
this.chat.isRestricted ||
!(await this.chat.getHistoryStorage()).count ||
3 years ago
(
2 years ago
!Object.keys(this.bubbles).length ||
// ! WARNING ! ! ! ! ! ! REPLACE LINE ABOVE WITH THESE
2 years ago
Object.keys(this.bubbles).length &&
3 years ago
!this.getRenderedLength()
) ||
(this.chat.type === 'scheduled' && !Object.keys(this.bubbles).length)
)
) {
this.log('inject empty peer placeholder');
const message = await this.generateLocalFirstMessage(true);
return {renderPromise: this.processLocalMessageRender(message)};
3 years ago
}
}
public getHistory1(maxId?: number, reverse?: boolean, isBackLimit?: boolean, additionMsgId?: number, justLoad?: boolean) {
const middleware = this.getMiddleware(justLoad ? undefined : () => {
return (reverse ? this.getHistoryTopPromise : this.getHistoryBottomPromise) === waitPromise;
});
const result = this.getHistory(maxId, reverse, isBackLimit, additionMsgId, justLoad, middleware);
const waitPromise = result.then((res) => res && (res.waitPromise || res.promise));
(reverse ? this.getHistoryTopPromise = waitPromise : this.getHistoryBottomPromise = waitPromise);
waitPromise.then(() => {
if(!middleware()) {
return;
}
(reverse ? this.getHistoryTopPromise = undefined : this.getHistoryBottomPromise = undefined);
if(!justLoad) {
// preload more
2 years ago
// if(!isFirstMessageRender) {
if(this.chat.type === 'chat'/* || this.chat.type === 'discussion' */) {
/* const storage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId);
const isMaxIdInHistory = storage.history.indexOf(maxId) !== -1;
if(isMaxIdInHistory || true) { // * otherwise it is a search or jump */
2 years ago
setTimeout(() => {
if(reverse) {
this.loadMoreHistory(true, true);
} else {
this.loadMoreHistory(false, true);
}
}, 0);
// }
}
// }
}
});
3 years ago
return result;
3 years ago
}
/**
* Load and render history
* @param maxId max message id
* @param reverse 'true' means up
* @param isBackLimit is search
* @param additionMsgId for the last message
* @param justLoad do not render
*/
public async getHistory(
2 years ago
maxId = 0,
reverse = false,
isBackLimit = false,
additionMsgId = 0,
justLoad = false,
middleware?: () => boolean
): Promise<{cached: boolean, promise: Promise<void>, waitPromise: Promise<any>}> {
3 years ago
const peerId = this.peerId;
const isBroadcast = await this.managers.appPeersManager.isBroadcast(peerId);
2 years ago
// console.time('appImManager call getHistory');
const pageCount = Math.min(30, windowSize.height / 40/* * 1.25 */ | 0);
2 years ago
// const loadCount = Object.keys(this.bubbles).length > 0 ? 50 : pageCount;
const realLoadCount = isBroadcast ? 20 : (Object.keys(this.bubbles).length > 0 ? Math.max(35, pageCount) : pageCount);
2 years ago
// const realLoadCount = pageCount;//const realLoadCount = 50;
3 years ago
let loadCount = realLoadCount;
2 years ago
3 years ago
/* if(TEST_SCROLL) {
//loadCount = 1;
if(Object.keys(this.bubbles).length > 0)
return {cached: false, promise: Promise.resolve(true)};
} */
if(TEST_SCROLL !== undefined) {
if(TEST_SCROLL) {
if(Object.keys(this.bubbles).length > 0) {
--TEST_SCROLL;
}
} else {
return {cached: false, promise: Promise.resolve(), waitPromise: Promise.resolve()};
3 years ago
}
}
2 years ago
// //console.time('render history total');
3 years ago
let backLimit = 0;
if(isBackLimit) {
backLimit = loadCount;
if(!reverse) { // if not jump
loadCount = 0;
2 years ago
// maxId = this.appMessagesManager.incrementMessageId(maxId, 1);
3 years ago
}
}
let additionMsgIds: number[];
if(additionMsgId && !isBackLimit) {
if(this.chat.type === 'pinned') {
additionMsgIds = [additionMsgId];
} else {
const historyStorage = await this.chat.getHistoryStorage();
3 years ago
const slice = historyStorage.history.slice;
if(slice.length < loadCount && !slice.isEnd(SliceEnd.Both)) {
additionMsgIds = slice.slice();
// * filter last album, because we don't know is it the last item
for(let i = additionMsgIds.length - 1; i >= 0; --i) {
const message = await this.chat.getMessage(additionMsgIds[i]);
if((message as Message.message)?.grouped_id) additionMsgIds.splice(i, 1);
3 years ago
else break;
}
maxId = additionMsgIds[additionMsgIds.length - 1] || maxId;
}
}
}
2 years ago
/* const result = additionMsgID ?
{history: [additionMsgID]} :
3 years ago
appMessagesManager.getHistory(this.peerId, maxId, loadCount, backLimit); */
let result: AckedResult<MyHistoryResult> = await this.requestHistory(maxId, loadCount, backLimit) as any;
let resultPromise: typeof result['result'];
3 years ago
2 years ago
// const isFirstMessageRender = !!additionMsgID && result.cached && !appMessagesManager.getMessage(additionMsgID).grouped_id;
const isAdditionRender = additionMsgIds?.length && !result.cached;
const isFirstMessageRender = (this.isFirstLoad && backLimit && !result.cached) || isAdditionRender;
3 years ago
if(isAdditionRender) {
resultPromise = result.result;
result = {
cached: true,
result: Promise.resolve({history: additionMsgIds})
};
2 years ago
// additionMsgID = 0;
3 years ago
}
this.isFirstLoad = false;
const processResult = async(historyResult: Awaited<typeof result['result']>) => {
if((historyResult as HistoryResult).isEnd?.top) {
3 years ago
if(this.chat.type === 'discussion') { // * inject discussion start
const serviceStartMessageId = await this.managers.appMessagesManager.getThreadServiceMessageId(this.peerId, this.chat.threadId);
3 years ago
if(serviceStartMessageId) historyResult.history.push(serviceStartMessageId);
const mids = await this.chat.getMidsByMid(this.chat.threadId);
historyResult.history.push(...mids.reverse());
3 years ago
}
// synchronize bot placeholder appearance
await this.managers.appProfileManager.getProfileByPeerId(peerId);
// await this.setLoaded('top', true);
3 years ago
}
};
const sup = (historyResult: Awaited<typeof result['result']>) => {
3 years ago
return getHeavyAnimationPromise().then(() => {
return processResult(historyResult);
3 years ago
}).then(() => {
if(!isAdditionRender && additionMsgId) {
historyResult.history.unshift(additionMsgId);
}
return this.performHistoryResult(historyResult, reverse);
3 years ago
});
};
const processPromise = (_promise: typeof result['result']) => {
const promise = Promise.resolve(_promise).then((result) => {
if(middleware && !middleware()) {
throw PEER_CHANGED_ERROR;
3 years ago
}
if(justLoad) {
// нужно делать из-за ранней прогрузки
this.scrollable.onScroll();
// fastRaf(() => {
// this.scrollable.checkForTriggers();
// });
return;
3 years ago
}
return sup(result);
}, (err) => {
this.log.error('getHistory error:', err);
throw err;
});
2 years ago
3 years ago
return promise;
};
let promise: Promise<void>, cached: boolean;
if(!result.cached) {
3 years ago
cached = false;
promise = processPromise(result.result);
3 years ago
} else if(justLoad) {
// нужно делать из-за ранней прогрузки
this.scrollable.onScroll();
3 years ago
return null;
} else {
cached = true;
promise = sup(await result.result);
3 years ago
}
const waitPromise = isAdditionRender ? processPromise(resultPromise) : promise;
if(isFirstMessageRender && rootScope.settings.animationsEnabled/* && false */) {
let times = isAdditionRender ? 2 : 1;
this.messagesQueueOnRenderAdditional = () => {
this.log('messagesQueueOnRenderAdditional');
3 years ago
if(--times) return;
this.messagesQueueOnRenderAdditional = undefined;
2 years ago
3 years ago
const promise = this.animateAsLadder(additionMsgId, additionMsgIds, isAdditionRender, backLimit, maxId);
promise.then(() => {
3 years ago
setTimeout(() => { // preload messages
this.loadMoreHistory(reverse, true);
}, 0);
});
};
} else {
this.messagesQueueOnRenderAdditional = undefined;
}
if(justLoad) {
return null;
}
return {cached, promise, waitPromise};
3 years ago
}
public async setUnreadDelimiter() {
3 years ago
if(!(this.chat.type === 'chat' || this.chat.type === 'discussion')) {
return;
}
if(this.attachedUnreadBubble) {
return;
}
const historyMaxId = await this.chat.getHistoryMaxId();
let readMaxId = await this.managers.appMessagesManager.getReadMaxIdIfUnread(this.peerId, this.chat.threadId);
3 years ago
if(!readMaxId) return;
readMaxId = Object.keys(this.bubbles)
.filter((mid) => !this.bubbles[mid].classList.contains('is-out'))
.map((i) => +i)
3 years ago
.sort((a, b) => a - b)
.find((i) => i > readMaxId);
3 years ago
if(readMaxId && this.bubbles[readMaxId]) {
2 years ago
const bubble = this.bubbles[readMaxId];
3 years ago
if(this.firstUnreadBubble && this.firstUnreadBubble !== bubble) {
this.firstUnreadBubble.classList.remove('is-first-unread');
this.firstUnreadBubble = null;
}
if(readMaxId !== historyMaxId) {
3 years ago
bubble.classList.add('is-first-unread');
}
this.firstUnreadBubble = bubble;
this.attachedUnreadBubble = true;
}
}
public deleteEmptyDateGroups() {
const mustBeCount = this.stickyIntersector ? STICKY_OFFSET : 1;
let deleted = false;
3 years ago
for(const i in this.dateMessages) {
const dateMessage = this.dateMessages[i];
if(dateMessage.container.childElementCount === mustBeCount) { // only date div + sentinel div
dateMessage.container.remove();
if(this.stickyIntersector) {
this.stickyIntersector.unobserve(dateMessage.container, dateMessage.div);
}
delete this.dateMessages[i];
deleted = true;
// * no sense in it
/* if(dateMessage.div === this.previousStickyDate) {
this.previousStickyDate = undefined;
} */
3 years ago
}
}
if(!deleted) {
return;
}
if(!Object.keys(this.dateMessages).length) {
this.container.classList.remove('has-groups');
}
3 years ago
this.checkIfEmptyPlaceholderNeeded();
this.setStickyDateManually();
3 years ago
}
}
export function generateTail() {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttributeNS(null, 'viewBox', '0 0 11 20');
svg.setAttributeNS(null, 'width', '11');
svg.setAttributeNS(null, 'height', '20');
svg.classList.add('bubble-tail');
const use = document.createElementNS('http://www.w3.org/2000/svg', 'use');
use.setAttributeNS(null, 'href', '#message-tail-filled');
svg.append(use);
return svg;
}