66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import type { AppSupabaseClient } from '../supabase/client';
|
|
|
|
export interface PinnedMessage {
|
|
conversationId: string;
|
|
messageId: string;
|
|
pinnedBy: string;
|
|
pinnedAt: string;
|
|
}
|
|
|
|
interface Row {
|
|
conversation_id: string;
|
|
message_id: string;
|
|
pinned_by: string;
|
|
pinned_at: string;
|
|
}
|
|
|
|
function mapRow(r: Row): PinnedMessage {
|
|
return {
|
|
conversationId: r.conversation_id,
|
|
messageId: r.message_id,
|
|
pinnedBy: r.pinned_by,
|
|
pinnedAt: r.pinned_at,
|
|
};
|
|
}
|
|
|
|
export async function listPinnedMessages(
|
|
client: AppSupabaseClient,
|
|
conversationId: string,
|
|
): Promise<PinnedMessage[]> {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { data, error } = await (client as any)
|
|
.from('pinned_messages')
|
|
.select('conversation_id, message_id, pinned_by, pinned_at')
|
|
.eq('conversation_id', conversationId)
|
|
.order('pinned_at', { ascending: false });
|
|
if (error) throw error;
|
|
return (data ?? []).map(mapRow);
|
|
}
|
|
|
|
export async function pinMessage(
|
|
client: AppSupabaseClient,
|
|
conversationId: string,
|
|
messageId: string,
|
|
pinnedBy: string,
|
|
): Promise<void> {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { error } = await (client as any)
|
|
.from('pinned_messages')
|
|
.insert({ conversation_id: conversationId, message_id: messageId, pinned_by: pinnedBy });
|
|
if (error) throw error;
|
|
}
|
|
|
|
export async function unpinMessage(
|
|
client: AppSupabaseClient,
|
|
conversationId: string,
|
|
messageId: string,
|
|
): Promise<void> {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { error } = await (client as any)
|
|
.from('pinned_messages')
|
|
.delete()
|
|
.eq('conversation_id', conversationId)
|
|
.eq('message_id', messageId);
|
|
if (error) throw error;
|
|
}
|