UNPKG

@budytalk/activity-server

Version:

Complete social media content management server with built-in PostgreSQL database, real-time features, and zero-configuration setup

226 lines (184 loc) • 8.36 kB
// Bookmark functionality example for BudyTalk Content Server const { BudyTalkContent } = require('../dist/index'); async function bookmarkExample() { console.log('šŸ”– Testing BudyTalk Bookmark functionality...\n'); try { // Initialize the content server const budytalk = new BudyTalkContent({ port: 3000, database: { type: 'sqlite', url: './bookmark-demo.db' } }); await budytalk.initialize(); await budytalk.start(); console.log('āœ… Content server started on port 3000\n'); // Create user accounts console.log('šŸ“ Creating user accounts...'); const alice = await budytalk.syncUserAccount({ userId: 'user_alice_bookmark', username: 'alice_bookmark', displayName: 'Alice Bookmark', avatar: 'https://example.com/alice.jpg' }); const bob = await budytalk.syncUserAccount({ userId: 'user_bob_bookmark', username: 'bob_bookmark', displayName: 'Bob Bookmark', avatar: 'https://example.com/bob.jpg' }); console.log(`āœ… Created accounts for ${alice.displayName} and ${bob.displayName}\n`); // Alice creates various posts console.log('šŸ“ Creating posts to bookmark...'); const techPost = await budytalk.createPost({ userId: alice.userId, content: 'Amazing new JavaScript features in ES2024! šŸš€ #javascript #webdev', postType: 'text', hashtags: ['#javascript', '#webdev', '#es2024'], mentions: ['@bob_bookmark'] }); const recipePost = await budytalk.createPost({ userId: alice.userId, content: 'My grandmother\'s secret chocolate chip cookie recipe! šŸŖ', postType: 'text', hashtags: ['#recipe', '#cooking', '#cookies'] }); const travelPost = await budytalk.createPost({ userId: alice.userId, content: 'Just visited the most beautiful beach in Bali! šŸ–ļø', postType: 'image', media: [{ type: 'image/jpeg', url: 'https://example.com/bali-beach.jpg', filename: 'bali-beach.jpg', size: 2048000 }], hashtags: ['#travel', '#bali', '#beach'] }); console.log(`āœ… Created 3 posts for bookmarking\n`); // Bob bookmarks posts in different collections console.log('šŸ”– Bob bookmarking posts...'); // Bookmark tech post in "Programming" collection await budytalk.bookmarkPost({ userId: bob.userId, postId: techPost.id, collectionName: 'Programming', notes: 'Need to check out these ES2024 features for my next project' }); console.log('āœ… Bookmarked tech post in "Programming" collection'); // Bookmark recipe post in "Recipes" collection await budytalk.bookmarkPost({ userId: bob.userId, postId: recipePost.id, collectionName: 'Recipes', notes: 'Must try this recipe for the weekend!' }); console.log('āœ… Bookmarked recipe post in "Recipes" collection'); // Bookmark travel post in "Travel Inspiration" collection await budytalk.bookmarkPost({ userId: bob.userId, postId: travelPost.id, collectionName: 'Travel Inspiration', notes: 'Add Bali to my travel bucket list' }); console.log('āœ… Bookmarked travel post in "Travel Inspiration" collection\n'); // Get Bob's bookmark collections console.log('šŸ“š Checking Bob\'s bookmark collections...'); const collections = await budytalk.getUserBookmarkCollections(bob.userId); console.log('āœ… Bob\'s bookmark collections:'); collections.forEach(collection => { console.log(` šŸ“ ${collection.name}: ${collection.count} bookmarks`); }); console.log(''); // Get bookmarks from specific collection console.log('šŸ“– Getting bookmarks from "Programming" collection...'); const programmingBookmarks = await budytalk.getUserBookmarks(bob.userId, 'Programming'); console.log(`āœ… Found ${programmingBookmarks.length} programming bookmarks:`); programmingBookmarks.forEach(post => { console.log(` šŸ“ "${post.content?.substring(0, 50)}..."`); }); console.log(''); // Get all bookmarks console.log('šŸ“– Getting all of Bob\'s bookmarks...'); const allBookmarks = await budytalk.getUserBookmarks(bob.userId); console.log(`āœ… Bob has ${allBookmarks.length} total bookmarks\n`); // Alice also bookmarks some posts console.log('šŸ”– Alice bookmarking her own posts for reference...'); await budytalk.bookmarkPost({ userId: alice.userId, postId: recipePost.id, collectionName: 'My Recipes', notes: 'My own recipe - need to remember the exact measurements' }); console.log('āœ… Alice bookmarked her own recipe post\n'); // Try to bookmark the same post again (should fail) console.log('šŸ”– Testing duplicate bookmark prevention...'); try { await budytalk.bookmarkPost({ userId: bob.userId, postId: techPost.id, collectionName: 'Programming' }); console.log('āŒ Duplicate bookmark should have failed'); } catch (error) { console.log('āœ… Duplicate bookmark correctly prevented:', error.message); } console.log(''); // Remove a bookmark console.log('šŸ—‘ļø Removing a bookmark...'); await budytalk.removeBookmark(bob.userId, recipePost.id); console.log('āœ… Removed recipe bookmark\n'); // Check updated collections console.log('šŸ“š Checking updated bookmark collections...'); const updatedCollections = await budytalk.getUserBookmarkCollections(bob.userId); console.log('āœ… Bob\'s updated bookmark collections:'); updatedCollections.forEach(collection => { console.log(` šŸ“ ${collection.name}: ${collection.count} bookmarks`); }); console.log(''); // Demonstrate bookmark impact on trending algorithm console.log('šŸ“ˆ Checking trending posts (bookmarks affect engagement)...'); const trending = await budytalk.getTrendingPosts(5, 24); console.log(`āœ… Found ${trending.length} trending posts:`); trending.forEach((post, index) => { const engagement = (post.commentCount || 0) * 2 + (post.retweetCount || 0) * 3 + (post.bookmarkCount || 0); console.log(` ${index + 1}. "${post.content?.substring(0, 40)}..." (${post.bookmarkCount} bookmarks, ${engagement} engagement)`); }); console.log(''); console.log('šŸŽ‰ BOOKMARK EXAMPLE COMPLETED!'); console.log('=============================='); console.log('āœ… Bookmark Creation: Users can bookmark posts with collections and notes'); console.log('āœ… Collection Management: Organize bookmarks into named collections'); console.log('āœ… Bookmark Retrieval: Get bookmarks by collection or all at once'); console.log('āœ… Duplicate Prevention: Cannot bookmark the same post twice'); console.log('āœ… Bookmark Removal: Remove bookmarks when no longer needed'); console.log('āœ… Engagement Tracking: Bookmarks contribute to trending algorithm'); console.log('āœ… Personal Organization: Users can add notes to their bookmarks'); console.log(''); console.log('šŸ”– Key Bookmark Features:'); console.log(' šŸ“ Collections: Organize bookmarks by topic/category'); console.log(' šŸ“ Notes: Add personal notes to remember why you bookmarked'); console.log(' šŸ“Š Analytics: Bookmark counts affect post engagement scores'); console.log(' šŸ”’ Privacy: Only you can see your own bookmarks'); console.log(' šŸ—‚ļø Management: Easy retrieval and organization of saved content'); console.log(''); console.log('šŸš€ Perfect for saving important posts, recipes, articles, and inspiration!'); // Keep server running for testing console.log('\nā° Server will keep running for 30 seconds for testing...'); setTimeout(async () => { await budytalk.stop(); console.log('āœ… Server stopped'); }, 30000); } catch (error) { console.error('āŒ Bookmark example failed:', error.message); process.exit(1); } } // Run the example if (require.main === module) { bookmarkExample(); } module.exports = { bookmarkExample };