Browse Source

Edit channel & group respect rights

master
Eduard Kuzmenko 3 years ago
parent
commit
774dcce528
  1. 156
      src/components/sidebarRight/tabs/editChannel.ts
  2. 56
      src/components/sidebarRight/tabs/editGroup.ts
  3. 17
      src/components/sidebarRight/tabs/sharedMedia.ts
  4. 72
      src/lib/appManagers/appChatsManager.ts
  5. 2
      src/lib/appManagers/appPeersManager.ts

156
src/components/sidebarRight/tabs/editChannel.ts

@ -25,85 +25,109 @@ export default class AppEditChannelTab extends SliderSuperTab {
{ {
const section = new SettingSection({noDelimiter: true}); const section = new SettingSection({noDelimiter: true});
const inputFields: InputField[] = [];
const inputWrapper = document.createElement('div'); if(appChatsManager.hasRights(-this.peerId, 'change_info')) {
inputWrapper.classList.add('input-wrapper'); const inputFields: InputField[] = [];
this.nameInputField = new InputField({
label: 'Name',
name: 'channel-name',
maxLength: 255
});
this.descriptionInputField = new InputField({
label: 'Description',
name: 'channel-description',
maxLength: 255
});
this.nameInputField.setOriginalValue(appChatsManager.getChat(-this.peerId).title);
this.descriptionInputField.setOriginalValue(chatFull.about); const inputWrapper = document.createElement('div');
inputWrapper.classList.add('input-wrapper');
this.nameInputField = new InputField({
label: 'Name',
name: 'channel-name',
maxLength: 255
});
this.descriptionInputField = new InputField({
label: 'Description',
name: 'channel-description',
maxLength: 255
});
this.nameInputField.setOriginalValue(appChatsManager.getChat(-this.peerId).title);
inputWrapper.append(this.nameInputField.container, this.descriptionInputField.container); this.descriptionInputField.setOriginalValue(chatFull.about);
inputFields.push(this.nameInputField, this.descriptionInputField);
this.editPeer = new EditPeer({ inputWrapper.append(this.nameInputField.container, this.descriptionInputField.container);
peerId: this.peerId,
inputFields, inputFields.push(this.nameInputField, this.descriptionInputField);
listenerSetter: this.listenerSetter
});
this.content.append(this.editPeer.nextBtn);
const groupTypeRow = new Row({ this.editPeer = new EditPeer({
title: 'Channel Type', peerId: this.peerId,
subtitle: 'Private', inputFields,
clickable: true, listenerSetter: this.listenerSetter
icon: 'lock' });
}); this.content.append(this.editPeer.nextBtn);
section.content.append(this.editPeer.avatarEdit.container, inputWrapper);
attachClickEvent(this.editPeer.nextBtn, () => {
this.editPeer.nextBtn.disabled = true;
let promises: Promise<any>[] = [];
const id = -this.peerId;
if(this.nameInputField.isValid()) {
promises.push(appChatsManager.editTitle(id, this.nameInputField.value));
}
if(this.descriptionInputField.isValid()) {
promises.push(appChatsManager.editAbout(id, this.descriptionInputField.value));
}
if(this.editPeer.uploadAvatar) {
promises.push(this.editPeer.uploadAvatar().then(inputFile => {
return appChatsManager.editPhoto(id, inputFile);
}));
}
Promise.race(promises).finally(() => {
this.editPeer.nextBtn.removeAttribute('disabled');
this.close();
});
}, {listenerSetter: this.listenerSetter});
}
if(appChatsManager.hasRights(-this.peerId, 'change_type')) {
const channelTypeRow = new Row({
title: 'Channel Type',
subtitle: 'Private',
clickable: true,
icon: 'lock'
});
section.content.append(channelTypeRow.container);
}
if(appChatsManager.hasRights(-this.peerId, 'change_info')) {
const discussionRow = new Row({
title: 'Discussion',
subtitle: 'Add',
clickable: true,
icon: 'message'
});
section.content.append(discussionRow.container);
}
const administratorsRow = new Row({ const administratorsRow = new Row({
title: 'Administrators', title: 'Administrators',
subtitle: '5', subtitle: '' + chatFull.admins_count,
icon: 'admin', icon: 'admin',
clickable: true clickable: true
}); });
const signMessagesCheckboxField = new CheckboxField({ section.content.append(administratorsRow.container);
text: 'Sign Messages',
checked: false if(appChatsManager.hasRights(-this.peerId, 'change_info')) {
}); const signMessagesCheckboxField = new CheckboxField({
text: 'Sign Messages',
checked: false
});
section.content.append(this.editPeer.avatarEdit.container, inputWrapper, groupTypeRow.container, administratorsRow.container, signMessagesCheckboxField.label); section.content.append(signMessagesCheckboxField.label);
}
this.scrollable.append(section.container); this.scrollable.append(section.container);
attachClickEvent(this.editPeer.nextBtn, () => {
this.editPeer.nextBtn.disabled = true;
let promises: Promise<any>[] = [];
const id = -this.peerId;
if(this.nameInputField.isValid()) {
promises.push(appChatsManager.editTitle(id, this.nameInputField.value));
}
if(this.descriptionInputField.isValid()) {
promises.push(appChatsManager.editAbout(id, this.descriptionInputField.value));
}
if(this.editPeer.uploadAvatar) {
promises.push(this.editPeer.uploadAvatar().then(inputFile => {
return appChatsManager.editPhoto(id, inputFile);
}));
}
Promise.race(promises).finally(() => {
this.editPeer.nextBtn.removeAttribute('disabled');
this.close();
});
}, {listenerSetter: this.listenerSetter});
} }
{ {
@ -123,7 +147,7 @@ export default class AppEditChannelTab extends SliderSuperTab {
this.scrollable.append(section.container); this.scrollable.append(section.container);
} }
{ if(appChatsManager.hasRights(-this.peerId, 'delete_chat')) {
const section = new SettingSection({ const section = new SettingSection({
}); });
@ -133,7 +157,7 @@ export default class AppEditChannelTab extends SliderSuperTab {
attachClickEvent(btnDelete, () => { attachClickEvent(btnDelete, () => {
new PopupPeer('popup-delete-channel', { new PopupPeer('popup-delete-channel', {
peerId: this.peerId, peerId: this.peerId,
title: 'Delete Group?', title: 'Delete Channel?',
description: `Are you sure you want to delete this channel? All subscribers will be removed and all messages will be lost.`, description: `Are you sure you want to delete this channel? All subscribers will be removed and all messages will be lost.`,
buttons: addCancelButton([{ buttons: addCancelButton([{
text: 'DELETE', text: 'DELETE',

56
src/components/sidebarRight/tabs/editGroup.ts

@ -57,21 +57,29 @@ export default class AppEditGroupTab extends SliderSuperTab {
}); });
this.content.append(this.editPeer.nextBtn); this.content.append(this.editPeer.nextBtn);
//section.content.append(this.editPeer.avatarEdit.container, inputWrapper); section.content.append(this.editPeer.avatarEdit.container, inputWrapper);
if(appChatsManager.hasRights(-this.peerId, 'change_type')) {
const groupTypeRow = new Row({
title: 'Group Type',
subtitle: 'Private',
clickable: true,
icon: 'lock'
});
section.content.append(groupTypeRow.container);
}
const groupTypeRow = new Row({ if(appChatsManager.hasRights(-this.peerId, 'change_permissions')) {
title: 'Group Type', const permissionsRow = new Row({
subtitle: 'Private', title: 'Permissions',
clickable: true, subtitle: '8/8',
icon: 'lock' icon: 'permissions',
}); clickable: true
});
const permissionsRow = new Row({ section.content.append(permissionsRow.container);
title: 'Permissions', }
subtitle: '8/8',
icon: 'permissions',
clickable: true
});
const administratorsRow = new Row({ const administratorsRow = new Row({
title: 'Administrators', title: 'Administrators',
@ -80,7 +88,7 @@ export default class AppEditGroupTab extends SliderSuperTab {
clickable: true clickable: true
}); });
section.content.append(this.editPeer.avatarEdit.container, inputWrapper, groupTypeRow.container, permissionsRow.container, administratorsRow.container); section.content.append(administratorsRow.container);
this.scrollable.append(section.container); this.scrollable.append(section.container);
@ -123,20 +131,24 @@ export default class AppEditGroupTab extends SliderSuperTab {
clickable: true clickable: true
}); });
const showChatHistoryCheckboxField = new CheckboxField({ section.content.append(membersRow.container);
text: 'Show chat history for new members'
});
if(appChatsManager.isChannel(-this.peerId) && !(chatFull as ChatFull.channelFull).pFlags.hidden_prehistory) { if(appChatsManager.hasRights(-this.peerId, 'change_permissions')) {
showChatHistoryCheckboxField.value = true; const showChatHistoryCheckboxField = new CheckboxField({
text: 'Show chat history for new members'
});
if(appChatsManager.isChannel(-this.peerId) && !(chatFull as ChatFull.channelFull).pFlags.hidden_prehistory) {
showChatHistoryCheckboxField.value = true;
}
section.content.append(showChatHistoryCheckboxField.label);
} }
section.content.append(membersRow.container, showChatHistoryCheckboxField.label);
this.scrollable.append(section.container); this.scrollable.append(section.container);
} }
if(appChatsManager.isChannel(-this.peerId)) { if(appChatsManager.isChannel(-this.peerId) && appChatsManager.hasRights(-this.peerId, 'delete_chat')) {
const section = new SettingSection({}); const section = new SettingSection({});
const btnDelete = Button('btn-primary btn-transparent danger', {icon: 'delete', text: 'Delete Group'}); const btnDelete = Button('btn-primary btn-transparent danger', {icon: 'delete', text: 'Delete Group'});

17
src/components/sidebarRight/tabs/sharedMedia.ts

@ -19,6 +19,8 @@ import AppEditGroupTab from "./editGroup";
import PeerTitle from "../../peerTitle"; import PeerTitle from "../../peerTitle";
import AppEditChannelTab from "./editChannel"; import AppEditChannelTab from "./editChannel";
import AppEditContactTab from "./editContact"; import AppEditContactTab from "./editContact";
import appChatsManager from "../../../lib/appManagers/appChatsManager";
import { Chat } from "../../../layer";
let setText = (text: string, el: HTMLDivElement) => { let setText = (text: string, el: HTMLDivElement) => {
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
@ -207,7 +209,7 @@ export default class AppSharedMediaTab implements SliderTab {
const peerId = this.peerId; const peerId = this.peerId;
if(needClear) { if(needClear) {
this.profileElements.subtitle.innerHTML = ''; this.profileElements.subtitle.innerHTML = ''; // ! HERE U CAN FIND WHITESPACE
} }
appImManager.getPeerStatus(this.peerId).then((subtitle) => { appImManager.getPeerStatus(this.peerId).then((subtitle) => {
@ -215,7 +217,7 @@ export default class AppSharedMediaTab implements SliderTab {
return; return;
} }
this.profileElements.subtitle.innerHTML = subtitle; this.profileElements.subtitle.innerHTML = subtitle || '';
}); });
}; };
@ -368,7 +370,16 @@ export default class AppSharedMediaTab implements SliderTab {
dialog: true dialog: true
}).element); }).element);
this.editBtn.style.display = ''; if(peerId > 0) {
if(peerId !== rootScope.myId && appUsersManager.isContact(peerId)) {
this.editBtn.style.display = '';
}
} else {
const chat: Chat = appChatsManager.getChat(-peerId);
if(chat._ === 'chat' || (chat as Chat.channel).admin_rights) {
this.editBtn.style.display = '';
}
}
this.setPeerStatus(true); this.setPeerStatus(true);
} }

72
src/lib/appManagers/appChatsManager.ts

@ -14,7 +14,7 @@ import appUsersManager from "./appUsersManager";
export type Channel = Chat.channel; export type Channel = Chat.channel;
export type ChatRights = 'send' | 'edit_title' | 'edit_photo' | 'invite' | 'pin' | 'deleteRevoke' | 'delete'; export type ChatRights = 'send' | 'change_info' | 'change_permissions' | 'change_type' | 'pin_messages' | 'deleteRevoke' | 'delete_chat';
export type UserTyping = Partial<{userId: number, action: SendMessageAction, timeout: number}>; export type UserTyping = Partial<{userId: number, action: SendMessageAction, timeout: number}>;
@ -172,13 +172,13 @@ export class AppChatsManager {
} }
public hasRights(id: number, action: ChatRights, flag?: keyof ChatBannedRights['pFlags']) { public hasRights(id: number, action: ChatRights, flag?: keyof ChatBannedRights['pFlags']) {
const chat = this.getChat(id); const chat: Chat = this.getChat(id);
if(chat._ === 'chatEmpty') return false; if(chat._ === 'chatEmpty') return false;
if(chat._ === 'chatForbidden' || if(chat._ === 'chatForbidden' ||
chat._ === 'channelForbidden' || chat._ === 'channelForbidden' ||
chat.pFlags.kicked || (chat as Chat.chat).pFlags.kicked ||
(chat.pFlags.left && !chat.pFlags.megagroup)) { (chat.pFlags.left && !(chat as Chat.channel).pFlags.megagroup)) {
return false; return false;
} }
@ -186,19 +186,23 @@ export class AppChatsManager {
return true; return true;
} }
const rights = chat.admin_rights || chat.banned_rights || chat.default_banned_rights; const rights = chat.admin_rights || (chat as Chat.channel).banned_rights || chat.default_banned_rights;
let myFlags: {[flag in keyof ChatBannedRights['pFlags'] | keyof ChatAdminRights['pFlags']]: true}; if(!rights) {
if(rights) myFlags = rights.pFlags; return false;
}
let myFlags: Partial<{[flag in keyof ChatBannedRights['pFlags'] | keyof ChatAdminRights['pFlags']]: true}> = {};
if(rights) myFlags = rights.pFlags as any;
switch(action) { switch(action) {
// good // good
case 'send': { case 'send': {
if(flag && myFlags && myFlags[flag]) { if(flag && myFlags[flag]) {
return false; return false;
} }
if(chat._ === 'channel') { if(chat._ === 'channel') {
if((!chat.pFlags.megagroup && !myFlags?.post_messages)) { if((!chat.pFlags.megagroup && !myFlags.post_messages)) {
return false; return false;
} }
} }
@ -206,49 +210,27 @@ export class AppChatsManager {
break; break;
} }
// good // * revoke foreign messages
case 'deleteRevoke': { case 'deleteRevoke': {
if(chat._ === 'channel') { return !!myFlags.delete_messages;
return !!myFlags?.delete_messages;
} else if(!chat.pFlags.admin) {
return false;
}
break;
} }
// good case 'pin_messages': {
case 'pin': { return rights._ === 'chatAdminRights' ? myFlags[action] || !!myFlags.post_messages : !myFlags[action];
if(chat._ === 'channel') { }
return chat.admin_rights ? !!myFlags.pin_messages || !!myFlags.post_messages : myFlags && !myFlags.pin_messages;
} else {
if(myFlags?.pin_messages && !chat.pFlags.admin) {
return false;
}
}
break; case 'change_info': {
return rights._ === 'chatAdminRights' ? myFlags[action] : !myFlags[action];
} }
case 'edit_title': // * only creator can do that
case 'edit_photo': case 'change_type':
case 'invite': { case 'delete_chat': {
if(chat._ === 'channel') { return false;
if(chat.pFlags.megagroup) { }
if(!(action === 'invite' && chat.pFlags.democracy)) {
return false;
}
} else {
return false;
}
} else {
if(chat.pFlags.admins_enabled &&
!chat.pFlags.admin) {
return false;
}
}
break; case 'change_permissions': {
return rights._ === 'chatAdminRights' && myFlags['ban_users'];
} }
} }

2
src/lib/appManagers/appPeersManager.ts

@ -42,7 +42,7 @@ export class AppPeersManager {
} */ } */
public canPinMessage(peerId: number) { public canPinMessage(peerId: number) {
return peerId > 0 || appChatsManager.hasRights(-peerId, 'pin'); return peerId > 0 || appChatsManager.hasRights(-peerId, 'pin_messages');
} }
public getPeerPhoto(peerId: number) { public getPeerPhoto(peerId: number) {

Loading…
Cancel
Save