@budytalk/activity-server
Version:
Complete social media content management server with built-in PostgreSQL database, real-time features, and zero-configuration setup
213 lines (169 loc) • 7.39 kB
JavaScript
// API usage example for BudyTalk Content Server
const axios = require('axios');
const BASE_URL = 'http://localhost:3000';
async function apiExample() {
console.log('🌐 Testing BudyTalk Content Server API...\n');
try {
// Test 1: Create user accounts
console.log('📝 Test 1: Creating user accounts via API');
const alice = await axios.post(`${BASE_URL}/api/content/users`, {
userId: 'user_alice_123',
username: 'alice_api',
displayName: 'Alice API User',
avatar: 'https://example.com/alice.jpg'
});
const bob = await axios.post(`${BASE_URL}/api/content/users`, {
userId: 'user_bob_456',
username: 'bob_api',
displayName: 'Bob API User',
avatar: 'https://example.com/bob.jpg'
});
console.log(`✅ Created user accounts: ${alice.data.displayName}, ${bob.data.displayName}\n`);
// Test 2: Set up following
console.log('📝 Test 2: Setting up following relationship');
await axios.post(`${BASE_URL}/api/content/follows`, {
followerId: bob.data.userId,
followingId: alice.data.userId
});
console.log('✅ Bob is now following Alice\n');
// Test 3: Create posts
console.log('📝 Test 3: Creating posts via API');
// Text post
const textPostResponse = await axios.post(`${BASE_URL}/api/content/posts`, {
userId: alice.data.userId,
content: 'Hello from the API! This is a test post 🚀',
postType: 'text',
hashtags: ['#api', '#test', '#budytalk'],
mentions: ['@bob_api']
});
const textPostId = textPostResponse.data.id;
console.log(`✅ Created text post: ${textPostId}`);
// Image post
const imagePostResponse = await axios.post(`${BASE_URL}/api/content/posts`, {
userId: alice.data.userId,
content: 'Check out this amazing sunset! 🌅',
postType: 'image',
media: [{
type: 'image/jpeg',
url: 'https://example.com/sunset.jpg',
filename: 'sunset.jpg',
size: 1024000
}],
hashtags: ['#sunset', '#nature', '#photography']
});
console.log(`✅ Created image post: ${imagePostResponse.data.id}`);
// Poll post
const pollPostResponse = await axios.post(`${BASE_URL}/api/content/posts`, {
userId: alice.data.userId,
content: 'API Poll: What\'s your favorite feature?',
postType: 'poll',
poll: {
question: 'What\'s your favorite feature?',
options: [
{ text: 'Real-time Timeline' },
{ text: 'Reactions System' },
{ text: 'Comments & Replies' },
{ text: 'Trending Algorithm' }
],
settings: {
allowMultipleChoices: false,
showResults: 'after_vote',
duration: 24
}
}
});
console.log(`✅ Created poll post: ${pollPostResponse.data.id}\n`);
// Test 4: Add reactions
console.log('📝 Test 4: Adding reactions via API');
await axios.post(`${BASE_URL}/api/content/reactions`, {
userId: bob.data.userId,
type: 'like',
postId: textPostId
});
console.log('✅ Bob liked Alice\'s text post');
await axios.post(`${BASE_URL}/api/content/reactions`, {
userId: bob.data.userId,
type: 'love',
postId: imagePostResponse.data.id
});
console.log('✅ Bob loved Alice\'s image post\n');
// Test 5: Add comments
console.log('📝 Test 5: Adding comments via API');
await axios.post(`${BASE_URL}/api/content/comments`, {
userId: bob.data.userId,
postId: textPostId,
content: 'Great post Alice! The API is working perfectly! 👏',
mentions: ['@alice_api']
});
console.log('✅ Bob commented on Alice\'s post\n');
// Test 6: Retweet posts
console.log('📝 Test 6: Retweeting posts via API');
// Simple retweet
await axios.post(`${BASE_URL}/api/content/retweets`, {
userId: bob.data.userId,
postId: imagePostResponse.data.id
});
console.log('✅ Bob retweeted Alice\'s image post');
// Retweet with comment
await axios.post(`${BASE_URL}/api/content/retweets`, {
userId: bob.data.userId,
postId: textPostId,
comment: 'Everyone should see this amazing API demo! 🔥'
});
console.log('✅ Bob retweeted Alice\'s text post with comment\n');
// Test 7: Get feeds and timelines
console.log('📝 Test 7: Retrieving feeds and timelines via API');
// Get Alice's feed (her own posts)
const aliceFeedResponse = await axios.get(`${BASE_URL}/api/content/users/${alice.data.userId}/feed?limit=10`);
console.log(`✅ Alice's feed: ${aliceFeedResponse.data.length} posts`);
// Get Bob's timeline (posts from followed users)
const bobTimelineResponse = await axios.get(`${BASE_URL}/api/content/users/${bob.data.userId}/timeline?limit=10`);
console.log(`✅ Bob's timeline: ${bobTimelineResponse.data.length} posts`);
// Get trending posts
const trendingResponse = await axios.get(`${BASE_URL}/api/content/trending?limit=5&timeframe=24`);
console.log(`✅ Trending posts: ${trendingResponse.data.length} posts\n`);
// Test 8: Unfollow
console.log('📝 Test 8: Unfollowing via API');
await axios.delete(`${BASE_URL}/api/content/follows`, {
data: {
followerId: bob.data.userId,
followingId: alice.data.userId
}
});
console.log('✅ Bob unfollowed Alice\n');
// Verify timeline is empty after unfollowing
const bobTimelineAfterUnfollow = await axios.get(`${BASE_URL}/api/content/users/${bob.data.userId}/timeline?limit=10`);
console.log(`✅ Bob's timeline after unfollowing: ${bobTimelineAfterUnfollow.data.length} posts\n`);
console.log('🎉 API TEST SUMMARY:');
console.log('===================');
console.log('✅ User Account Management: Create and manage user accounts');
console.log('✅ Following System: Follow and unfollow users');
console.log('✅ Post Creation: Text, image, and poll posts');
console.log('✅ Reactions: Like, love, and other reaction types');
console.log('✅ Comments: Add comments with mentions');
console.log('✅ Retweets: Simple retweets and retweets with comments');
console.log('✅ Timeline Management: Personal feeds and timelines');
console.log('✅ Trending Algorithm: Discover popular content');
console.log('✅ Real-time Updates: WebSocket integration ready');
console.log('');
console.log('🚀 All API endpoints working perfectly!');
console.log('📱 Ready to integrate with your user management system!');
} catch (error) {
console.error('❌ API test failed:', error.response?.data || error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', error.response.data);
}
throw error;
}
}
// Run the API test
if (require.main === module) {
console.log('⚠️ Make sure the BudyTalk Content Server is running on port 3000');
console.log(' You can start it with: npm run dev-server\n');
apiExample().catch(error => {
console.error('❌ API test failed:', error.message);
process.exit(1);
});
}
module.exports = { apiExample };