Browse Source

Fix editing messages webPage

Fix showing webPage on message without it
master
Eduard Kuzmenko 4 years ago
parent
commit
c2dbe531f0
  1. 16
      src/components/chat/input.ts
  2. 30
      src/lib/appManagers/appMessagesManager.ts
  3. 41
      src/lib/appManagers/appWebPagesManager.ts
  4. 4
      src/lib/rootScope.ts

16
src/components/chat/input.ts

@ -911,16 +911,22 @@ export default class ChatInput {
cancelEvent(e); cancelEvent(e);
if(this.willSendWebPage) { if(this.willSendWebPage) {
this.noWebPage = true; const lastUrl = this.lastUrl;
this.willSendWebPage = null; let needReturn = false;
if(this.helperType) { if(this.helperType) {
//if(this.helperFunc) { //if(this.helperFunc) {
this.helperFunc(); this.helperFunc();
//} //}
return; needReturn = true;
} }
// * restore values
this.lastUrl = lastUrl;
this.noWebPage = true;
this.willSendWebPage = null;
if(needReturn) return;
} }
this.clearHelper(); this.clearHelper();
@ -1019,7 +1025,7 @@ export default class ChatInput {
}); });
} else { } else {
this.appMessagesManager.sendText(this.chat.peerId, str, { this.appMessagesManager.sendText(this.chat.peerId, str, {
replyToMsgId: this.replyToMsgId == 0 ? undefined : this.replyToMsgId, replyToMsgId: this.replyToMsgId || this.replyToMsgId,
noWebPage: this.noWebPage, noWebPage: this.noWebPage,
webPage: this.willSendWebPage webPage: this.willSendWebPage
}); });

30
src/lib/appManagers/appMessagesManager.ts

@ -156,8 +156,8 @@ export class AppMessagesManager {
rootScope.on('webpage_updated', (e) => { rootScope.on('webpage_updated', (e) => {
const eventData = e.detail; const eventData = e.detail;
eventData.msgs.forEach((msgId) => { eventData.msgs.forEach((mid) => {
const message = this.getMessage(msgId) as Message.message; const message = this.getMessage(mid) as Message.message;
if(!message) return; if(!message) return;
message.media = { message.media = {
_: 'messageMediaWebPage', _: 'messageMediaWebPage',
@ -166,7 +166,7 @@ export class AppMessagesManager {
rootScope.broadcast('message_edit', { rootScope.broadcast('message_edit', {
peerId: this.getMessagePeer(message), peerId: this.getMessagePeer(message),
mid: msgId, mid: mid,
justMedia: true justMedia: true
}); });
}); });
@ -320,7 +320,7 @@ export class AppMessagesManager {
return obj.deferred; return obj.deferred;
} }
public editMessage(messageId: number, text: string, options: Partial<{ public editMessage(mid: number, text: string, options: Partial<{
noWebPage: true, noWebPage: true,
newMedia: any newMedia: any
}> = {}): Promise<void> { }> = {}): Promise<void> {
@ -328,8 +328,8 @@ export class AppMessagesManager {
return Promise.reject({type: 'MESSAGE_EDIT_FORBIDDEN'}); return Promise.reject({type: 'MESSAGE_EDIT_FORBIDDEN'});
} */ } */
if(messageId < 0) { if(mid < 0) {
return this.invokeAfterMessageIsSent(messageId, 'edit', (mid) => { return this.invokeAfterMessageIsSent(mid, 'edit', (mid) => {
this.log('invoke editMessage callback', mid); this.log('invoke editMessage callback', mid);
return this.editMessage(mid, text, options); return this.editMessage(mid, text, options);
}); });
@ -341,12 +341,12 @@ export class AppMessagesManager {
text = RichTextProcessor.parseMarkdown(text, entities); text = RichTextProcessor.parseMarkdown(text, entities);
} }
const message = this.getMessage(messageId); const message = this.getMessage(mid);
const peerId = this.getMessagePeer(message); const peerId = this.getMessagePeer(message);
return apiManager.invokeApi('messages.editMessage', { return apiManager.invokeApi('messages.editMessage', {
peer: appPeersManager.getInputPeerById(peerId), peer: appPeersManager.getInputPeerById(peerId),
id: appMessagesIdsManager.getMessageLocalId(messageId), id: appMessagesIdsManager.getMessageLocalId(mid),
message: text, message: text,
media: options.newMedia, media: options.newMedia,
entities: entities ? this.getInputEntities(entities) : undefined, entities: entities ? this.getInputEntities(entities) : undefined,
@ -3306,9 +3306,13 @@ export class AppMessagesManager {
break; break;
} }
const oldMessage = this.messagesStorage[mid];
if(oldMessage.media?.webpage) {
appWebPagesManager.deleteWebPageFromPending(oldMessage.media.webpage, mid);
}
// console.trace(dT(), 'edit message', message) // console.trace(dT(), 'edit message', message)
this.saveMessages([message]/* , {isEdited: true} */); this.saveMessages([message]);
safeReplaceObject(this.messagesStorage[mid], message);
const dialog = this.getDialogByPeerId(peerId)[0]; const dialog = this.getDialogByPeerId(peerId)[0];
const isTopMessage = dialog && dialog.top_message == mid; const isTopMessage = dialog && dialog.top_message == mid;
@ -3489,6 +3493,12 @@ export class AppMessagesManager {
if(smth?.file_reference) { if(smth?.file_reference) {
referenceDatabase.deleteContext(smth.file_reference, {type: 'message', messageId: mid}); referenceDatabase.deleteContext(smth.file_reference, {type: 'message', messageId: mid});
} }
// @ts-ignore
if(message.media.webpage) {
// @ts-ignore
appWebPagesManager.deleteWebPageFromPending(message.media.webpage, mid);
}
} }
if(!message.pFlags.out && message.pFlags.unread) { if(!message.pFlags.out && message.pFlags.unread) {

41
src/lib/appManagers/appWebPagesManager.ts

@ -7,8 +7,12 @@ import { safeReplaceObject } from "../../helpers/object";
import { limitSymbols } from "../../helpers/string"; import { limitSymbols } from "../../helpers/string";
export class AppWebPagesManager { export class AppWebPagesManager {
webpages: any = {}; private webpages: any = {};
pendingWebPages: any = {}; private pendingWebPages: {
[webPageId: string]: {
[mid: string]: true
}
} = {};
constructor() { constructor() {
rootScope.on('apiUpdate', (e) => { rootScope.on('apiUpdate', (e) => {
@ -22,7 +26,7 @@ export class AppWebPagesManager {
}); });
} }
public saveWebPage(apiWebPage: any, messageId?: number, mediaContext?: ReferenceContext) { public saveWebPage(apiWebPage: any, mid?: number, mediaContext?: ReferenceContext) {
if(apiWebPage.photo && apiWebPage.photo._ === 'photo') { if(apiWebPage.photo && apiWebPage.photo._ === 'photo') {
//appPhotosManager.savePhoto(apiWebPage.photo, mediaContext); //appPhotosManager.savePhoto(apiWebPage.photo, mediaContext);
apiWebPage.photo = appPhotosManager.savePhoto(apiWebPage.photo, mediaContext); apiWebPage.photo = appPhotosManager.savePhoto(apiWebPage.photo, mediaContext);
@ -65,20 +69,20 @@ export class AppWebPagesManager {
}); });
if(apiWebPage.type != 'photo' && if(apiWebPage.type != 'photo' &&
apiWebPage.type != 'video' && apiWebPage.type != 'video' &&
apiWebPage.type != 'gif' && apiWebPage.type != 'gif' &&
apiWebPage.type != 'document' && apiWebPage.type != 'document' &&
!apiWebPage.description && !apiWebPage.description &&
apiWebPage.photo) { apiWebPage.photo) {
apiWebPage.type = 'photo'; apiWebPage.type = 'photo';
} }
if(messageId) { if(mid) {
if(this.pendingWebPages[apiWebPage.id] === undefined) { if(this.pendingWebPages[apiWebPage.id] === undefined) {
this.pendingWebPages[apiWebPage.id] = {}; this.pendingWebPages[apiWebPage.id] = {};
} }
this.pendingWebPages[apiWebPage.id][messageId] = true; this.pendingWebPages[apiWebPage.id][mid] = true;
} }
if(this.webpages[apiWebPage.id] === undefined) { if(this.webpages[apiWebPage.id] === undefined) {
@ -87,21 +91,32 @@ export class AppWebPagesManager {
safeReplaceObject(this.webpages[apiWebPage.id], apiWebPage); safeReplaceObject(this.webpages[apiWebPage.id], apiWebPage);
} }
if(!messageId && this.pendingWebPages[apiWebPage.id] !== undefined) { if(!mid && this.pendingWebPages[apiWebPage.id] !== undefined) {
const msgs: number[] = []; const msgs: number[] = [];
for(let msgId in this.pendingWebPages[apiWebPage.id]) { for(const msgId in this.pendingWebPages[apiWebPage.id]) {
msgs.push(+msgId); msgs.push(+msgId);
} }
rootScope.broadcast('webpage_updated', { rootScope.broadcast('webpage_updated', {
id: apiWebPage.id, id: apiWebPage.id,
msgs: msgs msgs
}); });
} }
return apiWebPage; return apiWebPage;
} }
public deleteWebPageFromPending(webPage: any, mid: number) {
const id = webPage.id;
if(this.pendingWebPages[id] && this.pendingWebPages[id][mid]) {
delete this.pendingWebPages[id][mid];
if(!Object.keys(this.pendingWebPages[id]).length) {
delete this.pendingWebPages[id];
}
}
}
public getWebPage(id: string) { public getWebPage(id: string) {
return this.webpages[id]; return this.webpages[id];
} }

4
src/lib/rootScope.ts

@ -91,9 +91,9 @@ class RootScope {
} }
public broadcast = <T extends keyof BroadcastEvents>(name: T, detail?: BroadcastEvents[T]) => { public broadcast = <T extends keyof BroadcastEvents>(name: T, detail?: BroadcastEvents[T]) => {
if(name != 'user_update') { /* if(name != 'user_update') {
console.debug('Broadcasting ' + name + ' event, with args:', detail); console.debug('Broadcasting ' + name + ' event, with args:', detail);
} } */
const myCustomEvent = new CustomEvent(name, {detail}); const myCustomEvent = new CustomEvent(name, {detail});
document.dispatchEvent(myCustomEvent); document.dispatchEvent(myCustomEvent);

Loading…
Cancel
Save