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.

301 lines
8.6 KiB

4 years ago
import { MOUNT_CLASS_TO } from "../../config/debug";
import { isObject } from "../../helpers/object";
import { ChatPhoto, DialogPeer, InputDialogPeer, InputNotifyPeer, InputPeer, Peer, Update, UserProfilePhoto } from "../../layer";
import { LangPackKey } from "../langPack";
import { RichTextProcessor } from "../richtextprocessor";
import rootScope from "../rootScope";
import appChatsManager from "./appChatsManager";
import appUsersManager from "./appUsersManager";
5 years ago
// https://github.com/eelcohn/Telegram-API/wiki/Calculating-color-for-a-Telegram-user-on-IRC
/*
HTML-color IRC-color Description
#c03d33 4 red
#4fad2d 3 green
#d09306 7 yellow
#168acd 10 blue
#8544d6 6 purple
#cd4073 13 pink
#2996ad 11 sea
#ce671b 5 orange
*/
const DialogColorsFg = ['#fc5c51', '#0fb297', '#d09306', '#3d72ed', '#895dd5', '#cd4073', '#00c1a6', '#fa790f'];
const DialogColors = ['red', 'green', 'yellow', 'blue', 'violet', 'pink', 'cyan', 'orange'];
const DialogColorsMap = [0, 7, 4, 1, 6, 3, 5];
export type PeerType = 'channel' | 'chat' | 'megagroup' | 'group' | 'saved';
export class AppPeersManager {
constructor() {
rootScope.on('apiUpdate', (e) => {
const update = e as Update;
//console.log('on apiUpdate', update);
switch(update._) {
case 'updatePeerBlocked': {
rootScope.broadcast('peer_block', {peerId: this.getPeerId(update.peer_id), blocked: update.blocked});
break;
}
}
});
}
/* public savePeerInstance(peerId: number, instance: any) {
if(peerId < 0) appChatsManager.saveApiChat(instance);
else appUsersManager.saveApiUser(instance);
} */
public canPinMessage(peerId: number) {
return peerId > 0 || appChatsManager.hasRights(-peerId, 'pin_messages');
}
public getPeerPhoto(peerId: number): UserProfilePhoto.userProfilePhoto | ChatPhoto.chatPhoto {
const photo = peerId > 0
? appUsersManager.getUserPhoto(peerId)
: appChatsManager.getChatPhoto(-peerId);
return photo._ !== 'chatPhotoEmpty' && photo._ !== 'userProfilePhotoEmpty' ? photo : null;
}
5 years ago
public getPeerMigratedTo(peerId: number) {
if(peerId >= 0) {
5 years ago
return false;
}
let chat = appChatsManager.getChat(-peerId);
5 years ago
if(chat && chat.migrated_to && chat.pFlags.deactivated) {
return this.getPeerId(chat.migrated_to);
5 years ago
}
5 years ago
return false;
}
5 years ago
public getPeerTitle(peerId: number | any, plainText = false, onlyFirstName = false) {
if(!peerId) {
peerId = rootScope.myId;
}
5 years ago
let peer: any = {};
if(!isObject(peerId)) {
peer = this.getPeer(peerId);
} else peer = peerId;
5 years ago
let title = '';
if(peerId > 0) {
5 years ago
if(peer.first_name) title += peer.first_name;
if(peer.last_name) title += ' ' + peer.last_name;
if(!title) title = peer.pFlags.deleted ? 'Deleted Account' : peer.username;
5 years ago
else title = title.trim();
} else {
title = peer.title;
}
if(onlyFirstName) {
title = title.split(' ')[0];
}
5 years ago
return plainText ? title : RichTextProcessor.wrapEmojiText(title);
}
public getOutputPeer(peerId: number): Peer {
if(peerId > 0) {
return {_: 'peerUser', user_id: peerId};
5 years ago
}
let chatId = -peerId;
if(appChatsManager.isChannel(chatId)) {
return {_: 'peerChannel', channel_id: chatId};
5 years ago
}
return {_: 'peerChat', chat_id: chatId};
}
5 years ago
public getPeerString(peerId: number) {
if(peerId > 0) {
return appUsersManager.getUserString(peerId);
5 years ago
}
return appChatsManager.getChatString(-peerId);
}
5 years ago
public getPeerUsername(peerId: number): string {
if(peerId > 0) {
return appUsersManager.getUser(peerId).username || '';
5 years ago
}
return appChatsManager.getChat(-peerId).username || '';
}
5 years ago
public getPeer(peerId: number) {
return peerId > 0
? appUsersManager.getUser(peerId)
: appChatsManager.getChat(-peerId)
}
5 years ago
public getPeerId(peerId: Peer | InputPeer | number | string): number {
if(typeof(peerId) === 'number') return peerId;
else if(isObject(peerId)) return (peerId as Peer.peerUser).user_id || -((peerId as Peer.peerChannel).channel_id || (peerId as Peer.peerChat).chat_id);
else if(!peerId) return 0;
const isUser = (peerId as string).charAt(0) === 'u';
const peerParams = (peerId as string).substr(1).split('_');
5 years ago
return isUser ? +peerParams[0] : -peerParams[0] || 0;
}
5 years ago
public getDialogPeer(peerId: number): DialogPeer {
return {
_: 'dialogPeer',
peer: this.getOutputPeer(peerId)
};
}
public isChannel(peerId: number): boolean {
return (peerId < 0) && appChatsManager.isChannel(-peerId);
}
5 years ago
public isMegagroup(peerId: number) {
return (peerId < 0) && appChatsManager.isMegagroup(-peerId);
}
public isAnyGroup(peerId: number): boolean {
return (peerId < 0) && !appChatsManager.isBroadcast(-peerId);
}
public isBroadcast(peerId: number): boolean {
return this.isChannel(peerId) && !this.isMegagroup(peerId);
}
public isBot(peerId: number): boolean {
return (peerId > 0) && appUsersManager.isBot(peerId);
}
/* 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
};
}
} */
public getInputNotifyPeerById(peerId: number, ignorePeerId: true): Exclude<InputNotifyPeer, InputNotifyPeer.inputNotifyPeer>;
public getInputNotifyPeerById(peerId: number, ignorePeerId?: false): InputNotifyPeer.inputNotifyPeer;
public getInputNotifyPeerById(peerId: number, ignorePeerId?: boolean): InputNotifyPeer {
if(ignorePeerId) {
if(peerId > 0) {
return {_: 'inputNotifyUsers'};
} else {
if(appPeersManager.isBroadcast(peerId)) {
return {_: 'inputNotifyBroadcasts'};
} else {
return {_: 'inputNotifyChats'};
}
}
} else {
return {
_: 'inputNotifyPeer',
peer: this.getInputPeerById(peerId)
};
}
}
public getInputPeerById(peerId: number): InputPeer {
if(!peerId) {
return {_: 'inputPeerEmpty'};
5 years ago
}
if(peerId < 0) {
const chatId = -peerId;
if(!appChatsManager.isChannel(chatId)) {
return appChatsManager.getChatInputPeer(chatId);
5 years ago
} else {
return appChatsManager.getChannelInputPeer(chatId);
5 years ago
}
}
5 years ago
return {
_: 'inputPeerUser',
user_id: peerId,
access_hash: appUsersManager.getUser(peerId).access_hash
5 years ago
};
}
5 years ago
public getInputDialogPeerById(peerId: number): InputDialogPeer {
return {
_: 'inputDialogPeer',
peer: this.getInputPeerById(peerId)
}
}
public getPeerColorById(peerId: number, pic = true) {
if(!peerId) return '';
const idx = DialogColorsMap[(peerId < 0 ? -peerId : peerId) % 7];
const color = (pic ? DialogColors : DialogColorsFg)[idx];
return color;
}
public getPeerSearchText(peerId: number) {
let text;
if(peerId > 0) {
text = '%pu ' + appUsersManager.getUserSearchText(peerId);
} else if(peerId < 0) {
const chat = appChatsManager.getChat(-peerId);
5 years ago
text = '%pg ' + (chat.title || '');
}
return text;
}
public getDialogType(peerId: number): PeerType {
if(appPeersManager.isMegagroup(peerId)) {
return 'megagroup';
} else if(appPeersManager.isChannel(peerId)) {
return 'channel';
} else if(peerId < 0) {
return 'group';
} else {
return peerId === rootScope.myId ? 'saved' : 'chat';
}
}
public getDeleteButtonText(peerId: number): LangPackKey {
switch(this.getDialogType(peerId)) {
case 'channel':
return 'ChatList.Context.LeaveChannel';
case 'megagroup':
return 'ChatList.Context.LeaveGroup';
case 'group':
return 'ChatList.Context.DeleteAndExit';
default:
return 'ChatList.Context.DeleteChat';
}
}
}
5 years ago
const appPeersManager = new AppPeersManager();
MOUNT_CLASS_TO.appPeersManager = appPeersManager;
export default appPeersManager;