Browse Source

Fix chat leave

Close deleted chat & channel
master
morethanwords 4 years ago
parent
commit
7f746738f5
  1. 10
      src/components/dialogsContextMenu.ts
  2. 5
      src/components/sidebarLeft/tabs/newChannel.ts
  3. 5
      src/components/sidebarLeft/tabs/newGroup.ts
  4. 46
      src/lib/appManagers/appChatsManager.ts
  5. 6
      src/lib/appManagers/appImManager.ts
  6. 11
      src/lib/appManagers/appMessagesManager.ts

10
src/components/dialogsContextMenu.ts

@ -56,12 +56,12 @@ export default class DialogsContextMenu { @@ -56,12 +56,12 @@ export default class DialogsContextMenu {
this.buttons.delete.addEventListener('click', () => {
let firstName = appPeersManager.getPeerTitle(this.selectedID, false, true);
let callbackFlush = (justClear: boolean) => {
let callbackFlush = (justClear?: true) => {
appMessagesManager.flushHistory(this.selectedID, justClear);
};
let callbackLeave = () => {
appChatsManager.leaveChannel(-this.selectedID);
appChatsManager.leave(-this.selectedID);
};
let title: string, description: string, buttons: PopupButton[];
@ -96,7 +96,7 @@ export default class DialogsContextMenu { @@ -96,7 +96,7 @@ export default class DialogsContextMenu {
buttons = [{
text: 'DELETE FOR ME AND ' + firstName,
isDanger: true,
callback: () => callbackFlush(false)
callback: () => callbackFlush()
}, {
text: 'DELETE JUST FOR ME',
isDanger: true,
@ -112,7 +112,7 @@ export default class DialogsContextMenu { @@ -112,7 +112,7 @@ export default class DialogsContextMenu {
buttons = [{
text: 'DELETE SAVED MESSAGES',
isDanger: true,
callback: () => callbackFlush(false)
callback: () => callbackFlush()
}];
break;
@ -124,7 +124,7 @@ export default class DialogsContextMenu { @@ -124,7 +124,7 @@ export default class DialogsContextMenu {
buttons = [{
text: 'DELETE AND LEAVE ' + firstName,
isDanger: true,
callback: () => callbackFlush(true)
callback: () => callbackLeave()
}];
break;

5
src/components/sidebarLeft/tabs/newChannel.ts

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
import appSidebarLeft, { AppSidebarLeft } from "..";
import { InputFile } from "../../../layer";
import appChatsManager from "../../../lib/appManagers/appChatsManager";
import PopupAvatar from "../../popupAvatar";
import { SliderTab } from "../../slider";
@ -10,7 +11,7 @@ export default class AppNewChannelTab implements SliderTab { @@ -10,7 +11,7 @@ export default class AppNewChannelTab implements SliderTab {
private channelDescriptionInput = this.container.querySelector('.new-channel-description') as HTMLInputElement;
private nextBtn = this.container.querySelector('.btn-corner') as HTMLButtonElement;
private backBtn = this.container.querySelector('.sidebar-close-button') as HTMLButtonElement;
private uploadAvatar: () => Promise<any> = null;
private uploadAvatar: () => Promise<InputFile> = null;
constructor() {
this.container.querySelector('.avatar-edit').addEventListener('click', () => {
@ -35,7 +36,7 @@ export default class AppNewChannelTab implements SliderTab { @@ -35,7 +36,7 @@ export default class AppNewChannelTab implements SliderTab {
this.nextBtn.disabled = true;
appChatsManager.createChannel(title, about).then((channelID) => {
if(this.uploadAvatar) {
this.uploadAvatar().then((inputFile: any) => {
this.uploadAvatar().then((inputFile) => {
appChatsManager.editPhoto(channelID, inputFile);
});
}

5
src/components/sidebarLeft/tabs/newGroup.ts

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
import appSidebarLeft, { AppSidebarLeft } from "..";
import { InputFile } from "../../../layer";
import appChatsManager from "../../../lib/appManagers/appChatsManager";
import appDialogsManager from "../../../lib/appManagers/appDialogsManager";
import appUsersManager from "../../../lib/appManagers/appUsersManager";
@ -14,7 +15,7 @@ export default class AppNewGroupTab implements SliderTab { @@ -14,7 +15,7 @@ export default class AppNewGroupTab implements SliderTab {
private groupNameInput = this.container.querySelector('.new-group-name') as HTMLInputElement;
private nextBtn = this.container.querySelector('.btn-corner') as HTMLButtonElement;
private searchGroup = new SearchGroup(' ', 'contacts', true, 'new-group-members disable-hover', false);
private uploadAvatar: () => Promise<any> = null;
private uploadAvatar: () => Promise<InputFile> = null;
private userIDs: number[];
constructor() {
@ -35,7 +36,7 @@ export default class AppNewGroupTab implements SliderTab { @@ -35,7 +36,7 @@ export default class AppNewGroupTab implements SliderTab {
this.nextBtn.disabled = true;
appChatsManager.createChat(title, this.userIDs).then((chatID) => {
if(this.uploadAvatar) {
this.uploadAvatar().then((inputFile: any) => {
this.uploadAvatar().then((inputFile) => {
appChatsManager.editPhoto(chatID, inputFile);
});
}

46
src/lib/appManagers/appChatsManager.ts

@ -1,10 +1,11 @@ @@ -1,10 +1,11 @@
import { ChatAdminRights, ChatBannedRights, ChatFull, ChatParticipants, InputChannel, InputChatPhoto, InputPeer, Updates } from "../../layer";
import { ChatAdminRights, ChatBannedRights, ChatFull, ChatParticipants, InputChannel, InputChatPhoto, InputFile, InputPeer, Updates } from "../../layer";
import apiManager from '../mtproto/mtprotoworker';
import { RichTextProcessor } from "../richtextprocessor";
import $rootScope from "../rootScope";
import searchIndexManager from "../searchIndexManager";
import { copy, getAbbreviation, isObject, numberWithCommas, safeReplaceObject } from "../utils";
import apiUpdatesManager from "./apiUpdatesManager";
import appMessagesManager from "./appMessagesManager";
import appProfileManager from "./appProfileManager";
import appUsersManager from "./appUsersManager";
@ -296,13 +297,13 @@ export class AppChatsManager { @@ -296,13 +297,13 @@ export class AppChatsManager {
};
}
public hasChat(id: number, allowMin?: any) {
let chat = this.chats[id]
public hasChat(id: number, allowMin?: true) {
const chat = this.chats[id]
return isObject(chat) && (allowMin || !chat.pFlags.min);
}
public getChatPhoto(id: number) {
let chat = this.getChat(id);
const chat = this.getChat(id);
if(this.cachedPhotoLocations[id] === undefined) {
this.cachedPhotoLocations[id] = chat && chat.photo ? chat.photo : {empty: true};
@ -312,7 +313,7 @@ export class AppChatsManager { @@ -312,7 +313,7 @@ export class AppChatsManager {
}
public getChatString(id: number) {
let chat = this.getChat(id);
const chat = this.getChat(id);
if(this.isChannel(id)) {
return (this.isMegagroup(id) ? 's' : 'c') + id + '_' + chat.access_hash;
}
@ -328,8 +329,8 @@ export class AppChatsManager { @@ -328,8 +329,8 @@ export class AppChatsManager {
}
public wrapForFull(id: number, fullChat: any) {
let chatFull = copy(fullChat);
let chat = this.getChat(id);
const chatFull = copy(fullChat);
const chat = this.getChat(id);
if(!chatFull.participants_count) {
chatFull.participants_count = chat.participants_count;
@ -351,10 +352,10 @@ export class AppChatsManager { @@ -351,10 +352,10 @@ export class AppChatsManager {
}
public wrapParticipants(id: number, participants: any[]) {
let chat = this.getChat(id);
let myID = appUsersManager.getSelf().id;
const chat = this.getChat(id);
const myID = appUsersManager.getSelf().id;
if(this.isChannel(id)) {
let isAdmin = chat.pFlags.creator;
const isAdmin = chat.pFlags.creator;
participants.forEach((participant) => {
participant.canLeave = myID == participant.user_id;
participant.canKick = isAdmin && participant._ == 'channelParticipant';
@ -363,7 +364,7 @@ export class AppChatsManager { @@ -363,7 +364,7 @@ export class AppChatsManager {
participant.user = appUsersManager.getUser(participant.user_id);
});
} else {
let isAdmin = chat.pFlags.creator || chat.pFlags.admins_enabled && chat.pFlags.admin;
const isAdmin = chat.pFlags.creator || chat.pFlags.admins_enabled && chat.pFlags.admin;
participants.forEach((participant) => {
participant.canLeave = myID == participant.user_id;
participant.canKick = !participant.canLeave && (
@ -393,8 +394,8 @@ export class AppChatsManager { @@ -393,8 +394,8 @@ export class AppChatsManager {
}
public inviteToChannel(id: number, userIDs: number[]) {
let input = this.getChannelInput(id);
let usersInputs = userIDs.map(u => appUsersManager.getUserInput(u));
const input = this.getChannelInput(id);
const usersInputs = userIDs.map(u => appUsersManager.getUserInput(u));
return apiManager.invokeApi('channels.inviteToChannel', {
channel: input,
@ -415,7 +416,7 @@ export class AppChatsManager { @@ -415,7 +416,7 @@ export class AppChatsManager {
});
}
public editPhoto(id: number, inputFile: any) {
public editPhoto(id: number, inputFile: InputFile) {
const isChannel = this.isChannel(id);
const inputChatPhoto: InputChatPhoto.inputChatUploadedPhoto = {
@ -500,6 +501,23 @@ export class AppChatsManager { @@ -500,6 +501,23 @@ export class AppChatsManager {
channel: this.getChannelInput(id)
}).then(this.onChatUpdated.bind(this, id));
}
public deleteChatUser(id: number, userID: number) {
return apiManager.invokeApi('messages.deleteChatUser', {
chat_id: id,
user_id: appUsersManager.getUserInput(userID)
}).then(this.onChatUpdated.bind(this, id));
}
public leaveChat(id: number) {
return this.deleteChatUser(id, appUsersManager.getSelf().id).then(() => {
return appMessagesManager.flushHistory(-id);
});
}
public leave(id: number) {
return this.isChannel(id) ? this.leaveChannel(id) : this.leaveChat(id);
}
}
export default new AppChatsManager();

6
src/lib/appManagers/appImManager.ts

@ -362,6 +362,12 @@ export class AppImManager { @@ -362,6 +362,12 @@ export class AppImManager {
});
});
$rootScope.$on('dialog_drop', (e) => {
if(e.detail.peerID == this.peerID) {
this.setPeer(0);
}
});
$rootScope.$on('apiUpdate', (e) => {
let update = e.detail;

11
src/lib/appManagers/appMessagesManager.ts

@ -2096,14 +2096,9 @@ export class AppMessagesManager { @@ -2096,14 +2096,9 @@ export class AppMessagesManager {
});
}
private doFlushHistory(inputPeer: any, justClear: boolean): Promise<true> {
let flags = 0;
if(justClear) {
flags |= 1;
}
private doFlushHistory(inputPeer: any, justClear?: true): Promise<true> {
return apiManager.invokeApi('messages.deleteHistory', {
flags: flags,
just_clear: justClear,
peer: inputPeer,
max_id: 0
}).then((affectedHistory) => {
@ -2124,7 +2119,7 @@ export class AppMessagesManager { @@ -2124,7 +2119,7 @@ export class AppMessagesManager {
})
}
public async flushHistory(peerID: number, justClear: boolean) {
public async flushHistory(peerID: number, justClear?: true) {
if(appPeersManager.isChannel(peerID)) {
let promise = this.getHistory(peerID, 0, 1);

Loading…
Cancel
Save