-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.es2017.js
105 lines (80 loc) · 2.89 KB
/
app.es2017.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-disable no-console */
/* ********* INIT ********* */
const config = {
apiKey: 'AIzaSyD06laJHfLHqF8qbFSR-YTTyFpYX2iG-YM',
authDomain: 'prototype-a7997.firebaseapp.com',
databaseURL: 'https://prototype-a7997.firebaseio.com',
projectId: 'prototype-a7997',
storageBucket: 'prototype-a7997.appspot.com',
messagingSenderId: '1046663529113',
};
firebase.initializeApp(config);
const DATABASE_TIMESTAMP = firebase.database.ServerValue.TIMESTAMP;
const FIRESTORE_TIMESTAMP = firebase.firestore.FieldValue.serverTimestamp();
/* ********* APP ********* */
function presence() {
const { uid } = firebase.auth().currentUser;
firebase.database().ref('.info/connected').on('value', async connected => {
if (connected.val() === false) {
firebase.firestore().doc(`/users/${uid}`).set({ state: 'offline', modified: FIRESTORE_TIMESTAMP });
return;
}
await firebase.database().ref(`/users/${uid}`).onDisconnect().set({ state: 'offline', modified: DATABASE_TIMESTAMP });
firebase.firestore().doc(`/users/${uid}`).set({ state: 'online', modified: FIRESTORE_TIMESTAMP });
firebase.database().ref(`/users/${uid}`).set({ state: 'online', modified: DATABASE_TIMESTAMP });
});
}
function status(success = () => {}, failure = () => {}) {
const { uid } = firebase.auth().currentUser;
return new Promise((resolve, reject) => {
const unbind = firebase.firestore().doc(`/users/${uid}`).onSnapshot(
document => {
const { ref, exists } = document;
const result = { data: { ...(exists ? document.data() : null) }, ref, exists, unbind };
success(result);
resolve(result);
},
err => {
failure(err);
reject(err);
},
);
});
}
function monitor() {
const { uid } = firebase.auth().currentUser;
firebase.firestore().collection('users')
.where('state', '==', 'online')
.onSnapshot(collection => {
collection.docChanges.forEach(change => {
let text;
if (change.type === 'added') {
text = `😎 ${change.doc.id} ONLINE`;
} else if (change.type === 'removed') {
text = `😵 ${change.doc.id} OFFLINE`;
}
if (text) {
let el = document.querySelector(`#UID-${change.doc.id}`);
if (!el) {
el = document.createElement('div');
el.id = `UID-${change.doc.id}`;
if (change.doc.id === uid) {
el.classList.add('me');
}
document.body.appendChild(el);
}
el.innerHTML = text;
}
});
});
}
(async () => {
const user = await firebase.auth().signInAnonymously();
presence();
// const result = await status();
// console.log(`User ${user.uid} is ${JSON.stringify(result.data)}`);
status(result => {
console.log(result.data.state === 'online' ? '😎' : '😵', `${user.uid} ${JSON.stringify(result.data)}`);
});
monitor();
})();