@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
JavaScript
// 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 };