diff --git a/app/components/post_list/post_list.tsx b/app/components/post_list/post_list.tsx index 23644b902..f3cdf2e03 100644 --- a/app/components/post_list/post_list.tsx +++ b/app/components/post_list/post_list.tsx @@ -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]); diff --git a/app/init/launch.ts b/app/init/launch.ts index 9db93c37a..2fee4a45d 100644 --- a/app/init/launch.ts +++ b/app/init/launch.ts @@ -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)); + } +} diff --git a/app/queries/servers/post.ts b/app/queries/servers/post.ts index f47b42402..e618cd567 100644 --- a/app/queries/servers/post.ts +++ b/app/queries/servers/post.ts @@ -190,6 +190,11 @@ export const queryPostsById = (database: Database, postIds: string[], sort?: Q.S return database.get(POST).query(...clauses); }; +export const queryPostsByType = (database: Database, type: string) => { + const clauses: Q.Clause[] = [Q.where('type', type)]; + return database.get(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) {