Rocket.Chat.ReactNative/app/containers/MessageComposer/helpers/draftMessage.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-01-25 14:11:07 +00:00
import log from '../../../lib/methods/helpers/log';
import database from '../../../lib/database';
import { getSubscriptionByRoomId } from '../../../lib/database/services/Subscription';
import { getThreadById } from '../../../lib/database/services/Thread';
export const loadDraftMessage = async ({ rid, tmid }: { rid?: string; tmid?: string }): Promise<string> => {
if (tmid) {
const thread = await getThreadById(tmid);
if (thread && thread.draftMessage) {
return thread.draftMessage;
}
} else if (rid) {
const subscription = await getSubscriptionByRoomId(rid);
if (subscription && subscription.draftMessage) {
return subscription.draftMessage;
}
}
return '';
};
export const saveDraftMessage = async ({
rid,
tmid,
draftMessage
}: {
rid?: string;
tmid?: string;
draftMessage: string;
}): Promise<void> => {
let obj;
if (tmid) {
obj = await getThreadById(tmid);
} else if (rid) {
obj = await getSubscriptionByRoomId(rid);
}
if (obj && obj.draftMessage !== draftMessage) {
try {
const db = database.active;
const object = obj;
await db.write(async () => {
await object.update(r => {
r.draftMessage = draftMessage;
});
});
} catch (e) {
log(e);
}
}
};