Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tp-frontend/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IPFSService {

private statusCallback: ((status: string, peerCount: number) => void) | null = null;

async initialize(): Promise<void> {
async initialize(): Promise<Helia> {
try {
const libp2p = await createLibp2p({
transports: [
Expand Down Expand Up @@ -59,10 +59,12 @@ class IPFSService {
this.setupPeerMonitoring();

this.updateStatus();
return this.manager.helia;
} catch (error) {
console.error('Failed to initialize Helia:', error);
this.manager.status = 'Connection failed';
this.updateStatus();
throw error;
}
}

Expand Down
89 changes: 88 additions & 1 deletion tp-frontend/orbitdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OrbitDBService {
private manager: OrbitDBManager = {
orbitdb: null,
databases: new Map(),

status: 'Not initialized'
};

Expand Down Expand Up @@ -55,7 +56,10 @@ class OrbitDBService {

try {
const dbName = 'thirdplace-test';
const db = await this.manager.orbitdb.open(dbName, { type: 'documents' });
const db = await this.manager.orbitdb.open(dbName, {
type: 'documents',
sync: false // Disable sync to avoid pubsub issues
});
this.manager.databases.set(dbName, db);

console.log('Test database created:', db.address);
Expand Down Expand Up @@ -107,6 +111,89 @@ class OrbitDBService {
}
}

async createActivityPostsDatabase(): Promise<string | null> {
if (!this.manager.orbitdb) {
console.error('OrbitDB not initialized');
return null;
}

try {
const dbName = 'thirdplace-posts';
const db = await this.manager.orbitdb.open(dbName, {
type: 'documents',
sync: false // Disable sync to avoid pubsub issues
});
this.manager.databases.set(dbName, db);

console.log('Activity posts database created:', db.address);
return db.address;
} catch (error) {
console.error('Failed to create activity posts database:', error);
return null;
}
}

async submitActivityPost(postData: any): Promise<string | null> {
// Ensure posts database exists
let db = this.manager.databases.get('thirdplace-posts');
if (!db) {
const address = await this.createActivityPostsDatabase();
if (!address) {
return null;
}
db = this.manager.databases.get('thirdplace-posts');
}

if (!db) {
console.error('Activity posts database not available');
return null;
}

try {
// Generate a unique ID and add metadata
const activityPost = {
_id: Date.now().toString(),
id: Date.now().toString(),
...postData,
author: 'user_' + Math.random().toString(36).substr(2, 9), // Temporary user ID
createdAt: new Date().toISOString(),
endDate: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // 24 hours from now
proposedTime: new Date(Date.now() + 2 * 60 * 60 * 1000).toISOString(), // 2 hours from now
status: 'active',
genderBalance: 'any',
// Mock coordinates for San Francisco
latitude: 37.7749 + (Math.random() - 0.5) * 0.01,
longitude: -122.4194 + (Math.random() - 0.5) * 0.01,
geohash: '9q8yy' // Mock geohash for SF area
};

const hash = await db.put(activityPost);
console.log('Activity post submitted with hash:', hash);
console.log('Post data:', activityPost);
return hash;
} catch (error) {
console.error('Failed to submit activity post:', error);
return null;
}
}

async getAllActivityPosts(): Promise<any[] | null> {
const db = this.manager.databases.get('thirdplace-posts');
if (!db) {
console.error('Activity posts database not found');
return null;
}

try {
const allPosts = await db.all();
console.log('Retrieved all activity posts:', allPosts);
return allPosts;
} catch (error) {
console.error('Failed to retrieve activity posts:', error);
return null;
}
}

async stop(): Promise<void> {
if (this.manager.orbitdb) {
for (const [name, db] of this.manager.databases) {
Expand Down
Loading