Browse Source

Fix chat infinity loading down side

master
Eduard Kuzmenko 4 years ago
parent
commit
b9d6f916a4
  1. 52
      src/components/chat/bubbles.ts
  2. 8
      src/components/sidebarRight/tabs/sharedMedia.ts
  3. 14
      src/lib/appManagers/appChatsManager.ts
  4. 26
      src/lib/appManagers/appMessagesManager.ts

52
src/components/chat/bubbles.ts

@ -37,7 +37,7 @@ import Chat from "./chat"; @@ -37,7 +37,7 @@ import Chat from "./chat";
import ListenerSetter from "../../helpers/listenerSetter";
import PollElement from "../poll";
import AudioElement from "../audio";
import { MessageReplies, MessageReplyHeader } from "../../layer";
import { MessageEntity, MessageReplies, MessageReplyHeader } from "../../layer";
const IGNORE_ACTIONS = ['messageActionHistoryClear'];
@ -752,7 +752,7 @@ export default class ChatBubbles { @@ -752,7 +752,7 @@ export default class ChatBubbles {
if(!this.peerId || /* TEST_SCROLL || */ this.chat.setPeerPromise || (top && this.getHistoryTopPromise) || (!top && this.getHistoryBottomPromise)) return;
// warning, если иды только отрицательные то вниз не попадёт (хотя мб и так не попадёт)
let history = Object.keys(this.bubbles).map(id => +id).filter(id => id > 0).sort((a, b) => a - b);
const history = Object.keys(this.bubbles).map(id => +id).sort((a, b) => a - b);
if(!history.length) return;
if(top && !this.scrolledAll) {
@ -1100,15 +1100,15 @@ export default class ChatBubbles { @@ -1100,15 +1100,15 @@ export default class ChatBubbles {
const samePeer = this.peerId == peerId;
const historyStorage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId);
let topMessage = lastMsgId <= 0 ? lastMsgId : historyStorage.maxId ?? 0;
let topMessage = this.chat.type === 'pinned' ? this.appMessagesManager.pinnedMessages[peerId].maxId : historyStorage.maxId ?? 0;
const isTarget = lastMsgId !== undefined;
if(!isTarget && historyStorage.maxId) {
if(!isTarget && topMessage) {
const isUnread = this.appMessagesManager.isHistoryUnread(peerId, this.chat.threadId);
if(/* dialog.unread_count */isUnread && !samePeer) {
lastMsgId = historyStorage.readMaxId;
} else {
lastMsgId = historyStorage.maxId;
lastMsgId = topMessage;
//lastMsgID = topMessage;
}
}
@ -1122,7 +1122,7 @@ export default class ChatBubbles { @@ -1122,7 +1122,7 @@ export default class ChatBubbles {
this.scrollable.scrollIntoView(mounted.bubble);
this.highlightBubble(mounted.bubble);
this.chat.setListenerResult('setPeer', lastMsgId, false);
} else if(historyStorage.maxId && !isJump) {
} else if(topMessage && !isJump) {
//this.log('will scroll down', this.scroll.scrollTop, this.scroll.scrollHeight);
this.scroll.scrollTop = this.scroll.scrollHeight;
this.chat.setListenerResult('setPeer', lastMsgId, true);
@ -1143,7 +1143,7 @@ export default class ChatBubbles { @@ -1143,7 +1143,7 @@ export default class ChatBubbles {
this.log('setPeer peerId:', this.peerId, historyStorage, lastMsgId, topMessage);
// add last message, bc in getHistory will load < max_id
const additionMsgId = isJump || (this.chat.type !== 'chat' && this.chat.type !== 'discussion') ? 0 : topMessage;
const additionMsgId = isJump || this.chat.type === 'scheduled' ? 0 : topMessage;
/* this.setPeerPromise = null;
this.preloader.detach();
@ -1219,7 +1219,7 @@ export default class ChatBubbles { @@ -1219,7 +1219,7 @@ export default class ChatBubbles {
this.lazyLoadQueue.unlock();
//if(dialog && lastMsgID && lastMsgID != topMessage && (this.bubbles[lastMsgID] || this.firstUnreadBubble)) {
if(historyStorage.maxId && (isTarget || isJump)) {
if(topMessage && (isTarget || isJump)) {
if(this.scrollable.scrollLocked) {
clearTimeout(this.scrollable.scrollLocked);
this.scrollable.scrollLocked = 0;
@ -1260,8 +1260,8 @@ export default class ChatBubbles { @@ -1260,8 +1260,8 @@ export default class ChatBubbles {
this.log('scrolledAllDown:', this.scrolledAllDown);
//if(!this.unreaded.length && dialog) { // lol
if(this.scrolledAllDown && historyStorage.maxId) { // lol
this.appMessagesManager.readHistory(peerId, historyStorage.maxId);
if(this.scrolledAllDown && topMessage) { // lol
this.appMessagesManager.readHistory(peerId, topMessage);
}
if(this.chat.type === 'chat') {
@ -1545,7 +1545,7 @@ export default class ChatBubbles { @@ -1545,7 +1545,7 @@ export default class ChatBubbles {
let messageMedia = message.media;
let messageMessage: string, totalEntities: any[];
let messageMessage: string, totalEntities: MessageEntity[];
if(messageMedia?.document && !['video', 'gif'].includes(messageMedia.document.type)) {
// * just filter these cases for documents caption
} else if(message.grouped_id && albumMustBeRenderedFull) {
@ -1567,7 +1567,7 @@ export default class ChatBubbles { @@ -1567,7 +1567,7 @@ export default class ChatBubbles {
});
if(totalEntities && !messageMedia) {
let emojiEntities = totalEntities.filter((e: any) => e._ == 'messageEntityEmoji');
let emojiEntities = totalEntities.filter((e) => e._ == 'messageEntityEmoji');
let strLength = messageMessage.length;
let emojiStrLength = emojiEntities.reduce((acc: number, curr: any) => acc + curr.length, 0);
@ -2436,24 +2436,28 @@ export default class ChatBubbles { @@ -2436,24 +2436,28 @@ export default class ChatBubbles {
if(!reverse) { // if not jump
loadCount = 0;
maxId += 1;
maxId = this.appMessagesManager.incrementMessageId(maxId, 1);
}
}
let additionMsgIds: number[];
if(additionMsgId && !isBackLimit) {
const historyStorage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId);
if(historyStorage.history.length < loadCount) {
additionMsgIds = historyStorage.history.slice();
// * filter last album, because we don't know is this the last item
for(let i = additionMsgIds.length - 1; i >= 0; --i) {
const message = this.chat.getMessage(additionMsgIds[i]);
if(message.grouped_id) additionMsgIds.splice(i, 1);
else break;
}
if(this.chat.type === 'pinned') {
additionMsgIds = [additionMsgId];
} else {
const historyStorage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId);
if(historyStorage.history.length < loadCount) {
additionMsgIds = historyStorage.history.slice();
// * filter last album, because we don't know is this the last item
for(let i = additionMsgIds.length - 1; i >= 0; --i) {
const message = this.chat.getMessage(additionMsgIds[i]);
if(message.grouped_id) additionMsgIds.splice(i, 1);
else break;
}
maxId = additionMsgIds[additionMsgIds.length - 1] || maxId;
maxId = additionMsgIds[additionMsgIds.length - 1] || maxId;
}
}
}

8
src/components/sidebarRight/tabs/sharedMedia.ts

@ -904,13 +904,7 @@ export default class AppSharedMediaTab implements SliderTab { @@ -904,13 +904,7 @@ export default class AppSharedMediaTab implements SliderTab {
setText(appPeersManager.getPeerUsername(peerId), this.profileElements.username);
}
let dialog = appMessagesManager.getDialogByPeerId(peerId)[0];
if(dialog) {
let muted = false;
if(dialog.notify_settings && dialog.notify_settings.mute_until) {
muted = new Date(dialog.notify_settings.mute_until * 1000) > new Date();
}
}
this.profileElements.notificationsCheckbox.checked = !appMessagesManager.isPeerMuted(peerId);
} else {
window.requestAnimationFrame(() => {
this.profileElements.notificationsRow.style.display = 'none';

14
src/lib/appManagers/appChatsManager.ts

@ -385,10 +385,20 @@ export class AppChatsManager { @@ -385,10 +385,20 @@ export class AppChatsManager {
public getChatMembersString(id: number) {
const chat = this.getChat(id);
const chatFull = appProfileManager.chatsFull[id];
let count: number;
if(chatFull) {
if(chatFull._ === 'channelFull') {
count = chatFull.participants_count;
} else {
count = (chatFull.participants as ChatParticipants.chatParticipants).participants?.length;
}
} else {
count = chat.participants_count || chat.participants?.participants.length;
}
const isChannel = this.isBroadcast(id);
const participants_count = chat.participants_count || chat.participants?.participants.length || 1;
return numberWithCommas(participants_count) + ' ' + (isChannel ? 'followers' : 'members');
return numberWithCommas(count || 1) + ' ' + (isChannel ? 'followers' : 'members');
}
public wrapForFull(id: number, fullChat: any) {

26
src/lib/appManagers/appMessagesManager.ts

@ -86,6 +86,9 @@ export type MessagesStorage = { @@ -86,6 +86,9 @@ export type MessagesStorage = {
[mid: string]: any
};
export class AppMessagesManager {
public static MESSAGE_ID_INCREMENT = 0x10000;
public static MESSAGE_ID_OFFSET = 0xFFFFFFFF;
public messagesStorageByPeerId: {[peerId: string]: MessagesStorage} = {};
public groupedMessagesStorage: {[groupId: string]: MessagesStorage} = {}; // will be used for albums
public scheduledMessagesStorage: {[peerId: string]: MessagesStorage} = {};
@ -1905,35 +1908,39 @@ export class AppMessagesManager { @@ -1905,35 +1908,39 @@ export class AppMessagesManager {
}
public generateMessageId(messageId: number, temp = false) {
const q = 0xFFFFFFFF;
const q = AppMessagesManager.MESSAGE_ID_OFFSET;
const num = temp ? ++this.tempNum : 0;
if(messageId >= q) {
if(temp) {
return messageId + (num & 0xFFFF);
return messageId + (num & (AppMessagesManager.MESSAGE_ID_INCREMENT - 1));
}
return messageId;
}
return q + (messageId * 0x10000 + (num & 0xFFFF));
return q + (messageId * AppMessagesManager.MESSAGE_ID_INCREMENT + (num & (AppMessagesManager.MESSAGE_ID_INCREMENT - 1)));
}
/**
* * will ignore outgoing offset
*/
public getLocalMessageId(messageId: number) {
const q = 0xFFFFFFFF;
const q = AppMessagesManager.MESSAGE_ID_OFFSET;
if(messageId <= q) {
return messageId;
}
const l = 0xFFFF;
const l = AppMessagesManager.MESSAGE_ID_INCREMENT - 1;
const used = messageId & l;
if(used !== l) {
messageId -= used + 1;
}
return (messageId - q) / 0x10000;
return (messageId - q) / AppMessagesManager.MESSAGE_ID_INCREMENT;
}
public incrementMessageId(messageId: number, increment: number) {
return this.generateMessageId(this.getLocalMessageId(messageId) + increment);
}
public saveMessages(messages: any[], options: Partial<{
@ -3870,6 +3877,11 @@ export class AppMessagesManager { @@ -3870,6 +3877,11 @@ export class AppMessagesManager {
delete message.pFlags.pinned;
}
}
/* const info = this.pinnedMessages[peerId];
if(info) {
info.count += messages.length * (werePinned ? 1 : -1);
} */
delete this.pinnedMessages[peerId];
appStateManager.getState().then(state => {
@ -4273,7 +4285,7 @@ export class AppMessagesManager { @@ -4273,7 +4285,7 @@ export class AppMessagesManager {
historyStorage.count = (historyResult as MessagesMessages.messagesMessagesSlice).count || historyResult.messages.length;
if(!maxId && historyResult.messages.length) {
maxId = (historyResult.messages[0] as MyMessage).mid + 1;
maxId = this.incrementMessageId((historyResult.messages[0] as MyMessage).mid, 1);
}
let offset = 0;

Loading…
Cancel
Save