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 // Look for any of these emojis in the text
39 for (const testEmoji of toiletEmojis) {
40 if (options.text.includes(testEmoji)) {
41 emoji = testEmoji;
42 cleanText = options.text.replace(testEmoji, '').trim();
43 break;
44 }
45 }
46
47 // Create a record directly using the Agent, following the cred.blue pattern
48 const flushRecord = {
49 $type: 'im.flushing.right.now',
50 text: cleanText,
51 emoji: emoji,
52 createdAt: new Date().toISOString(),
53 };
54
55 console.log('Creating flush record:', flushRecord);
56
57 // Use the agent to create the record directly in the user's PDS
58 const result = await agent.api.com.atproto.repo.createRecord({
59 repo: session.sub, // Use the user's DID
60 collection: 'im.flushing.right.now',
61 record: flushRecord,
62 });
63
64 console.log('Post created successfully:', result);
65 return result;
66 } catch (error) {
67 console.error('Failed to create post:', error);
68 throw error;
69 }
70}