From ea566a3a1d0629d195caa0ccef580a3bd16f7aca Mon Sep 17 00:00:00 2001 From: morethanwords Date: Mon, 12 Oct 2020 00:21:42 +0300 Subject: [PATCH] Send 1 concrete item from album Forward: exclude deleted accounts --- src/components/appSelectPeers.ts | 12 +++---- src/components/chat/contextMenu.ts | 24 ++++++++++--- src/lib/appManagers/appImManager.ts | 2 +- src/lib/appManagers/appMessagesIDsManager.ts | 37 +++++++++----------- src/lib/appManagers/appMessagesManager.ts | 31 ++++++---------- src/lib/appManagers/appUsersManager.ts | 19 +++++++++- 6 files changed, 72 insertions(+), 53 deletions(-) diff --git a/src/components/appSelectPeers.ts b/src/components/appSelectPeers.ts index b30b2f08..40916c73 100644 --- a/src/components/appSelectPeers.ts +++ b/src/components/appSelectPeers.ts @@ -1,12 +1,12 @@ -import Scrollable from "./scrollable"; -import appMessagesManager, { Dialog } from "../lib/appManagers/appMessagesManager"; -import { cancelEvent, findUpClassName, findUpAttribute } from "../lib/utils"; -import appDialogsManager from "../lib/appManagers/appDialogsManager"; import appChatsManager, { ChatRights } from "../lib/appManagers/appChatsManager"; -import appUsersManager from "../lib/appManagers/appUsersManager"; +import appDialogsManager from "../lib/appManagers/appDialogsManager"; +import appMessagesManager, { Dialog } from "../lib/appManagers/appMessagesManager"; import appPeersManager from "../lib/appManagers/appPeersManager"; import appPhotosManager from "../lib/appManagers/appPhotosManager"; +import appUsersManager from "../lib/appManagers/appUsersManager"; import $rootScope from "../lib/rootScope"; +import { cancelEvent, findUpAttribute, findUpClassName } from "../lib/utils"; +import Scrollable from "./scrollable"; type PeerType = 'contacts' | 'dialogs'; @@ -172,7 +172,7 @@ export class AppSelectPeers { if(this.chatRightsAction) { dialogs = dialogs.filter(d => { - return d.peerID > 0 || appChatsManager.hasRights(-d.peerID, this.chatRightsAction); + return (d.peerID > 0 && (this.chatRightsAction != 'send' || appUsersManager.canSendToUser(d.peerID))) || appChatsManager.hasRights(-d.peerID, this.chatRightsAction); }); } diff --git a/src/components/chat/contextMenu.ts b/src/components/chat/contextMenu.ts index 9aa15ec0..ac6c7b4e 100644 --- a/src/components/chat/contextMenu.ts +++ b/src/components/chat/contextMenu.ts @@ -11,8 +11,11 @@ import { PopupButton, PopupPeer } from "../popup"; import appSidebarRight from "../sidebarRight"; export class ChatContextMenu { - private buttons: (ButtonMenuItemOptions & {verify: (peerID: number, msgID: number) => boolean})[]; + private buttons: (ButtonMenuItemOptions & {verify: (peerID: number, msgID: number, target: HTMLElement) => boolean})[]; private element: HTMLElement; + + private target: HTMLElement; + public peerID: number; public msgID: number; constructor(private attachTo: HTMLElement) { @@ -41,17 +44,21 @@ export class ChatContextMenu { const msgID = +bubble.dataset.mid; if(!msgID) return; - const peerID = $rootScope.selectedPeerID; + this.peerID = $rootScope.selectedPeerID; this.msgID = msgID; + this.target = e.target as HTMLElement; this.buttons.forEach(button => { - const good = button.verify(peerID, msgID); + const good = button.verify(this.peerID, this.msgID, this.target); button.element.classList.toggle('hide', !good); }); const side: 'left' | 'right' = bubble.classList.contains('is-in') ? 'left' : 'right'; positionMenu(e, this.element, side); - openBtnMenu(this.element); + openBtnMenu(this.element, () => { + this.peerID = this.msgID = 0; + this.target = null; + }); /////this.log('contextmenu', e, bubble, msgID, side); }); @@ -175,7 +182,14 @@ export class ChatContextMenu { }; private onForwardClick = () => { - appSidebarRight.forwardTab.open([this.msgID]); + let msgID: number; + + const albumItem = findUpClassName(this.target, 'album-item'); + if(albumItem) { + msgID = +albumItem.dataset.mid; + } + + appSidebarRight.forwardTab.open([msgID]); }; private onDeleteClick = () => { diff --git a/src/lib/appManagers/appImManager.ts b/src/lib/appManagers/appImManager.ts index 1d056967..07f0dc87 100644 --- a/src/lib/appManagers/appImManager.ts +++ b/src/lib/appManagers/appImManager.ts @@ -1260,7 +1260,7 @@ export class AppImManager { const hasRights = isChannel && appChatsManager.hasRights(-peerID, 'send'); this.chatInner.classList.toggle('has-rights', hasRights); - const canWrite = (!isChannel || hasRights) && (peerID < 0 || !appUsersManager.getUser(peerID).pFlags.deleted); + const canWrite = (!isChannel || hasRights) && (peerID < 0 || appUsersManager.canSendToUser(peerID)); this.chatInput.style.display = canWrite ? '' : 'none'; this.chatInner.classList.toggle('is-chat-input-hidden', !canWrite); diff --git a/src/lib/appManagers/appMessagesIDsManager.ts b/src/lib/appManagers/appMessagesIDsManager.ts index 441eee72..04b9061c 100644 --- a/src/lib/appManagers/appMessagesIDsManager.ts +++ b/src/lib/appManagers/appMessagesIDsManager.ts @@ -1,6 +1,6 @@ export class AppMessagesIDsManager { - public channelLocals = {} as any; - public channelsByLocals = {} as any; + public channelLocals: {[channelID: string]: number} = {}; + public channelsByLocals: {[localStart: string]: number} = {}; public channelCurLocal = 0; public fullMsgIDModulus = 4294967296; @@ -8,8 +8,9 @@ export class AppMessagesIDsManager { if(!channelID || msgID <= 0) { return msgID; } + msgID = this.getMessageLocalID(msgID); - var localStart = this.channelLocals[channelID]; + let localStart = this.channelLocals[channelID]; if(!localStart) { localStart = (++this.channelCurLocal) * this.fullMsgIDModulus; this.channelsByLocals[localStart] = channelID; @@ -20,36 +21,32 @@ export class AppMessagesIDsManager { } public getMessageIDInfo(fullMsgID: number) { - if (fullMsgID < this.fullMsgIDModulus) { + if(fullMsgID < this.fullMsgIDModulus) { return [fullMsgID, 0]; } - var msgID = fullMsgID % this.fullMsgIDModulus; - var channelID = this.channelsByLocals[fullMsgID - msgID]; + + const msgID = fullMsgID % this.fullMsgIDModulus; + const channelID = this.channelsByLocals[fullMsgID - msgID]; return [msgID, channelID]; } public getMessageLocalID(fullMsgID: number) { - if(!fullMsgID) { - return 0; - } - return fullMsgID % this.fullMsgIDModulus; + return fullMsgID ? fullMsgID % this.fullMsgIDModulus : 0; } - public splitMessageIDsByChannels (mids: any[]) { - var msgIDsByChannels: {[channelID: number]: number[]} = {}; - var midsByChannels: {[channelID: number]: number[]} = {}; - var i; - var mid, msgChannel; - var channelID; - for(i = 0; i < mids.length; i++) { - mid = mids[i]; - msgChannel = this.getMessageIDInfo(mid); - channelID = msgChannel[1]; + public splitMessageIDsByChannels(mids: number[]) { + const msgIDsByChannels: {[channelID: number]: number[]} = {}; + const midsByChannels: {[channelID: number]: number[]} = {}; + for(const mid of mids) { + const msgChannel = this.getMessageIDInfo(mid); + const channelID = msgChannel[1]; + if(msgIDsByChannels[channelID] === undefined) { msgIDsByChannels[channelID] = []; midsByChannels[channelID] = []; } + msgIDsByChannels[channelID].push(msgChannel[0]); midsByChannels[channelID].push(mid); } diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 4fd97af0..7ca5a58e 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -1,7 +1,7 @@ import ProgressivePreloader from "../../components/preloader"; import { CancellablePromise, deferredPromise } from "../../helpers/cancellablePromise"; import { Dialog as MTDialog, DialogFilter, DialogPeer, DocumentAttribute, InputMessage, Message, MessagesDialogs, MessagesFilter, MessagesMessages, MessagesPeerDialogs, MethodDeclMap, PhotoSize, Update } from "../../layer"; -import { Modify } from "../../types"; +import { InvokeApiOptions, Modify } from "../../types"; import { bigint, nextRandomInt } from "../bin_utils"; import { logger } from "../logger"; import type { ApiFileManager } from '../mtproto/apiFileManager'; @@ -1997,38 +1997,29 @@ export class AppMessagesManager { } public forwardMessages(peerID: number, mids: number[], options: Partial<{ - withMyScore: boolean + withMyScore: true }> = {}) { peerID = appPeersManager.getPeerMigratedTo(peerID) || peerID; mids = mids.sort((a, b) => a - b); - var flags = 0; - if(options.withMyScore) { - flags |= 256; - } + const splitted = appMessagesIDsManager.splitMessageIDsByChannels(mids); + const promises: Promise[] = []; - let splitted = appMessagesIDsManager.splitMessageIDsByChannels(mids); - let promises: any[] = []; - - for(let channelID in splitted.msgIDs) { - let msgIDs = splitted.msgIDs[channelID]; - let len = msgIDs.length; - let randomIDs = []; - for(let i = 0; i < len; i++) { - randomIDs.push([nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)]); - } + for(const channelID in splitted.msgIDs) { + const msgIDs = splitted.msgIDs[channelID]; + const randomIDs: [number, number][] = msgIDs.map(() => [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)]); - let sentRequestOptions: any = {}; + const sentRequestOptions: InvokeApiOptions = {}; if(this.pendingAfterMsgs[peerID]) { sentRequestOptions.afterMessageID = this.pendingAfterMsgs[peerID].messageID; } - let promise = apiManager.invokeApi('messages.forwardMessages', { - flags: flags, + const promise = apiManager.invokeApi('messages.forwardMessages', { from_peer: appPeersManager.getInputPeerByID(-channelID), id: msgIDs, random_id: randomIDs as any, - to_peer: appPeersManager.getInputPeerByID(peerID) + to_peer: appPeersManager.getInputPeerByID(peerID), + with_my_score: options.withMyScore }, sentRequestOptions).then((updates) => { apiUpdatesManager.processUpdateMessage(updates); }, () => {}).then(() => { diff --git a/src/lib/appManagers/appUsersManager.ts b/src/lib/appManagers/appUsersManager.ts index a1167b40..4ba9d8c7 100644 --- a/src/lib/appManagers/appUsersManager.ts +++ b/src/lib/appManagers/appUsersManager.ts @@ -11,6 +11,8 @@ import appChatsManager from "./appChatsManager"; import appPeersManager from "./appPeersManager"; import appStateManager from "./appStateManager"; +// TODO: updateUserBlocked + /* export type User = { _: 'user', access_hash: string, @@ -110,7 +112,17 @@ export class AppUsersManager { break; } - + + /* // @ts-ignore + case 'updateUserBlocked': { + const id = (update as any).user_id; + const blocked: boolean = (update as any).blocked; + + const user = this.getUser(id); + if(user) { + } + break; + } */ /* case 'updateContactLink': this.onContactUpdated(update.user_id, update.my_link._ == 'contactLinkContact'); @@ -420,6 +432,11 @@ export class AppUsersManager { return isObject(user) && (allowMin || !user.pFlags.min); } + public canSendToUser(id: number) { + const user = this.getUser(id); + return !user.pFlags.deleted; + } + public getUserPhoto(id: number) { var user = this.getUser(id);