forked from 20266142/Overheard
25 lines
741 B
JavaScript
25 lines
741 B
JavaScript
import { collection, query, where, getDocs } from 'firebase/firestore';
|
|
import { getDb, isFirebaseConfigured } from './config.js';
|
|
import { getQueryPrefix } from '$lib/utils/geohash.js';
|
|
|
|
export async function getNearbyMessages(lat, lng) {
|
|
if (!isFirebaseConfigured()) {
|
|
console.warn(
|
|
'Firebase가 설정되지 않아 메시지를 불러오지 못했습니다. 프로젝트 루트에 .env 파일을 추가하세요.'
|
|
);
|
|
return [];
|
|
}
|
|
|
|
const prefix = getQueryPrefix(lat, lng);
|
|
const db = getDb();
|
|
|
|
const q = query(
|
|
collection(db, 'messages'),
|
|
where('geohash', '>=', prefix),
|
|
where('geohash', '<', prefix + 'z')
|
|
);
|
|
|
|
const snapshot = await getDocs(q);
|
|
return snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
|
|
}
|