[Refactoring] TeactN: Support async action handlers

This commit is contained in:
Alexander Zinchuk 2022-03-19 21:19:14 +01:00
parent e204fa36a5
commit 4f3c7a1a69
28 changed files with 1603 additions and 1799 deletions

View File

@ -101,7 +101,7 @@ addActionHandler('sendBotCommand', (global, actions, payload) => {
);
});
addActionHandler('restartBot', (global, actions, payload) => {
addActionHandler('restartBot', async (global, actions, payload) => {
const { chatId } = payload;
const { currentUserId } = global;
const chat = selectCurrentChat(global);
@ -110,7 +110,6 @@ addActionHandler('restartBot', (global, actions, payload) => {
return;
}
(async () => {
const result = await callApi('unblockContact', bot.id, bot.accessHash);
if (!result) {
return;
@ -118,46 +117,40 @@ addActionHandler('restartBot', (global, actions, payload) => {
setGlobal(removeBlockedContact(getGlobal(), bot.id));
void sendBotCommand(chat, currentUserId, '/start', undefined, selectSendAs(global, chatId));
})();
});
addActionHandler('loadTopInlineBots', (global) => {
addActionHandler('loadTopInlineBots', async (global) => {
const { lastRequestedAt } = global.topInlineBots;
if (lastRequestedAt && getServerTime(global.serverTimeOffset) - lastRequestedAt < TOP_PEERS_REQUEST_COOLDOWN) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchTopInlineBots');
if (!result) {
return;
return undefined;
}
const { ids, users } = result;
let newGlobal = getGlobal();
newGlobal = addUsers(newGlobal, buildCollectionByKey(users, 'id'));
newGlobal = {
...newGlobal,
global = getGlobal();
global = addUsers(global, buildCollectionByKey(users, 'id'));
global = {
...global,
topInlineBots: {
...newGlobal.topInlineBots,
...global.topInlineBots,
userIds: ids,
lastRequestedAt: getServerTime(global.serverTimeOffset),
},
};
setGlobal(newGlobal);
})();
return global;
});
addActionHandler('queryInlineBot', ((global, actions, payload) => {
addActionHandler('queryInlineBot', async (global, actions, payload) => {
const {
chatId, username, query, offset,
} = payload;
(async () => {
let inlineBotData = global.inlineBots.byUsername[username];
if (inlineBotData === false) {
return;
}
@ -198,13 +191,11 @@ addActionHandler('queryInlineBot', ((global, actions, payload) => {
offset,
});
});
})();
}));
});
addActionHandler('sendInlineBotResult', (global, actions, payload) => {
const { id, queryId } = payload;
const currentMessageList = selectCurrentMessageList(global);
if (!currentMessageList || !id) {
return;
}
@ -246,7 +237,7 @@ addActionHandler('resetInlineBot', (global, actions, payload) => {
setGlobal(replaceInlineBotSettings(global, username, inlineBotData));
});
addActionHandler('startBot', (global, actions, payload) => {
addActionHandler('startBot', async (global, actions, payload) => {
const { botId, param } = payload;
const bot = selectUser(global, botId);
@ -254,12 +245,10 @@ addActionHandler('startBot', (global, actions, payload) => {
return;
}
(async () => {
await callApi('startBot', {
bot,
startParam: param,
});
})();
});
async function searchInlineBot({

View File

@ -9,8 +9,6 @@ import {
handleUpdateGroupCallParticipants, handleUpdateGroupCallConnection,
} from '../../../lib/secret-sauce';
import { ApiUpdate } from '../../../api/types';
import { GROUP_CALL_VOLUME_MULTIPLIER } from '../../../config';
import { callApi } from '../../../api/gramjs';
import { selectChat, selectCurrentMessageList, selectUser } from '../../selectors';
@ -35,7 +33,7 @@ import callFallbackAvatarPath from '../../../assets/call-fallback-avatar.png';
const FALLBACK_INVITE_EXPIRE_SECONDS = 1800; // 30 min
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
const { activeGroupCallId } = global.groupCalls;
switch (update['@type']) {
@ -88,7 +86,7 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
return undefined;
});
addActionHandler('leaveGroupCall', (global, actions, payload) => {
addActionHandler('leaveGroupCall', async (global, actions, payload) => {
const {
isFromLibrary, shouldDiscard, shouldRemove, rejoin,
} = payload || {};
@ -99,7 +97,6 @@ addActionHandler('leaveGroupCall', (global, actions, payload) => {
setGlobal(updateActiveGroupCall(global, { connectionState: 'disconnected' }, groupCall.participantsCount - 1));
(async () => {
await callApi('leaveGroupCall', {
call: groupCall,
});
@ -148,17 +145,15 @@ addActionHandler('leaveGroupCall', (global, actions, payload) => {
if (rejoin) {
actions.joinGroupCall(rejoin);
}
})();
});
addActionHandler('toggleGroupCallVideo', (global) => {
addActionHandler('toggleGroupCallVideo', async (global) => {
const groupCall = selectActiveGroupCall(global);
const user = selectUser(global, global.currentUserId!);
if (!user || !groupCall) {
return;
}
(async () => {
await toggleStream('video');
await callApi('editGroupCallParticipant', {
@ -166,7 +161,6 @@ addActionHandler('toggleGroupCallVideo', (global) => {
videoStopped: !isStreamEnabled('video'),
participant: user,
});
})();
});
addActionHandler('requestToSpeak', (global, actions, payload) => {
@ -202,7 +196,7 @@ addActionHandler('setGroupCallParticipantVolume', (global, actions, payload) =>
});
});
addActionHandler('toggleGroupCallMute', (global, actions, payload) => {
addActionHandler('toggleGroupCallMute', async (global, actions, payload) => {
const { participantId, value } = payload || {};
const groupCall = selectActiveGroupCall(global);
const user = selectUser(global, participantId || global.currentUserId!);
@ -210,7 +204,6 @@ addActionHandler('toggleGroupCallMute', (global, actions, payload) => {
return;
}
(async () => {
const muted = value === undefined ? isStreamEnabled('audio', user.id) : value;
if (!participantId) {
@ -224,17 +217,15 @@ addActionHandler('toggleGroupCallMute', (global, actions, payload) => {
muted,
participant: user,
});
})();
});
addActionHandler('toggleGroupCallPresentation', (global, actions, payload) => {
addActionHandler('toggleGroupCallPresentation', async (global, actions, payload) => {
const groupCall = selectActiveGroupCall(global);
const user = selectUser(global, global.currentUserId!);
if (!user || !groupCall) {
return;
}
(async () => {
const value = payload?.value !== undefined ? payload?.value : !isStreamEnabled('presentation');
if (value) {
const params = await startSharingScreen();
@ -258,10 +249,9 @@ addActionHandler('toggleGroupCallPresentation', (global, actions, payload) => {
presentationPaused: !isStreamEnabled('presentation'),
participant: user,
});
})();
});
addActionHandler('connectToActiveGroupCall', (global, actions) => {
addActionHandler('connectToActiveGroupCall', async (global, actions) => {
const groupCall = selectActiveGroupCall(global);
if (!groupCall) return;
@ -283,7 +273,6 @@ addActionHandler('connectToActiveGroupCall', (global, actions) => {
if (!currentUserId) return;
(async () => {
const params = await joinGroupCall(currentUserId, audioContext, audioElement, actions.apiUpdate);
const result = await callApi('joinGroupCall', {
@ -301,10 +290,9 @@ addActionHandler('connectToActiveGroupCall', (global, actions) => {
if (!chat) return;
await loadFullChat(chat);
}
})();
});
addActionHandler('inviteToCallFallback', (global, actions, payload) => {
addActionHandler('inviteToCallFallback', async (global, actions, payload) => {
const { chatId } = selectCurrentMessageList(global) || {};
if (!chatId) {
return;
@ -317,7 +305,6 @@ addActionHandler('inviteToCallFallback', (global, actions, payload) => {
const { shouldRemove } = payload;
(async () => {
const fallbackChannelTitle = selectCallFallbackChannelTitle(global);
let fallbackChannel = Object.values(global.chats.byId).find((channel) => {
@ -379,5 +366,4 @@ addActionHandler('inviteToCallFallback', (global, actions, payload) => {
actions.openChat({ id: fallbackChannel.id });
actions.createGroupCall({ chatId: fallbackChannel.id });
actions.closeCallFallbackConfirm();
})();
});

View File

@ -48,8 +48,7 @@ const INFINITE_LOOP_MARKER = 100;
const runThrottledForLoadTopChats = throttle((cb) => cb(), 3000, true);
const runDebouncedForLoadFullChat = debounce((cb) => cb(), 500, false, true);
addActionHandler('preloadTopChatMessages', (global, actions) => {
(async () => {
addActionHandler('preloadTopChatMessages', async (global, actions) => {
const preloadedChatIds = new Set<string>();
for (let i = 0; i < TOP_CHAT_MESSAGES_PRELOAD_LIMIT; i++) {
@ -66,7 +65,6 @@ addActionHandler('preloadTopChatMessages', (global, actions) => {
actions.loadViewportMessages({ chatId: nextChatId, threadId: MAIN_THREAD_ID });
}
})();
});
addActionHandler('openChat', (global, actions, payload) => {
@ -107,40 +105,36 @@ addActionHandler('openChat', (global, actions, payload) => {
}
});
addActionHandler('openLinkedChat', (global, actions, payload) => {
addActionHandler('openLinkedChat', async (global, actions, payload) => {
const { id } = payload!;
const chat = selectChat(global, id);
if (!chat) {
return;
}
(async () => {
const chatFullInfo = await callApi('fetchFullChat', chat);
if (chatFullInfo?.fullInfo?.linkedChatId) {
actions.openChat({ id: chatFullInfo.fullInfo.linkedChatId });
}
})();
});
addActionHandler('focusMessageInComments', (global, actions, payload) => {
addActionHandler('focusMessageInComments', async (global, actions, payload) => {
const { chatId, threadId, messageId } = payload!;
const chat = selectChat(global, chatId);
if (!chat) {
return;
}
(async () => {
const result = await callApi('requestThreadInfoUpdate', { chat, threadId });
if (!result) {
return;
}
actions.focusMessage({ chatId, threadId, messageId });
})();
});
addActionHandler('openSupportChat', (global, actions) => {
addActionHandler('openSupportChat', async (global, actions) => {
const chat = selectSupportChat(global);
if (chat) {
actions.openChat({ id: chat.id, shouldReplaceHistory: true });
@ -149,12 +143,10 @@ addActionHandler('openSupportChat', (global, actions) => {
actions.openChat({ id: TMP_CHAT_ID, shouldReplaceHistory: true });
(async () => {
const result = await callApi('fetchChat', { type: 'support' });
if (result) {
actions.openChat({ id: result.chatId, shouldReplaceHistory: true });
}
})();
});
addActionHandler('openTipsChat', (global, actions, payload) => {
@ -167,13 +159,12 @@ addActionHandler('openTipsChat', (global, actions, payload) => {
actions.openChatByUsername({ username: `${TIPS_USERNAME}${usernamePostfix}` });
});
addActionHandler('loadAllChats', (global, actions, payload) => {
addActionHandler('loadAllChats', async (global, actions, payload) => {
const listType = payload.listType as 'active' | 'archived';
const { onReplace } = payload;
let { shouldReplace } = payload;
let i = 0;
(async () => {
while (shouldReplace || !getGlobal().chats.isFullyLoaded[listType]) {
if (i++ >= INFINITE_LOOP_MARKER) {
if (DEBUG) {
@ -207,7 +198,6 @@ addActionHandler('loadAllChats', (global, actions, payload) => {
shouldReplace = false;
}
}
})();
});
addActionHandler('loadFullChat', (global, actions, payload) => {
@ -503,23 +493,20 @@ addActionHandler('toggleChatUnread', (global, actions, payload) => {
}
});
addActionHandler('openChatByInvite', (global, actions, payload) => {
addActionHandler('openChatByInvite', async (global, actions, payload) => {
const { hash } = payload!;
(async () => {
const result = await callApi('openChatByInvite', hash);
if (!result) {
return;
}
actions.openChat({ id: result.chatId });
})();
});
addActionHandler('openChatByPhoneNumber', (global, actions, payload) => {
addActionHandler('openChatByPhoneNumber', async (global, actions, payload) => {
const { phoneNumber } = payload!;
(async () => {
// Open temporary empty chat to make the click response feel faster
actions.openChat({ id: TMP_CHAT_ID });
@ -533,7 +520,6 @@ addActionHandler('openChatByPhoneNumber', (global, actions, payload) => {
}
actions.openChat({ id: chat.id });
})();
});
addActionHandler('openTelegramLink', (global, actions, payload) => {
@ -604,24 +590,21 @@ addActionHandler('openTelegramLink', (global, actions, payload) => {
}
});
addActionHandler('acceptInviteConfirmation', (global, actions, payload) => {
addActionHandler('acceptInviteConfirmation', async (global, actions, payload) => {
const { hash } = payload!;
(async () => {
const result = await callApi('importChatInvite', { hash });
if (!result) {
return;
}
actions.openChat({ id: result.id });
})();
});
addActionHandler('openChatByUsername', (global, actions, payload) => {
addActionHandler('openChatByUsername', async (global, actions, payload) => {
const {
username, messageId, commentId, startParam,
} = payload!;
(async () => {
const chat = selectCurrentChat(global);
if (!commentId) {
@ -650,19 +633,17 @@ addActionHandler('openChatByUsername', (global, actions, payload) => {
if (!messageId) return;
await openCommentsByUsername(actions, username, messageId, commentId);
})();
void openCommentsByUsername(actions, username, messageId, commentId);
});
addActionHandler('togglePreHistoryHidden', (global, actions, payload) => {
addActionHandler('togglePreHistoryHidden', async (global, actions, payload) => {
const { chatId, isEnabled } = payload!;
let chat = selectChat(global, chatId);
let chat = selectChat(global, chatId);
if (!chat) {
return;
}
(async () => {
if (isChatBasicGroup(chat)) {
chat = await callApi('migrateChat', chat);
@ -674,7 +655,6 @@ addActionHandler('togglePreHistoryHidden', (global, actions, payload) => {
}
void callApi('togglePreHistoryHidden', { chat, isEnabled });
})();
});
addActionHandler('updateChatDefaultBannedRights', (global, actions, payload) => {
@ -688,21 +668,20 @@ addActionHandler('updateChatDefaultBannedRights', (global, actions, payload) =>
void callApi('updateChatDefaultBannedRights', { chat, bannedRights });
});
addActionHandler('updateChatMemberBannedRights', (global, actions, payload) => {
addActionHandler('updateChatMemberBannedRights', async (global, actions, payload) => {
const { chatId, userId, bannedRights } = payload!;
let chat = selectChat(global, chatId);
const user = selectUser(global, userId);
if (!chat || !user) {
return;
return undefined;
}
(async () => {
if (isChatBasicGroup(chat)) {
chat = await callApi('migrateChat', chat);
if (!chat) {
return;
return undefined;
}
actions.openChat({ id: chat.id });
@ -710,11 +689,12 @@ addActionHandler('updateChatMemberBannedRights', (global, actions, payload) => {
await callApi('updateChatMemberBannedRights', { chat, user, bannedRights });
const newGlobal = getGlobal();
const chatAfterUpdate = selectChat(newGlobal, chatId);
global = getGlobal();
const chatAfterUpdate = selectChat(global, chatId);
if (!chatAfterUpdate || !chatAfterUpdate.fullInfo) {
return;
return undefined;
}
const { members, kickedMembers } = chatAfterUpdate.fullInfo;
@ -722,7 +702,7 @@ addActionHandler('updateChatMemberBannedRights', (global, actions, payload) => {
const isBanned = Boolean(bannedRights.viewMessages);
const isUnblocked = !Object.keys(bannedRights).length;
setGlobal(updateChat(newGlobal, chatId, {
return updateChat(global, chatId, {
fullInfo: {
...chatAfterUpdate.fullInfo,
...(members && isBanned && {
@ -739,27 +719,24 @@ addActionHandler('updateChatMemberBannedRights', (global, actions, payload) => {
kickedMembers: kickedMembers.filter((m) => m.userId !== userId),
}),
},
}));
})();
});
});
addActionHandler('updateChatAdmin', (global, actions, payload) => {
addActionHandler('updateChatAdmin', async (global, actions, payload) => {
const {
chatId, userId, adminRights, customTitle,
} = payload!;
let chat = selectChat(global, chatId);
const user = selectUser(global, userId);
if (!chat || !user) {
return;
return undefined;
}
(async () => {
if (isChatBasicGroup(chat)) {
chat = await callApi('migrateChat', chat);
if (!chat) {
return;
return undefined;
}
actions.openChat({ id: chat.id });
@ -770,17 +747,16 @@ addActionHandler('updateChatAdmin', (global, actions, payload) => {
});
const chatAfterUpdate = await callApi('fetchFullChat', chat);
const newGlobal = getGlobal();
if (!chatAfterUpdate || !chatAfterUpdate.fullInfo) {
return;
if (!chatAfterUpdate?.fullInfo) {
return undefined;
}
const { adminMembers } = chatAfterUpdate.fullInfo;
const isDismissed = !Object.keys(adminRights).length;
setGlobal(updateChat(newGlobal, chatId, {
global = getGlobal();
return updateChat(global, chatId, {
fullInfo: {
...chatAfterUpdate.fullInfo,
...(adminMembers && isDismissed && {
@ -794,21 +770,19 @@ addActionHandler('updateChatAdmin', (global, actions, payload) => {
)),
}),
},
}));
})();
});
});
addActionHandler('updateChat', (global, actions, payload) => {
addActionHandler('updateChat', async (global, actions, payload) => {
const {
chatId, title, about, photo,
} = payload!;
const chat = selectChat(global, chatId);
if (!chat) {
return;
return undefined;
}
(async () => {
setGlobal(updateManagementProgress(getGlobal(), ManagementProgress.InProgress));
await Promise.all([
@ -823,8 +797,7 @@ addActionHandler('updateChat', (global, actions, payload) => {
: undefined,
]);
setGlobal(updateManagementProgress(getGlobal(), ManagementProgress.Complete));
})();
return updateManagementProgress(getGlobal(), ManagementProgress.Complete);
});
addActionHandler('toggleSignatures', (global, actions, payload) => {
@ -838,11 +811,10 @@ addActionHandler('toggleSignatures', (global, actions, payload) => {
void callApi('toggleSignatures', { chat, isEnabled });
});
addActionHandler('loadGroupsForDiscussion', () => {
(async () => {
addActionHandler('loadGroupsForDiscussion', async (global) => {
const groups = await callApi('fetchGroupsForDiscussion');
if (!groups) {
return;
return undefined;
}
const addedById = groups.reduce((result, group) => {
@ -853,18 +825,18 @@ addActionHandler('loadGroupsForDiscussion', () => {
return result;
}, {} as Record<string, ApiChat>);
const global = addChats(getGlobal(), addedById);
setGlobal({
global = getGlobal();
global = addChats(global, addedById);
return {
...global,
chats: {
...global.chats,
forDiscussionIds: Object.keys(addedById),
},
});
})();
};
});
addActionHandler('linkDiscussionGroup', (global, actions, payload) => {
addActionHandler('linkDiscussionGroup', async (global, actions, payload) => {
const { channelId, chatId } = payload!;
const channel = selectChat(global, channelId);
@ -873,7 +845,6 @@ addActionHandler('linkDiscussionGroup', (global, actions, payload) => {
return;
}
(async () => {
if (isChatBasicGroup(chat)) {
chat = await callApi('migrateChat', chat);
@ -899,10 +870,9 @@ addActionHandler('linkDiscussionGroup', (global, actions, payload) => {
}
void callApi('setDiscussionGroup', { channel, chat });
})();
});
addActionHandler('unlinkDiscussionGroup', (global, actions, payload) => {
addActionHandler('unlinkDiscussionGroup', async (global, actions, payload) => {
const { channelId } = payload!;
const channel = selectChat(global, channelId);
@ -915,12 +885,10 @@ addActionHandler('unlinkDiscussionGroup', (global, actions, payload) => {
chat = selectChat(global, channel.fullInfo.linkedChatId);
}
(async () => {
await callApi('setDiscussionGroup', { channel });
if (chat) {
loadFullChat(chat);
}
})();
});
addActionHandler('setActiveChatFolder', (global, actions, payload) => {
@ -933,33 +901,31 @@ addActionHandler('setActiveChatFolder', (global, actions, payload) => {
};
});
addActionHandler('loadMoreMembers', (global) => {
(async () => {
addActionHandler('loadMoreMembers', async (global) => {
const { chatId } = selectCurrentMessageList(global) || {};
const chat = chatId ? selectChat(global, chatId) : undefined;
if (!chat || isChatBasicGroup(chat)) {
return;
return undefined;
}
const offset = (chat.fullInfo?.members?.length) || undefined;
const result = await callApi('fetchMembers', chat.id, chat.accessHash!, 'recent', offset);
if (!result) {
return;
return undefined;
}
const { members, users } = result;
if (!members || !members.length) {
return;
return undefined;
}
global = getGlobal();
global = addUsers(global, buildCollectionByKey(users, 'id'));
global = addChatMembers(global, chat, members);
setGlobal(global);
})();
return global;
});
addActionHandler('addChatMembers', (global, actions, payload) => {
addActionHandler('addChatMembers', async (global, actions, payload) => {
const { chatId, memberIds } = payload;
const chat = selectChat(global, chatId);
const users = (memberIds as string[]).map((userId) => selectUser(global, userId)).filter<ApiUser>(Boolean as any);
@ -969,14 +935,12 @@ addActionHandler('addChatMembers', (global, actions, payload) => {
}
actions.setNewChatMembersDialogState(NewChatMembersProgress.Loading);
(async () => {
await callApi('addChatMembers', chat, users);
actions.setNewChatMembersDialogState(NewChatMembersProgress.Closed);
loadFullChat(chat);
})();
});
addActionHandler('deleteChatMember', (global, actions, payload) => {
addActionHandler('deleteChatMember', async (global, actions, payload) => {
const { chatId, userId } = payload;
const chat = selectChat(global, chatId);
const user = selectUser(global, userId);
@ -985,10 +949,8 @@ addActionHandler('deleteChatMember', (global, actions, payload) => {
return;
}
(async () => {
await callApi('deleteChatMember', chat, user);
loadFullChat(chat);
})();
});
addActionHandler('toggleIsProtected', (global, actions, payload) => {
@ -1002,20 +964,17 @@ addActionHandler('toggleIsProtected', (global, actions, payload) => {
void callApi('toggleIsProtected', { chat, isProtected });
});
addActionHandler('setChatEnabledReactions', (global, actions, payload) => {
addActionHandler('setChatEnabledReactions', async (global, actions, payload) => {
const { chatId, enabledReactions } = payload;
const chat = selectChat(global, chatId);
if (!chat) return;
(async () => {
await callApi('setChatEnabledReactions', {
chat,
enabledReactions,
});
await loadFullChat(chat);
})();
void loadFullChat(chat);
});
async function loadChats(

View File

@ -33,7 +33,8 @@ addActionHandler('setGlobalSearchQuery', (global, actions, payload) => {
addActionHandler('setGlobalSearchDate', (global, actions, payload) => {
const { date } = payload!;
const maxDate = date ? timestampPlusDay(date) : date;
const newGlobal = updateGlobalSearch(global, {
global = updateGlobalSearch(global, {
date,
query: '',
resultsByType: {
@ -45,7 +46,8 @@ addActionHandler('setGlobalSearchDate', (global, actions, payload) => {
},
},
});
setGlobal(newGlobal);
setGlobal(global);
const { chatId } = global.globalSearch;
const chat = chatId ? selectChat(global, chatId) : undefined;
searchMessagesGlobal('', 'text', undefined, chat, maxDate, date);

View File

@ -1,9 +1,6 @@
import {
addActionHandler, getActions, getGlobal, setGlobal,
} from '../../index';
import { addActionHandler, getActions, getGlobal } from '../../index';
import { initApi, callApi } from '../../../api/gramjs';
import { GlobalState } from '../../types';
import {
LANG_CACHE_NAME,
@ -26,8 +23,7 @@ import {
} from '../../../util/sessions';
import { forceWebsync } from '../../../util/websync';
addActionHandler('initApi', (global: GlobalState, actions) => {
(async () => {
addActionHandler('initApi', async (global, actions) => {
if (!IS_TEST) {
await importLegacySession();
void clearLegacySessions();
@ -41,7 +37,6 @@ addActionHandler('initApi', (global: GlobalState, actions) => {
isMovSupported: IS_MOV_SUPPORTED,
isWebmSupported: IS_WEBM_SUPPORTED,
});
})();
});
addActionHandler('setAuthPhoneNumber', (global, actions, payload) => {
@ -127,8 +122,7 @@ addActionHandler('saveSession', (global, actions, payload) => {
}
});
addActionHandler('signOut', () => {
(async () => {
addActionHandler('signOut', async () => {
try {
await unsubscribe();
await callApi('destroy');
@ -138,7 +132,6 @@ addActionHandler('signOut', () => {
}
getActions().reset();
})();
});
addActionHandler('reset', () => {
@ -163,38 +156,35 @@ addActionHandler('reset', () => {
});
addActionHandler('disconnect', () => {
(async () => {
await callApi('disconnect');
})();
void callApi('disconnect');
});
addActionHandler('loadNearestCountry', (global) => {
addActionHandler('loadNearestCountry', async (global) => {
if (global.connectionState !== 'connectionStateReady') {
return;
return undefined;
}
(async () => {
const authNearestCountry = await callApi('fetchNearestCountry');
setGlobal({
return {
...getGlobal(),
authNearestCountry,
});
})();
};
});
addActionHandler('setDeviceToken', (global, actions, deviceToken) => {
setGlobal({
return {
...global,
push: {
deviceToken,
subscribedAt: Date.now(),
},
});
};
});
addActionHandler('deleteDeviceToken', (global) => {
const newGlobal = { ...global };
delete newGlobal.push;
setGlobal(newGlobal);
return {
...global,
push: undefined,
};
});

View File

@ -6,20 +6,19 @@ import { updateChat, updateManagement, updateManagementProgress } from '../../re
import { selectChat, selectCurrentMessageList, selectUser } from '../../selectors';
import { isChatBasicGroup } from '../../helpers';
addActionHandler('checkPublicLink', (global, actions, payload) => {
addActionHandler('checkPublicLink', async (global, actions, payload) => {
const { chatId } = selectCurrentMessageList(global) || {};
if (!chatId) {
return;
return undefined;
}
// No need to check the username if already in progress
if (global.management.progress === ManagementProgress.InProgress) {
return;
return undefined;
}
const { username } = payload!;
(async () => {
global = updateManagementProgress(global, ManagementProgress.InProgress);
global = updateManagement(global, chatId, { isUsernameAvailable: undefined });
setGlobal(global);
@ -31,20 +30,18 @@ addActionHandler('checkPublicLink', (global, actions, payload) => {
global, isUsernameAvailable ? ManagementProgress.Complete : ManagementProgress.Error,
);
global = updateManagement(global, chatId, { isUsernameAvailable });
setGlobal(global);
})();
return global;
});
addActionHandler('updatePublicLink', (global, actions, payload) => {
addActionHandler('updatePublicLink', async (global, actions, payload) => {
const { chatId } = selectCurrentMessageList(global) || {};
let chat = chatId && selectChat(global, chatId);
if (!chatId || !chat) {
return;
return undefined;
}
const { username } = payload!;
(async () => {
global = updateManagementProgress(global, ManagementProgress.InProgress);
setGlobal(global);
@ -52,7 +49,7 @@ addActionHandler('updatePublicLink', (global, actions, payload) => {
chat = await callApi('migrateChat', chat);
if (!chat) {
return;
return undefined;
}
actions.openChat({ id: chat.id });
@ -63,8 +60,7 @@ addActionHandler('updatePublicLink', (global, actions, payload) => {
global = getGlobal();
global = updateManagementProgress(global, result ? ManagementProgress.Complete : ManagementProgress.Error);
global = updateManagement(global, chatId, { isUsernameAvailable: undefined });
setGlobal(global);
})();
return global;
});
addActionHandler('updatePrivateLink', (global) => {
@ -91,35 +87,33 @@ addActionHandler('setOpenedInviteInfo', (global, actions, payload) => {
setGlobal(updateManagement(global, chatId, update));
});
addActionHandler('loadExportedChatInvites', (global, actions, payload) => {
addActionHandler('loadExportedChatInvites', async (global, actions, payload) => {
const {
chatId, adminId, isRevoked, limit,
} = payload!;
const peer = selectChat(global, chatId);
const admin = selectUser(global, adminId || global.currentUserId);
if (!peer || !admin) return;
if (!peer || !admin) return undefined;
(async () => {
const result = await callApi('fetchExportedChatInvites', {
peer, admin, isRevoked, limit,
});
if (!result) {
return;
return undefined;
}
const update = isRevoked ? { revokedInvites: result } : { invites: result };
setGlobal(updateManagement(getGlobal(), chatId, update));
})();
return updateManagement(getGlobal(), chatId, update);
});
addActionHandler('editExportedChatInvite', (global, actions, payload) => {
addActionHandler('editExportedChatInvite', async (global, actions, payload) => {
const {
chatId, link, isRevoked, expireDate, usageLimit, isRequestNeeded, title,
} = payload!;
const peer = selectChat(global, chatId);
if (!peer) return;
if (!peer) return undefined;
(async () => {
const result = await callApi('editExportedChatInvite', {
peer,
link,
@ -130,33 +124,35 @@ addActionHandler('editExportedChatInvite', (global, actions, payload) => {
title,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
let invites = global.management.byChatId[chatId].invites || [];
const revokedInvites = global.management.byChatId[chatId].revokedInvites || [];
const { oldInvite, newInvite } = result;
invites = invites.filter((current) => current.link !== oldInvite.link);
global = getGlobal();
const invites = (global.management.byChatId[chatId].invites || [])
.filter((current) => current.link !== oldInvite.link);
const revokedInvites = [...(global.management.byChatId[chatId].revokedInvites || [])];
if (newInvite.isRevoked) {
revokedInvites.unshift(newInvite);
} else {
invites.push(newInvite);
}
setGlobal(updateManagement(global, chatId, {
return updateManagement(global, chatId, {
invites,
revokedInvites,
}));
})();
});
});
addActionHandler('exportChatInvite', (global, actions, payload) => {
addActionHandler('exportChatInvite', async (global, actions, payload) => {
const {
chatId, expireDate, usageLimit, isRequestNeeded, title,
} = payload!;
const peer = selectChat(global, chatId);
if (!peer) return;
if (!peer) return undefined;
(async () => {
const result = await callApi('exportChatInvite', {
peer,
expireDate,
@ -165,72 +161,69 @@ addActionHandler('exportChatInvite', (global, actions, payload) => {
title,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
const invites = global.management.byChatId[chatId].invites || [];
setGlobal(updateManagement(global, chatId, {
return updateManagement(global, chatId, {
invites: [...invites, result],
}));
})();
});
});
addActionHandler('deleteExportedChatInvite', (global, actions, payload) => {
addActionHandler('deleteExportedChatInvite', async (global, actions, payload) => {
const {
chatId, link,
} = payload!;
const peer = selectChat(global, chatId);
if (!peer) return;
if (!peer) return undefined;
(async () => {
const result = await callApi('deleteExportedChatInvite', {
peer,
link,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
const managementState = global.management.byChatId[chatId];
setGlobal(updateManagement(global, chatId, {
return updateManagement(global, chatId, {
invites: managementState?.invites?.filter((invite) => invite.link !== link),
revokedInvites: managementState?.revokedInvites?.filter((invite) => invite.link !== link),
}));
})();
});
});
addActionHandler('deleteRevokedExportedChatInvites', (global, actions, payload) => {
addActionHandler('deleteRevokedExportedChatInvites', async (global, actions, payload) => {
const {
chatId, adminId,
} = payload!;
const peer = selectChat(global, chatId);
const admin = selectUser(global, adminId || global.currentUserId);
if (!peer || !admin) return;
if (!peer || !admin) return undefined;
(async () => {
const result = await callApi('deleteRevokedExportedChatInvites', {
peer,
admin,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
setGlobal(updateManagement(global, chatId, {
return updateManagement(global, chatId, {
revokedInvites: [],
}));
})();
});
});
addActionHandler('loadChatInviteImporters', (global, actions, payload) => {
addActionHandler('loadChatInviteImporters', async (global, actions, payload) => {
const {
chatId, link, offsetDate, offsetUserId, limit,
} = payload!;
const peer = selectChat(global, chatId);
const offsetUser = selectUser(global, offsetUserId);
if (!peer || (offsetUserId && !offsetUser)) return;
if (!peer || (offsetUserId && !offsetUser)) return undefined;
(async () => {
const result = await callApi('fetchChatInviteImporters', {
peer,
link,
@ -239,29 +232,31 @@ addActionHandler('loadChatInviteImporters', (global, actions, payload) => {
limit,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
const currentInviteInfo = global.management.byChatId[chatId]?.inviteInfo;
if (!currentInviteInfo?.invite || currentInviteInfo.invite.link !== link) return;
setGlobal(updateManagement(global, chatId, {
if (!currentInviteInfo?.invite || currentInviteInfo.invite.link !== link) {
return undefined;
}
return updateManagement(global, chatId, {
inviteInfo: {
...currentInviteInfo,
importers: result,
},
}));
})();
});
});
addActionHandler('loadChatInviteRequesters', (global, actions, payload) => {
addActionHandler('loadChatInviteRequesters', async (global, actions, payload) => {
const {
chatId, link, offsetDate, offsetUserId, limit,
} = payload!;
const peer = selectChat(global, chatId);
const offsetUser = selectUser(global, offsetUserId);
if (!peer || (offsetUserId && !offsetUser)) return;
if (!peer || (offsetUserId && !offsetUser)) return undefined;
(async () => {
const result = await callApi('fetchChatInviteImporters', {
peer,
link,
@ -271,29 +266,31 @@ addActionHandler('loadChatInviteRequesters', (global, actions, payload) => {
isRequested: true,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
const currentInviteInfo = global.management.byChatId[chatId]?.inviteInfo;
if (!currentInviteInfo?.invite || currentInviteInfo.invite.link !== link) return;
setGlobal(updateManagement(global, chatId, {
if (!currentInviteInfo?.invite || currentInviteInfo.invite.link !== link) {
return undefined;
}
return updateManagement(global, chatId, {
inviteInfo: {
...currentInviteInfo,
requesters: result,
},
}));
})();
});
});
addActionHandler('loadChatJoinRequests', (global, actions, payload) => {
addActionHandler('loadChatJoinRequests', async (global, actions, payload) => {
const {
chatId, offsetDate, offsetUserId, limit,
} = payload!;
const peer = selectChat(global, chatId);
const offsetUser = selectUser(global, offsetUserId);
if (!peer || (offsetUserId && !offsetUser)) return;
if (!peer || (offsetUserId && !offsetUser)) return undefined;
(async () => {
const result = await callApi('fetchChatInviteImporters', {
peer,
offsetDate,
@ -302,64 +299,61 @@ addActionHandler('loadChatJoinRequests', (global, actions, payload) => {
isRequested: true,
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
setGlobal(updateChat(global, chatId, { joinRequests: result }));
})();
return updateChat(global, chatId, { joinRequests: result });
});
addActionHandler('hideChatJoinRequest', (global, actions, payload) => {
addActionHandler('hideChatJoinRequest', async (global, actions, payload) => {
const {
chatId, userId, isApproved,
} = payload!;
const peer = selectChat(global, chatId);
const user = selectUser(global, userId);
if (!peer || !user) return;
if (!peer || !user) return undefined;
(async () => {
const result = await callApi('hideChatJoinRequest', {
peer,
user,
isApproved,
});
if (!result) return undefined;
if (!result) return;
global = getGlobal();
const targetChat = selectChat(global, chatId);
if (!targetChat) return;
setGlobal(updateChat(global, chatId, {
if (!targetChat) return undefined;
return updateChat(global, chatId, {
joinRequests: targetChat.joinRequests?.filter((importer) => importer.userId !== userId),
}));
})();
});
});
addActionHandler('hideAllChatJoinRequests', (global, actions, payload) => {
addActionHandler('hideAllChatJoinRequests', async (global, actions, payload) => {
const {
chatId, isApproved, link,
} = payload!;
const peer = selectChat(global, chatId);
if (!peer) return;
if (!peer) return undefined;
(async () => {
const result = await callApi('hideAllChatJoinRequests', {
peer,
isApproved,
link,
});
if (!result) return undefined;
if (!result) return;
global = getGlobal();
const targetChat = selectChat(global, chatId);
if (!targetChat) return;
if (!targetChat) return undefined;
setGlobal(updateChat(global, chatId, {
return updateChat(global, chatId, {
joinRequests: [],
fullInfo: {
...targetChat.fullInfo,
recentRequesterIds: [],
requestsPending: 0,
},
}));
})();
});
});

View File

@ -157,30 +157,30 @@ async function loadWithBudget(
}
}
addActionHandler('loadMessage', (global, actions, payload) => {
addActionHandler('loadMessage', async (global, actions, payload) => {
const {
chatId, messageId, replyOriginForId, threadUpdate,
} = payload!;
const chat = selectChat(global, chatId);
const chat = selectChat(global, chatId);
if (!chat) {
return;
return undefined;
}
(async () => {
const message = await loadMessage(chat, messageId, replyOriginForId);
if (message && threadUpdate) {
const { lastMessageId, isDeleting } = threadUpdate;
setGlobal(updateThreadUnreadFromForwardedMessage(
return updateThreadUnreadFromForwardedMessage(
getGlobal(),
message,
chatId,
lastMessageId,
isDeleting,
));
);
}
})();
return undefined;
});
addActionHandler('sendMessage', (global, actions, payload) => {
@ -425,8 +425,7 @@ addActionHandler('deleteScheduledMessages', (global, actions, payload) => {
}
});
addActionHandler('deleteHistory', (global, actions, payload) => {
(async () => {
addActionHandler('deleteHistory', async (global, actions, payload) => {
const { chatId, shouldDeleteForAll } = payload!;
const chat = selectChat(global, chatId);
if (!chat) {
@ -441,11 +440,9 @@ addActionHandler('deleteHistory', (global, actions, payload) => {
if (activeChat && activeChat.chatId === chatId) {
actions.openChat({ id: undefined });
}
})();
});
addActionHandler('reportMessages', (global, actions, payload) => {
(async () => {
addActionHandler('reportMessages', async (global, actions, payload) => {
const {
messageIds, reason, description,
} = payload!;
@ -466,11 +463,9 @@ addActionHandler('reportMessages', (global, actions, payload) => {
? 'Thank you! Your report will be reviewed by our team.'
: 'Error occured while submiting report. Please, try again later.',
});
})();
});
addActionHandler('sendMessageAction', (global, actions, payload) => {
(async () => {
addActionHandler('sendMessageAction', async (global, actions, payload) => {
const { action, chatId, threadId } = payload!;
if (chatId === global.currentUserId) return; // Message actions are disabled in Saved Messages
@ -480,7 +475,6 @@ addActionHandler('sendMessageAction', (global, actions, payload) => {
await callApi('sendMessageAction', {
peer: chat, threadId, action,
});
})();
});
addActionHandler('markMessageListRead', (global, actions, payload) => {
@ -960,23 +954,21 @@ addActionHandler('loadPinnedMessages', (global, actions, payload) => {
void loadPinnedMessages(chat);
});
addActionHandler('loadSeenBy', (global, actions, payload) => {
addActionHandler('loadSeenBy', async (global, actions, payload) => {
const { chatId, messageId } = payload;
const chat = selectChat(global, chatId);
if (!chat) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchSeenBy', { chat, messageId });
if (!result) {
return;
return undefined;
}
setGlobal(updateChatMessage(getGlobal(), chatId, messageId, {
return updateChatMessage(getGlobal(), chatId, messageId, {
seenByUserIds: result,
}));
})();
});
});
addActionHandler('saveDefaultSendAs', (global, actions, payload) => {
@ -997,31 +989,25 @@ addActionHandler('saveDefaultSendAs', (global, actions, payload) => {
});
});
addActionHandler('loadSendAs', (global, actions, payload) => {
addActionHandler('loadSendAs', async (global, actions, payload) => {
const { chatId } = payload;
const chat = selectChat(global, chatId);
if (!chat) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchSendAs', { chat });
if (!result) {
global = updateChat(global, chatId, {
return updateChat(getGlobal(), chatId, {
sendAsIds: [],
});
setGlobal(global);
return;
}
global = getGlobal();
global = addUsers(global, buildCollectionByKey(result.users, 'id'));
global = addChats(global, buildCollectionByKey(result.chats, 'id'));
global = updateChat(global, chatId, {
sendAsIds: result.ids,
});
setGlobal(global);
})();
global = updateChat(global, chatId, { sendAsIds: result.ids });
return global;
});
async function loadPinnedMessages(chat: ApiChat) {
@ -1060,25 +1046,23 @@ async function loadScheduledHistory(chat: ApiChat) {
setGlobal(global);
}
addActionHandler('loadSponsoredMessages', (global, actions, payload) => {
addActionHandler('loadSponsoredMessages', async (global, actions, payload) => {
const { chatId } = payload;
const chat = selectChat(global, chatId);
if (!chat) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchSponsoredMessages', { chat });
if (!result) {
return;
return undefined;
}
let newGlobal = updateSponsoredMessage(getGlobal(), chatId, result.messages[0]);
newGlobal = addUsers(newGlobal, buildCollectionByKey(result.users, 'id'));
newGlobal = addChats(newGlobal, buildCollectionByKey(result.chats, 'id'));
setGlobal(newGlobal);
})();
global = getGlobal();
global = updateSponsoredMessage(global, chatId, result.messages[0]);
global = addUsers(global, buildCollectionByKey(result.users, 'id'));
global = addChats(global, buildCollectionByKey(result.chats, 'id'));
return global;
});
addActionHandler('viewSponsoredMessage', (global, actions, payload) => {

View File

@ -1,4 +1,4 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { addActionHandler, getGlobal } from '../../index';
import { callApi } from '../../../api/gramjs';
import * as mediaLoader from '../../../util/mediaLoader';
import { ApiAppConfig, ApiMediaFormat } from '../../../api/types';
@ -19,12 +19,10 @@ const INTERACTION_RANDOM_OFFSET = 40;
let interactionLocalId = 0;
addActionHandler('loadAvailableReactions', () => {
(async () => {
addActionHandler('loadAvailableReactions', async () => {
const result = await callApi('getAvailableReactions');
if (!result) {
return;
return undefined;
}
// Preload animations
@ -37,11 +35,10 @@ addActionHandler('loadAvailableReactions', () => {
}
});
setGlobal({
return {
...getGlobal(),
availableReactions: result,
});
})();
};
});
addActionHandler('interactWithAnimatedEmoji', (global, actions, payload) => {
@ -196,25 +193,21 @@ addActionHandler('stopActiveReaction', (global, actions, payload) => {
};
});
addActionHandler('setDefaultReaction', (global, actions, payload) => {
addActionHandler('setDefaultReaction', async (global, actions, payload) => {
const { reaction } = payload;
(async () => {
const result = await callApi('setDefaultReaction', { reaction });
if (!result) {
return;
return undefined;
}
global = getGlobal();
setGlobal({
...global,
return {
...getGlobal(),
appConfig: {
...global.appConfig,
defaultReaction: reaction,
} as ApiAppConfig,
});
})();
};
});
addActionHandler('stopActiveEmojiInteraction', (global, actions, payload) => {
@ -226,17 +219,15 @@ addActionHandler('stopActiveEmojiInteraction', (global, actions, payload) => {
};
});
addActionHandler('loadReactors', (global, actions, payload) => {
addActionHandler('loadReactors', async (global, actions, payload) => {
const { chatId, messageId, reaction } = payload;
const chat = selectChat(global, chatId);
const message = selectChatMessage(global, chatId, messageId);
if (!chat || !message) {
return;
return undefined;
}
const offset = message.reactors?.nextOffset;
(async () => {
const result = await callApi('fetchMessageReactionsList', {
reaction,
chat,
@ -245,17 +236,18 @@ addActionHandler('loadReactors', (global, actions, payload) => {
});
if (!result) {
return;
return undefined;
}
global = getGlobal();
if (result.users?.length) {
global = addUsers(global, buildCollectionByKey(result.users, 'id'));
}
const { nextOffset, count, reactions } = result;
setGlobal(updateChatMessage(global, chatId, messageId, {
return updateChatMessage(global, chatId, messageId, {
reactors: {
nextOffset,
count,
@ -264,8 +256,7 @@ addActionHandler('loadReactors', (global, actions, payload) => {
...reactions,
],
},
}));
})();
});
});
addActionHandler('loadMessageReactions', (global, actions, payload) => {

View File

@ -18,15 +18,14 @@ import {
} from '../../reducers';
import { isUserId } from '../../helpers';
addActionHandler('updateProfile', (global, actions, payload) => {
addActionHandler('updateProfile', async (global, actions, payload) => {
const {
photo, firstName, lastName, bio: about, username,
} = payload!;
(async () => {
const { currentUserId } = global;
if (!currentUserId) {
return;
return undefined;
}
setGlobal({
@ -70,22 +69,20 @@ addActionHandler('updateProfile', (global, actions, payload) => {
}
}
setGlobal({
return {
...getGlobal(),
profileEdit: {
progress: ProfileEditProgress.Complete,
},
});
})();
};
});
addActionHandler('checkUsername', (global, actions, payload) => {
addActionHandler('checkUsername', async (global, actions, payload) => {
const { username } = payload!;
(async () => {
// No need to check the username if profile update is already in progress
if (global.profileEdit && global.profileEdit.progress === ProfileEditProgress.InProgress) {
return;
return undefined;
}
setGlobal({
@ -99,35 +96,32 @@ addActionHandler('checkUsername', (global, actions, payload) => {
const isUsernameAvailable = await callApi('checkUsername', username);
global = getGlobal();
setGlobal({
return {
...global,
profileEdit: {
...global.profileEdit!,
isUsernameAvailable,
},
});
})();
};
});
addActionHandler('loadWallpapers', () => {
(async () => {
addActionHandler('loadWallpapers', async () => {
const result = await callApi('fetchWallpapers');
if (!result) {
return;
return undefined;
}
const global = getGlobal();
setGlobal({
return {
...global,
settings: {
...global.settings,
loadedWallpapers: result.wallpapers,
},
});
})();
};
});
addActionHandler('uploadWallpaper', (global, actions, payload) => {
addActionHandler('uploadWallpaper', async (global, actions, payload) => {
const file = payload;
const previewBlobUrl = URL.createObjectURL(file);
@ -150,22 +144,21 @@ addActionHandler('uploadWallpaper', (global, actions, payload) => {
},
});
(async () => {
const result = await callApi('uploadWallpaper', file);
if (!result) {
return;
return undefined;
}
const { wallpaper } = result;
global = getGlobal();
if (!global.settings.loadedWallpapers) {
return;
return undefined;
}
const firstWallpaper = global.settings.loadedWallpapers[0];
if (!firstWallpaper || firstWallpaper.slug !== UPLOADING_WALLPAPER_SLUG) {
return;
return undefined;
}
const withLocalMedia = {
@ -176,7 +169,7 @@ addActionHandler('uploadWallpaper', (global, actions, payload) => {
},
};
setGlobal({
return {
...global,
settings: {
...global.settings,
@ -185,56 +178,48 @@ addActionHandler('uploadWallpaper', (global, actions, payload) => {
...global.settings.loadedWallpapers.slice(1),
],
},
});
})();
};
});
addActionHandler('loadBlockedContacts', () => {
(async () => {
addActionHandler('loadBlockedContacts', async (global) => {
const result = await callApi('fetchBlockedContacts');
if (!result) {
return;
return undefined;
}
let newGlobal = getGlobal();
global = getGlobal();
if (result.users?.length) {
newGlobal = addUsers(newGlobal, buildCollectionByKey(result.users, 'id'));
global = addUsers(global, buildCollectionByKey(result.users, 'id'));
}
if (result.chats?.length) {
newGlobal = updateChats(newGlobal, buildCollectionByKey(result.chats, 'id'));
global = updateChats(global, buildCollectionByKey(result.chats, 'id'));
}
newGlobal = {
...newGlobal,
global = {
...global,
blocked: {
...newGlobal.blocked,
ids: [...(newGlobal.blocked.ids || []), ...result.blockedIds],
...global.blocked,
ids: [...(global.blocked.ids || []), ...result.blockedIds],
totalCount: result.totalCount,
},
};
setGlobal(newGlobal);
})();
return global;
});
addActionHandler('blockContact', (global, actions, payload) => {
addActionHandler('blockContact', async (global, actions, payload) => {
const { contactId, accessHash } = payload!;
(async () => {
const result = await callApi('blockContact', contactId, accessHash);
if (!result) {
return;
return undefined;
}
const newGlobal = getGlobal();
setGlobal(addBlockedContact(newGlobal, contactId));
})();
return addBlockedContact(getGlobal(), contactId);
});
addActionHandler('unblockContact', (global, actions, payload) => {
addActionHandler('unblockContact', async (global, actions, payload) => {
const { contactId } = payload!;
let accessHash: string | undefined;
const isPrivate = isUserId(contactId);
@ -242,152 +227,128 @@ addActionHandler('unblockContact', (global, actions, payload) => {
if (isPrivate) {
const user = selectUser(global, contactId);
if (!user) {
return;
return undefined;
}
accessHash = user.accessHash;
}
(async () => {
const result = await callApi('unblockContact', contactId, accessHash);
if (!result) {
return;
return undefined;
}
const newGlobal = getGlobal();
setGlobal(removeBlockedContact(newGlobal, contactId));
})();
return removeBlockedContact(getGlobal(), contactId);
});
addActionHandler('loadAuthorizations', () => {
(async () => {
addActionHandler('loadAuthorizations', async () => {
const result = await callApi('fetchAuthorizations');
if (!result) {
return;
return undefined;
}
setGlobal({
return {
...getGlobal(),
activeSessions: result,
});
})();
};
});
addActionHandler('terminateAuthorization', (global, actions, payload) => {
addActionHandler('terminateAuthorization', async (global, actions, payload) => {
const { hash } = payload!;
(async () => {
const result = await callApi('terminateAuthorization', hash);
if (!result) {
return;
return undefined;
}
const newGlobal = getGlobal();
global = getGlobal();
setGlobal({
...newGlobal,
activeSessions: newGlobal.activeSessions.filter((session) => session.hash !== hash),
});
})();
return {
...global,
activeSessions: global.activeSessions.filter((session) => session.hash !== hash),
};
});
addActionHandler('terminateAllAuthorizations', () => {
(async () => {
addActionHandler('terminateAllAuthorizations', async (global) => {
const result = await callApi('terminateAllAuthorizations');
if (!result) {
return;
return undefined;
}
const global = getGlobal();
global = getGlobal();
setGlobal({
return {
...global,
activeSessions: global.activeSessions.filter((session) => session.isCurrent),
});
})();
};
});
addActionHandler('loadNotificationExceptions', (global) => {
addActionHandler('loadNotificationExceptions', async (global) => {
const { serverTimeOffset } = global;
(async () => {
const result = await callApi('fetchNotificationExceptions', { serverTimeOffset });
if (!result) {
return;
return undefined;
}
setGlobal(addNotifyExceptions(getGlobal(), result));
})();
return addNotifyExceptions(getGlobal(), result);
});
addActionHandler('loadNotificationSettings', (global) => {
addActionHandler('loadNotificationSettings', async (global) => {
const { serverTimeOffset } = global;
(async () => {
const result = await callApi('fetchNotificationSettings', {
serverTimeOffset,
});
if (!result) {
return;
return undefined;
}
setGlobal(replaceSettings(getGlobal(), result));
})();
return replaceSettings(getGlobal(), result);
});
addActionHandler('updateNotificationSettings', (global, actions, payload) => {
addActionHandler('updateNotificationSettings', async (global, actions, payload) => {
const { peerType, isSilent, shouldShowPreviews } = payload!;
(async () => {
const result = await callApi('updateNotificationSettings', peerType, { isSilent, shouldShowPreviews });
if (!result) {
return;
return undefined;
}
setGlobal(updateNotifySettings(getGlobal(), peerType, isSilent, shouldShowPreviews));
})();
return updateNotifySettings(getGlobal(), peerType, isSilent, shouldShowPreviews);
});
addActionHandler('updateWebNotificationSettings', (global, actions, payload) => {
(async () => {
setGlobal(replaceSettings(getGlobal(), payload));
const newGlobal = getGlobal();
const { hasPushNotifications, hasWebNotifications } = newGlobal.settings.byKey;
setGlobal(replaceSettings(global, payload));
const { hasPushNotifications, hasWebNotifications } = global.settings.byKey;
if (hasWebNotifications && hasPushNotifications) {
await subscribe();
void subscribe();
} else {
await unsubscribe();
void unsubscribe();
}
})();
});
addActionHandler('updateContactSignUpNotification', (global, actions, payload) => {
addActionHandler('updateContactSignUpNotification', async (global, actions, payload) => {
const { isSilent } = payload!;
(async () => {
const result = await callApi('updateContactSignUpNotification', isSilent);
if (!result) {
return;
return undefined;
}
setGlobal(replaceSettings(getGlobal(), { hasContactJoinedNotifications: !isSilent }));
})();
return replaceSettings(getGlobal(), { hasContactJoinedNotifications: !isSilent });
});
addActionHandler('loadLanguages', () => {
(async () => {
addActionHandler('loadLanguages', async () => {
const result = await callApi('fetchLanguages');
if (!result) {
return;
return undefined;
}
setGlobal(replaceSettings(getGlobal(), { languages: result }));
})();
return replaceSettings(getGlobal(), { languages: result });
});
addActionHandler('loadPrivacySettings', () => {
(async () => {
addActionHandler('loadPrivacySettings', async (global) => {
const [
phoneNumberSettings, lastSeenSettings, profilePhotoSettings, forwardsSettings, chatInviteSettings,
] = await Promise.all([
@ -401,10 +362,10 @@ addActionHandler('loadPrivacySettings', () => {
if (
!phoneNumberSettings || !lastSeenSettings || !profilePhotoSettings || !forwardsSettings || !chatInviteSettings
) {
return;
return undefined;
}
const global = getGlobal();
global = getGlobal();
global.settings.privacy.phoneNumber = phoneNumberSettings;
global.settings.privacy.lastSeen = lastSeenSettings;
@ -412,11 +373,10 @@ addActionHandler('loadPrivacySettings', () => {
global.settings.privacy.forwards = forwardsSettings;
global.settings.privacy.chatInvite = chatInviteSettings;
setGlobal(global);
})();
return global;
});
addActionHandler('setPrivacyVisibility', (global, actions, payload) => {
addActionHandler('setPrivacyVisibility', async (global, actions, payload) => {
const { privacyKey, visibility } = payload!;
const {
@ -424,7 +384,7 @@ addActionHandler('setPrivacyVisibility', (global, actions, payload) => {
} = global.settings;
if (!settings) {
return;
return undefined;
}
const rules = buildInputPrivacyRules(global, {
@ -433,27 +393,33 @@ addActionHandler('setPrivacyVisibility', (global, actions, payload) => {
deniedIds: [...settings.blockUserIds, ...settings.blockChatIds],
});
(async () => {
const result = await callApi('setPrivacySettings', privacyKey, rules);
if (result) {
const newGlobal = getGlobal();
newGlobal.settings.privacy[privacyKey as ApiPrivacyKey] = result;
setGlobal(newGlobal);
if (!result) {
return undefined;
}
})();
global = getGlobal();
return {
...global,
settings: {
...global.settings,
privacy: {
...global.settings.privacy,
[privacyKey]: result,
},
},
};
});
addActionHandler('setPrivacySettings', (global, actions, payload) => {
addActionHandler('setPrivacySettings', async (global, actions, payload) => {
const { privacyKey, isAllowList, contactsIds } = payload!;
const {
privacy: { [privacyKey as ApiPrivacyKey]: settings },
} = global.settings;
if (!settings) {
return;
return undefined;
}
const rules = buildInputPrivacyRules(global, {
@ -462,17 +428,23 @@ addActionHandler('setPrivacySettings', (global, actions, payload) => {
deniedIds: !isAllowList ? contactsIds : [...settings.blockUserIds, ...settings.blockChatIds],
});
(async () => {
const result = await callApi('setPrivacySettings', privacyKey, rules);
if (result) {
const newGlobal = getGlobal();
newGlobal.settings.privacy[privacyKey as ApiPrivacyKey] = result;
setGlobal(newGlobal);
if (!result) {
return undefined;
}
})();
global = getGlobal();
return {
...global,
settings: {
...global.settings,
privacy: {
...global.settings.privacy,
[privacyKey]: result,
},
},
};
});
function buildInputPrivacyRules(global: GlobalState, {
@ -547,50 +519,45 @@ addActionHandler('updateIsOnline', (global, actions, payload) => {
callApi('updateIsOnline', payload);
});
addActionHandler('loadContentSettings', () => {
(async () => {
addActionHandler('loadContentSettings', async () => {
const result = await callApi('fetchContentSettings');
if (!result) return;
if (!result) return undefined;
setGlobal(replaceSettings(getGlobal(), result));
})();
return replaceSettings(getGlobal(), result);
});
addActionHandler('updateContentSettings', (global, actions, payload) => {
(async () => {
addActionHandler('updateContentSettings', async (global, actions, payload) => {
setGlobal(replaceSettings(getGlobal(), { isSensitiveEnabled: payload }));
const result = await callApi('updateContentSettings', payload);
if (!result) {
setGlobal(replaceSettings(getGlobal(), { isSensitiveEnabled: !payload }));
return replaceSettings(getGlobal(), { isSensitiveEnabled: !payload });
}
})();
return undefined;
});
addActionHandler('loadCountryList', (global, actions, payload = {}) => {
addActionHandler('loadCountryList', async (global, actions, payload = {}) => {
let { langCode } = payload;
if (!langCode) langCode = global.settings.byKey.language;
(async () => {
const countryList = await callApi('fetchCountryList', { langCode });
if (!countryList) return;
if (!countryList) return undefined;
setGlobal({
return {
...getGlobal(),
countryList,
});
})();
};
});
addActionHandler('ensureTimeFormat', (global, actions) => {
addActionHandler('ensureTimeFormat', async (global, actions) => {
if (global.authNearestCountry) {
const timeFormat = COUNTRIES_WITH_12H_TIME_FORMAT.has(global.authNearestCountry.toUpperCase()) ? '12h' : '24h';
actions.setSettingOption({ timeFormat });
setTimeFormat(timeFormat);
}
(async () => {
if (getGlobal().settings.byKey.wasTimeFormatSetManually) {
if (global.settings.byKey.wasTimeFormatSetManually) {
return;
}
@ -600,18 +567,14 @@ addActionHandler('ensureTimeFormat', (global, actions) => {
actions.setSettingOption({ timeFormat });
setTimeFormat(timeFormat);
}
})();
});
addActionHandler('loadAppConfig', () => {
(async () => {
addActionHandler('loadAppConfig', async () => {
const appConfig = await callApi('fetchAppConfig');
if (!appConfig) return undefined;
if (!appConfig) return;
setGlobal({
return {
...getGlobal(),
appConfig,
});
})();
};
});

View File

@ -1,21 +1,19 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { addActionHandler, getGlobal } from '../../index';
import { callApi } from '../../../api/gramjs';
import { updateStatistics, updateStatisticsGraph } from '../../reducers';
import { selectChatMessages, selectChat } from '../../selectors';
addActionHandler('loadStatistics', (global, actions, payload) => {
addActionHandler('loadStatistics', async (global, actions, payload) => {
const { chatId } = payload;
const chat = selectChat(global, chatId);
if (!chat?.fullInfo) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchStatistics', { chat });
if (!result) {
return;
return undefined;
}
global = getGlobal();
@ -29,27 +27,24 @@ addActionHandler('loadStatistics', (global, actions, payload) => {
global = updateStatistics(global, chatId, result);
setGlobal(global);
})();
return global;
});
addActionHandler('loadStatisticsAsyncGraph', (global, actions, payload) => {
addActionHandler('loadStatisticsAsyncGraph', async (global, actions, payload) => {
const {
chatId, token, name, isPercentage,
} = payload;
const chat = selectChat(global, chatId);
if (!chat?.fullInfo) {
return;
return undefined;
}
(async () => {
const dcId = chat.fullInfo!.statisticsDcId;
const result = await callApi('fetchStatisticsAsyncGraph', { token, dcId, isPercentage });
if (!result) {
return;
return undefined;
}
setGlobal(updateStatisticsGraph(getGlobal(), chatId, name, result));
})();
return updateStatisticsGraph(getGlobal(), chatId, name, result);
});

View File

@ -25,14 +25,13 @@ addActionHandler('loadStickerSets', (global) => {
void loadStickerSets(hash);
});
addActionHandler('loadAddedStickers', (global, actions) => {
addActionHandler('loadAddedStickers', async (global, actions) => {
const { setIds: addedSetIds } = global.stickers.added;
const cached = global.stickers.setsById;
if (!addedSetIds || !addedSetIds.length) {
return;
}
(async () => {
for (let i = 0; i < addedSetIds.length; i++) {
const id = addedSetIds[i];
if (cached[id].stickers) {
@ -44,7 +43,6 @@ addActionHandler('loadAddedStickers', (global, actions) => {
await pause(ADDED_SETS_THROTTLE);
}
}
})();
});
addActionHandler('loadRecentStickers', (global) => {
@ -57,29 +55,26 @@ addActionHandler('loadFavoriteStickers', (global) => {
void loadFavoriteStickers(hash);
});
addActionHandler('loadGreetingStickers', (global) => {
addActionHandler('loadGreetingStickers', async (global) => {
const { hash } = global.stickers.greeting || {};
(async () => {
const greeting = await callApi('fetchStickersForEmoji', { emoji: '👋⭐️', hash });
if (!greeting) {
return;
return undefined;
}
const newGlobal = getGlobal();
global = getGlobal();
setGlobal({
...newGlobal,
return {
...global,
stickers: {
...newGlobal.stickers,
...global.stickers,
greeting: {
hash: greeting.hash,
stickers: greeting.stickers.filter((sticker) => sticker.emoji === '👋'),
},
},
});
})();
};
});
addActionHandler('loadFeaturedStickers', (global) => {
@ -141,12 +136,12 @@ addActionHandler('toggleStickerSet', (global, actions, payload) => {
void callApi(!installedDate ? 'installStickerSet' : 'uninstallStickerSet', { stickerSetId, accessHash });
});
addActionHandler('loadEmojiKeywords', (global, actions, payload: { language: LangCode }) => {
addActionHandler('loadEmojiKeywords', async (global, actions, payload: { language: LangCode }) => {
const { language } = payload;
let currentEmojiKeywords = global.emojiKeywords[language];
if (currentEmojiKeywords?.isLoading) {
return;
return undefined;
}
setGlobal({
@ -160,7 +155,6 @@ addActionHandler('loadEmojiKeywords', (global, actions, payload: { language: Lan
},
});
(async () => {
const emojiKeywords = await callApi('fetchEmojiKeywords', {
language,
fromVersion: currentEmojiKeywords ? currentEmojiKeywords.version : 0,
@ -170,7 +164,7 @@ addActionHandler('loadEmojiKeywords', (global, actions, payload: { language: Lan
currentEmojiKeywords = global.emojiKeywords[language];
if (!emojiKeywords) {
setGlobal({
return {
...global,
emojiKeywords: {
...global.emojiKeywords,
@ -179,12 +173,10 @@ addActionHandler('loadEmojiKeywords', (global, actions, payload: { language: Lan
isLoading: false,
},
},
});
return;
};
}
setGlobal({
return {
...global,
emojiKeywords: {
...global.emojiKeywords,
@ -197,8 +189,7 @@ addActionHandler('loadEmojiKeywords', (global, actions, payload: { language: Lan
},
},
},
});
})();
};
});
async function loadStickerSets(hash?: string) {

View File

@ -3,26 +3,23 @@ import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { callApi } from '../../../api/gramjs';
import { replaceSettings, updateTwoFaSettings } from '../../reducers';
addActionHandler('loadPasswordInfo', () => {
(async () => {
addActionHandler('loadPasswordInfo', async (global) => {
const result = await callApi('getPasswordInfo');
if (!result) {
return;
return undefined;
}
let global = getGlobal();
global = getGlobal();
global = replaceSettings(global, { hasPassword: result.hasPassword });
global = updateTwoFaSettings(global, { hint: result.hint });
setGlobal(global);
})();
return global;
});
addActionHandler('checkPassword', (global, actions, payload) => {
addActionHandler('checkPassword', async (global, actions, payload) => {
const { currentPassword, onSuccess } = payload;
setGlobal(updateTwoFaSettings(global, { isLoading: true, error: undefined }));
(async () => {
const isSuccess = await callApi('checkPassword', currentPassword);
setGlobal(updateTwoFaSettings(getGlobal(), { isLoading: false }));
@ -30,15 +27,13 @@ addActionHandler('checkPassword', (global, actions, payload) => {
if (isSuccess) {
onSuccess();
}
})();
});
addActionHandler('clearPassword', (global, actions, payload) => {
addActionHandler('clearPassword', async (global, actions, payload) => {
const { currentPassword, onSuccess } = payload;
setGlobal(updateTwoFaSettings(global, { isLoading: true, error: undefined }));
(async () => {
const isSuccess = await callApi('clearPassword', currentPassword);
setGlobal(updateTwoFaSettings(getGlobal(), { isLoading: false }));
@ -46,17 +41,15 @@ addActionHandler('clearPassword', (global, actions, payload) => {
if (isSuccess) {
onSuccess();
}
})();
});
addActionHandler('updatePassword', (global, actions, payload) => {
addActionHandler('updatePassword', async (global, actions, payload) => {
const {
currentPassword, password, hint, email, onSuccess,
} = payload;
setGlobal(updateTwoFaSettings(global, { isLoading: true, error: undefined }));
(async () => {
const isSuccess = await callApi('updatePassword', currentPassword, password, hint, email);
setGlobal(updateTwoFaSettings(getGlobal(), { isLoading: false }));
@ -64,17 +57,15 @@ addActionHandler('updatePassword', (global, actions, payload) => {
if (isSuccess) {
onSuccess();
}
})();
});
addActionHandler('updateRecoveryEmail', (global, actions, payload) => {
addActionHandler('updateRecoveryEmail', async (global, actions, payload) => {
const {
currentPassword, email, onSuccess,
} = payload;
setGlobal(updateTwoFaSettings(global, { isLoading: true, error: undefined }));
(async () => {
const isSuccess = await callApi('updateRecoveryEmail', currentPassword, email);
setGlobal(updateTwoFaSettings(getGlobal(), { isLoading: false, waitingEmailCodeLength: undefined }));
@ -82,7 +73,6 @@ addActionHandler('updateRecoveryEmail', (global, actions, payload) => {
if (isSuccess) {
onSuccess();
}
})();
});
addActionHandler('provideTwoFaEmailCode', (global, actions, payload) => {

View File

@ -32,17 +32,16 @@ addActionHandler('loadFullUser', (global, actions, payload) => {
runDebouncedForFetchFullUser(() => callApi('fetchFullUser', { id, accessHash }));
});
addActionHandler('loadUser', (global, actions, payload) => {
addActionHandler('loadUser', async (global, actions, payload) => {
const { userId } = payload!;
const user = selectUser(global, userId);
if (!user) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchUsers', { users: [user] });
if (!result) {
return;
return undefined;
}
const { users, userStatusesById } = result;
@ -50,13 +49,12 @@ addActionHandler('loadUser', (global, actions, payload) => {
global = getGlobal();
global = updateUsers(global, buildCollectionByKey(users, 'id'));
setGlobal(replaceUserStatuses(global, {
global = replaceUserStatuses(global, {
...global.users.statusesById,
...userStatusesById,
}));
});
setGlobal(global);
})();
return global;
});
addActionHandler('loadTopUsers', (global) => {
@ -75,18 +73,17 @@ addActionHandler('loadCurrentUser', () => {
void callApi('fetchCurrentUser');
});
addActionHandler('loadCommonChats', (global) => {
addActionHandler('loadCommonChats', async (global) => {
const { chatId } = selectCurrentMessageList(global) || {};
const user = chatId ? selectUser(global, chatId) : undefined;
if (!user || isUserBot(user) || user.commonChats?.isFullyLoaded) {
return;
return undefined;
}
(async () => {
const maxId = user.commonChats?.maxId;
const result = await callApi('fetchCommonChats', user.id, user.accessHash!, maxId);
if (!result) {
return;
return undefined;
}
const { chats, chatIds, isFullyLoaded } = result;
@ -102,8 +99,8 @@ addActionHandler('loadCommonChats', (global) => {
isFullyLoaded,
},
});
setGlobal(global);
})();
return global;
});
addActionHandler('updateContact', (global, actions, payload) => {
@ -223,32 +220,31 @@ async function deleteContact(userId: string) {
await callApi('deleteContact', { id, accessHash });
}
addActionHandler('loadProfilePhotos', (global, actions, payload) => {
addActionHandler('loadProfilePhotos', async (global, actions, payload) => {
const { profileId } = payload!;
const isPrivate = isUserId(profileId);
const user = isPrivate ? selectUser(global, profileId) : undefined;
const chat = !isPrivate ? selectChat(global, profileId) : undefined;
if (!user && !chat) {
return;
return undefined;
}
(async () => {
const result = await callApi('fetchProfilePhotos', user, chat);
if (!result || !result.photos) {
return;
return undefined;
}
let newGlobal = getGlobal();
global = getGlobal();
if (isPrivate) {
newGlobal = updateUser(newGlobal, profileId, { photos: result.photos });
global = updateUser(global, profileId, { photos: result.photos });
} else {
newGlobal = addUsers(newGlobal, buildCollectionByKey(result.users!, 'id'));
newGlobal = updateChat(newGlobal, profileId, { photos: result.photos });
global = addUsers(global, buildCollectionByKey(result.users!, 'id'));
global = updateChat(global, profileId, { photos: result.photos });
}
setGlobal(newGlobal);
})();
return global;
});
addActionHandler('setUserSearchQuery', (global, actions, payload) => {

View File

@ -1,6 +1,6 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { ApiUpdate, MAIN_THREAD_ID } from '../../../api/types';
import { MAIN_THREAD_ID } from '../../../api/types';
import { ARCHIVED_FOLDER_ID, MAX_ACTIVE_PINNED_CHATS } from '../../../config';
import { pick } from '../../../util/iteratees';
@ -25,7 +25,7 @@ const TYPING_STATUS_CLEAR_DELAY = 6000; // 6 seconds
// Enough to animate and mark as read in Message List
const CURRENT_CHAT_UNREAD_DELAY = 1500;
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updateChat': {
if (!update.noTopChatsRequest && !selectIsChatListed(global, update.id)) {
@ -33,8 +33,7 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
actions.loadTopChats();
}
const newGlobal = updateChat(global, update.id, update.chat, update.newProfilePhoto);
setGlobal(newGlobal);
setGlobal(updateChat(global, update.id, update.chat, update.newProfilePhoto));
if (update.chat.id) {
closeMessageNotifications({
@ -42,13 +41,14 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
lastReadInboxMessageId: update.chat.lastReadInboxMessageId,
});
}
break;
return undefined;
}
case 'updateChatJoin': {
const listType = selectChatListType(global, update.id);
if (!listType) {
break;
return undefined;
}
global = updateChatListIds(global, listType, [update.id]);
@ -59,19 +59,16 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
if (chat) {
actions.requestChatUpdate({ chatId: chat.id });
}
break;
return undefined;
}
case 'updateChatLeave': {
setGlobal(leaveChat(global, update.id));
break;
return leaveChat(global, update.id);
}
case 'updateChatInbox': {
setGlobal(updateChat(global, update.id, update.chat));
break;
return updateChat(global, update.id, update.chat);
}
case 'updateChatTypingStatus': {
@ -79,14 +76,14 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
setGlobal(updateChat(global, id, { typingStatus }));
setTimeout(() => {
const newGlobal = getGlobal();
const chat = selectChat(newGlobal, id);
global = getGlobal();
const chat = selectChat(global, id);
if (chat && typingStatus && chat.typingStatus && chat.typingStatus.timestamp === typingStatus.timestamp) {
setGlobal(updateChat(newGlobal, id, { typingStatus: undefined }));
setGlobal(updateChat(global, id, { typingStatus: undefined }));
}
}, TYPING_STATUS_CLEAR_DELAY);
break;
return undefined;
}
case 'newMessage': {
@ -94,12 +91,12 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
const { chatId: currentChatId, threadId, type: messageListType } = selectCurrentMessageList(global) || {};
if (message.senderId === global.currentUserId && !message.isFromScheduled) {
return;
return undefined;
}
const chat = selectChat(global, update.chatId);
if (!chat) {
return;
return undefined;
}
const isActiveChat = (
@ -126,14 +123,14 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
message,
});
break;
return undefined;
}
case 'updateMessage': {
const { message } = update;
const chat = selectChat(global, update.chatId);
if (!chat) {
return;
return undefined;
}
if (getMessageRecentReaction(message)) {
@ -142,14 +139,15 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
message,
});
}
break;
return undefined;
}
case 'updateCommonBoxMessages':
case 'updateChannelMessages': {
const { ids, messageUpdate } = update;
if (messageUpdate.hasUnreadMention !== false) {
return;
return undefined;
}
ids.forEach((id) => {
@ -162,34 +160,29 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
}
});
setGlobal(global);
break;
return global;
}
case 'updateChatFullInfo': {
const { fullInfo } = update;
const targetChat = global.chats.byId[update.id];
if (!targetChat) {
return;
return undefined;
}
setGlobal(updateChat(global, update.id, {
return updateChat(global, update.id, {
fullInfo: {
...targetChat.fullInfo,
...fullInfo,
},
}));
break;
});
}
case 'updatePinnedChatIds': {
const { ids, folderId } = update;
const listType = folderId === ARCHIVED_FOLDER_ID ? 'archived' : 'active';
global = {
return {
...global,
chats: {
...global.chats,
@ -199,16 +192,15 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
},
},
};
setGlobal(global);
break;
}
case 'updateChatPinned': {
const { id, isPinned } = update;
const listType = selectChatListType(global, id);
if (listType) {
if (!listType) {
return undefined;
}
const { [listType]: orderedPinnedIds } = global.chats.orderedPinnedIds;
let newOrderedPinnedIds = orderedPinnedIds || [];
@ -227,7 +219,7 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
newOrderedPinnedIds = [id, ...newOrderedPinnedIds];
}
global = {
return {
...global,
chats: {
...global.chats,
@ -239,17 +231,10 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
};
}
setGlobal(global);
break;
}
case 'updateChatListType': {
const { id, folderId } = update;
setGlobal(updateChatListType(global, id, folderId));
break;
return updateChatListType(global, id, folderId);
}
case 'updateChatFolder': {
@ -267,51 +252,45 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
? orderedIds && orderedIds.includes(id) ? orderedIds : [...(orderedIds || []), id]
: orderedIds ? orderedIds.filter((orderedId) => orderedId !== id) : undefined;
setGlobal({
return {
...global,
chatFolders: {
...global.chatFolders,
byId: newChatFoldersById,
orderedIds: newOrderedIds,
},
});
break;
};
}
case 'updateChatFoldersOrder': {
const { orderedIds } = update;
setGlobal({
return {
...global,
chatFolders: {
...global.chatFolders,
orderedIds,
},
});
break;
};
}
case 'updateRecommendedChatFolders': {
const { folders } = update;
setGlobal({
return {
...global,
chatFolders: {
...global.chatFolders,
recommended: folders,
},
});
break;
};
}
case 'updateChatMembers': {
const targetChat = global.chats.byId[update.id];
const { replacedMembers, addedMember, deletedMemberId } = update;
if (!targetChat) {
return;
return undefined;
}
let shouldUpdate = false;
@ -342,17 +321,17 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
const adminMembers = members.filter(({ isOwner, isAdmin }) => isOwner || isAdmin);
// TODO Kicked members?
setGlobal(updateChat(global, update.id, {
return updateChat(global, update.id, {
membersCount: members.length,
fullInfo: {
...targetChat.fullInfo,
members,
adminMembers,
},
}));
});
}
break;
return undefined;
}
case 'deleteProfilePhotos': {
@ -360,11 +339,12 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
const chat = global.chats.byId[chatId];
if (chat?.photos) {
setGlobal(updateChat(global, chatId, {
return updateChat(global, chatId, {
photos: chat.photos.filter((photo) => !ids.includes(photo.id)),
}));
});
}
break;
return undefined;
}
case 'draftMessage': {
@ -372,28 +352,31 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
chatId, formattedText, date, replyingToId,
} = update;
const chat = global.chats.byId[chatId];
if (!chat) {
return undefined;
}
if (chat) {
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'draft', formattedText);
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'replyingToId', replyingToId);
global = updateChat(global, chatId, { draftDate: date });
setGlobal(global);
}
break;
return global;
}
case 'showInvite': {
const { data } = update;
actions.showDialog({ data });
break;
return undefined;
}
case 'updatePendingJoinRequests': {
const { chatId, requestsPending, recentRequesterIds } = update;
const chat = global.chats.byId[chatId];
if (chat) {
if (!chat) {
return undefined;
}
global = updateChat(global, chatId, {
fullInfo: {
...chat.fullInfo,
@ -402,8 +385,10 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
},
});
setGlobal(global);
actions.loadChatJoinRequests({ chatId });
}
}
}
return undefined;
});

View File

@ -5,7 +5,6 @@ import {
import { GlobalState } from '../../types';
import {
ApiUpdate,
ApiUpdateAuthorizationState,
ApiUpdateAuthorizationError,
ApiUpdateConnectionState,
@ -20,7 +19,7 @@ import { selectNotifySettings } from '../../selectors';
import { forceWebsync } from '../../../util/websync';
import { getShippingError } from '../../../util/getReadableErrorText';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
if (DEBUG) {
if (update['@type'] !== 'updateUserStatus' && update['@type'] !== 'updateServerTimeOffset') {
// eslint-disable-next-line no-console

View File

@ -1,13 +1,12 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import {
ApiUpdate, ApiMessage, ApiPollResult, ApiThreadInfo, MAIN_THREAD_ID,
ApiMessage, ApiPollResult, ApiThreadInfo, MAIN_THREAD_ID,
} from '../../../api/types';
import { unique } from '../../../util/iteratees';
import { areDeepEqual } from '../../../util/areDeepEqual';
import { notifyAboutMessage } from '../../../util/notifications';
import { checkIfReactionAdded } from '../../helpers/reactions';
import {
updateChat,
deleteChatMessages,
@ -34,7 +33,7 @@ import {
selectPinnedIds,
selectScheduledMessage,
selectScheduledMessages,
isMessageInCurrentMessageList,
selectIsMessageInCurrentMessageList,
selectScheduledIds,
selectCurrentMessageList,
selectViewportIds,
@ -46,12 +45,12 @@ import {
selectLocalAnimatedEmoji,
} from '../../selectors';
import {
getMessageContent, isUserId, isMessageLocal, getMessageText,
getMessageContent, isUserId, isMessageLocal, getMessageText, checkIfReactionAdded,
} from '../../helpers';
const ANIMATION_DELAY = 350;
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'newMessage': {
const {
@ -73,7 +72,7 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
const newMessage = selectChatMessage(global, chatId, id)!;
if (isMessageInCurrentMessageList(global, chatId, message as ApiMessage)) {
if (selectIsMessageInCurrentMessageList(global, chatId, message as ApiMessage)) {
if (message.isOutgoing && !(message.content?.action)) {
const currentMessageList = selectCurrentMessageList(global);
if (currentMessageList) {
@ -186,7 +185,7 @@ addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
&& !message.isOutgoing
&& chat.lastMessage?.id === message.id
&& selectIsChatWithBot(global, chat)
&& isMessageInCurrentMessageList(global, chatId, message as ApiMessage)
&& selectIsMessageInCurrentMessageList(global, chatId, message as ApiMessage)
&& selectIsViewportNewest(global, chatId, message.threadInfo?.threadId || MAIN_THREAD_ID)
) {
actions.focusLastMessage();
@ -602,11 +601,11 @@ function updateListedAndViewportIds(global: GlobalState, actions: GlobalActions,
if (selectIsViewportNewest(global, chatId, MAIN_THREAD_ID)) {
// Always keep the first unread message in the viewport list
const firstUnreadId = selectFirstUnreadId(global, chatId, MAIN_THREAD_ID);
const newGlobal = addViewportId(global, chatId, MAIN_THREAD_ID, id);
const newViewportIds = selectViewportIds(newGlobal, chatId, MAIN_THREAD_ID);
const candidateGlobal = addViewportId(global, chatId, MAIN_THREAD_ID, id);
const newViewportIds = selectViewportIds(candidateGlobal, chatId, MAIN_THREAD_ID);
if (!firstUnreadId || newViewportIds!.includes(firstUnreadId)) {
global = newGlobal;
global = candidateGlobal;
}
}

View File

@ -1,13 +1,12 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { ApiUpdate } from '../../../api/types';
import { ApiPrivacyKey, PaymentStep } from '../../../types';
import {
addBlockedContact, removeBlockedContact, setConfirmPaymentUrl, setPaymentStep,
} from '../../reducers';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updatePeerBlocked':
if (update.isBlocked) {

View File

@ -1,10 +1,8 @@
import { addActionHandler } from '../../index';
import { ApiUpdate } from '../../../api/types';
import { clearPayment } from '../../reducers';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updatePaymentStateCompleted': {
return clearPayment(global);

View File

@ -1,10 +1,8 @@
import { addActionHandler, setGlobal } from '../../index';
import { ApiUpdate } from '../../../api/types';
import { GlobalState } from '../../types';
import { addNotifyException, updateChat, updateNotifySettings } from '../../reducers';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate): GlobalState | undefined => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updateNotifySettings': {
return updateNotifySettings(global, update.peerType, update.isSilent, update.shouldShowPreviews);

View File

@ -1,10 +1,8 @@
import { addActionHandler } from '../../index';
import { ApiUpdate } from '../../../api/types';
import { updateStickerSet } from '../../reducers';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updateStickerSet': {
return updateStickerSet(global, update.id, update.stickerSet);

View File

@ -1,9 +1,6 @@
import { addActionHandler } from '../../index';
import { ApiUpdate } from '../../../api/types';
import { GlobalState } from '../../types';
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate): GlobalState | undefined => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'updateTwoFaStateWaitCode': {
return {

View File

@ -1,6 +1,6 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import { ApiUpdate, ApiUserStatus } from '../../../api/types';
import { ApiUserStatus } from '../../../api/types';
import { deleteContact, replaceUserStatuses, updateUser } from '../../reducers';
import { throttle } from '../../../util/schedulers';
@ -27,7 +27,7 @@ function flushStatusUpdates() {
pendingStatusUpdates = {};
}
addActionHandler('apiUpdate', (global, actions, update: ApiUpdate) => {
addActionHandler('apiUpdate', (global, actions, update) => {
switch (update['@type']) {
case 'deleteContact': {
return deleteContact(global, update.id);

View File

@ -102,13 +102,12 @@ addActionHandler('toggleGroupCallPanel', (global) => {
};
});
addActionHandler('subscribeToGroupCallUpdates', (global, actions, payload) => {
addActionHandler('subscribeToGroupCallUpdates', async (global, actions, payload) => {
const { subscribed, id } = payload!;
const groupCall = selectGroupCall(global, id);
if (!groupCall) return;
(async () => {
if (subscribed) {
await fetchGroupCall(groupCall);
await fetchGroupCallParticipants(groupCall);
@ -118,10 +117,9 @@ addActionHandler('subscribeToGroupCallUpdates', (global, actions, payload) => {
subscribed,
call: groupCall,
});
})();
});
addActionHandler('createGroupCall', (global, actions, payload) => {
addActionHandler('createGroupCall', async (global, actions, payload) => {
const { chatId } = payload;
const chat = selectChat(global, chatId);
@ -129,7 +127,6 @@ addActionHandler('createGroupCall', (global, actions, payload) => {
return;
}
(async () => {
const result = await callApi('createGroupCall', {
peer: chat,
});
@ -143,10 +140,9 @@ addActionHandler('createGroupCall', (global, actions, payload) => {
}));
actions.joinGroupCall({ id: result.id, accessHash: result.accessHash });
})();
});
addActionHandler('createGroupCallInviteLink', (global, actions) => {
addActionHandler('createGroupCallInviteLink', async (global, actions) => {
const groupCall = selectActiveGroupCall(global);
if (!groupCall || !groupCall.chatId) {
@ -160,7 +156,6 @@ addActionHandler('createGroupCallInviteLink', (global, actions) => {
const canInvite = Boolean(chat.username);
(async () => {
let { inviteLink } = chat.fullInfo!;
if (canInvite) {
inviteLink = await callApi('exportGroupCallInvite', {
@ -177,13 +172,11 @@ addActionHandler('createGroupCallInviteLink', (global, actions) => {
actions.showNotification({
message: 'Link copied to clipboard',
});
})();
});
addActionHandler('joinVoiceChatByLink', (global, actions, payload) => {
addActionHandler('joinVoiceChatByLink', async (global, actions, payload) => {
const { username, inviteHash } = payload!;
(async () => {
const chat = await fetchChatByUsername(username);
if (!chat) {
@ -196,11 +189,10 @@ addActionHandler('joinVoiceChatByLink', (global, actions, payload) => {
if (full?.groupCall) {
actions.joinGroupCall({ id: full.groupCall.id, accessHash: full.groupCall.accessHash, inviteHash });
}
})();
});
addActionHandler('joinGroupCall', (global, actions, payload) => {
if (!ARE_CALLS_SUPPORTED) return;
addActionHandler('joinGroupCall', async (global, actions, payload) => {
if (!ARE_CALLS_SUPPORTED) return undefined;
const {
chatId, id, accessHash, inviteHash,
@ -208,26 +200,25 @@ addActionHandler('joinGroupCall', (global, actions, payload) => {
createAudioElement();
(async () => {
await initializeSoundsForSafari();
const { groupCalls: { activeGroupCallId } } = global;
let groupCall = id ? selectGroupCall(global, id) : selectChatGroupCall(global, chatId);
if (groupCall?.id === activeGroupCallId) {
actions.toggleGroupCallPanel();
return;
return undefined;
}
if (activeGroupCallId) {
actions.leaveGroupCall({
rejoin: payload,
});
return;
return undefined;
}
if (groupCall && activeGroupCallId === groupCall.id) {
actions.toggleGroupCallPanel();
return;
return undefined;
}
if (!groupCall && (!id || !accessHash)) {
@ -237,10 +228,9 @@ addActionHandler('joinGroupCall', (global, actions, payload) => {
});
}
if (!groupCall) return;
if (!groupCall) return undefined;
global = getGlobal();
global = updateGroupCall(
global,
groupCall.id,
@ -251,16 +241,15 @@ addActionHandler('joinGroupCall', (global, actions, payload) => {
undefined,
groupCall.participantsCount + 1,
);
setGlobal({
global = {
...global,
groupCalls: {
...global.groupCalls,
activeGroupCallId: groupCall.id,
isGroupCallPanelHidden: false,
},
});
})();
};
return global;
});
addActionHandler('playGroupCallSound', (global, actions, payload) => {

View File

@ -265,10 +265,10 @@ addActionHandler('openPollResults', (global, actions, payload) => {
if (!shouldOpenInstantly) {
window.setTimeout(() => {
const newGlobal = getGlobal();
global = getGlobal();
setGlobal({
...newGlobal,
...global,
pollResults: {
chatId,
messageId,
@ -277,22 +277,24 @@ addActionHandler('openPollResults', (global, actions, payload) => {
});
}, POLL_RESULT_OPEN_DELAY_MS);
} else if (chatId !== global.pollResults.chatId || messageId !== global.pollResults.messageId) {
setGlobal({
return {
...global,
pollResults: {
chatId,
messageId,
voters: {},
},
});
};
}
return undefined;
});
addActionHandler('closePollResults', (global) => {
setGlobal({
return {
...global,
pollResults: {},
});
};
});
addActionHandler('focusLastMessage', (global, actions) => {

View File

@ -1,6 +1,5 @@
import { addActionHandler } from '../../index';
import { GlobalState } from '../../types';
import { ApiError } from '../../../api/types';
import { IS_SINGLE_COLUMN_LAYOUT, IS_TABLET_COLUMN_LAYOUT } from '../../../util/environment';
@ -33,7 +32,7 @@ addActionHandler('resetLeftColumnWidth', (global) => {
};
});
addActionHandler('toggleManagement', (global): GlobalState | undefined => {
addActionHandler('toggleManagement', (global) => {
const { chatId } = selectCurrentMessageList(global) || {};
if (!chatId) {
@ -54,7 +53,7 @@ addActionHandler('toggleManagement', (global): GlobalState | undefined => {
};
});
addActionHandler('requestNextManagementScreen', (global, actions, payload): GlobalState | undefined => {
addActionHandler('requestNextManagementScreen', (global, actions, payload) => {
const { screen } = payload || {};
const { chatId } = selectCurrentMessageList(global) || {};
@ -77,7 +76,7 @@ addActionHandler('requestNextManagementScreen', (global, actions, payload): Glob
};
});
addActionHandler('closeManagement', (global): GlobalState | undefined => {
addActionHandler('closeManagement', (global) => {
const { chatId } = selectCurrentMessageList(global) || {};
if (!chatId) {

View File

@ -16,8 +16,9 @@ addActionHandler('openPaymentModal', (global, actions, payload) => {
});
addActionHandler('closePaymentModal', (global) => {
const newGlobal = clearPayment(global);
return closeInvoice(newGlobal);
global = clearPayment(global);
global = closeInvoice(global);
return global;
});
addActionHandler('addPaymentError', (global, actions, payload) => {

View File

@ -202,7 +202,7 @@ export function selectThreadByMessage(global: GlobalState, chatId: string, messa
});
}
export function isMessageInCurrentMessageList(global: GlobalState, chatId: string, message: ApiMessage) {
export function selectIsMessageInCurrentMessageList(global: GlobalState, chatId: string, message: ApiMessage) {
const currentMessageList = selectCurrentMessageList(global);
if (!currentMessageList) {
return false;

View File

@ -29,7 +29,7 @@ type ActionHandler = (
global: GlobalState,
actions: Actions,
payload: any,
) => GlobalState | void;
) => GlobalState | void | Promise<GlobalState | void>;
type MapStateToProps<OwnProps = undefined> = ((global: GlobalState, ownProps: OwnProps) => AnyLiteral);
@ -80,11 +80,21 @@ export function getActions() {
function handleAction(name: string, payload?: ActionPayload, options?: ActionOptions) {
actionHandlers[name]?.forEach((handler) => {
const newGlobal = handler(currentGlobal, actions, payload);
const response = handler(currentGlobal, actions, payload);
if (!response) {
return;
}
if (typeof response.then === 'function') {
response.then((newGlobal: GlobalState | void) => {
if (newGlobal) {
setGlobal(newGlobal, options);
}
});
} else {
setGlobal(response, options);
}
});
}
function updateContainers() {
@ -244,7 +254,7 @@ export function typify<ProjectGlobalState, ActionPayloads, NonTypedActionNames e
global: ProjectGlobalState,
actions: ProjectActions,
payload: ProjectActionTypes[ActionName],
) => ProjectGlobalState | void;
) => ProjectGlobalState | void | Promise<ProjectGlobalState | void>;
};
return {