Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
8.8 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
2 years ago
*
3 years ago
* Originally from:
* https://github.com/zhukov/webogram
* Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
* https://github.com/zhukov/webogram/blob/master/LICENSE
*/
2 years ago
import type {Chat, ChatPhoto, DialogPeer, InputChannel, InputDialogPeer, InputNotifyPeer, InputPeer, Peer, Update, User, UserProfilePhoto} from '../../layer';
import type {LangPackKey} from '../langPack';
import {getRestrictionReason} from '../../helpers/restrictions';
import isObject from '../../helpers/object/isObject';
import {AppManager} from './manager';
import getPeerId from './utils/peers/getPeerId';
import isUser from './utils/peers/isUser';
import isAnyChat from './utils/peers/isAnyChat';
import {NULL_PEER_ID} from '../mtproto/mtproto_config';
3 years ago
export type PeerType = 'channel' | 'chat' | 'megagroup' | 'group' | 'saved';
3 years ago
export class AppPeersManager extends AppManager {
public get peerId() {
return this.appUsersManager.userId.toPeerId();
}
3 years ago
/* public savePeerInstance(peerId: PeerId, instance: any) {
3 years ago
if(peerId < 0) appChatsManager.saveApiChat(instance);
else appUsersManager.saveApiUser(instance);
} */
3 years ago
public canPinMessage(peerId: PeerId) {
3 years ago
return peerId.isUser() || this.appChatsManager.hasRights(peerId.toChatId(), 'pin_messages');
3 years ago
}
3 years ago
public getPeerPhoto(peerId: PeerId): UserProfilePhoto.userProfilePhoto | ChatPhoto.chatPhoto {
if(this.isRestricted(peerId)) {
return;
}
2 years ago
const photo = peerId.isUser() ?
this.appUsersManager.getUserPhoto(peerId.toUserId()) :
this.appChatsManager.getChatPhoto(peerId.toChatId());
3 years ago
return photo._ !== 'chatPhotoEmpty' && photo._ !== 'userProfilePhotoEmpty' ? photo : undefined;
3 years ago
}
3 years ago
public getPeerMigratedTo(peerId: PeerId) {
if(peerId.isUser()) {
3 years ago
return false;
}
3 years ago
const chat: Chat.chat = this.appChatsManager.getChat(peerId.toChatId());
3 years ago
if(chat && chat.migrated_to && chat.pFlags.deactivated) {
3 years ago
return getPeerId(chat.migrated_to as InputChannel.inputChannel);
3 years ago
}
2 years ago
3 years ago
return false;
}
3 years ago
public getOutputPeer(peerId: PeerId): Peer {
if(peerId.isUser()) {
return {_: 'peerUser', user_id: peerId.toUserId()};
3 years ago
}
3 years ago
const chatId = peerId.toChatId();
3 years ago
if(this.appChatsManager.isChannel(chatId)) {
3 years ago
return {_: 'peerChannel', channel_id: chatId};
}
return {_: 'peerChat', chat_id: chatId};
}
3 years ago
public getPeerString(peerId: PeerId) {
if(peerId.isUser()) {
3 years ago
return this.appUsersManager.getUserString(peerId.toUserId());
3 years ago
}
3 years ago
return this.appChatsManager.getChatString(peerId.toChatId());
3 years ago
}
3 years ago
public getPeerUsername(peerId: PeerId): string {
return this.getPeer(peerId).username || '';
3 years ago
}
3 years ago
public getPeer(peerId: PeerId) {
2 years ago
return peerId.isUser() ?
this.appUsersManager.getUser(peerId.toUserId()) :
this.appChatsManager.getChat(peerId.toChatId());
3 years ago
}
public getPeerId(peerId: Parameters<typeof getPeerId>[0]) {
return getPeerId(peerId) || (isObject<InputPeer>(peerId) && peerId._ === 'inputPeerSelf' && this.peerId) || NULL_PEER_ID;
}
3 years ago
public getDialogPeer(peerId: PeerId): DialogPeer {
3 years ago
return {
_: 'dialogPeer',
peer: this.getOutputPeer(peerId)
};
}
3 years ago
public isChannel(peerId: PeerId): boolean {
3 years ago
return !peerId.isUser() && this.appChatsManager.isChannel(peerId.toChatId());
3 years ago
}
3 years ago
public isMegagroup(peerId: PeerId) {
3 years ago
return !peerId.isUser() && this.appChatsManager.isMegagroup(peerId.toChatId());
3 years ago
}
3 years ago
public isAnyGroup(peerId: PeerId): boolean {
3 years ago
return !peerId.isUser() && !this.appChatsManager.isBroadcast(peerId.toChatId());
3 years ago
}
3 years ago
public isBroadcast(peerId: PeerId): boolean {
3 years ago
return this.isChannel(peerId) && !this.isMegagroup(peerId);
}
3 years ago
public isBot(peerId: PeerId): boolean {
3 years ago
return peerId.isUser() && this.appUsersManager.isBot(peerId.toUserId());
3 years ago
}
public isContact(peerId: PeerId): boolean {
3 years ago
return peerId.isUser() && this.appUsersManager.isContact(peerId.toUserId());
3 years ago
}
public isUser(peerId: PeerId)/* : peerId is UserId */ {
return isUser(peerId);
3 years ago
}
2 years ago
3 years ago
public isAnyChat(peerId: PeerId) {
return isAnyChat(peerId);
3 years ago
}
public isRestricted(peerId: PeerId) {
3 years ago
return peerId.isUser() ? this.appUsersManager.isRestricted(peerId.toUserId()) : this.appChatsManager.isRestricted(peerId.toChatId());
}
public getRestrictionReasonText(peerId: PeerId) {
const peer: Chat.channel | User.user = this.getPeer(peerId);
const reason = peer.restriction_reason ? getRestrictionReason(peer.restriction_reason) : undefined;
if(reason) {
return reason.text;
} else {
return peerId.isUser() ? 'This user is restricted' : 'This chat is restricted';
}
}
3 years ago
/* public getInputPeer(peerString: string): InputPeer {
var firstChar = peerString.charAt(0);
var peerParams = peerString.substr(1).split('_');
let id = +peerParams[0];
if(firstChar === 'u') {
//appUsersManager.saveUserAccess(id, peerParams[1]);
return {
_: 'inputPeerUser',
user_id: id,
access_hash: peerParams[1]
};
} else if(firstChar === 'c' || firstChar === 's') {
//appChatsManager.saveChannelAccess(id, peerParams[1]);
if(firstChar === 's') {
appChatsManager.saveIsMegagroup(id);
}
return {
_: 'inputPeerChannel',
channel_id: id,
access_hash: peerParams[1] || '0'
};
} else {
return {
_: 'inputPeerChat',
chat_id: id
};
}
} */
3 years ago
public getInputNotifyPeerById(peerId: PeerId, ignorePeerId: true): Exclude<InputNotifyPeer, InputNotifyPeer.inputNotifyPeer>;
public getInputNotifyPeerById(peerId: PeerId, ignorePeerId?: false): InputNotifyPeer.inputNotifyPeer;
public getInputNotifyPeerById(peerId: PeerId, ignorePeerId?: boolean): InputNotifyPeer {
3 years ago
if(ignorePeerId) {
3 years ago
if(peerId.isUser()) {
3 years ago
return {_: 'inputNotifyUsers'};
} else {
3 years ago
if(this.isBroadcast(peerId)) {
3 years ago
return {_: 'inputNotifyBroadcasts'};
} else {
return {_: 'inputNotifyChats'};
}
}
} else {
return {
2 years ago
_: 'inputNotifyPeer',
3 years ago
peer: this.getInputPeerById(peerId)
};
}
}
3 years ago
public getInputPeerById(peerId: PeerId): InputPeer {
3 years ago
if(!peerId) {
return {_: 'inputPeerEmpty'};
}
3 years ago
if(!peerId.isUser()) {
const chatId = peerId.toChatId();
3 years ago
return this.appChatsManager.getInputPeer(chatId);
3 years ago
}
3 years ago
const userId = peerId.toUserId();
3 years ago
return this.appUsersManager.getUserInputPeer(userId);
3 years ago
}
/**
* ! use it only in safe places like requests
*/
3 years ago
public getInputPeerSelf(): InputPeer.inputPeerSelf {
return {_: 'inputPeerSelf'};
3 years ago
}
3 years ago
public getInputDialogPeerById(peerId: PeerId | InputPeer): InputDialogPeer {
3 years ago
return {
_: 'inputDialogPeer',
3 years ago
peer: isObject<InputPeer>(peerId) ? peerId : this.getInputPeerById(peerId)
};
3 years ago
}
3 years ago
public getPeerSearchText(peerId: PeerId) {
let text: string;
if(this.isUser(peerId)) {
3 years ago
text = '%pu ' + this.appUsersManager.getUserSearchText(peerId.toUserId());
3 years ago
} else {
3 years ago
const chat = this.appChatsManager.getChat(peerId.toChatId());
3 years ago
text = '%pg ' + (chat.title || '');
}
3 years ago
3 years ago
return text;
}
3 years ago
public getDialogType(peerId: PeerId): PeerType {
if(this.isMegagroup(peerId)) {
3 years ago
return 'megagroup';
3 years ago
} else if(this.isChannel(peerId)) {
3 years ago
return 'channel';
3 years ago
} else if(!this.isUser(peerId)) {
3 years ago
return 'group';
} else {
return peerId === this.peerId ? 'saved' : 'chat';
3 years ago
}
}
2 years ago
public getDeleteButtonText(peerId: PeerId): Extract<LangPackKey, 'ChannelDelete' | 'ChatList.Context.LeaveChannel' | 'DeleteMega' | 'ChatList.Context.LeaveGroup' | 'ChatList.Context.DeleteChat'> {
3 years ago
switch(this.getDialogType(peerId)) {
case 'channel':
3 years ago
return this.appChatsManager.hasRights(peerId.toChatId(), 'delete_chat') ? 'ChannelDelete' : 'ChatList.Context.LeaveChannel';
3 years ago
case 'megagroup':
case 'group':
3 years ago
return this.appChatsManager.hasRights(peerId.toChatId(), 'delete_chat') ? 'DeleteMega' : 'ChatList.Context.LeaveGroup';
2 years ago
3 years ago
default:
return 'ChatList.Context.DeleteChat';
}
}
public noForwards(peerId: PeerId) {
if(peerId.isUser()) return false;
else {
3 years ago
const chat = this.appChatsManager.getChatTyped(peerId.toChatId());
return !!(chat as Chat.chat).pFlags?.noforwards;
}
}
3 years ago
}
export type IsPeerType = 'isChannel' | 'isMegagroup' | 'isAnyGroup' | 'isBroadcast' | 'isBot' | 'isContact' | 'isUser' | 'isAnyChat';