|
1 | 1 | const functions = require('firebase-functions'); |
2 | 2 | const admin = require('firebase-admin'); |
| 3 | +const { FieldValue } = require('firebase-admin/firestore'); |
3 | 4 | const Mixpanel = require('mixpanel'); |
4 | 5 |
|
5 | | -admin.initializeApp(functions.config().firebase); |
| 6 | +// Initialize with staging project when running in emulator |
| 7 | +if (process.env.FUNCTIONS_EMULATOR === 'true') { |
| 8 | + admin.initializeApp({ |
| 9 | + projectId: 'staging-zenuml-27954', |
| 10 | + }); |
| 11 | +} else { |
| 12 | + admin.initializeApp(functions.config().firebase); |
| 13 | +} |
6 | 14 | const db = admin.firestore(); |
7 | 15 |
|
8 | 16 | //Mixpanel project: Confluence Analytics(new) |
@@ -112,6 +120,136 @@ exports.sync_diagram = functions.https.onRequest(async (req, res) => { |
112 | 120 | request.end(); |
113 | 121 | }); |
114 | 122 |
|
| 123 | +exports.create_share = functions.https.onRequest(async (req, res) => { |
| 124 | + try { |
| 125 | + // Skip auth verification for local development if needed |
| 126 | + let decoded; |
| 127 | + if (process.env.FUNCTIONS_EMULATOR === 'true' && req.body.token === 'local-dev-token') { |
| 128 | + // Mock user for local testing |
| 129 | + decoded = { uid: 'local-test-user' }; |
| 130 | + } else { |
| 131 | + decoded = await verifyIdToken(req.body.token); |
| 132 | + } |
| 133 | + const itemId = req.body.id; |
| 134 | + |
| 135 | + if (!itemId) { |
| 136 | + return res.status(400).json({ error: 'Item ID is required' }); |
| 137 | + } |
| 138 | + |
| 139 | + // Get the item from Firestore |
| 140 | + const itemRef = db.collection('items').doc(itemId); |
| 141 | + const doc = await itemRef.get(); |
| 142 | + |
| 143 | + if (!doc.exists) { |
| 144 | + return res.status(404).json({ error: 'Item not found' }); |
| 145 | + } |
| 146 | + |
| 147 | + const itemData = doc.data(); |
| 148 | + |
| 149 | + // Verify user owns this item |
| 150 | + if (itemData.createdBy !== decoded.uid) { |
| 151 | + return res.status(403).json({ error: 'Unauthorized access' }); |
| 152 | + } |
| 153 | + |
| 154 | + // Generate or reuse share token |
| 155 | + const crypto = require('crypto'); |
| 156 | + const shareToken = itemData.shareToken || crypto.randomBytes(16).toString('hex'); |
| 157 | + |
| 158 | + // Update item with sharing info |
| 159 | + await itemRef.update({ |
| 160 | + isShared: true, |
| 161 | + shareToken: shareToken, |
| 162 | + sharedAt: FieldValue.serverTimestamp() |
| 163 | + }); |
| 164 | + |
| 165 | + // Generate content hash for cache busting |
| 166 | + const contentHash = crypto.createHash('md5').update(itemData.js || '').digest('hex'); |
| 167 | + |
| 168 | + // Return in the same format as the old API |
| 169 | + // Use origin from frontend request, with fallback to environment-specific defaults |
| 170 | + let baseUrl = req.body.origin; |
| 171 | + if (!baseUrl) { |
| 172 | + if (process.env.FUNCTIONS_EMULATOR === 'true') { |
| 173 | + // Local development fallback |
| 174 | + baseUrl = 'http://localhost:3000'; |
| 175 | + } else if (process.env.GCLOUD_PROJECT === 'staging-zenuml-27954') { |
| 176 | + // Staging environment fallback |
| 177 | + baseUrl = 'https://staging.zenuml.com'; |
| 178 | + } else { |
| 179 | + // Production environment fallback |
| 180 | + baseUrl = 'https://app.zenuml.com'; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + res.json({ |
| 185 | + page_share: `${baseUrl}?id=${itemId}&share-token=${shareToken}`, |
| 186 | + md5: contentHash |
| 187 | + }); |
| 188 | + |
| 189 | + } catch (error) { |
| 190 | + console.error('Error creating share:', error); |
| 191 | + res.status(500).json({ error: 'Failed to create share' }); |
| 192 | + } |
| 193 | +}); |
| 194 | + |
| 195 | +exports.get_shared_item = functions.https.onRequest(async (req, res) => { |
| 196 | + try { |
| 197 | + console.log('=== DEBUG: get_shared_item ==='); |
| 198 | + console.log('Full request URL:', req.url); |
| 199 | + console.log('Query object:', req.query); |
| 200 | + console.log('All query keys:', Object.keys(req.query)); |
| 201 | + |
| 202 | + const itemId = req.query.id; |
| 203 | + const shareToken = req.query.token || req.query['share-token']; |
| 204 | + |
| 205 | + console.log('Parsed params:', { itemId, shareToken }); |
| 206 | + console.log('ItemId type:', typeof itemId, 'ShareToken type:', typeof shareToken); |
| 207 | + |
| 208 | + if (!itemId || !shareToken) { |
| 209 | + console.log('Missing required params - itemId exists:', !!itemId, 'shareToken exists:', !!shareToken); |
| 210 | + return res.status(400).json({ error: 'Item ID and share token are required' }); |
| 211 | + } |
| 212 | + |
| 213 | + // Get the item from Firestore using Admin SDK (bypasses security rules) |
| 214 | + const itemRef = db.collection('items').doc(itemId); |
| 215 | + const doc = await itemRef.get(); |
| 216 | + |
| 217 | + if (!doc.exists) { |
| 218 | + console.log('DEBUG: Item not found in Firestore'); |
| 219 | + return res.status(404).json({ error: 'Item not found' }); |
| 220 | + } |
| 221 | + |
| 222 | + const itemData = doc.data(); |
| 223 | + console.log('DEBUG: Item data loaded:', { |
| 224 | + id: itemData.id, |
| 225 | + title: itemData.title, |
| 226 | + isShared: itemData.isShared, |
| 227 | + hasShareToken: !!itemData.shareToken, |
| 228 | + shareTokenMatch: itemData.shareToken === shareToken, |
| 229 | + createdBy: itemData.createdBy, |
| 230 | + actualToken: itemData.shareToken, |
| 231 | + requestedToken: shareToken |
| 232 | + }); |
| 233 | + |
| 234 | + // Verify item is shared and token matches |
| 235 | + if (!itemData.isShared || itemData.shareToken !== shareToken) { |
| 236 | + console.log('DEBUG: Token validation failed'); |
| 237 | + return res.status(403).json({ error: 'Invalid share token or item not shared' }); |
| 238 | + } |
| 239 | + |
| 240 | + console.log('DEBUG: Token validation passed, returning item'); |
| 241 | + // Return item data with read-only flag |
| 242 | + res.json({ |
| 243 | + ...itemData, |
| 244 | + isReadOnly: true |
| 245 | + }); |
| 246 | + |
| 247 | + } catch (error) { |
| 248 | + console.error('Error getting shared item:', error); |
| 249 | + res.status(500).json({ error: 'Failed to get shared item' }); |
| 250 | + } |
| 251 | +}); |
| 252 | + |
115 | 253 | exports.track = functions.https.onRequest(async (req, res) => { |
116 | 254 | if (!req.body.event) { |
117 | 255 | console.log('missing req.body.event'); |
|
0 commit comments