Browse Source

Pending message fixes:

Fix poll voting
Fix message editing
master
morethanwords 4 years ago
parent
commit
f249f7b82b
  1. 20
      src/components/poll.ts
  2. 45
      src/lib/appManagers/appMessagesManager.ts
  3. 9
      src/lib/appManagers/appPollsManager.ts

20
src/components/poll.ts

@ -1,13 +1,13 @@ @@ -1,13 +1,13 @@
import appPollsManager, { PollResults, Poll } from "../lib/appManagers/appPollsManager";
import { RichTextProcessor } from "../lib/richtextprocessor";
import { findUpClassName, cancelEvent } from "../lib/utils";
import appSidebarRight from "./sidebarRight";
import mediaSizes from "../helpers/mediaSizes";
import { isTouchSupported } from "../helpers/touchSupport";
import appImManager from "../lib/appManagers/appImManager";
import appPollsManager, { Poll, PollResults } from "../lib/appManagers/appPollsManager";
import serverTimeManager from "../lib/mtproto/serverTimeManager";
import { ripple } from "./ripple";
import mediaSizes from "../helpers/mediaSizes";
import { RichTextProcessor } from "../lib/richtextprocessor";
import $rootScope from "../lib/rootScope";
import { isTouchSupported } from "../helpers/touchSupport";
import { cancelEvent, findUpClassName } from "../lib/utils";
import { ripple } from "./ripple";
import appSidebarRight from "./sidebarRight";
let lineTotalLength = 0;
const tailLength = 9;
@ -380,11 +380,17 @@ export default class PollElement extends HTMLElement { @@ -380,11 +380,17 @@ export default class PollElement extends HTMLElement {
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
// вызывается при изменении одного из перечисленных выше атрибутов
console.log('Poll: attributeChangedCallback', name, oldValue, newValue, this.isConnected);
if(name == 'poll-id') {
this.pollID = newValue;
} else if(name == 'message-id') {
this.mid = +newValue;
}
if(this.mid > 0 && oldValue !== undefined && +oldValue < 0) {
this.disconnectedCallback();
connectedPolls.push({id: this.pollID, element: this});
}
}
adoptedCallback() {

45
src/lib/appManagers/appMessagesManager.ts

@ -464,7 +464,14 @@ export class AppMessagesManager { @@ -464,7 +464,14 @@ export class AppMessagesManager {
public pendingTopMsgs: {[peerID: string]: number} = {};
public sendFilePromise: CancellablePromise<void> = Promise.resolve();
public tempID = -1;
public tempFinalizeCallbacks: any = {};
public tempFinalizeCallbacks: {
[mid: string]: {
[callbackName: string]: Partial<{
deferred: CancellablePromise<void>,
callback: (mid: number) => Promise<any>
}>
}
} = {};
public lastSearchFilter: any = {};
public lastSearchResults: any = [];
@ -663,27 +670,28 @@ export class AppMessagesManager { @@ -663,27 +670,28 @@ export class AppMessagesManager {
return sendEntites;
}
public invokeAfterMessageIsSent(messageID: number, callbackName: string, callback: (mid: number) => Promise<any>) {
const finalize = this.tempFinalizeCallbacks[messageID] ?? (this.tempFinalizeCallbacks[messageID] = {});
const obj = finalize[callbackName] ?? (finalize[callbackName] = {deferred: deferredPromise<void>()});
obj.callback = callback;
return obj.deferred;
}
public editMessage(messageID: number, text: string, options: Partial<{
noWebPage: true,
newMedia: any
}> = {}) {
}> = {}): Promise<void> {
/* if(!this.canEditMessage(messageID)) {
return Promise.reject({type: 'MESSAGE_EDIT_FORBIDDEN'});
} */
if(messageID < 0) {
if(this.tempFinalizeCallbacks[messageID] === undefined) {
this.tempFinalizeCallbacks[messageID] = {}
}
const promise = new Promise((resolve, reject) => {
this.tempFinalizeCallbacks[messageID].edit = (mid: number) => {
this.log('invoke callback', mid)
this.editMessage(mid, text).then(resolve, reject);
}
return this.invokeAfterMessageIsSent(messageID, 'edit', (mid) => {
this.log('invoke editMessage callback', mid);
return this.editMessage(mid, text, options);
});
return promise;
}
let entities: any[];
@ -4010,12 +4018,15 @@ export class AppMessagesManager { @@ -4010,12 +4018,15 @@ export class AppMessagesManager {
}
public finalizePendingMessageCallbacks(tempID: number, mid: number) {
var callbacks = this.tempFinalizeCallbacks[tempID];
const callbacks = this.tempFinalizeCallbacks[tempID];
this.log.warn(callbacks, tempID);
if(callbacks !== undefined) {
callbacks.forEach((callback: any) => {
callback(mid);
});
for(const name in callbacks) {
const {deferred, callback} = callbacks[name];
this.log(`finalizePendingMessageCallbacks: will invoke ${name} callback`);
callback(mid).then(deferred.resolve, deferred.reject);
}
delete this.tempFinalizeCallbacks[tempID];
}

9
src/lib/appManagers/appPollsManager.ts

@ -159,7 +159,7 @@ class AppPollsManager { @@ -159,7 +159,7 @@ class AppPollsManager {
};
}
public sendVote(mid: number, optionIDs: number[]) {
public sendVote(mid: number, optionIDs: number[]): Promise<void> {
const message = appMessagesManager.getMessage(mid);
const poll: Poll = message.media.poll;
@ -170,6 +170,13 @@ class AppPollsManager { @@ -170,6 +170,13 @@ class AppPollsManager {
const inputPeer = appPeersManager.getInputPeerByID(message.peerID);
const messageID = message.id;
if(mid < 0) {
return appMessagesManager.invokeAfterMessageIsSent(mid, 'sendVote', (mid) => {
this.log('invoke sendVote callback');
return this.sendVote(mid, optionIDs);
});
}
return apiManager.invokeApi('messages.sendVote', {
peer: inputPeer,
msg_id: messageID,

Loading…
Cancel
Save