-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharingUtils.js
72 lines (68 loc) · 2.52 KB
/
sharingUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// sharingUtils.js
import { ref, update, get } from 'firebase/database';
import { db } from './firebaseConfig';
/**
* Returns the sharing status between the current user and a target user.
* @param {string} currentUid – Your UID.
* @param {string} targetUid – The target user's UID.
* @returns {Promise<{amSharing: boolean, amReceiving: boolean}>}
*/
export const getSharingStatus = async (currentUid, targetUid) => {
try {
// Check whether targetUid is in your sharingWith and receivingFrom lists.
const sharingWithSnap = await get(ref(db, `users/${currentUid}/sharingWith/${targetUid}`));
const receivingFromSnap = await get(ref(db, `users/${currentUid}/receivingFrom/${targetUid}`));
return {
amSharing: sharingWithSnap.exists(),
amReceiving: receivingFromSnap.exists(),
};
} catch (error) {
console.error('Error fetching sharing status:', error);
return { amSharing: false, amReceiving: false };
}
};
/**
* Begins sharing your location with the target user.
*/
export const shareLocation = async (currentUid, targetUid) => {
try {
const updates = {};
// Add targetUid to your sharingWith list…
updates[`users/${currentUid}/sharingWith/${targetUid}`] = true;
// …and add your uid to the target's receivingFrom list.
updates[`users/${targetUid}/receivingFrom/${currentUid}`] = true;
await update(ref(db), updates);
} catch (error) {
console.error('Error sharing location:', error);
}
};
/**
* Stops sharing your location with the target user.
*/
export const stopSharingLocation = async (currentUid, targetUid) => {
try {
const updates = {};
// Remove targetUid from your sharingWith list…
updates[`users/${currentUid}/sharingWith/${targetUid}`] = null;
// …and remove your uid from the target's receivingFrom list.
updates[`users/${targetUid}/receivingFrom/${currentUid}`] = null;
await update(ref(db), updates);
} catch (error) {
console.error('Error stopping sharing location:', error);
}
};
/**
* Stops receiving the target user's location.
*/
export const stopReceivingLocation = async (currentUid, targetUid) => {
try {
const updates = {};
// Remove targetUid from your receivingFrom list…
updates[`users/${currentUid}/receivingFrom/${targetUid}`] = null;
// …and remove your uid from the target's sharingWith list.
updates[`users/${targetUid}/sharingWith/${currentUid}`] = null;
await update(ref(db), updates);
} catch (error) {
console.error('Error stopping receiving location:', error);
}
};