sending message nicely

This commit is contained in:
Arjun Patel
2022-02-05 20:04:13 -08:00
parent e92a54623c
commit 9a82b249ac
6 changed files with 83 additions and 8 deletions
+4 -4
View File
@@ -11,9 +11,9 @@ export default class Conversation {
activeMembers: string[]; // userIds
membersInLiveRoom: string[] = [] as string[]; // all members in a live call right now for this convo
cachedAudioClips: AudioClip[] = [] as AudioClip[];
cachedDrawerItems: Link[] = [] as Link[];
tldrClips: AudioClip[] = [] as AudioClip[];
cachedAudioClip?: AudioClip; // last message pretty much
cachedDrawerItem?: Link; // last link pretty much
cachedTldrClip?: string;
createdDate: Timestamp = Timestamp.now();
createdByUserId: string;
@@ -68,7 +68,7 @@ export enum ConversationMemberState {
export class AudioClip {
id: string = uuid();
audioDataUrl: string;
audioDataUrl?: string;
senderUserId: string;
createdDate: Timestamp = Timestamp.now();
+1
View File
@@ -14,6 +14,7 @@ enum Collections {
// v2
conversations = "conversations",
conversationMembers = "members",
conversationAudioClips = "audioClips",
}
export default Collections;
@@ -9,9 +9,13 @@ import {
runTransaction,
writeBatch,
} from "firebase/firestore";
import Conversation, { ConversationMember } from "../models/conversation";
import Conversation, {
AudioClip,
ConversationMember,
ConversationMemberState,
} from "../models/conversation";
import Collections from "./collections";
import { ConversationMemberState } from "../models/conversation";
import { cloudStorageService } from "./index";
export default class ConversationService {
private db: Firestore = getFirestore();
@@ -103,4 +107,47 @@ export default class ConversationService {
await setDoc(docRef, newUpdates, { merge: true });
}
async sendAudioClip(
createdByUserId: string,
audioMp3File: File,
convoId: string
) {
// upload to cloud storage
const downloadUrl = await cloudStorageService.uploadMessageAudioFile(
audioMp3File
);
const newAudioClip = new AudioClip(downloadUrl, createdByUserId);
newAudioClip.audioDataUrl = downloadUrl;
const audioClipRef = doc(
this.db,
Collections.conversations,
convoId,
Collections.conversationAudioClips,
newAudioClip.id
);
const conversationDoc = doc(this.db, Collections.conversations, convoId);
await runTransaction(this.db, async (transaction) => {
// want to add to the long term collection of all audio clips for a conversation
transaction.set(
audioClipRef,
{ ...newAudioClip, createdDate: serverTimestamp() },
{ merge: true }
);
// want to make updates to the conversation document itself: set the last item for just the basic information to be
// published to users
transaction.set(
conversationDoc,
{
lastActivityDate: serverTimestamp(),
cachedAudioClip: { ...newAudioClip, createdDate: serverTimestamp() },
},
{ merge: true }
);
});
}
}
+3
View File
@@ -1,6 +1,9 @@
import CloudStorageService from "./cloudStorageService";
import ConversationService from "./conversationService";
import UserService from "./userService";
export const conversationService = new ConversationService();
export const userService = new UserService();
export const cloudStorageService = new CloudStorageService();
@@ -20,6 +20,9 @@ import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import MicRecorder from "mic-recorder-to-mp3";
import { v4 as uuidv4 } from "uuid";
import { conversationService } from "../../../common/services/index";
import { AudioClip } from "@nirvana/common/models/conversation";
import { useAuth } from "../../contexts/authContext";
// New instance
const recorder = new MicRecorder({
@@ -29,12 +32,14 @@ const recorder = new MicRecorder({
const MAX_SHORTCUT_MAPPINGS_PRIORITY = 8;
export default function Priority() {
const { currUser } = useAuth();
const priorityConvos = useRecoilValue(priorityConvosSelector);
const [mapShortcutsToConvoId, setmapShortcutsToConvoId] = useState<
Map<string, string>
>(new Map<string, string>());
const setSelectedConversationId = useSetRecoilState(
const [selectedConvoId, setSelectedConversationId] = useRecoilState(
selectedPriorityConvoAtom
);
@@ -110,6 +115,10 @@ export default function Priority() {
const startRecording = () => {
// check if there is a person selected
if (!selectedConvoId) {
toast.error("You must select a person first!");
return;
}
// check that we have mic permissions
if (!hasMicPermissions) {
@@ -149,10 +158,15 @@ export default function Priority() {
setIsRecording(false);
if (!selectedConvoId) {
toast.error("problem in sending clip");
return;
}
recorder
.stop()
.getMp3()
.then(([buffer, blob]) => {
.then(async ([buffer, blob]) => {
const blobURL = URL.createObjectURL(blob);
const file = new File(buffer, uuidv4() + ".mp3", {
@@ -163,6 +177,16 @@ export default function Priority() {
const player = new Audio(URL.createObjectURL(file));
// player.onended = onEndedPlaying;
player.play();
// send to service which handles the upload to blob and also the conversation database
await conversationService.sendAudioClip(
currUser!.uid,
file,
selectedConvoId
);
console.log("successfully sent audio clip");
toast.success("successfully sent audio clip");
})
.catch((error) => {
console.log(error);