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.

260 lines
7.1 KiB

import { isObject } from "../../helpers/object";
import { DialogPeer, InputDialogPeer, InputPeer, Peer } from "../../layer";
import { MOUNT_CLASS_TO } from "../mtproto/mtproto_config";
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 = ['#c03d33', '#4fad2d', '#d09306', '#168acd', '#8544d6', '#cd4073', '#2996ad', '#ce671b'];
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 {
/* 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');
}
public getPeerPhoto(peerId: number) {
return peerId > 0
? appUsersManager.getUserPhoto(peerId)
: appChatsManager.getChatPhoto(-peerId);
}
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(peerString: any/* Peer | number | string */): number {
if(typeof(peerString) === 'number') return peerString;
else if(isObject(peerString)) return peerString.user_id ? peerString.user_id : -(peerString.channel_id || peerString.chat_id);
else if(!peerString) return 0;
const isUser = peerString.charAt(0) == 'u';
const peerParams = peerString.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(id: number): boolean {
return this.isChannel(id) && !this.isMegagroup(id);
}
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 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) {
switch(this.getDialogType(peerId)) {
case 'megagroup':
case 'channel':
return 'Leave';
default:
return 'Delete';
}
}
}
5 years ago
const appPeersManager = new AppPeersManager();
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.appPeersManager = appPeersManager);
export default appPeersManager;