2021-04-08 15:52:31 +02:00
|
|
|
|
/*
|
|
|
|
|
* https://github.com/morethanwords/tweb
|
|
|
|
|
* Copyright (C) 2019-2021 Eduard Kuzmenko
|
|
|
|
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-06 20:27:29 +01:00
|
|
|
|
import type {DialogFilter, ForumTopic, Update} from '../../layer';
|
2022-08-04 08:49:54 +02:00
|
|
|
|
import type {Dialog} from '../appManagers/appMessagesManager';
|
|
|
|
|
import forEachReverse from '../../helpers/array/forEachReverse';
|
|
|
|
|
import copy from '../../helpers/object/copy';
|
|
|
|
|
import {AppManager} from '../appManagers/manager';
|
|
|
|
|
import findAndSplice from '../../helpers/array/findAndSplice';
|
|
|
|
|
import assumeType from '../../helpers/assumeType';
|
|
|
|
|
import {FOLDER_ID_ALL, FOLDER_ID_ARCHIVE, REAL_FOLDERS, REAL_FOLDER_ID, START_LOCAL_ID} from '../mtproto/mtproto_config';
|
|
|
|
|
import makeError from '../../helpers/makeError';
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
export type MyDialogFilter = DialogFilter.dialogFilter;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
|
const convertment = [
|
2022-08-04 08:49:54 +02:00
|
|
|
|
['pinned_peers', 'pinnedPeerIds'],
|
|
|
|
|
['exclude_peers', 'excludePeerIds'],
|
2021-10-21 15:16:43 +02:00
|
|
|
|
['include_peers', 'includePeerIds']
|
|
|
|
|
] as ['pinned_peers' | 'exclude_peers' | 'include_peers', 'pinnedPeerIds' | 'excludePeerIds' | 'includePeerIds'][];
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
const PREPENDED_FILTERS = REAL_FOLDERS.size;
|
|
|
|
|
|
|
|
|
|
const LOCAL_FILTER: MyDialogFilter = {
|
|
|
|
|
_: 'dialogFilter',
|
|
|
|
|
pFlags: {},
|
|
|
|
|
flags: 0,
|
|
|
|
|
id: 0,
|
|
|
|
|
title: '',
|
|
|
|
|
exclude_peers: [],
|
|
|
|
|
include_peers: [],
|
|
|
|
|
pinned_peers: [],
|
|
|
|
|
excludePeerIds: [],
|
|
|
|
|
includePeerIds: [],
|
2022-08-04 08:49:54 +02:00
|
|
|
|
pinnedPeerIds: []
|
2022-07-22 21:04:46 +02:00
|
|
|
|
};
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
|
|
|
|
export default class FiltersStorage extends AppManager {
|
|
|
|
|
private filters: {[filterId: string]: MyDialogFilter};
|
2022-07-22 21:04:46 +02:00
|
|
|
|
private filtersArr: Array<MyDialogFilter>;
|
|
|
|
|
private localFilters: {[filterId: string]: MyDialogFilter};
|
|
|
|
|
private localId: number;
|
2021-11-12 16:53:59 +01:00
|
|
|
|
private reloadedPeerIds: Set<PeerId>;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
protected after() {
|
2021-12-20 04:58:04 +01:00
|
|
|
|
this.clear(true);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
this.apiUpdatesManager.addMultipleEventsListeners({
|
2021-04-24 19:06:24 +02:00
|
|
|
|
updateDialogFilter: this.onUpdateDialogFilter,
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-26 23:50:05 +02:00
|
|
|
|
updateDialogFilters: this.onUpdateDialogFilters,
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2021-04-24 19:06:24 +02:00
|
|
|
|
updateDialogFilterOrder: this.onUpdateDialogFilterOrder
|
|
|
|
|
});
|
2021-11-12 18:40:13 +01:00
|
|
|
|
|
|
|
|
|
// delete peers when dialog is being dropped
|
|
|
|
|
/* rootScope.addEventListener('peer_deleted', (peerId) => {
|
|
|
|
|
for(const filterId in this.filters) {
|
|
|
|
|
const filter = this.filters[filterId];
|
|
|
|
|
let modified = false;
|
2022-06-17 18:01:43 +02:00
|
|
|
|
[filter.pinned_peers, filter.include_peers, filter.exclude_peers].forEach((arr) => {
|
2021-11-12 18:40:13 +01:00
|
|
|
|
forEachReverse(arr, (inputPeer, idx) => {
|
2022-06-17 18:01:43 +02:00
|
|
|
|
if(getPeerId(inputPeer) === peerId) {
|
2021-11-12 18:40:13 +01:00
|
|
|
|
arr.splice(idx, 1);
|
|
|
|
|
modified = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(modified) {
|
|
|
|
|
this.saveDialogFilter(filter, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}); */
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
2022-07-26 23:50:05 +02:00
|
|
|
|
this.rootScope.addEventListener('premium_toggle', () => {
|
|
|
|
|
this.onUpdateDialogFilters({_: 'updateDialogFilters'});
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
return this.appStateManager.getState().then((state) => {
|
2022-07-22 21:04:46 +02:00
|
|
|
|
const filtersArr = this.prependFilters(state.filtersArr);
|
|
|
|
|
filtersArr.map((filter) => {
|
|
|
|
|
this.saveDialogFilter(filter, false, true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
/**
|
|
|
|
|
* ! use it only with saving
|
|
|
|
|
*/
|
|
|
|
|
private prependFilters(filters: DialogFilter[]) {
|
|
|
|
|
filters = filters.slice();
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
const allChatsFilter = this.localFilters[FOLDER_ID_ALL];
|
|
|
|
|
const archiveFilter = this.localFilters[FOLDER_ID_ARCHIVE];
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
const allChatsFilterIndex = filters.findIndex((filter) => filter._ === 'dialogFilterDefault' || filter.id === FOLDER_ID_ALL);
|
|
|
|
|
if(allChatsFilterIndex !== -1) filters[allChatsFilterIndex] = allChatsFilter;
|
|
|
|
|
else filters.unshift(allChatsFilter);
|
|
|
|
|
|
|
|
|
|
findAndSplice(filters, (filter) => (filter as MyDialogFilter).id === FOLDER_ID_ARCHIVE);
|
|
|
|
|
filters.splice(/* 1 */filters[0] === allChatsFilter ? 1 : 0, 0, archiveFilter);
|
|
|
|
|
|
2022-07-26 17:24:29 +02:00
|
|
|
|
this.localId = START_LOCAL_ID;
|
|
|
|
|
filters.forEach((filter) => {
|
|
|
|
|
delete filter.localId;
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
return filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private generateLocalFilter(id: REAL_FOLDER_ID) {
|
|
|
|
|
const filter: MyDialogFilter = {...copy(LOCAL_FILTER), id};
|
|
|
|
|
if(id === FOLDER_ID_ALL) {
|
|
|
|
|
filter.pFlags.exclude_archived = true;
|
|
|
|
|
} else if(id === FOLDER_ID_ARCHIVE) {
|
|
|
|
|
filter.pFlags.exclude_unarchived = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(REAL_FOLDERS.has(id)) {
|
|
|
|
|
filter.pinnedPeerIds = this.dialogsStorage.getPinnedOrders(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filter;
|
2021-04-24 19:06:24 +02:00
|
|
|
|
}
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
// private getLocalFilter(id: number) {
|
2022-07-22 21:04:46 +02:00
|
|
|
|
// return this.filters[id] ??= this.generateLocalFilter(id);
|
2022-06-17 18:01:43 +02:00
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public clear = (init?: boolean) => {
|
2021-06-26 04:26:29 +02:00
|
|
|
|
if(!init) {
|
2022-06-17 18:01:43 +02:00
|
|
|
|
// safeReplaceObject(this.filters, {});
|
2021-12-20 04:58:04 +01:00
|
|
|
|
this.reloadedPeerIds.clear();
|
2022-06-17 18:01:43 +02:00
|
|
|
|
this.clearFilters();
|
2021-12-20 04:58:04 +01:00
|
|
|
|
} else {
|
|
|
|
|
this.filters = {};
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.filtersArr = [];
|
2021-12-20 04:58:04 +01:00
|
|
|
|
this.reloadedPeerIds = new Set();
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.localFilters = {};
|
|
|
|
|
for(const filterId of REAL_FOLDERS) {
|
|
|
|
|
this.localFilters[filterId] = this.generateLocalFilter(filterId as REAL_FOLDER_ID);
|
|
|
|
|
}
|
2021-06-26 04:26:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.localId = START_LOCAL_ID;
|
2022-06-17 18:01:43 +02:00
|
|
|
|
};
|
2021-06-25 17:18:18 +02:00
|
|
|
|
|
2021-04-24 19:06:24 +02:00
|
|
|
|
private onUpdateDialogFilter = (update: Update.updateDialogFilter) => {
|
|
|
|
|
if(update.filter) {
|
|
|
|
|
this.saveDialogFilter(update.filter as any);
|
|
|
|
|
} else if(this.filters[update.id]) { // Папка удалена
|
2022-08-04 08:49:54 +02:00
|
|
|
|
// this.getDialogFilters(true);
|
2021-06-11 14:52:53 +02:00
|
|
|
|
this.rootScope.dispatchEvent('filter_delete', this.filters[update.id]);
|
2021-04-24 19:06:24 +02:00
|
|
|
|
delete this.filters[update.id];
|
2022-07-22 21:04:46 +02:00
|
|
|
|
findAndSplice(this.filtersArr, (filter) => (filter as DialogFilter.dialogFilter).id === update.id);
|
2021-04-24 19:06:24 +02:00
|
|
|
|
}
|
2021-04-28 13:08:14 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.pushToState();
|
2021-04-24 19:06:24 +02:00
|
|
|
|
};
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-26 23:50:05 +02:00
|
|
|
|
private onUpdateDialogFilters = (update: Update.updateDialogFilters) => {
|
2022-08-04 08:49:54 +02:00
|
|
|
|
// console.warn('updateDialogFilters', update);
|
2022-07-26 23:50:05 +02:00
|
|
|
|
|
|
|
|
|
const oldFilters = copy(this.filters);
|
|
|
|
|
|
|
|
|
|
this.getDialogFilters(true).then((filters) => {
|
|
|
|
|
for(const _filterId in oldFilters) {
|
|
|
|
|
const filterId = +_filterId;
|
|
|
|
|
if(!filters.find((filter) => filter.id === filterId)) { // * deleted
|
|
|
|
|
this.onUpdateDialogFilter({_: 'updateDialogFilter', id: filterId});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.onUpdateDialogFilterOrder({_: 'updateDialogFilterOrder', order: filters.map((filter) => filter.id)});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-24 19:06:24 +02:00
|
|
|
|
private onUpdateDialogFilterOrder = (update: Update.updateDialogFilterOrder) => {
|
2022-08-04 08:49:54 +02:00
|
|
|
|
// console.log('updateDialogFilterOrder', update);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-26 17:24:29 +02:00
|
|
|
|
const order = update.order.slice();
|
|
|
|
|
if(!order.includes(FOLDER_ID_ARCHIVE)) {
|
|
|
|
|
order.splice(order[0] === FOLDER_ID_ALL ? 1 : 0, 0, FOLDER_ID_ARCHIVE);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.localId = START_LOCAL_ID;
|
2022-07-26 17:24:29 +02:00
|
|
|
|
order.forEach((filterId) => {
|
2021-04-24 19:06:24 +02:00
|
|
|
|
const filter = this.filters[filterId];
|
2022-07-22 21:04:46 +02:00
|
|
|
|
delete filter.localId;
|
|
|
|
|
this.setLocalId(filter);
|
2021-04-24 19:06:24 +02:00
|
|
|
|
});
|
|
|
|
|
|
2022-07-26 17:24:29 +02:00
|
|
|
|
this.rootScope.dispatchEvent('filter_order', order);
|
2021-04-28 13:08:14 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.pushToState();
|
2021-04-24 19:06:24 +02:00
|
|
|
|
};
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
private pushToState() {
|
|
|
|
|
this.appStateManager.pushToState('filtersArr', this.filtersArr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 20:27:29 +01:00
|
|
|
|
public testDialogForFilter(dialog: Dialog | ForumTopic.forumTopic, filter?: MyDialogFilter) {
|
|
|
|
|
if(!filter) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(REAL_FOLDERS.has(filter.id)) {
|
2023-01-06 20:27:29 +01:00
|
|
|
|
return (dialog as Dialog).folder_id === filter.id && this.dialogsStorage.canSaveDialogByPeerId(dialog.peerId);
|
2022-06-17 18:01:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 03:02:00 +02:00
|
|
|
|
const peerId = dialog.peerId;
|
|
|
|
|
|
2021-11-10 01:45:01 +01:00
|
|
|
|
// * check whether dialog exists
|
|
|
|
|
if(!this.appMessagesManager.getDialogOnly(peerId)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
// exclude_peers
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(filter.excludePeerIds.includes(peerId)) {
|
2021-09-02 03:02:00 +02:00
|
|
|
|
return false;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// include_peers
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(filter.includePeerIds.includes(peerId)) {
|
2021-09-02 03:02:00 +02:00
|
|
|
|
return true;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pFlags = filter.pFlags;
|
|
|
|
|
|
|
|
|
|
// exclude_archived
|
2023-01-06 20:27:29 +01:00
|
|
|
|
if(pFlags.exclude_archived && (dialog as Dialog).folder_id === FOLDER_ID_ARCHIVE) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// exclude_read
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(pFlags.exclude_read && !this.appMessagesManager.isDialogUnread(dialog)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// exclude_muted
|
2023-01-06 20:27:29 +01:00
|
|
|
|
if(pFlags.exclude_muted && this.appNotificationsManager.isPeerLocalMuted({peerId}) && !(dialog.unread_mentions_count && dialog.unread_count)) {
|
2021-12-20 04:58:04 +01:00
|
|
|
|
return false;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(this.appPeersManager.isAnyChat(peerId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
// broadcasts
|
2020-12-11 03:06:16 +01:00
|
|
|
|
if(pFlags.broadcasts && this.appPeersManager.isBroadcast(peerId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// groups
|
2020-12-11 03:06:16 +01:00
|
|
|
|
if(pFlags.groups && this.appPeersManager.isAnyGroup(peerId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-21 15:16:43 +02:00
|
|
|
|
const userId = peerId.toUserId();
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
// bots
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(this.appUsersManager.isBot(userId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return !!pFlags.bots;
|
|
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
// non_contacts
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(pFlags.non_contacts && !this.appUsersManager.isContact(userId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// contacts
|
2021-10-21 15:16:43 +02:00
|
|
|
|
if(pFlags.contacts && this.appUsersManager.isContact(userId)) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 03:02:00 +02:00
|
|
|
|
public testDialogForFilterId(dialog: Dialog, filterId: number) {
|
|
|
|
|
return this.testDialogForFilter(dialog, this.filters[filterId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getFilter(filterId: number) {
|
|
|
|
|
return this.filters[filterId];
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
public getFilters() {
|
|
|
|
|
return this.filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public clearFilters() {
|
|
|
|
|
const filters = this.getFilters();
|
|
|
|
|
for(const filterId in filters) { // delete filters
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(REAL_FOLDERS.has(+filterId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
this.onUpdateDialogFilter({
|
|
|
|
|
_: 'updateDialogFilter',
|
2022-08-04 08:49:54 +02:00
|
|
|
|
id: +filterId
|
2022-06-17 18:01:43 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async toggleDialogPin(peerId: PeerId, filterId: number) {
|
2020-12-11 03:06:16 +01:00
|
|
|
|
const filter = this.filters[filterId];
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
|
const index = filter.pinnedPeerIds.indexOf(peerId);
|
|
|
|
|
const wasPinned = index !== -1;
|
|
|
|
|
|
|
|
|
|
if(wasPinned) {
|
|
|
|
|
filter.pinned_peers.splice(index, 1);
|
|
|
|
|
filter.pinnedPeerIds.splice(index, 1);
|
|
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
if(!wasPinned) {
|
2022-06-17 18:01:43 +02:00
|
|
|
|
if(filter.pinned_peers.length >= (await this.apiManager.getConfig()).pinned_infolder_count_max) {
|
2022-08-04 04:42:17 +02:00
|
|
|
|
return Promise.reject(makeError('PINNED_DIALOGS_TOO_MUCH'));
|
2021-07-17 18:38:51 +02:00
|
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
|
filter.pinned_peers.unshift(this.appPeersManager.getInputPeerById(peerId));
|
|
|
|
|
filter.pinnedPeerIds.unshift(peerId);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
return this.updateDialogFilter(filter);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 02:07:39 +01:00
|
|
|
|
public createDialogFilter(filter: MyDialogFilter, prepend?: boolean) {
|
2022-06-17 18:01:43 +02:00
|
|
|
|
const maxId = Math.max(1, ...Object.keys(this.filters).map((i) => +i));
|
2020-11-10 01:47:00 +01:00
|
|
|
|
filter = copy(filter);
|
2020-12-11 03:06:16 +01:00
|
|
|
|
filter.id = maxId + 1;
|
2021-11-10 02:07:39 +01:00
|
|
|
|
return this.updateDialogFilter(filter, undefined, prepend);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 02:07:39 +01:00
|
|
|
|
public updateDialogFilter(filter: MyDialogFilter, remove = false, prepend = false) {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
const flags = remove ? 0 : 1;
|
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
return this.apiManager.invokeApi('messages.updateDialogFilter', {
|
2020-11-10 01:47:00 +01:00
|
|
|
|
flags,
|
|
|
|
|
id: filter.id,
|
|
|
|
|
filter: remove ? undefined : this.getOutputDialogFilter(filter)
|
2023-01-25 15:21:38 +01:00
|
|
|
|
}).then((bool) => { // возможно нужна проверка и откат, если результат не ТРУ
|
2022-08-04 08:49:54 +02:00
|
|
|
|
// console.log('updateDialogFilter bool:', bool);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
/* if(!this.filters[filter.id]) {
|
|
|
|
|
this.saveDialogFilter(filter);
|
|
|
|
|
}
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
rootScope.$broadcast('filter_update', filter); */
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
this.onUpdateDialogFilter({
|
|
|
|
|
_: 'updateDialogFilter',
|
|
|
|
|
id: filter.id,
|
|
|
|
|
filter: remove ? undefined : filter as any
|
|
|
|
|
});
|
2021-11-10 02:07:39 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
if(prepend) {
|
|
|
|
|
const f: MyDialogFilter[] = [];
|
|
|
|
|
for(const filterId in this.filters) {
|
|
|
|
|
const filter = this.filters[filterId];
|
|
|
|
|
++filter.localId;
|
|
|
|
|
f.push(filter);
|
|
|
|
|
}
|
2021-11-10 02:07:39 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
filter.localId = START_LOCAL_ID;
|
2021-11-10 02:07:39 +01:00
|
|
|
|
|
2023-01-25 15:21:38 +01:00
|
|
|
|
const order = f.sort((a, b) => a.localId - b.localId).map((filter) => filter.id);
|
|
|
|
|
this.onUpdateDialogFilterOrder({
|
|
|
|
|
_: 'updateDialogFilterOrder',
|
|
|
|
|
order
|
|
|
|
|
});
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 15:50:48 +01:00
|
|
|
|
public updateDialogFiltersOrder(order: number[]) {
|
|
|
|
|
return this.apiManager.invokeApi('messages.updateDialogFiltersOrder', {
|
|
|
|
|
order
|
|
|
|
|
}).then(() => {
|
|
|
|
|
this.onUpdateDialogFilterOrder({
|
|
|
|
|
_: 'updateDialogFilterOrder',
|
|
|
|
|
order
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
public getOutputDialogFilter(filter: MyDialogFilter) {
|
2021-10-21 15:16:43 +02:00
|
|
|
|
const c = copy(filter);
|
|
|
|
|
/* convertment.forEach(([from, to]) => {
|
|
|
|
|
c[from] = c[to].map((peerId) => this.appPeersManager.getInputPeerById(peerId));
|
|
|
|
|
}); */
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
|
this.filterIncludedPinnedPeers(filter);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private filterIncludedPinnedPeers(filter: MyDialogFilter) {
|
|
|
|
|
forEachReverse(filter.includePeerIds, (peerId, idx) => {
|
|
|
|
|
if(filter.pinnedPeerIds.includes(peerId)) {
|
|
|
|
|
filter.include_peers.splice(idx, 1);
|
|
|
|
|
filter.includePeerIds.splice(idx, 1);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// private spliceMissingPeerIds(filterId: number, type: ArgumentTypes<FiltersStorage['reloadMissingPeerIds']>[1], missingPeerIds: PeerId[]) {
|
|
|
|
|
// const filter = this.getFilter(filterId);
|
|
|
|
|
// const peers = filter && filter[type];
|
|
|
|
|
// if(!peers?.length) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// let spliced = false;
|
|
|
|
|
// missingPeerIds.forEach((peerId) => {
|
2022-06-17 18:01:43 +02:00
|
|
|
|
// const inputPeer = findAndSplice(peers, (inputPeer) => getPeerId(inputPeer) === peerId);
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// if(inputPeer) {
|
|
|
|
|
// spliced = true;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if(spliced) {
|
|
|
|
|
// this.onUpdateDialogFilter({
|
|
|
|
|
// _: 'updateDialogFilter',
|
|
|
|
|
// id: filterId,
|
|
|
|
|
// filter
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2021-11-12 16:53:59 +01:00
|
|
|
|
|
2023-01-06 20:27:29 +01:00
|
|
|
|
public reloadMissingPeerIds(
|
|
|
|
|
filterId: number,
|
|
|
|
|
type: 'pinned_peers' | 'include_peers' | 'exclude_peers' = 'pinned_peers'
|
|
|
|
|
) {
|
2022-04-17 00:20:30 +02:00
|
|
|
|
const filter = this.getFilter(filterId);
|
2023-01-06 20:27:29 +01:00
|
|
|
|
const peers = filter?.[type];
|
2022-04-17 00:20:30 +02:00
|
|
|
|
if(!peers?.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// const missingPeerIds: PeerId[] = [];
|
2022-04-17 00:20:30 +02:00
|
|
|
|
const reloadDialogs = peers.filter((inputPeer) => {
|
2022-07-26 17:24:29 +02:00
|
|
|
|
const peerId = this.appPeersManager.getPeerId(inputPeer);
|
2022-04-17 00:20:30 +02:00
|
|
|
|
const isAlreadyReloaded = this.reloadedPeerIds.has(peerId);
|
|
|
|
|
const dialog = this.appMessagesManager.getDialogOnly(peerId);
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// if(isAlreadyReloaded && !dialog) {
|
|
|
|
|
// missingPeerIds.push(peerId);
|
|
|
|
|
// }
|
2022-04-17 00:20:30 +02:00
|
|
|
|
|
|
|
|
|
const reload = !isAlreadyReloaded && !dialog;
|
|
|
|
|
return reload;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(!reloadDialogs.length) {
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// if(missingPeerIds.length) {
|
|
|
|
|
// this.spliceMissingPeerIds(filterId, type, missingPeerIds);
|
|
|
|
|
// }
|
2022-04-17 00:20:30 +02:00
|
|
|
|
|
|
|
|
|
return;
|
2021-11-12 16:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 00:20:30 +02:00
|
|
|
|
const reloadPromises = reloadDialogs.map((inputPeer) => {
|
2022-07-26 17:24:29 +02:00
|
|
|
|
const peerId = this.appPeersManager.getPeerId(inputPeer);
|
2022-04-17 00:20:30 +02:00
|
|
|
|
const promise = this.appMessagesManager.reloadConversation(inputPeer)
|
|
|
|
|
.then((dialog) => {
|
|
|
|
|
this.reloadedPeerIds.add(peerId);
|
|
|
|
|
|
|
|
|
|
return dialog ? undefined : peerId;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const reloadPromise = Promise.all(reloadPromises).then((missingPeerIds) => {
|
|
|
|
|
missingPeerIds = missingPeerIds.filter(Boolean);
|
|
|
|
|
if(!missingPeerIds.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 17:01:43 +02:00
|
|
|
|
// this.spliceMissingPeerIds(filterId, type, missingPeerIds);
|
2022-04-17 00:20:30 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reloadPromise;
|
2021-11-12 16:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 01:47:00 +01:00
|
|
|
|
public async getDialogFilters(overwrite = false): Promise<MyDialogFilter[]> {
|
|
|
|
|
const keys = Object.keys(this.filters);
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(keys.length > PREPENDED_FILTERS && !overwrite) {
|
|
|
|
|
return keys.map((filterId) => this.filters[filterId]).sort((a, b) => a.localId - b.localId);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
const filters = await this.apiManager.invokeApiSingle('messages.getDialogFilters');
|
2022-07-26 17:24:29 +02:00
|
|
|
|
const prepended = this.prependFilters(filters);
|
|
|
|
|
return prepended.map((filter) => this.saveDialogFilter(filter, overwrite)).filter(Boolean);
|
2022-06-17 18:01:43 +02:00
|
|
|
|
}
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
|
public getSuggestedDialogsFilters() {
|
|
|
|
|
return this.apiManager.invokeApi('messages.getSuggestedDialogFilters');
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
public saveDialogFilter(filter: DialogFilter, update = true, silent?: boolean) {
|
2021-10-21 15:16:43 +02:00
|
|
|
|
// defineNotNumerableProperties(filter, ['includePeerIds', 'excludePeerIds', 'pinnedPeerIds']);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(filter._ === 'dialogFilterDefault') {
|
|
|
|
|
filter = this.localFilters[FOLDER_ID_ALL];
|
|
|
|
|
}
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
|
|
|
|
assumeType<MyDialogFilter>(filter);
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(!REAL_FOLDERS.has(filter.id)) {
|
|
|
|
|
convertment.forEach(([from, to]) => {
|
|
|
|
|
assumeType<MyDialogFilter>(filter);
|
2022-07-26 17:24:29 +02:00
|
|
|
|
filter[to] = filter[from].map((peer) => this.appPeersManager.getPeerId(peer));
|
2022-07-22 21:04:46 +02:00
|
|
|
|
});
|
2021-10-21 15:16:43 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.filterIncludedPinnedPeers(filter);
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
filter.include_peers = filter.pinned_peers.concat(filter.include_peers);
|
|
|
|
|
filter.includePeerIds = filter.pinnedPeerIds.concat(filter.includePeerIds);
|
|
|
|
|
}
|
2020-11-10 01:47:00 +01:00
|
|
|
|
|
2021-10-21 18:40:45 +02:00
|
|
|
|
const oldFilter = this.filters[filter.id];
|
|
|
|
|
if(oldFilter) {
|
|
|
|
|
Object.assign(oldFilter, filter);
|
2020-11-10 01:47:00 +01:00
|
|
|
|
} else {
|
|
|
|
|
this.filters[filter.id] = filter;
|
|
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
this.setLocalId(filter);
|
2022-08-04 08:49:54 +02:00
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
if(!silent) {
|
|
|
|
|
if(update) {
|
|
|
|
|
this.rootScope.dispatchEvent('filter_update', filter);
|
|
|
|
|
} else if(!oldFilter) {
|
|
|
|
|
this.rootScope.dispatchEvent('filter_new', filter);
|
|
|
|
|
}
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
2022-06-17 18:01:43 +02:00
|
|
|
|
|
|
|
|
|
return filter;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:04:46 +02:00
|
|
|
|
private setLocalId(filter: MyDialogFilter) {
|
|
|
|
|
if(filter.localId !== undefined) {
|
|
|
|
|
if(filter.localId >= this.localId) {
|
|
|
|
|
this.localId = filter.localId + 1;
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-07-22 21:04:46 +02:00
|
|
|
|
filter.localId = this.localId++ as MyDialogFilter['localId'];
|
|
|
|
|
findAndSplice(this.filtersArr, (_filter) => _filter.id === filter.id);
|
|
|
|
|
this.filtersArr.push(filter);
|
|
|
|
|
this.pushToState();
|
2020-11-10 01:47:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-26 23:50:05 +02:00
|
|
|
|
|
|
|
|
|
public async isFilterIdAvailable(filterId: number) {
|
|
|
|
|
if(REAL_FOLDERS.has(filterId)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 19:49:33 +02:00
|
|
|
|
const limit = await this.apiManager.getLimit('folders');
|
|
|
|
|
const isFolderAvailable = this.filtersArr.filter((filter) => !REAL_FOLDERS.has(filter.id)).slice(0, limit).some((filter) => filter.id === filterId);
|
2022-07-26 23:50:05 +02:00
|
|
|
|
|
|
|
|
|
return isFolderAvailable;
|
|
|
|
|
}
|
2021-03-08 23:15:44 +01:00
|
|
|
|
}
|