-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fire.js
150 lines (123 loc) · 3.17 KB
/
Fire.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import FireBaseKeys from './config.js'
import firebase from 'firebase'
require('firebase/firestore');
import PushNotifications from './utilities/PushNotifications';
class Fire {
constructor() {
firebase.initializeApp(FireBaseKeys);
}
addPost = async ({text, localUri, userName, userAvatar}) => {
const remoteUri = await this.uploadPhotoAsync(localUri, `photos/${this.uid}/${Date.now()}`)
return new Promise((res, rej) => {
this.firestore.collection('posts').add({
text,
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri,
userName,
userAvatar
})
.then(ref => {
res(ref)
})
.catch(error => {
console.log(error.message);
rej(error);
});
});
};
uploadPhotoAsync = async (uri, filename) => {
//const path = `photos/${this.uid}/${Date.now()}.jpg`
return new Promise(async (res, rej) =>{
const response = await fetch(uri);
const file = await response.blob();
let upload = firebase.storage().ref(filename).put(file);
upload.on(
'state_changed',
snapshot => {},
err => {
rej(err);
},
async () => {
const url = await upload.snapshot.ref.getDownloadURL();
res(url);
}
);
});
};
createUser = async user => {
let remoteUri = null;
try {
await firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
let db = this.firestore.collection('users').doc(this.uid);
db.set({
name: user.name,
email: user.email,
avatar: null
});
if(user.avatar) {
remoteUri = await this.uploadPhotoAsync(user.avatar, `avatars/${this.uid}`);
db.set({avatar: remoteUri}, {merge:true});
}
} catch (error) {
alert(`Error:` , error);
}
};
signOut = () => {
firebase.auth().signOut();
}
sendMessage = messages => {
messages.forEach(item => {
const message = {
text: item.text,
timestamp: firebase.database.ServerValue.TIMESTAMP,
user: item.user
};
this.messageDb.push(message);
});
this.firestore
.collection('users')
.get()
.then(snapshot => {
snapshot.forEach(doc => {
if (doc.id !== this.uid) {
PushNotifications.sendMessageNotification(doc.data().push_token);
}
});
})
.catch(err => {
console.log('Error getting documents', err);
});
};
parse = message => {
const { user, text, timestamp } = message.val();
const { key: _id } = message;
const createdAt = new Date(timestamp);
return {
_id,
createdAt,
text,
user
};
};
getMessage = callback => {
this.messageDb.on('child_added', snapshot => callback(this.parse(snapshot)));
}
off() {
this.messageDb.off();
}
get messageDb() {
return firebase.database().ref('messages');
}
get firestore() {
return firebase.firestore();
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
get timestamp() {
return Date.now();
}
}
Fire.shared = new Fire();
export default Fire;