Browse Source

Temp commit

master
Eduard Kuzmenko 3 years ago
parent
commit
5fb4ea6670
  1. 46
      src/components/chat/bubbles.ts
  2. 172
      src/helpers/slicedArray.ts
  3. 2
      src/lib/appManagers/appImManager.ts
  4. 189
      src/lib/appManagers/appMessagesManager.ts

46
src/components/chat/bubbles.ts

@ -1380,7 +1380,7 @@ export default class ChatBubbles {
const isTarget = lastMsgId !== undefined; 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) // * this one will fix topMessage for null message in history (e.g. channel comments with only 1 comment and it is a topMessage)
if(this.chat.type !== 'pinned' && topMessage && !historyStorage.history.includes(topMessage)) { if(this.chat.type !== 'pinned' && topMessage && !historyStorage.history.slice.includes(topMessage)) {
topMessage = 0; topMessage = 0;
} }
@ -1709,6 +1709,9 @@ export default class ChatBubbles {
/* if(DEBUG) { /* if(DEBUG) {
this.log.debug('message to render:', message); this.log.debug('message to render:', message);
} */ } */
if(!bubble && this.bubbles[message.mid]) {
return;
}
//return; //return;
const albumMustBeRenderedFull = this.chat.type !== 'pinned'; const albumMustBeRenderedFull = this.chat.type !== 'pinned';
@ -2683,7 +2686,7 @@ export default class ChatBubbles {
if(!reverse) { // if not jump if(!reverse) { // if not jump
loadCount = 0; loadCount = 0;
maxId = this.appMessagesManager.incrementMessageId(maxId, 1); //maxId = this.appMessagesManager.incrementMessageId(maxId, 1);
} }
} }
@ -2694,9 +2697,9 @@ export default class ChatBubbles {
} else { } else {
const historyStorage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId); const historyStorage = this.appMessagesManager.getHistoryStorage(peerId, this.chat.threadId);
if(historyStorage.history.length < loadCount) { if(historyStorage.history.length < loadCount) {
additionMsgIds = historyStorage.history.slice(); additionMsgIds = historyStorage.history.slice.slice();
// * filter last album, because we don't know is this the last item // * filter last album, because we don't know is it the last item
for(let i = additionMsgIds.length - 1; i >= 0; --i) { for(let i = additionMsgIds.length - 1; i >= 0; --i) {
const message = this.chat.getMessage(additionMsgIds[i]); const message = this.chat.getMessage(additionMsgIds[i]);
if(message.grouped_id) additionMsgIds.splice(i, 1); if(message.grouped_id) additionMsgIds.splice(i, 1);
@ -2716,7 +2719,7 @@ export default class ChatBubbles {
//const isFirstMessageRender = !!additionMsgID && result instanceof Promise && !appMessagesManager.getMessage(additionMsgID).grouped_id; //const isFirstMessageRender = !!additionMsgID && result instanceof Promise && !appMessagesManager.getMessage(additionMsgID).grouped_id;
const isAdditionRender = additionMsgIds?.length; const isAdditionRender = additionMsgIds?.length;
const isFirstMessageRender = (this.isFirstLoad && backLimit) || isAdditionRender; const isFirstMessageRender = (this.isFirstLoad && backLimit && result instanceof Promise) || isAdditionRender;
if(isAdditionRender) { if(isAdditionRender) {
resultPromise = result as Promise<any>; resultPromise = result as Promise<any>;
result = {history: additionMsgIds}; result = {history: additionMsgIds};
@ -2726,7 +2729,7 @@ export default class ChatBubbles {
this.isFirstLoad = false; this.isFirstLoad = false;
const processResult = (historyResult: typeof result) => { const processResult = (historyResult: typeof result) => {
if(this.chat.type === 'discussion' && 'offsetIdOffset' in historyResult) { /* if(this.chat.type === 'discussion' && 'offsetIdOffset' in historyResult) {
const isTopEnd = historyResult.offsetIdOffset >= (historyResult.count - loadCount); const isTopEnd = historyResult.offsetIdOffset >= (historyResult.count - loadCount);
//this.log('discussion got history', loadCount, backLimit, historyResult, isTopEnd); //this.log('discussion got history', loadCount, backLimit, historyResult, isTopEnd);
@ -2737,7 +2740,23 @@ export default class ChatBubbles {
historyResult.history.push(...this.chat.getMidsByMid(this.chat.threadId).reverse()); historyResult.history.push(...this.chat.getMidsByMid(this.chat.threadId).reverse());
this.scrollable.loadedAll.top = true; this.scrollable.loadedAll.top = true;
} }
} } */
};
const sup = (result: HistoryResult) => {
/* if(maxId && result.history?.length) {
if(this.bubbles[maxId]) {
result.history.findAndSplice(mid => mid === maxId);
}
} */
processResult(result);
////console.timeEnd('render history total');
return getHeavyAnimationPromise().then(() => {
return this.performHistoryResult(result.history || [], reverse, isBackLimit, !isAdditionRender && additionMsgId);
});
}; };
const processPromise = (result: Promise<HistoryResult>) => { const processPromise = (result: Promise<HistoryResult>) => {
@ -2756,13 +2775,7 @@ export default class ChatBubbles {
} }
//console.timeEnd('appImManager call getHistory'); //console.timeEnd('appImManager call getHistory');
processResult(result); return sup(result);
////console.timeEnd('render history total');
return getHeavyAnimationPromise().then(() => {
return this.performHistoryResult(result.history || [], reverse, isBackLimit, !isAdditionRender && additionMsgId);
});
}, (err) => { }, (err) => {
this.log.error('getHistory error:', err); this.log.error('getHistory error:', err);
throw err; throw err;
@ -2780,10 +2793,7 @@ export default class ChatBubbles {
} else { } else {
cached = true; cached = true;
//this.log('getHistory cached result by maxId:', maxId, reverse, isBackLimit, result, peerId, justLoad); //this.log('getHistory cached result by maxId:', maxId, reverse, isBackLimit, result, peerId, justLoad);
processResult(result); promise = sup(result as HistoryResult);
promise = getHeavyAnimationPromise().then(() => {
return this.performHistoryResult((result as HistoryResult).history || [], reverse, isBackLimit, !isAdditionRender && additionMsgId);
});
//return (reverse ? this.getHistoryTopPromise = promise : this.getHistoryBottomPromise = promise); //return (reverse ? this.getHistoryTopPromise = promise : this.getHistoryBottomPromise = promise);
//return this.performHistoryResult(result.history || [], reverse, isBackLimit, additionMsgID, true); //return this.performHistoryResult(result.history || [], reverse, isBackLimit, additionMsgID, true);
} }

172
src/helpers/slicedArray.ts

@ -0,0 +1,172 @@
/**
* Descend sorted storage
*/
type ItemType = number;
export class Slice extends Array<ItemType> {
constructor(protected slicedArray: SlicedArray, items: ItemType[] = []) {
super(...items);
}
}
// TODO: Clear empty arrays after deleting items
export default class SlicedArray {
private slices: Slice[]/* = [[7,6,5],[4,3,2],[1,0,-1]] */;
constructor() {
this.slices = [new Slice(this)];
}
public insertSlice(slice: ItemType[]) {
if(!this.slices[0].length) {
this.slices[0].push(...slice);
return;
}
const lowerBound = slice[slice.length - 1];
const upperBound = slice[0];
let foundSlice: Slice, lowerIndex = -1, upperIndex = -1;
for(let i = 0; i < this.slices.length; ++i) {
foundSlice = this.slices[i];
lowerIndex = foundSlice.indexOf(lowerBound);
upperIndex = foundSlice.indexOf(upperBound);
if(upperIndex !== -1 && -1 !== lowerIndex) {
break;
} else if(upperIndex !== -1 || -1 !== lowerIndex) {
break;
}
}
if(upperIndex !== -1 && -1 !== lowerIndex) {
} else if(upperIndex !== -1) { // ([1, 2, 3] | [1, 2, 3, 4, 5]) -> [1, 2, 3, 4, 5]
const sliced = slice.slice(foundSlice.length - upperIndex);
foundSlice.push(...sliced);
} else if(lowerIndex !== -1) { // ([1, 2, 3] | [-1, 0, 1]) -> [-1, 0, 1, 2, 3]
const sliced = slice.slice(0, slice.length - lowerIndex - 1);
foundSlice.unshift(...sliced);
} else {
let insertIndex = 0;
for(const length = this.slices.length; insertIndex < length; ++insertIndex) { // * maybe should iterate from the end, could be faster ?
const s = this.slices[insertIndex];
if(slice[0] > s[0]) {
break;
}
}
this.slices.splice(insertIndex, 0, new Slice(this, slice));
}
this.flatten();
}
private flatten() {
if(this.slices.length < 2) {
return;
}
for(let i = 0, length = this.slices.length; i < (length - 1); ++i) {
const prevSlice = this.slices[i];
const nextSlice = this.slices[i + 1];
const upperIndex = prevSlice.indexOf(nextSlice[0]);
if(upperIndex !== -1) {
this.slices.splice(i + 1, 1);
length--;
this.insertSlice(nextSlice);
}
}
}
// *
get slice() {
return this.slices[0];
}
get length() {
return this.slice.length;
}
public findSlice(item: ItemType) {
for(let i = 0; i < this.slices.length; ++i) {
const slice = this.slices[i];
const index = slice.indexOf(item);
if(index !== -1) {
return {slice, index};
}
}
return undefined;
}
public findSliceOffset(maxId: number) {
for(let i = 0; i < this.slices.length; ++i) {
let offset = 0;
const slice = this.slices[i];
for(; offset < slice.length; offset++) {
if(maxId > slice[offset]) {
if(!offset) { // because can't find 3 in [[5,4], [2,1]]
return undefined;
}
return {slice, offset: offset - 1};
}
}
}
return undefined;
}
// * https://core.telegram.org/api/offsets
public sliceMe(offsetId: number, add_offset: number, limit: number) {
let slice = this.slice;
let offset = 0;
if(offsetId) {
const pos = this.findSliceOffset(offsetId);
if(!pos) {
return undefined;
}
slice = pos.slice;
offset = pos.offset;
if(slice.includes(offsetId) && add_offset < 0) {
add_offset += 1;
}
}
let sliceEnd = offset + add_offset + limit;
//const fixHalfBackLimit = add_offset && !(limit / add_offset % 2) && (sliceEnd % 2) ? 1 : 0;
//sliceEnd += fixHalfBackLimit;
return {
slice: slice.slice(Math.max(offset + add_offset, 0), sliceEnd),
offsetIdOffset: offset
};
}
public unshift(item: ItemType) {
this.slice.unshift(item);
}
/* public push(item: ItemType) {
this.slice.push(item);
} */
public delete(item: ItemType) {
const found = this.findSlice(item);
if(found) {
found.slice.splice(found.index, 1);
}
}
}
(window as any).slicedArray = new SlicedArray();

2
src/lib/appManagers/appImManager.ts

@ -332,7 +332,7 @@ export class AppImManager {
const history = appMessagesManager.getHistoryStorage(chat.peerId, chat.threadId); const history = appMessagesManager.getHistoryStorage(chat.peerId, chat.threadId);
if(history.history.length) { if(history.history.length) {
let goodMid: number; let goodMid: number;
for(const mid of history.history) { for(const mid of history.history.slice) {
const message = chat.getMessage(mid); const message = chat.getMessage(mid);
const good = this.myId === chat.peerId ? message.fromId === this.myId : message.pFlags.out; const good = this.myId === chat.peerId ? message.fromId === this.myId : message.pFlags.out;

189
src/lib/appManagers/appMessagesManager.ts

@ -36,6 +36,7 @@ import pushHeavyTask from "../../helpers/heavyQueue";
import { getFileNameByLocation } from "../../helpers/fileName"; import { getFileNameByLocation } from "../../helpers/fileName";
import appProfileManager from "./appProfileManager"; import appProfileManager from "./appProfileManager";
import DEBUG, { MOUNT_CLASS_TO } from "../../config/debug"; import DEBUG, { MOUNT_CLASS_TO } from "../../config/debug";
import SlicedArray from "../../helpers/slicedArray";
//console.trace('include'); //console.trace('include');
// TODO: если удалить сообщение в непрогруженном диалоге, то при обновлении, из-за стейта, последнего сообщения в чатлисте не будет // TODO: если удалить сообщение в непрогруженном диалоге, то при обновлении, из-за стейта, последнего сообщения в чатлисте не будет
@ -45,7 +46,7 @@ const APITIMEOUT = 0;
export type HistoryStorage = { export type HistoryStorage = {
count: number | null, count: number | null,
history: number[], history: SlicedArray,
maxId?: number, maxId?: number,
readPromise?: Promise<void>, readPromise?: Promise<void>,
@ -88,6 +89,7 @@ export type MessagesStorage = {
//generateIndex: (message: any) => void //generateIndex: (message: any) => void
[mid: string]: any [mid: string]: any
}; };
export class AppMessagesManager { export class AppMessagesManager {
public static MESSAGE_ID_INCREMENT = 0x10000; public static MESSAGE_ID_INCREMENT = 0x10000;
public static MESSAGE_ID_OFFSET = 0xFFFFFFFF; public static MESSAGE_ID_OFFSET = 0xFFFFFFFF;
@ -234,7 +236,7 @@ export class AppMessagesManager {
const processDialog = (dialog: MTDialog.dialog) => { const processDialog = (dialog: MTDialog.dialog) => {
const historyStorage = this.getHistoryStorage(dialog.peerId); const historyStorage = this.getHistoryStorage(dialog.peerId);
const history = [].concat(historyStorage.history); const history = [].concat(historyStorage.history.slice);
//dialog = copy(dialog); //dialog = copy(dialog);
let removeUnread = 0; let removeUnread = 0;
for(const mid of history) { for(const mid of history) {
@ -1514,7 +1516,7 @@ export class AppMessagesManager {
if(pendingData) { if(pendingData) {
const {peerId, tempId, storage} = pendingData; const {peerId, tempId, storage} = pendingData;
const historyStorage = this.getHistoryStorage(peerId); const historyStorage = this.getHistoryStorage(peerId);
const pos = historyStorage.history.indexOf(tempId); const pos = historyStorage.history.findSlice(tempId);
apiUpdatesManager.processUpdateMessage({ apiUpdatesManager.processUpdateMessage({
_: 'updateShort', _: 'updateShort',
@ -1524,8 +1526,8 @@ export class AppMessagesManager {
} }
}); });
if(pos !== -1) { if(pos) {
historyStorage.history.splice(pos, 1); pos.slice.splice(pos.index, 1);
} }
delete this.pendingByRandomId[randomId]; delete this.pendingByRandomId[randomId];
@ -2906,16 +2908,13 @@ export class AppMessagesManager {
} }
let historyStorage = this.getHistoryStorage(peerId); let historyStorage = this.getHistoryStorage(peerId);
if(historyStorage === undefined/* && !message.deleted */) { // warning /* if(historyStorage === undefined) { // warning
historyStorage.history.push(mid); historyStorage.history.push(mid);
/* if(mid < 0 && message.pFlags.unread) {
dialog.unread_count++;
} */
if(this.mergeReplyKeyboard(historyStorage, message)) { if(this.mergeReplyKeyboard(historyStorage, message)) {
rootScope.broadcast('history_reply_markup', {peerId}); rootScope.broadcast('history_reply_markup', {peerId});
} }
} else if(!historyStorage.history.length) { } else */if(!historyStorage.history.slice.length) {
historyStorage.history.push(mid); historyStorage.history.unshift(mid);
} }
historyStorage.maxId = mid; historyStorage.maxId = mid;
@ -3059,17 +3058,17 @@ export class AppMessagesManager {
let storage: { let storage: {
count?: number; count?: number;
history: number[]; history: SlicedArray;
}; };
// * костыль для limit 1, если нужно и получить сообщение, и узнать количество сообщений // * костыль для limit 1, если нужно и получить сообщение, и узнать количество сообщений
if(peerId && !backLimit && !maxId && !query && limit !== 1 && !threadId/* && inputFilter._ !== 'inputMessagesFilterPinned' */) { if(peerId && !backLimit && !maxId && !query && limit !== 1 && !threadId/* && inputFilter._ !== 'inputMessagesFilterPinned' */) {
storage = beta ? storage = beta ?
this.getSearchStorage(peerId, inputFilter._) : this.getSearchStorage(peerId, inputFilter._) as any :
this.getHistoryStorage(peerId); this.getHistoryStorage(peerId);
let filtering = true; let filtering = true;
const history = maxId ? storage.history.slice(storage.history.indexOf(maxId) + 1) : storage.history; const history = /* maxId ? storage.history.slice(storage.history.indexOf(maxId) + 1) : */storage.history;
if(storage !== undefined && history.length) { if(storage !== undefined && history.length) {
const neededContents: { const neededContents: {
@ -3149,7 +3148,7 @@ export class AppMessagesManager {
if(filtering) { if(filtering) {
const storage = this.getMessagesStorage(peerId); const storage = this.getMessagesStorage(peerId);
for(let i = 0, length = history.length; i < length; i++) { for(let i = 0, length = history.length; i < length; i++) {
const message = storage[history[i]]; const message = storage[history.slice[i]];
if(!message) continue; if(!message) continue;
@ -3260,13 +3259,13 @@ export class AppMessagesManager {
appChatsManager.saveApiChats(searchResult.chats); appChatsManager.saveApiChats(searchResult.chats);
this.saveMessages(searchResult.messages); this.saveMessages(searchResult.messages);
if(beta && storage && (!maxId || storage.history[storage.history.length - 1] === maxId)) { /* if(beta && storage && (!maxId || storage.history[storage.history.length - 1] === maxId)) {
const storage = this.getSearchStorage(peerId, inputFilter._); const storage = this.getSearchStorage(peerId, inputFilter._);
const add = (searchResult.messages.map((m: any) => m.mid) as number[]).filter(mid => storage.history.indexOf(mid) === -1); const add = (searchResult.messages.map((m: any) => m.mid) as number[]).filter(mid => storage.history.indexOf(mid) === -1);
storage.history.push(...add); storage.history.push(...add);
storage.history.sort((a, b) => b - a); storage.history.sort((a, b) => b - a);
storage.count = searchResult.count; storage.count = searchResult.count;
} } */
if(DEBUG) { if(DEBUG) {
this.log('getSearch result:', inputFilter, searchResult); this.log('getSearch result:', inputFilter, searchResult);
@ -3592,10 +3591,10 @@ export class AppMessagesManager {
if(threadId) { if(threadId) {
//threadId = this.getLocalMessageId(threadId); //threadId = this.getLocalMessageId(threadId);
if(!this.threadsStorage[peerId]) this.threadsStorage[peerId] = {}; if(!this.threadsStorage[peerId]) this.threadsStorage[peerId] = {};
return this.threadsStorage[peerId][threadId] ?? (this.threadsStorage[peerId][threadId] = {count: null, history: []}); return this.threadsStorage[peerId][threadId] ?? (this.threadsStorage[peerId][threadId] = {count: null, history: new SlicedArray()});
} }
return this.historiesStorage[peerId] ?? (this.historiesStorage[peerId] = {count: null, history: []}); return this.historiesStorage[peerId] ?? (this.historiesStorage[peerId] = {count: null, history: new SlicedArray()});
} }
public handleUpdate(update: Update) { public handleUpdate(update: Update) {
@ -3615,10 +3614,7 @@ export class AppMessagesManager {
const message = this.getMessageFromStorage(storage, mid); const message = this.getMessageFromStorage(storage, mid);
if(!message.deleted) { if(!message.deleted) {
const historyStorage = this.getHistoryStorage(peerId); const historyStorage = this.getHistoryStorage(peerId);
const pos = historyStorage.history.indexOf(tempId); historyStorage.history.delete(tempId);
if(pos !== -1) {
historyStorage.history.splice(pos, 1);
}
this.finalizePendingMessageCallbacks(storage, tempId, mid); this.finalizePendingMessageCallbacks(storage, tempId, mid);
} else { } else {
@ -3660,13 +3656,15 @@ export class AppMessagesManager {
const historyStorage = this.getHistoryStorage(peerId); const historyStorage = this.getHistoryStorage(peerId);
this.updateMessageRepliesIfNeeded(message); this.updateMessageRepliesIfNeeded(message);
const history = historyStorage.history; if(historyStorage.history.findSlice(message.mid)) {
if(history.indexOf(message.mid) !== -1) {
return false; return false;
} }
const history = historyStorage.history.slice;
const topMsgId = history[0]; const topMsgId = history[0];
history.unshift(message.mid); history.unshift(message.mid);
if(message.mid < topMsgId) { if(message.mid < topMsgId) {
//this.log.error('this should\'nt have happenned!', message, history);
history.sort((a, b) => { history.sort((a, b) => {
return b - a; return b - a;
}); });
@ -4039,7 +4037,7 @@ export class AppMessagesManager {
const channelId: number = update.channel_id; const channelId: number = update.channel_id;
const messages: number[] = []; const messages: number[] = [];
const peerId: number = -channelId; const peerId: number = -channelId;
const history = this.getHistoryStorage(peerId).history; const history = this.getHistoryStorage(peerId).history.slice;
if(history.length) { if(history.length) {
history.forEach((msgId: number) => { history.forEach((msgId: number) => {
if(!update.available_min_id || msgId <= update.available_min_id) { if(!update.available_min_id || msgId <= update.available_min_id) {
@ -4066,8 +4064,9 @@ export class AppMessagesManager {
const historyStorage = this.getHistoryStorage(peerId); const historyStorage = this.getHistoryStorage(peerId);
//if(historyStorage !== undefined) { //if(historyStorage !== undefined) {
const newHistory = historyStorage.history.filter(mid => !historyUpdated.msgs[mid]); for(const mid in historyUpdated.msgs) {
historyStorage.history = newHistory; historyStorage.history.delete(+mid);
}
if(historyUpdated.count && if(historyUpdated.count &&
historyStorage.count !== null && historyStorage.count !== null &&
historyStorage.count > 0) { historyStorage.count > 0) {
@ -4430,10 +4429,7 @@ export class AppMessagesManager {
const historyStorage = this.getHistoryStorage(peerId); const historyStorage = this.getHistoryStorage(peerId);
// this.log('pending', randomID, historyStorage.pending) // this.log('pending', randomID, historyStorage.pending)
const pos = historyStorage.history.indexOf(tempId); historyStorage.history.delete(tempId);
if(pos !== -1) {
historyStorage.history.splice(pos, 1);
}
const message = this.getMessageFromStorage(storage, tempId); const message = this.getMessageFromStorage(storage, tempId);
if(!message.deleted) { if(!message.deleted) {
@ -4572,29 +4568,27 @@ export class AppMessagesManager {
}); });
} }
/**
* * https://core.telegram.org/api/offsets, offset_id is inclusive
*/
public getHistory(peerId: number, maxId = 0, limit: number, backLimit?: number, threadId?: number): Promise<HistoryResult> | HistoryResult { public getHistory(peerId: number, maxId = 0, limit: number, backLimit?: number, threadId?: number): Promise<HistoryResult> | HistoryResult {
/* if(this.migratedFromTo[peerId]) {
peerId = this.migratedFromTo[peerId];
} */
const historyStorage = this.getHistoryStorage(peerId, threadId); const historyStorage = this.getHistoryStorage(peerId, threadId);
let offset = 0; let offset = 0;
let offsetNotFound = false; /*
let offsetFound = true;
const reqPeerId = peerId;
if(maxId) { if(maxId) {
offsetNotFound = true; offsetFound = false;
for(; offset < historyStorage.history.length; offset++) { for(; offset < historyStorage.history.length; offset++) {
if(maxId > historyStorage.history[offset]) { if(maxId > historyStorage.history.slice[offset]) {
offsetNotFound = false; offsetFound = true;
break; break;
} }
} }
} }
if(!offsetNotFound && ( if(offsetFound && (
historyStorage.count !== null && historyStorage.history.length === historyStorage.count || historyStorage.count !== null && historyStorage.history.length === historyStorage.count ||
historyStorage.history.length >= offset + limit historyStorage.history.length >= offset + limit
)) { )) {
@ -4606,7 +4600,7 @@ export class AppMessagesManager {
limit = limit; limit = limit;
} }
const history = historyStorage.history.slice(offset, offset + limit); const history = historyStorage.history.slice.slice(offset, offset + limit);
return { return {
count: historyStorage.count, count: historyStorage.count,
history: history, history: history,
@ -4614,20 +4608,16 @@ export class AppMessagesManager {
}; };
} }
if(offsetNotFound) { if(offsetFound) {
offset = 0; offset = 0;
} } */
if((backLimit || maxId) && historyStorage.history.indexOf(maxId) === -1) {
if(backLimit) {
offset = -backLimit;
limit += backLimit;
}
return this.requestHistory(reqPeerId, maxId, limit, offset, undefined, threadId).then((historyResult) => { if(backLimit) {
offset = -backLimit;
limit += backLimit;
/* return this.requestHistory(reqPeerId, maxId, limit, offset, undefined, threadId).then((historyResult) => {
historyStorage.count = (historyResult as MessagesMessages.messagesMessagesSlice).count || historyResult.messages.length; historyStorage.count = (historyResult as MessagesMessages.messagesMessagesSlice).count || historyResult.messages.length;
/* if(!!this.migratedToFrom[peerId]) {
historyStorage.count++;
} */
const history = (historyResult.messages as MyMessage[]).map(message => message.mid); const history = (historyResult.messages as MyMessage[]).map(message => message.mid);
return { return {
@ -4635,73 +4625,77 @@ export class AppMessagesManager {
history, history,
offsetIdOffset: (historyResult as MessagesMessages.messagesMessagesSlice).offset_id_offset || 0 offsetIdOffset: (historyResult as MessagesMessages.messagesMessagesSlice).offset_id_offset || 0
}; };
}); }); */
} }
return this.fillHistoryStorage(peerId, maxId, limit, historyStorage, threadId).then(() => { const haveSlice = historyStorage.history.sliceMe(maxId, offset, limit);
let offset = 0; if(haveSlice && (haveSlice.slice.length === limit || haveSlice.slice.includes(historyStorage.maxId))) {
if(maxId) { return {
for(; offset < historyStorage.history.length; offset++) { count: historyStorage.count,
if(maxId > historyStorage.history[offset]) { history: haveSlice.slice,
break; offsetIdOffset: haveSlice.offsetIdOffset
} };
} }
}
const history = historyStorage.history.slice(backLimit ? Math.max(offset - backLimit, 0) : offset, offset + limit); return this.fillHistoryStorage(peerId, maxId, limit, offset, historyStorage, threadId).then(() => {
const slice = historyStorage.history.sliceMe(maxId, offset, limit);
return { return {
count: historyStorage.count, count: historyStorage.count,
history, history: slice?.slice || [],
offsetIdOffset: offset offsetIdOffset: slice?.offsetIdOffset || historyStorage.count
}; };
}); });
} }
public fillHistoryStorage(peerId: number, maxId: number, fullLimit: number, historyStorage: HistoryStorage, threadId?: number): Promise<boolean> { public fillHistoryStorage(peerId: number, maxId: number, fullLimit: number, offset: number, historyStorage: HistoryStorage, threadId?: number): Promise<void> {
// this.log('fill history storage', peerId, maxId, fullLimit, angular.copy(historyStorage))
//const offset = (this.migratedFromTo[peerId] && !maxId) ? 1 : 0;
const offset = 0;
return this.requestHistory(peerId, maxId, fullLimit, offset, undefined, threadId).then((historyResult) => { return this.requestHistory(peerId, maxId, fullLimit, offset, undefined, threadId).then((historyResult) => {
historyStorage.count = (historyResult as MessagesMessages.messagesMessagesSlice).count || historyResult.messages.length; historyStorage.count = (historyResult as MessagesMessages.messagesMessagesSlice).count || historyResult.messages.length;
if(!maxId && historyResult.messages.length) { /* if(!maxId && historyResult.messages.length) {
maxId = this.incrementMessageId((historyResult.messages[0] as MyMessage).mid, 1); maxId = this.incrementMessageId((historyResult.messages[0] as MyMessage).mid, 1);
} }
let offset = 0; const wasTotalCount = historyStorage.history.length; */
if(maxId) {
for(; offset < historyStorage.history.length; offset++) {
if(maxId > historyStorage.history[offset]) {
break;
}
}
}
const wasTotalCount = historyStorage.history.length;
historyStorage.history.splice(offset, historyStorage.history.length - offset);
historyResult.messages.forEach((message) => { historyResult.messages.forEach((message) => {
if(this.mergeReplyKeyboard(historyStorage, message)) { if(this.mergeReplyKeyboard(historyStorage, message)) {
rootScope.broadcast('history_reply_markup', {peerId}); rootScope.broadcast('history_reply_markup', {peerId});
} }
historyStorage.history.push((message as MyMessage).mid);
}); });
const mids = historyResult.messages.map((message) => (message as MyMessage).mid);
/* if(maxId && !mids.includes(maxId)) {
let i = 0;
for(const length = mids.length; i < length; ++i) {
if(maxId > mids[i]) {
break;
}
}
mids.splice(i, 0, maxId);
} */
historyStorage.history.insertSlice(mids);
/* const isBackLimit = offset < 0 && -offset !== fullLimit;
if(isBackLimit) {
return;
}
const totalCount = historyStorage.history.length; const totalCount = historyStorage.history.length;
fullLimit -= (totalCount - wasTotalCount); fullLimit -= (totalCount - wasTotalCount);
/* const migratedNextPeer = this.migratedFromTo[peerId]; const migratedNextPeer = this.migratedFromTo[peerId];
const migratedPrevPeer = this.migratedToFrom[peerId] const migratedPrevPeer = this.migratedToFrom[peerId]
const isMigrated = migratedNextPeer !== undefined || migratedPrevPeer !== undefined; const isMigrated = migratedNextPeer !== undefined || migratedPrevPeer !== undefined;
if(isMigrated) { if(isMigrated) {
historyStorage.count = Math.max(historyStorage.count, totalCount) + 1; historyStorage.count = Math.max(historyStorage.count, totalCount) + 1;
} */ }
if(fullLimit > 0) { if(fullLimit > 0) {
maxId = historyStorage.history[totalCount - 1]; maxId = historyStorage.history.slice[totalCount - 1];
/* if(isMigrated) { if(isMigrated) {
if(!historyResult.messages.length) { if(!historyResult.messages.length) {
if(migratedPrevPeer) { if(migratedPrevPeer) {
maxId = 0; maxId = 0;
@ -4713,12 +4707,10 @@ export class AppMessagesManager {
} }
return this.fillHistoryStorage(peerId, maxId, fullLimit, historyStorage, threadId); return this.fillHistoryStorage(peerId, maxId, fullLimit, historyStorage, threadId);
} else */if(totalCount < historyStorage.count) { } else if(totalCount < historyStorage.count) {
return this.fillHistoryStorage(peerId, maxId, fullLimit, historyStorage, threadId); return this.fillHistoryStorage(peerId, maxId, fullLimit, offset, historyStorage, threadId);
} }
} } */
return true;
}); });
} }
@ -4727,9 +4719,8 @@ export class AppMessagesManager {
//rootScope.broadcast('history_request'); //rootScope.broadcast('history_request');
const options = { const options: any = {
peer: appPeersManager.getInputPeerById(peerId), peer: appPeersManager.getInputPeerById(peerId),
msg_id: this.getServerMessageId(threadId) || 0,
offset_id: this.getServerMessageId(maxId) || 0, offset_id: this.getServerMessageId(maxId) || 0,
offset_date: offsetDate, offset_date: offsetDate,
add_offset: offset, add_offset: offset,
@ -4739,6 +4730,10 @@ export class AppMessagesManager {
hash: 0 hash: 0
}; };
if(threadId) {
options.msg_id = this.getServerMessageId(threadId) || 0;
}
const promise: ReturnType<AppMessagesManager['requestHistory']> = apiManager.invokeApi(threadId ? 'messages.getReplies' : 'messages.getHistory', options, { const promise: ReturnType<AppMessagesManager['requestHistory']> = apiManager.invokeApi(threadId ? 'messages.getReplies' : 'messages.getHistory', options, {
//timeout: APITIMEOUT, //timeout: APITIMEOUT,
noErrorBox: true noErrorBox: true

Loading…
Cancel
Save