[MM-60236] Remove ephemeral posts on refresh/during initial load (#8260)

* fix: remove ephemeral posts on refresh

* fix: remove ephemeral posts during app launch

* fix: remove code to await the cleanup

* use built in constant instead of string `system_ephemeral`

* remove sort from `queryPostsByType()`

* switch back to the `await` approach in `cleanupEphemeralPosts`

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
TheoForger 2024-11-01 11:28:06 -04:00 committed by GitHub
parent 2a02a61b6b
commit ece82d75ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import React, {type ReactElement, useCallback, useEffect, useMemo, useRef, useSt
import {DeviceEventEmitter, type ListRenderItemInfo, Platform, type StyleProp, StyleSheet, type ViewStyle, type NativeSyntheticEvent, type NativeScrollEvent} from 'react-native';
import Animated, {type AnimatedStyle} from 'react-native-reanimated';
import {removePost} from '@actions/local/post';
import {fetchPosts, fetchPostThread} from '@actions/remote/post';
import CombinedUserActivity from '@components/post_list/combined_user_activity';
import DateSeparator from '@components/post_list/date_separator';
@ -13,6 +14,7 @@ import NewMessagesLine from '@components/post_list/new_message_line';
import Post from '@components/post_list/post';
import ThreadOverview from '@components/post_list/thread_overview';
import {Events, Screens} from '@constants';
import {PostTypes} from '@constants/post';
import {useServerUrl} from '@context/server';
import {useTheme} from '@context/theme';
import {getDateForDateLine, preparePostList} from '@utils/post_list';
@ -172,6 +174,10 @@ const PostList = ({
}
await fetchPostThread(serverUrl, rootId, options);
}
const removalPromises = posts.
filter((post) => post.type === PostTypes.EPHEMERAL).
map((post) => removePost(serverUrl, post));
await Promise.all(removalPromises);
setRefreshing(false);
}, [channelId, location, posts, rootId, disablePullToRefresh]);

View file

@ -5,15 +5,19 @@ import Emm from '@mattermost/react-native-emm';
import {Alert, AppState, DeviceEventEmitter, Linking, Platform} from 'react-native';
import {Notifications} from 'react-native-notifications';
import {removePost} from '@actions/local/post';
import {switchToChannelById} from '@actions/remote/channel';
import {appEntry, pushNotificationEntry, upgradeEntry} from '@actions/remote/entry';
import {fetchAndSwitchToThread} from '@actions/remote/thread';
import LocalConfig from '@assets/config.json';
import {DeepLink, Events, Launch, PushNotification} from '@constants';
import {PostTypes} from '@constants/post';
import DatabaseManager from '@database/manager';
import {getActiveServerUrl, getServerCredentials, removeServerCredentials} from '@init/credentials';
import PerformanceMetricsManager from '@managers/performance_metrics_manager';
import {getLastViewedChannelIdAndServer, getOnboardingViewed, getLastViewedThreadIdAndServer} from '@queries/app/global';
import {getAllServers} from '@queries/app/servers';
import {queryPostsByType} from '@queries/servers/post';
import {getThemeForCurrentTeam} from '@queries/servers/preference';
import {getCurrentUserId} from '@queries/servers/system';
import {queryMyTeams} from '@queries/servers/team';
@ -113,6 +117,8 @@ const launchApp = async (props: LaunchProps) => {
serverUrl = await getActiveServerUrl();
}
cleanupEphemeralPosts();
if (serverUrl) {
const credentials = await getServerCredentials(serverUrl);
if (credentials) {
@ -252,3 +258,17 @@ export const getLaunchPropsFromNotification = async (notification: NotificationW
return launchProps;
};
async function cleanupEphemeralPosts() {
const servers = await getAllServers();
for (const server of servers) {
const database = DatabaseManager.serverDatabases[server.url]?.database;
if (!database) {
continue;
}
/* eslint-disable-next-line no-await-in-loop */
const posts = await queryPostsByType(database, PostTypes.EPHEMERAL).fetch();
posts.forEach((post) => removePost(server.url, post));
}
}

View file

@ -190,6 +190,11 @@ export const queryPostsById = (database: Database, postIds: string[], sort?: Q.S
return database.get<PostModel>(POST).query(...clauses);
};
export const queryPostsByType = (database: Database, type: string) => {
const clauses: Q.Clause[] = [Q.where('type', type)];
return database.get<PostModel>(POST).query(...clauses);
};
export const queryPostsBetween = (database: Database, earliest: number, latest: number, sort: Q.SortOrder | null, userId?: string, channelId?: string, rootId?: string) => {
const andClauses = [Q.where('create_at', Q.between(earliest, latest))];
if (channelId) {