(file)
| 123 | // Saves a new message containing an image in Firebase. |
| 124 | // This first saves the image in Firebase storage. |
| 125 | async function saveImageMessage(file) { |
| 126 | try { |
| 127 | // 1 - We add a message with a loading icon that will get updated with the shared image. |
| 128 | const messageRef = await addDoc(collection(getFirestore(), 'messages'), { |
| 129 | name: getUserName(), |
| 130 | imageUrl: LOADING_IMAGE_URL, |
| 131 | profilePicUrl: getProfilePicUrl(), |
| 132 | timestamp: serverTimestamp() |
| 133 | }); |
| 134 | |
| 135 | // 2 - Upload the image to Cloud Storage. |
| 136 | const filePath = `${getAuth().currentUser.uid}/${messageRef.id}/${file.name}`; |
| 137 | const newImageRef = ref(getStorage(), filePath); |
| 138 | const fileSnapshot = await uploadBytesResumable(newImageRef, file); |
| 139 | |
| 140 | // 3 - Generate a public URL for the file. |
| 141 | const publicImageUrl = await getDownloadURL(newImageRef); |
| 142 | |
| 143 | // 4 - Update the chat message placeholder with the image’s URL. |
| 144 | await updateDoc(messageRef,{ |
| 145 | imageUrl: publicImageUrl, |
| 146 | storageUri: fileSnapshot.metadata.fullPath |
| 147 | }); |
| 148 | } catch (error) { |
| 149 | console.error('There was an error uploading a file to Cloud Storage:', error); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Saves the messaging device token to Cloud Firestore. |
| 154 | async function saveMessagingDeviceToken() { |
no test coverage detected