Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled post Actions #8603

Merged
merged 18 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/actions/remote/scheduled_post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ jest.mock('@managers/websocket_manager', () => ({
getClient: jest.fn(() => mockWebSocketClient),
}));

jest.mock('@utils/scheduled_post', () => {
return {
isScheduledPostModel: jest.fn(() => false),
};
});

const mockedGetConfigValue = jest.mocked(getConfigValue);

beforeAll(() => {
Expand Down Expand Up @@ -184,23 +190,23 @@ describe('fetchScheduledPosts', () => {

describe('updateScheduledPost', () => {
it('update Schedule post - handle not found database', async () => {
const result = await updateScheduledPost('foo', scheduledPost) as unknown as {error: Error};
const result = await updateScheduledPost('foo', scheduledPost, 123) as unknown as {error: Error};
expect(result).toEqual({error: 'foo database not found'});
expect(logError).toHaveBeenCalled();
expect(forceLogoutIfNecessary).toHaveBeenCalled();
});

it('update Schedule post - base case', async () => {
await operator.handleUsers({users: [user1], prepareRecordsOnly: false});
const result = await updateScheduledPost(serverUrl, scheduledPost);
const result = await updateScheduledPost(serverUrl, scheduledPost, 123);
expect(result.error).toBeUndefined();
expect(result.scheduledPost).toEqual(scheduledPost);
});

it('update Schedule post - request error', async () => {
const error = new Error('custom error');
mockClient.updateScheduledPost = jest.fn().mockRejectedValueOnce(error);
const result = await updateScheduledPost(serverUrl, scheduledPost);
const result = await updateScheduledPost(serverUrl, scheduledPost, 123);
expect(result.error).toBe('custom error');
expect(logError).toHaveBeenCalledWith('error on updateScheduledPost', error.message);
expect(forceLogoutIfNecessary).toHaveBeenCalledWith(serverUrl, error);
Expand All @@ -211,7 +217,7 @@ describe('updateScheduledPost', () => {
await operator.handleUsers({users: [user1], prepareRecordsOnly: false});
const mockResponse = {...scheduledPost, update_at: Date.now()};
mockClient.updateScheduledPost.mockImplementationOnce(() => Promise.resolve(mockResponse));
const result = await updateScheduledPost(serverUrl, scheduledPost, undefined, true);
const result = await updateScheduledPost(serverUrl, scheduledPost, 123, undefined, true);
expect(result.error).toBeUndefined();
expect(result.scheduledPost).toEqual(mockResponse);
expect(spyHandleScheduledPosts).not.toHaveBeenCalled();
Expand Down
10 changes: 7 additions & 3 deletions app/actions/remote/scheduled_post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import DatabaseManager from '@database/manager';
import NetworkManager from '@managers/network_manager';
import websocketManager from '@managers/websocket_manager';
import {getConfigValue} from '@queries/servers/system';
import ScheduledPostModel from '@typings/database/models/servers/scheduled_post';
import {getFullErrorMessage} from '@utils/errors';
import {logError} from '@utils/log';
import {isScheduledPostModel} from '@utils/scheduled_post';

import {forceLogoutIfNecessary} from './session';

Expand Down Expand Up @@ -41,11 +43,13 @@ export async function createScheduledPost(serverUrl: string, schedulePost: Sched
}
}

export async function updateScheduledPost(serverUrl: string, scheduledPost: ScheduledPost, connectionId?: string, fetchOnly = false) {
export async function updateScheduledPost(serverUrl: string, scheduledPost: ScheduledPost | ScheduledPostModel, updateTime: number, connectionId?: string, fetchOnly = false) {
try {
const {operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const {operator, database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const client = NetworkManager.getClient(serverUrl);
const response = await client.updateScheduledPost(scheduledPost, connectionId);
const normalizedScheduledPost = isScheduledPostModel(scheduledPost) ? await scheduledPost.toApi(database) : scheduledPost;
normalizedScheduledPost.scheduled_at = updateTime;
const response = await client.updateScheduledPost(normalizedScheduledPost, connectionId);

if (response && !fetchOnly) {
await operator.handleScheduledPosts({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Screens} from '@constants';
import {useServerUrl} from '@context/server';
import {useTheme} from '@context/theme';
import {useIsTablet} from '@hooks/device';
import {DRAFT_OPTIONS_BUTTON} from '@screens/draft_options';
import {DRAFT_OPTIONS_BUTTON} from '@screens/draft_scheduled_post_options';
import {DRAFT_TYPE_SCHEDULED, type DraftType} from '@screens/global_drafts/constants';
import {openAsBottomSheet} from '@screens/navigation';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {useTheme} from '@context/theme';
import {DEFAULT_LOCALE} from '@i18n';
import {DRAFT_TYPE_SCHEDULED, type DraftType} from '@screens/global_drafts/constants';
import {getReadableTimestamp} from '@utils/datetime';
import {getErrorStringFromCode, type ScheduledPostErrorCode} from '@utils/scheduled_post';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
import {typography} from '@utils/typography';
import {getUserTimezone} from '@utils/user';
Expand Down Expand Up @@ -183,7 +184,7 @@ const DraftAndScheduledPostHeader: React.FC<Props> = ({
size={12}
color={theme.buttonColor}
/>
<Text style={style.errorText}>{'CHANNEL IS READ-ONLY'}</Text>
<Text style={style.errorText}>{getErrorStringFromCode(intl, scheduledPostErrorCode as ScheduledPostErrorCode)}</Text>
</View>
}
</View>
Expand Down
Loading