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.

590 lines
17 KiB

import { numberWithCommas } from "../../helpers/number";
import { isObject, safeReplaceObject, copy } from "../../helpers/object";
import { ChatAdminRights, ChatBannedRights, ChatFull, ChatParticipants, InputChannel, InputChatPhoto, InputFile, InputPeer, SendMessageAction, Updates } from "../../layer";
import apiManager from '../mtproto/mtprotoworker';
import { MOUNT_CLASS_TO } from "../mtproto/mtproto_config";
import { RichTextProcessor } from "../richtextprocessor";
import rootScope from "../rootScope";
import apiUpdatesManager from "./apiUpdatesManager";
import appMessagesManager from "./appMessagesManager";
import appProfileManager from "./appProfileManager";
import appStateManager from "./appStateManager";
import appUsersManager from "./appUsersManager";
export type Channel = {
_: 'channel',
flags: number,
pFlags: Partial<{
creator: true,
left: true,
broadcast: true,
verified: true,
megagroup: true,
restricted: true,
signatures: true,
min: true,
scam: true,
has_link: true,
has_geo: true,
slowmode_enabled: true
}>,
id: number,
access_hash?: string,
title: string,
username?: string,
photo: any,
date: number,
version: number,
restriction_reason?: any,
admin_rights?: any,
banned_rights?: any,
default_banned_rights?: any,
participants_count: number
};
4 years ago
export type Chat = {
_: 'chat',
flags: number,
pFlags: Partial<{
creator: true,
kicked: true,
left: true,
deactivated: true
}>,
id: number,
title: string,
photo: any,
participants_count: number,
date: number,
version: number,
migrated_to?: any,
admin_rights?: any,
default_banned_rights?: any
};
export type ChatRights = 'send' | 'edit_title' | 'edit_photo' | 'invite' | 'pin' | 'deleteRevoke' | 'delete';
export type UserTyping = Partial<{userId: number, action: SendMessageAction, timeout: number}>;
4 years ago
export class AppChatsManager {
public chats: {[id: number]: Channel | Chat | any} = {};
//public usernames: any = {};
//public channelAccess: any = {};
//public megagroups: {[id: number]: true} = {};
public cachedPhotoLocations: {[id: number]: any} = {};
4 years ago
public megagroupOnlines: {[id: number]: {timestamp: number, onlines: number}} = {};
public typingsInPeer: {[peerId: number]: UserTyping[]} = {};
4 years ago
constructor() {
rootScope.on('apiUpdate', (e) => {
4 years ago
// console.log('on apiUpdate', update)
const update = e.detail;
4 years ago
switch(update._) {
case 'updateChannel':
const channelId = update.channel_id;
4 years ago
//console.log('updateChannel:', update);
rootScope.broadcast('channel_settings', {channelId: channelId});
4 years ago
break;
case 'updateUserTyping':
case 'updateChatUserTyping': {
if(rootScope.myId == update.user_id) {
return;
}
const peerId = update._ == 'updateUserTyping' ? update.user_id : -update.chat_id;
const typings = this.typingsInPeer[peerId] ?? (this.typingsInPeer[peerId] = []);
let typing = typings.find(t => t.userId == update.user_id);
if(!typing) {
typing = {
userId: update.user_id
};
typings.push(typing);
}
//console.log('updateChatUserTyping', update, typings);
typing.action = update.action;
if(!appUsersManager.hasUser(update.user_id)) {
if(update._ == 'updateChatUserTyping') {
if(update.chat_id && appChatsManager.hasChat(update.chat_id) && !appChatsManager.isChannel(update.chat_id)) {
appProfileManager.getChatFull(update.chat_id);
}
}
//return;
}
appUsersManager.forceUserOnline(update.user_id);
if(typing.timeout !== undefined) clearTimeout(typing.timeout);
typing.timeout = window.setTimeout(() => {
delete typing.timeout;
typings.findAndSplice(t => t.userId == update.user_id);
rootScope.broadcast('peer_typings', {peerId, typings});
if(!typings.length) {
delete this.typingsInPeer[peerId];
}
}, 6000);
rootScope.broadcast('peer_typings', {peerId, typings});
break;
}
4 years ago
}
});
appStateManager.getState().then((state) => {
this.chats = state.chats;
});
4 years ago
}
public saveApiChats(apiChats: any[]) {
apiChats.forEach(chat => this.saveApiChat(chat));
4 years ago
}
public saveApiChat(chat: any) {
if(!isObject(chat)) {
4 years ago
return;
}
// * exclude from state
// defineNotNumerableProperties(chat, ['rTitle', 'initials']);
4 years ago
//chat.rTitle = chat.title || 'chat_title_deleted';
chat.rTitle = RichTextProcessor.wrapRichText(chat.title, {noLinks: true, noLinebreaks: true}) || 'chat_title_deleted';
4 years ago
const oldChat = this.chats[chat.id];
4 years ago
chat.initials = RichTextProcessor.getAbbreviation(chat.title);
4 years ago
if(chat.pFlags === undefined) {
chat.pFlags = {};
4 years ago
}
if(chat.pFlags.min) {
if(oldChat !== undefined) {
4 years ago
return;
}
}
if(chat._ == 'channel' &&
chat.participants_count === undefined &&
oldChat !== undefined &&
oldChat.participants_count) {
chat.participants_count = oldChat.participants_count;
4 years ago
}
/* if(chat.username) {
let searchUsername = searchIndexManager.cleanUsername(chat.username);
this.usernames[searchUsername] = chat.id;
} */
4 years ago
let changedPhoto = false;
if(oldChat === undefined) {
this.chats[chat.id] = chat;
4 years ago
} else {
let oldPhoto = oldChat.photo && oldChat.photo.photo_small;
let newPhoto = chat.photo && chat.photo.photo_small;
if(JSON.stringify(oldPhoto) !== JSON.stringify(newPhoto)) {
changedPhoto = true;
}
safeReplaceObject(oldChat, chat);
rootScope.broadcast('chat_update', chat.id);
4 years ago
}
if(this.cachedPhotoLocations[chat.id] !== undefined) {
safeReplaceObject(this.cachedPhotoLocations[chat.id], chat &&
chat.photo ? chat.photo : {empty: true});
4 years ago
}
if(changedPhoto) {
rootScope.broadcast('avatar_update', -chat.id);
}
4 years ago
}
public getChat(id: number) {
if(id < 0) id = -id;
return this.chats[id] || {_: 'chatEmpty', id: id, deleted: true, access_hash: ''/* this.channelAccess[id] */};
4 years ago
}
public hasRights(id: number, action: ChatRights, flag?: keyof ChatBannedRights['pFlags']) {
const chat = this.getChat(id);
if(chat._ == 'chatEmpty') return false;
4 years ago
if(chat._ == 'chatForbidden' ||
chat._ == 'channelForbidden' ||
chat.pFlags.kicked ||
chat.pFlags.left) {
return false;
}
4 years ago
if(chat.pFlags.creator) {
return true;
}
const rights = chat.admin_rights || chat.banned_rights || chat.default_banned_rights;
let myFlags: {[flag in keyof ChatBannedRights['pFlags'] | keyof ChatAdminRights['pFlags']]: true};
if(rights) myFlags = rights.pFlags;
4 years ago
switch(action) {
// good
case 'send': {
if(flag && myFlags && myFlags[flag]) {
return false;
}
if(chat._ == 'channel') {
if((!chat.pFlags.megagroup && !myFlags?.post_messages)) {
return false;
}
}
break;
}
// good
case 'deleteRevoke': {
if(chat._ == 'channel') {
return !!myFlags?.delete_messages;
} else if(!chat.pFlags.admin) {
4 years ago
return false;
}
break;
}
// good
case 'pin': {
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;
}
}
4 years ago
break;
}
4 years ago
case 'edit_title':
case 'edit_photo':
case 'invite': {
4 years ago
if(chat._ == 'channel') {
if(chat.pFlags.megagroup) {
if(!(action == 'invite' && chat.pFlags.democracy)) {
4 years ago
return false;
}
} else {
return false;
}
} else {
if(chat.pFlags.admins_enabled &&
!chat.pFlags.admin) {
return false;
}
}
4 years ago
break;
}
4 years ago
}
4 years ago
return true;
}
/* public resolveUsername(username: string) {
4 years ago
return this.usernames[username] || 0;
} */
4 years ago
/* public saveChannelAccess(id: number, accessHash: string) {
4 years ago
this.channelAccess[id] = accessHash;
} */
4 years ago
/* public saveIsMegagroup(id: number) {
4 years ago
this.megagroups[id] = true;
} */
4 years ago
public isChannel(id: number) {
if(id < 0) id = -id;
const chat = this.chats[id];
if(chat && (chat._ == 'channel' || chat._ == 'channelForbidden')/* || this.channelAccess[id] */) {
4 years ago
return true;
}
return false;
}
public isMegagroup(id: number) {
/* if(this.megagroups[id]) {
4 years ago
return true;
} */
4 years ago
const chat = this.chats[id];
4 years ago
if(chat && chat._ == 'channel' && chat.pFlags.megagroup) {
return true;
}
return false;
}
public isBroadcast(id: number) {
return this.isChannel(id) && !this.isMegagroup(id);
}
public getChannelInput(id: number): InputChannel {
if(id < 0) id = -id;
4 years ago
return {
_: 'inputChannel',
channel_id: id,
access_hash: this.getChat(id).access_hash/* || this.channelAccess[id] */ || 0
4 years ago
};
}
public getChatInputPeer(id: number): InputPeer.inputPeerChat {
return {
_: 'inputPeerChat',
chat_id: id
};
}
public getChannelInputPeer(id: number): InputPeer.inputPeerChannel {
return {
_: 'inputPeerChannel',
channel_id: id,
access_hash: this.getChat(id).access_hash/* || this.channelAccess[id] */ || 0
};
}
public hasChat(id: number, allowMin?: true) {
const chat = this.chats[id]
4 years ago
return isObject(chat) && (allowMin || !chat.pFlags.min);
}
public getChatPhoto(id: number) {
const chat = this.getChat(id);
4 years ago
if(this.cachedPhotoLocations[id] === undefined) {
this.cachedPhotoLocations[id] = chat && chat.photo ? chat.photo : {empty: true};
}
return this.cachedPhotoLocations[id];
}
/* public getChatString(id: number) {
const chat = this.getChat(id);
4 years ago
if(this.isChannel(id)) {
return (this.isMegagroup(id) ? 's' : 'c') + id + '_' + chat.access_hash;
}
return 'g' + id;
} */
4 years ago
public getChatMembersString(id: number) {
const chat = this.getChat(id);
const isChannel = this.isBroadcast(id);
const participants_count = chat.participants_count || chat.participants?.participants.length || 0;
return numberWithCommas(participants_count) + ' ' + (isChannel ? 'followers' : 'members');
}
4 years ago
public wrapForFull(id: number, fullChat: any) {
const chatFull = copy(fullChat);
const chat = this.getChat(id);
4 years ago
if(!chatFull.participants_count) {
4 years ago
chatFull.participants_count = chat.participants_count;
}
if(chatFull.participants &&
chatFull.participants._ == 'chatParticipants') {
chatFull.participants.participants = this.wrapParticipants(id, chatFull.participants.participants);
}
if(chatFull.about) {
4 years ago
chatFull.rAbout = RichTextProcessor.wrapRichText(chatFull.about, {noLinebreaks: true});
}
//chatFull.peerString = this.getChatString(id);
4 years ago
chatFull.chat = chat;
return chatFull;
}
public wrapParticipants(id: number, participants: any[]) {
const chat = this.getChat(id);
const myId = appUsersManager.getSelf().id;
4 years ago
if(this.isChannel(id)) {
const isAdmin = chat.pFlags.creator;
4 years ago
participants.forEach((participant) => {
participant.canLeave = myId == participant.user_id;
4 years ago
participant.canKick = isAdmin && participant._ == 'channelParticipant';
// just for order by last seen
participant.user = appUsersManager.getUser(participant.user_id);
});
} else {
const isAdmin = chat.pFlags.creator || chat.pFlags.admins_enabled && chat.pFlags.admin;
4 years ago
participants.forEach((participant) => {
participant.canLeave = myId == participant.user_id;
4 years ago
participant.canKick = !participant.canLeave && (
chat.pFlags.creator ||
participant._ == 'chatParticipant' && (isAdmin || myId == participant.inviter_id)
4 years ago
);
// just for order by last seen
participant.user = appUsersManager.getUser(participant.user_id);
});
}
4 years ago
return participants;
}
public createChannel(title: string, about: string): Promise<number> {
return apiManager.invokeApi('channels.createChannel', {
broadcast: true,
title,
about
}).then((updates: any) => {
apiUpdatesManager.processUpdateMessage(updates);
return updates.chats[0].id;
});
}
public inviteToChannel(id: number, userIds: number[]) {
const input = this.getChannelInput(id);
const usersInputs = userIds.map(u => appUsersManager.getUserInput(u));
return apiManager.invokeApi('channels.inviteToChannel', {
channel: input,
users: usersInputs
}).then(updates => {
apiUpdatesManager.processUpdateMessage(updates);
});
}
public createChat(title: string, userIds: number[]): Promise<number> {
return apiManager.invokeApi('messages.createChat', {
users: userIds.map(u => appUsersManager.getUserInput(u)),
title
}).then(updates => {
apiUpdatesManager.processUpdateMessage(updates);
return (updates as any as Updates.updates).chats[0].id;
});
}
public editPhoto(id: number, inputFile: InputFile) {
const isChannel = this.isChannel(id);
const inputChatPhoto: InputChatPhoto.inputChatUploadedPhoto = {
_: 'inputChatUploadedPhoto',
file: inputFile
};
if(isChannel) {
return apiManager.invokeApi('channels.editPhoto', {
channel: this.getChannelInput(id),
photo: inputChatPhoto
}).then(updates => {
apiUpdatesManager.processUpdateMessage(updates);
});
} else {
return apiManager.invokeApi('messages.editChatPhoto', {
chat_id: id,
photo: inputChatPhoto
}).then(updates => {
apiUpdatesManager.processUpdateMessage(updates);
});
}
}
public async getOnlines(id: number): Promise<number> {
if(this.isMegagroup(id)) {
const timestamp = Date.now() / 1000 | 0;
const cached = this.megagroupOnlines[id] ?? (this.megagroupOnlines[id] = {timestamp: 0, onlines: 1});
if((timestamp - cached.timestamp) < 60) {
return cached.onlines;
}
const res = await apiManager.invokeApi('messages.getOnlines', {
peer: this.getChannelInputPeer(id)
});
const onlines = res.onlines ?? 1;
cached.timestamp = timestamp;
cached.onlines = onlines;
return onlines;
} else if(this.isBroadcast(id)) {
return 1;
}
const chatInfo = await appProfileManager.getChatFull(id);
const _participants = (chatInfo as ChatFull.chatFull).participants as ChatParticipants.chatParticipants;
if(_participants && _participants.participants) {
const participants = _participants.participants;
return participants.reduce((acc: number, participant) => {
const user = appUsersManager.getUser(participant.user_id);
if(user && user.status && user.status._ == 'userStatusOnline') {
return acc + 1;
}
return acc;
}, 0);
} else {
return 1;
}
}
private onChatUpdated = (chatId: number, updates: any) => {
//console.log('onChatUpdated', chatId, updates);
apiUpdatesManager.processUpdateMessage(updates);
if(updates &&
/* updates.updates &&
updates.updates.length && */
this.isChannel(chatId)) {
appProfileManager.invalidateChannelParticipants(chatId);
}
};
public leaveChannel(id: number) {
return apiManager.invokeApi('channels.leaveChannel', {
channel: this.getChannelInput(id)
}).then(this.onChatUpdated.bind(this, id));
}
public joinChannel(id: number) {
return apiManager.invokeApi('channels.joinChannel', {
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);
}
4 years ago
}
const appChatsManager = new AppChatsManager();
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.appChatsManager = appChatsManager);
export default appChatsManager;