-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase_wrapper.js
110 lines (93 loc) · 3.9 KB
/
firebase_wrapper.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
var firebaseWrapper = (function() {
var publicScope = {};
// Firebase Global variables
var messagesDbRef;
var messagesAttachmentsDbRef;
var messagesAttachmentsRef;
/** Function to init Firebase **/
publicScope.initFirebase = function() {
var config = {
apiKey: "AIzaSyCLeU7v5DxQiOBqXDKM28D3UZZsXCVqYJY",
authDomain: "fir-sample-chat-94755.firebaseapp.com",
databaseURL: "https://fir-sample-chat-94755.firebaseio.com",
projectId: "fir-sample-chat-94755",
storageBucket: "fir-sample-chat-94755.appspot.com",
messagingSenderId: "938343873148"
};
firebase.initializeApp(config);
// Setting the global database and storage references
messagesDbRef = firebase.database().ref('messages');
messagesAttachmentsDbRef = firebase.database().ref('attachments');
messagesAttachmentsRef = firebase.storage().ref('attachments/');
}
/** Function to retrieve conversations data **/
publicScope.initConversationData = function(callback) {
messagesDbRef.once("value").then(function(snapshot) {
messagesPerConversation = snapshot.numChildren();
conversationTimestamp = snapshot.val()[Object.keys(snapshot.val())[messagesPerConversation-1]].timestamp
callback(messagesPerConversation, conversationTimestamp);
});
}
/** Function to add a data listener **/
publicScope.startListeningToChatMessages = function(callback) {
messagesDbRef.on('child_added', function(snapshot) {
var chatItem = snapshot.val();
callback(chatItem);
});
}
publicScope.startListeningToAttachments = function(callback) {
messagesAttachmentsDbRef.on('child_added', function(snapshot) {
var chatAttachmentItem = snapshot.val();
callback(chatAttachmentItem);
});
}
/** Function to push chat message **/
publicScope.pushChatItem = function(chatItem) {
messagesDbRef.push(chatItem);
}
/** Function to push attachment message **/
publicScope.pushAttachmentItem = function(attachmentItem) {
messagesAttachmentsDbRef.push(attachmentItem);
}
/** Function to upload file to firebase storage **/
publicScope.uploadFileToStorage = function(file, callback) {
var fileName = file.name
// TODO add id, so files with same name doesn't get overriden
var filePath = usernameLabel.text().trim() + '/' + file.name;
// Upload file and metadata to the object
var uploadTask = messagesAttachmentsRef.child(filePath).put(file);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Uploading: Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Uploading: Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Uploading: Upload is running');
break;
}
}, function(error) {
switch (error.code) {
case 'storage/unauthorized':
console.log("Uploading: User doesn't have permission to access the object");
break;
case 'storage/canceled':
console.log("Uploading: User canceled the upload");
break;
case 'storage/unknown':
console.log("Uploading: Unknown error occurred, inspect error.serverResponse");
break;
}
}, function() {
console.log("Uploading: completed successfully");
var downloadURL = uploadTask.snapshot.downloadURL;
callback(fileName, downloadURL);
});
}
//Return only the public parts
return publicScope;
}());