This repository has no description
1import { OAuthSession } from '@atproto/oauth-client-browser';
2import { Agent } from '@atproto/api';
3
4// API client for OAuth session using @atproto/api Agent
5
6// Create a post using the OAuth session with @atproto/api Agent
7export async function createPost(session: OAuthSession, options: {
8 text: string;
9 reply?: {
10 root: { uri: string; cid: string };
11 parent: { uri: string; cid: string };
12 };
13 embed?: any;
14 langs?: string[];
15 createdAt?: string;
16}) {
17 // Ensure we're on the client side
18 if (typeof window === 'undefined') {
19 throw new Error('API client can only be used on the client side');
20 }
21
22 try {
23 console.log('Creating post with session:', session.sub);
24 console.log('Post text:', options.text);
25
26 // Create an Agent instance using the OAuth session
27 const agent = new Agent(session);
28
29 // Extract emoji from text if present, default to toilet
30 let emoji = '🚽';
31 let cleanText = options.text;
32
33 // Simple emoji extraction - look for common toilet/bathroom emojis
34 const toiletEmojis = ['🚽', '🧻', '💩', '💨', '🚾', '🧼', '🪠', '🚻', '🩸', '💧', '💦', '😌',
35 '😣', '🤢', '🤮', '🥴', '😮💨', '😳', '😵', '🌾', '🍦', '📱', '📖', '💭',
36 '1️⃣', '2️⃣', '🟡', '🟤'];
37
38 // Sort emojis by length (longest first) to handle compound emojis correctly
39 const sortedEmojis = [...toiletEmojis].sort((a, b) => b.length - a.length);
40
41 // Look for any of these emojis in the text
42 for (const testEmoji of sortedEmojis) {
43 if (options.text.includes(testEmoji)) {
44 emoji = testEmoji;
45 cleanText = options.text.replace(testEmoji, '').trim();
46 break;
47 }
48 }
49
50 // Create a record directly using the Agent, following the cred.blue pattern
51 const flushRecord = {
52 $type: 'im.flushing.right.now',
53 text: cleanText,
54 emoji: emoji,
55 createdAt: new Date().toISOString(),
56 };
57
58 console.log('Creating flush record:', flushRecord);
59
60 // Use the agent to create the record directly in the user's PDS
61 const result = await agent.api.com.atproto.repo.createRecord({
62 repo: session.sub, // Use the user's DID
63 collection: 'im.flushing.right.now',
64 record: flushRecord,
65 });
66
67 console.log('Post created successfully:', result);
68 return result;
69 } catch (error) {
70 console.error('Failed to create post:', error);
71 throw error;
72 }
73}