Forums: Various fixes 4 (#2254)

This commit is contained in:
Alexander Zinchuk 2023-01-10 02:07:55 +01:00
parent b4f283d207
commit 941d5c8919
6 changed files with 33 additions and 13 deletions

View File

@ -1359,6 +1359,8 @@ export function buildLocalForwardedMessage({
isInAlbum,
isForwardingAllowed: true,
replyToTopMessageId: toThreadId,
...(toThreadId && toChat?.isForum && { isTopicReply: true }),
...(emojiOnlyCount && { emojiOnlyCount }),
// Forward info doesn't get added when users forwards his own messages, also when forwarding audio
...(message.chatId !== currentUserId && !isAudio && !noAuthors && {

View File

@ -41,6 +41,7 @@ export interface ApiChat {
fakeType?: ApiFakeType;
isForum?: boolean;
topics?: Record<number, ApiTopic>;
listedTopicIds?: number[];
topicsCount?: number;
orderedPinnedTopicIds?: number[];

View File

@ -198,7 +198,7 @@ const Chat: FC<OwnProps & StateProps> = ({
// Load the forum topics to display unread count badge
useEffect(() => {
if (isIntersecting && lastSyncTime && isForum && chat && chat.topics === undefined) {
if (isIntersecting && lastSyncTime && isForum && chat && chat.listedTopicIds === undefined) {
loadTopics({ chatId });
}
}, [chat, chatId, isForum, isIntersecting, lastSyncTime, loadTopics]);

View File

@ -41,7 +41,7 @@ import {
updateTopics,
deleteTopic,
updateTopic,
updateThreadInfo,
updateThreadInfo, updateListedTopicIds,
} from '../../reducers';
import {
selectChat, selectUser, selectChatListType, selectIsChatPinned,
@ -1338,18 +1338,22 @@ addActionHandler('loadTopics', async (global, actions, payload) => {
const chat = selectChat(global, chatId);
if (!chat) return;
if (!force && chat.topics && Object.values(chat.topics).length === chat.topicsCount) {
if (!force && chat.listedTopicIds && chat.listedTopicIds.length === chat.topicsCount) {
return;
}
const offsetTopic = !force && chat.topics ? Object.values(chat.topics).reduce((acc, el) => {
if (!acc || el.lastMessageId < acc.lastMessageId) {
const offsetTopic = !force && chat.listedTopicIds ? chat.listedTopicIds.reduce((acc, el) => {
const topic = chat.topics?.[el];
const accTopic = chat.topics?.[acc];
if (!topic) return acc;
if (!accTopic || topic.lastMessageId < accTopic.lastMessageId) {
return el;
}
return acc;
}) : undefined;
const { id: offsetTopicId, date: offsetDate, lastMessageId: offsetId } = offsetTopic || {};
const { id: offsetTopicId, date: offsetDate, lastMessageId: offsetId } = (offsetTopic
&& chat.topics?.[offsetTopic]) || {};
const result = await callApi('fetchTopics', {
chat, offsetTopicId, offsetId, offsetDate, limit: offsetTopicId ? TOPICS_SLICE : TOPICS_SLICE_SECOND_LOAD,
});
@ -1361,6 +1365,7 @@ addActionHandler('loadTopics', async (global, actions, payload) => {
global = addChats(global, buildCollectionByKey(result.chats, 'id'));
global = addMessages(global, result.messages);
global = updateTopics(global, chatId, result.count, result.topics);
global = updateListedTopicIds(global, chatId, result.topics.map((topic) => topic.id));
Object.entries(result.draftsById || {}).forEach(([threadId, draft]) => {
global = replaceThreadParam(global, chatId, Number(threadId), 'draft', draft?.formattedText);
global = replaceThreadParam(global, chatId, Number(threadId), 'replyingToId', draft?.replyingToId);

View File

@ -769,6 +769,13 @@ function updateChatLastMessage(
const chat = chats.byId[chatId];
const currentLastMessage = chat?.lastMessage;
const topic = chat?.isForum ? selectTopicFromMessage(global, message) : undefined;
if (topic) {
global = updateTopic(global, chatId, topic.id, {
lastMessageId: message.id,
});
}
if (currentLastMessage && !force) {
const isSameOrNewer = (
currentLastMessage.id === message.id || currentLastMessage.id === message.previousLocalId
@ -780,12 +787,6 @@ function updateChatLastMessage(
}
global = updateChat(global, chatId, { lastMessage: message });
const topic = chat?.isForum ? selectTopicFromMessage(global, message) : undefined;
if (topic) {
global = updateTopic(global, chatId, topic.id, {
lastMessageId: message.id,
});
}
return global;
}

View File

@ -5,7 +5,7 @@ import type {
import { ARCHIVED_FOLDER_ID } from '../../config';
import {
areSortedArraysEqual, buildCollectionByKey, omit,
areSortedArraysEqual, buildCollectionByKey, omit, unique,
} from '../../util/iteratees';
import { selectChat, selectChatListType } from '../selectors';
import { updateThread, updateThreadInfo } from './messages';
@ -252,6 +252,17 @@ export function addChatMembers(global: GlobalState, chat: ApiChat, membersToAdd:
});
}
export function updateListedTopicIds(
global: GlobalState, chatId: string, topicIds: number[],
): GlobalState {
return updateChat(global, chatId, {
listedTopicIds: unique([
...(global.chats.byId[chatId]?.listedTopicIds || []),
...topicIds,
]),
});
}
export function updateTopics(
global: GlobalState, chatId: string, topicsCount: number, topics: ApiTopic[],
): GlobalState {