Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and retrieve display name #193

Merged
merged 15 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions server/src/actions/getSingleConnectedUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { connectedUsersCollection } from '../utilities/firebaseInit'

aaditkamat marked this conversation as resolved.
Show resolved Hide resolved
export const getConnectedUserDisplayName = async (socketID: string) => {
try {
return connectedUsersCollection.doc(socketID).displayName;
} catch (error) {
console.error(error);
return null;
}
}

10 changes: 10 additions & 0 deletions server/src/actions/updateConnectedUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export const updateUserLocation = async (socketID: string, lat: number, lon: num
return false
}
}

export const updateUserDisplayName = async (socketID: string, displayName: string) => {
try {
await connectedUsersCollection.doc(socketID).update({ displayName: displayName })
return true
} catch (error) {
console.error(error.message)
return false
}
}
32 changes: 29 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import 'geofire-common'
import { Message } from './types/Message';
import { createMessage } from './actions/createMessage'
import { createUser } from './actions/createConnectedUser'
import { toggleUserConnectionStatus, updateUserLocation } from './actions/updateConnectedUser'
import { toggleUserConnectionStatus, updateUserLocation, updateUserDisplayName } from './actions/updateConnectedUser'
import { deleteConnectedUserByUID } from './actions/deleteConnectedUser'
import { getConnectedUserDisplayName } from './actions/getSingleConnectedUser'
import {geohashForLocation} from 'geofire-common';
import { findNearbyUsers } from './actions/getConnectedUsers'
import { findNearbyUsers } from './actions/getMultipleConnectedUsers'
aaditkamat marked this conversation as resolved.
Show resolved Hide resolved
import { ConnectedUser } from './types/User';
import { getAuth } from 'firebase-admin/auth';

Expand Down Expand Up @@ -132,8 +133,33 @@ io.on('connection', async (socket: any) => {
console.error("[WS] Error calling updateLocation:", error.message)
}
})
socket.on('updateDisplayName', async (newDisplayName, ack) => {
aaditkamat marked this conversation as resolved.
Show resolved Hide resolved
try {
const success = await updateUserDisplayName(socket.id, newDisplayName)
if (success) {
console.log(`[WS] Updated display name of user <${socket.id}> to <${newDisplayName}> successfully.`)
if (ack) ack("display name updated")
} else {
throw new Error("updateDisplayName() failed.")
}
} catch (error) {
console.error("[WS] Error calling updateDisplayName:", error.message)
}
})
socket.on('retrieveDisplayName', async (ack) => {
try {
const success = await getConnectedUserDisplayName(socket.id)
if (success) {
console.log(`[WS] Retrieved display name of user <${socket.id}> successfully.`)
if (ack) ack("display name retrieved")
} else {
throw new Error("retrieveDisplayName() failed.")
}
} catch (error) {
console.error("[WS] Error calling retrieveDisplayName:", error.message)
}
})
})

socketServer.listen(socket_port, () => {
console.log(`[WS] Listening for new connections on port ${socket_port}.`)
})
Expand Down
Loading