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

30
src/lib/appManagers/appMessagesManager.ts

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

31
src/lib/appManagers/appWebPagesManager.ts

@ -7,8 +7,12 @@ import { safeReplaceObject } from "../../helpers/object"; @@ -7,8 +7,12 @@ import { safeReplaceObject } from "../../helpers/object";
import { limitSymbols } from "../../helpers/string";
export class AppWebPagesManager {
webpages: any = {};
pendingWebPages: any = {};
private webpages: any = {};
private pendingWebPages: {
[webPageId: string]: {
[mid: string]: true
}
} = {};
constructor() {
rootScope.on('apiUpdate', (e) => {
@ -22,7 +26,7 @@ export class AppWebPagesManager { @@ -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') {
//appPhotosManager.savePhoto(apiWebPage.photo, mediaContext);
apiWebPage.photo = appPhotosManager.savePhoto(apiWebPage.photo, mediaContext);
@ -73,12 +77,12 @@ export class AppWebPagesManager { @@ -73,12 +77,12 @@ export class AppWebPagesManager {
apiWebPage.type = 'photo';
}
if(messageId) {
if(mid) {
if(this.pendingWebPages[apiWebPage.id] === undefined) {
this.pendingWebPages[apiWebPage.id] = {};
}
this.pendingWebPages[apiWebPage.id][messageId] = true;
this.pendingWebPages[apiWebPage.id][mid] = true;
}
if(this.webpages[apiWebPage.id] === undefined) {
@ -87,21 +91,32 @@ export class AppWebPagesManager { @@ -87,21 +91,32 @@ export class AppWebPagesManager {
safeReplaceObject(this.webpages[apiWebPage.id], apiWebPage);
}
if(!messageId && this.pendingWebPages[apiWebPage.id] !== undefined) {
if(!mid && this.pendingWebPages[apiWebPage.id] !== undefined) {
const msgs: number[] = [];
for(let msgId in this.pendingWebPages[apiWebPage.id]) {
for(const msgId in this.pendingWebPages[apiWebPage.id]) {
msgs.push(+msgId);
}
rootScope.broadcast('webpage_updated', {
id: apiWebPage.id,
msgs: msgs
msgs
});
}
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) {
return this.webpages[id];
}

4
src/lib/rootScope.ts

@ -91,9 +91,9 @@ class RootScope { @@ -91,9 +91,9 @@ class RootScope {
}
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);
}
} */
const myCustomEvent = new CustomEvent(name, {detail});
document.dispatchEvent(myCustomEvent);

Loading…
Cancel
Save