From 967e849aae2a461f4a963df280cc864a46db09cc Mon Sep 17 00:00:00 2001 From: morethanwords Date: Sat, 10 Apr 2021 22:29:58 +0400 Subject: [PATCH] Added translation of saved Translated saved in search Update search index on peer's update --- src/components/appSelectPeers.ts | 7 +-- src/components/peerTitle.ts | 4 +- src/components/sidebarLeft/index.ts | 15 ++++-- .../sidebarRight/tabs/sharedMedia.ts | 38 +++++++------- src/helpers/object.ts | 6 +++ src/lang.ts | 1 + src/lib/appManagers/appChatsManager.ts | 16 ++---- src/lib/appManagers/appMessagesManager.ts | 14 ++++-- src/lib/appManagers/appUsersManager.ts | 50 ++++++++----------- src/lib/langPack.ts | 3 ++ src/lib/rootScope.ts | 2 + src/lib/searchIndexManager.ts | 12 +++-- src/lib/storages/dialogs.ts | 4 ++ 13 files changed, 99 insertions(+), 73 deletions(-) diff --git a/src/components/appSelectPeers.ts b/src/components/appSelectPeers.ts index 9d17314e..9ae992c8 100644 --- a/src/components/appSelectPeers.ts +++ b/src/components/appSelectPeers.ts @@ -11,7 +11,7 @@ import appPeersManager from "../lib/appManagers/appPeersManager"; import appPhotosManager from "../lib/appManagers/appPhotosManager"; import appUsersManager from "../lib/appManagers/appUsersManager"; import rootScope from "../lib/rootScope"; -import { cancelEvent } from "../helpers/dom"; +import { cancelEvent, replaceContent } from "../helpers/dom"; import Scrollable from "./scrollable"; import { FocusDirection } from "../helpers/fastSmoothScroll"; import CheckboxField from "./checkboxField"; @@ -20,6 +20,7 @@ import { safeAssign } from "../helpers/object"; import { i18n, LangPackKey, _i18n } from "../lib/langPack"; import findUpAttribute from "../helpers/dom/findUpAttribute"; import findUpClassName from "../helpers/dom/findUpClassName"; +import PeerTitle from "./peerTitle"; type PeerType = 'contacts' | 'dialogs' | 'channelParticipants'; @@ -475,7 +476,7 @@ export default class AppSelectPeers { div.dataset.key = '' + peerId; if(typeof(peerId) === 'number') { if(title === undefined) { - title = peerId === rootScope.myId ? 'Saved' : appPeersManager.getPeerTitle(peerId, false, true); + title = new PeerTitle({peerId, onlyFirstName: true}).element; } avatarEl.setAttribute('peer', '' + peerId); @@ -485,7 +486,7 @@ export default class AppSelectPeers { if(typeof(title) === 'string') { div.innerHTML = title; } else { - div.innerHTML = ''; + replaceContent(div, title); div.append(title); } } diff --git a/src/components/peerTitle.ts b/src/components/peerTitle.ts index c36ec33c..b6107508 100644 --- a/src/components/peerTitle.ts +++ b/src/components/peerTitle.ts @@ -7,6 +7,8 @@ import { MOUNT_CLASS_TO } from "../config/debug"; import appPeersManager from "../lib/appManagers/appPeersManager"; import rootScope from "../lib/rootScope"; +import { replaceContent } from "../helpers/dom"; +import { i18n } from "../lib/langPack"; export type PeerTitleOptions = { peerId: number, @@ -59,7 +61,7 @@ export default class PeerTitle { if(this.peerId !== rootScope.myId || !this.dialog) { this.element.innerHTML = appPeersManager.getPeerTitle(this.peerId, this.plainText, this.onlyFirstName); } else { - this.element.innerHTML = this.onlyFirstName ? 'Saved' : 'Saved Messages'; + replaceContent(this.element, i18n(this.onlyFirstName ? 'Saved' : 'SavedMessages')); } } } diff --git a/src/components/sidebarLeft/index.ts b/src/components/sidebarLeft/index.ts index 18cb9bb1..4e4d0532 100644 --- a/src/components/sidebarLeft/index.ts +++ b/src/components/sidebarLeft/index.ts @@ -34,6 +34,8 @@ import { isMobileSafari } from "../../helpers/userAgent"; import appNavigationController from "../appNavigationController"; import findUpClassName from "../../helpers/dom/findUpClassName"; import findUpTag from "../../helpers/dom/findUpTag"; +import PeerTitle from "../peerTitle"; +import { replaceContent } from "../../helpers/dom"; export const LEFT_COLUMN_ACTIVE_CLASSNAME = 'is-left-column-shown'; @@ -290,7 +292,7 @@ export class AppSidebarLeft extends SidebarSlider { searchSuper.nav.parentElement.append(helper); - const renderEntity = (peerId: any, title?: string) => { + const renderEntity = (peerId: any, title?: string | HTMLElement) => { const div = document.createElement('div'); div.classList.add('selector-user'/* , 'scale-in' */); @@ -302,7 +304,7 @@ export class AppSidebarLeft extends SidebarSlider { div.dataset.key = '' + peerId; if(typeof(peerId) === 'number') { if(title === undefined) { - title = peerId === rootScope.myId ? 'Saved' : appPeersManager.getPeerTitle(peerId, false, true); + title = new PeerTitle({peerId, onlyFirstName: true}).element; } avatarEl.setAttribute('peer', '' + peerId); @@ -311,7 +313,12 @@ export class AppSidebarLeft extends SidebarSlider { } if(title) { - div.innerHTML = title; + if(typeof(title) === 'string') { + div.innerHTML = title; + } else { + replaceContent(div, title); + div.append(title); + } } div.insertAdjacentElement('afterbegin', avatarEl); @@ -446,7 +453,7 @@ export class AppSidebarLeft extends SidebarSlider { this.newBtnMenu.classList.add('is-hidden'); this.toolsBtn.parentElement.firstElementChild.classList.toggle('state-back', true); - if(!isMobileSafari) { + if(!isMobileSafari && !appNavigationController.findItemByType('global-search')) { appNavigationController.pushItem({ onPop: () => { close(); diff --git a/src/components/sidebarRight/tabs/sharedMedia.ts b/src/components/sidebarRight/tabs/sharedMedia.ts index c7055add..2abe9aa0 100644 --- a/src/components/sidebarRight/tabs/sharedMedia.ts +++ b/src/components/sidebarRight/tabs/sharedMedia.ts @@ -497,28 +497,32 @@ class PeerProfile { } public setAvatar() { - const photo = appPeersManager.getPeerPhoto(this.peerId); - - if(photo) { - const oldAvatars = this.avatars; - this.avatars = new PeerProfileAvatars(); - this.avatars.setPeer(this.peerId); - this.avatars.info.append(this.name, this.subtitle); + if(this.peerId !== rootScope.myId) { + const photo = appPeersManager.getPeerPhoto(this.peerId); - this.avatar.remove(); + if(photo) { + const oldAvatars = this.avatars; + this.avatars = new PeerProfileAvatars(); + this.avatars.setPeer(this.peerId); + this.avatars.info.append(this.name, this.subtitle); - if(oldAvatars) oldAvatars.container.replaceWith(this.avatars.container); - else this.element.prepend(this.avatars.container); - } else { - if(this.avatars) { - this.avatars.container.remove(); - this.avatars = undefined; - } + this.avatar.remove(); + + if(oldAvatars) oldAvatars.container.replaceWith(this.avatars.container); + else this.element.prepend(this.avatars.container); - this.avatar.setAttribute('peer', '' + this.peerId); + return; + } + } - this.section.content.prepend(this.avatar, this.name, this.subtitle); + if(this.avatars) { + this.avatars.container.remove(); + this.avatars = undefined; } + + this.avatar.setAttribute('peer', '' + this.peerId); + + this.section.content.prepend(this.avatar, this.name, this.subtitle); } public fillProfileElements() { diff --git a/src/helpers/object.ts b/src/helpers/object.ts index 5f29fd4b..8cccf63e 100644 --- a/src/helpers/object.ts +++ b/src/helpers/object.ts @@ -67,6 +67,10 @@ export function getObjectKeysAndSort(object: any, sort: 'asc' | 'desc' = 'asc') } export function safeReplaceObject(wasObject: any, newObject: any) { + if(!wasObject) { + return newObject; + } + for(var key in wasObject) { if(!newObject.hasOwnProperty(key) && key.charAt(0) !== '$') { delete wasObject[key]; @@ -78,6 +82,8 @@ export function safeReplaceObject(wasObject: any, newObject: any) { wasObject[key] = newObject[key]; //} } + + return wasObject; } /** diff --git a/src/lang.ts b/src/lang.ts index eb0e758d..49812b8c 100644 --- a/src/lang.ts +++ b/src/lang.ts @@ -90,6 +90,7 @@ const lang = { "MarkupTooltip.LinkPlaceholder": "Enter URL...", "MediaViewer.Context.Download": "Download", "Profile": "Profile", + "Saved": "Saved", // * android "ActionCreateChannel": "Channel created", diff --git a/src/lib/appManagers/appChatsManager.ts b/src/lib/appManagers/appChatsManager.ts index 16fa77f4..da2ed556 100644 --- a/src/lib/appManagers/appChatsManager.ts +++ b/src/lib/appManagers/appChatsManager.ts @@ -36,7 +36,6 @@ export class AppChatsManager { //public usernames: any = {}; //public channelAccess: any = {}; //public megagroups: {[id: number]: true} = {}; - public cachedPhotoLocations: {[id: number]: ChatPhoto} = {}; public megagroupOnlines: {[id: number]: {timestamp: number, onlines: number}} = {}; @@ -184,11 +183,6 @@ export class AppChatsManager { rootScope.broadcast('chat_update', chat.id); } - if(this.cachedPhotoLocations[chat.id] !== undefined) { - safeReplaceObject(this.cachedPhotoLocations[chat.id], chat && - chat.photo ? chat.photo : {empty: true}); - } - if(changedPhoto) { rootScope.broadcast('avatar_update', -chat.id); } @@ -388,13 +382,9 @@ export class AppChatsManager { public getChatPhoto(id: number) { const chat: Chat.chat = this.getChat(id); - if(this.cachedPhotoLocations[id] === undefined) { - this.cachedPhotoLocations[id] = chat && chat.photo || { - _: 'chatPhotoEmpty' - }; - } - - return this.cachedPhotoLocations[id]; + return chat && chat.photo || { + _: 'chatPhotoEmpty' + }; } public getChatString(id: number) { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index ee1402e1..2fb66956 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -184,7 +184,6 @@ export class AppMessagesManager { private reloadConversationsPromise: Promise; private reloadConversationsPeers: number[] = []; - private dialogsIndex = searchIndexManager.createIndex(); private cachedResults: { query: string, count: number, @@ -228,6 +227,15 @@ export class AppMessagesManager { }); }); + rootScope.on('language_change', (e) => { + const peerId = appUsersManager.getSelf().id; + const dialog = this.getDialogByPeerId(peerId)[0]; + if(dialog) { + const peerText = appPeersManager.getPeerSearchText(peerId); + searchIndexManager.indexObject(peerId, peerText, this.dialogsStorage.dialogsIndex); + } + }); + rootScope.on('webpage_updated', (e) => { const eventData = e; eventData.msgs.forEach((mid) => { @@ -1650,7 +1658,7 @@ export class AppMessagesManager { this.cachedResults.query = query; this.cachedResults.folderId = folderId; - const results = searchIndexManager.search(query, this.dialogsIndex); + const results = searchIndexManager.search(query, this.dialogsStorage.dialogsIndex); this.cachedResults.dialogs = []; @@ -3070,7 +3078,7 @@ export class AppMessagesManager { } const peerText = appPeersManager.getPeerSearchText(peerId); - searchIndexManager.indexObject(peerId, peerText, this.dialogsIndex); + searchIndexManager.indexObject(peerId, peerText, this.dialogsStorage.dialogsIndex); let mid: number, message; if(dialog.top_message) { diff --git a/src/lib/appManagers/appUsersManager.ts b/src/lib/appManagers/appUsersManager.ts index 23103255..7dc945ed 100644 --- a/src/lib/appManagers/appUsersManager.ts +++ b/src/lib/appManagers/appUsersManager.ts @@ -35,7 +35,6 @@ export class AppUsersManager { private users: {[userId: number]: User} = {}; private usernames: {[username: string]: number} = {}; //public userAccess: {[userId: number]: string} = {}; - private cachedPhotoLocations: {[userId: number]: UserProfilePhoto} = {}; private contactsIndex = searchIndexManager.createIndex(); private contactsFillPromise: Promise>; public contactsList: Set = new Set(); @@ -80,17 +79,12 @@ export class AppUsersManager { if(user) { this.forceUserOnline(userId); - if(!user.photo) { - user.photo = update.photo; + if(update.photo._ === 'userProfilePhotoEmpty') { + delete user.photo; } else { - safeReplaceObject(user.photo, update.photo); + user.photo = safeReplaceObject(user.photo, update.photo); } - - if(this.cachedPhotoLocations[userId] !== undefined) { - safeReplaceObject(this.cachedPhotoLocations[userId], update.photo ? - update.photo : {empty: true}); - } - + rootScope.broadcast('user_update', userId); rootScope.broadcast('avatar_update', userId); } else console.warn('No user by id:', userId); @@ -120,6 +114,11 @@ export class AppUsersManager { } }); + rootScope.on('language_change', (e) => { + const userId = this.getSelf().id; + searchIndexManager.indexObject(userId, this.getUserSearchText(userId), this.contactsIndex); + }); + appStateManager.getState().then((state) => { this.users = state.users; @@ -198,12 +197,16 @@ export class AppUsersManager { return ''; } - const serviceText = user.pFlags.self ? 'Saved Messages' : ''; - return (user.first_name || '') + - ' ' + (user.last_name || '') + - ' ' + (user.phone || '') + - ' ' + (user.username || '') + - ' ' + serviceText; + const arr: string[] = [ + user.first_name, + user.last_name, + user.phone, + user.username, + user.pFlags.self ? I18n.format('SavedMessages', true) : '', + user.pFlags.self ? 'Saved Messages' : '' + ]; + + return arr.filter(Boolean).join(' '); } public getContacts(query?: string, includeSaved = false) { @@ -351,11 +354,6 @@ export class AppUsersManager { rootScope.broadcast('user_update', userId); - if(this.cachedPhotoLocations[userId] !== undefined) { - safeReplaceObject(this.cachedPhotoLocations[userId], user && - user.photo ? user.photo : {empty: true}); - } - if(changedTitle) { rootScope.broadcast('peer_title_edit', user.id); } @@ -514,13 +512,9 @@ export class AppUsersManager { public getUserPhoto(id: number) { const user = this.getUser(id); - if(this.cachedPhotoLocations[id] === undefined) { - this.cachedPhotoLocations[id] = user && user.photo || { - _: 'userProfilePhotoEmpty' - }; - } - - return this.cachedPhotoLocations[id]; + return user && user.photo || { + _: 'userProfilePhotoEmpty' + }; } public getUserString(id: number) { diff --git a/src/lib/langPack.ts b/src/lib/langPack.ts index 3e8e12ff..54b92ac6 100644 --- a/src/lib/langPack.ts +++ b/src/lib/langPack.ts @@ -13,6 +13,7 @@ import { LangPackDifference, LangPackString } from "../layer"; import apiManager from "./mtproto/mtprotoworker"; import sessionStorage from "./sessionStorage"; import App from "../config/app"; +import rootScope from "./rootScope"; export const langPack: {[actionType: string]: LangPackKey} = { "messageActionChatCreate": "ActionCreateGroup", @@ -204,6 +205,8 @@ namespace I18n { strings.set(string.key as LangPackKey, string); } + rootScope.broadcast('language_change'); + const elements = Array.from(document.querySelectorAll(`.i18n`)) as HTMLElement[]; elements.forEach(element => { const instance = weakMap.get(element); diff --git a/src/lib/rootScope.ts b/src/lib/rootScope.ts index 00d504bd..d302e9c4 100644 --- a/src/lib/rootScope.ts +++ b/src/lib/rootScope.ts @@ -108,6 +108,8 @@ export type BroadcastEvents = { 'notify_settings': Update.updateNotifySettings, 'notify_peer_type_settings': {key: Exclude, settings: PeerNotifySettings}, + + 'language_change': void, }; class RootScope extends EventListenerBase { diff --git a/src/lib/searchIndexManager.ts b/src/lib/searchIndexManager.ts index f34af01c..ee2d5741 100644 --- a/src/lib/searchIndexManager.ts +++ b/src/lib/searchIndexManager.ts @@ -54,12 +54,16 @@ class SearchIndexManager { } public indexObject(id: number, searchText: string, searchIndex: SearchIndex) { - if(searchIndex.fullTexts.hasOwnProperty(id)) { + /* if(searchIndex.fullTexts.hasOwnProperty(id)) { return false; + } */ + + if(searchText.trim()) { + searchText = this.cleanSearchText(searchText); } - searchText = this.cleanSearchText(searchText); - if(!searchText.length) { + if(!searchText) { + delete searchIndex.fullTexts[id]; return false; } @@ -139,4 +143,4 @@ class SearchIndexManager { } } -export default new SearchIndexManager(); \ No newline at end of file +export default new SearchIndexManager(); diff --git a/src/lib/storages/dialogs.ts b/src/lib/storages/dialogs.ts index 70698b80..9f801249 100644 --- a/src/lib/storages/dialogs.ts +++ b/src/lib/storages/dialogs.ts @@ -15,6 +15,7 @@ import type { AppChatsManager } from "../appManagers/appChatsManager"; import type { AppMessagesManager, Dialog, MyMessage } from "../appManagers/appMessagesManager"; import type { AppPeersManager } from "../appManagers/appPeersManager"; import type { ServerTimeManager } from "../mtproto/serverTimeManager"; +import searchIndexManager from "../searchIndexManager"; export default class DialogsStorage { public dialogs: {[peerId: string]: Dialog} = {}; @@ -25,6 +26,8 @@ export default class DialogsStorage { public pinnedOrders: {[folder_id: number]: number[]}; private dialogsNum: number; + public dialogsIndex = searchIndexManager.createIndex(); + constructor(private appMessagesManager: AppMessagesManager, private appChatsManager: AppChatsManager, private appPeersManager: AppPeersManager, private serverTimeManager: ServerTimeManager) { this.reset(); } @@ -199,6 +202,7 @@ export default class DialogsStorage { if(foundDialog[0]) { this.byFolders[foundDialog[0].folder_id].splice(foundDialog[1], 1); delete this.dialogs[peerId]; + searchIndexManager.indexObject(peerId, '', this.dialogsIndex); } return foundDialog;