Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions front/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import RoomList from './components/RoomList.vue';
import Login from './components/Login.vue';

// note: this assumes port 5000 isn't that bad ?
const srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk");
let srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk");

interface RoomData {
title: string,
Expand Down Expand Up @@ -73,7 +73,8 @@ export default defineComponent({
mounted() {
this.fetchRooms();
// polling
srv.socket.on("message", this.messages.push); // when the app receive a message event, it push and display this message
srv.socket.on("message",this.messages.push);// when the app receive a message event, it push and display this message

srv.socket.on("people", users => this.users = users); // actualise the userList component when a user is added or removed
},
methods: {
Expand Down Expand Up @@ -104,7 +105,6 @@ export default defineComponent({
},
async displayRoom(room: Room) { // called when selecting a room in RoomList
this.selectedRoom = room;
srv.join(this.selectedRoom.id); // emit join event to inform the server that the client have selected a room.
this.users = await Promise.all(room.users.map(id => srv.getUserByid(id)));
this.messages = await srv.getAllMessagesInRoom(room);
},
Expand Down Expand Up @@ -138,20 +138,23 @@ export default defineComponent({
// update room list
await this.fetchRooms();
},
async joinRoom(id: string) {
async joinRoom(id: string,pseudo: string) {
if (this.loggedUser === null) {
console.warn("trying to join room without being logged in.")
return;
}
const room = await srv.getRoom(id);
// add current user to the room
await srv.addUserToRoom(room,this.loggedUser);
const roomsUser = await srv.getUserByid(id);
this.roomEditorHidden = true;
await srv.updateUser(this.loggedUser.id,pseudo,"", [ roomsUser.rooms.push(room.id) ];
await this.fetchRooms();
},
async onLogin(pseudo: string) {
this.loggedUser = await srv.getOrCreateUser(pseudo);
await this.fetchRooms();
console.log(this.loggedUser.id)
srv.login(this.loggedUser.id); // send the client id to register in backend server
},
},
Expand Down
21 changes: 13 additions & 8 deletions front/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ interface ServerToClientEvents {
}

interface ClientToServerEvents {
join: (roomId: string) => void,
login: (id: string) => void,
join: (roomId: {roomId: string} ) => void,
login: (id: {userId: string}) => void,
start_writing: () => void,
stop_writing: () => void,
}
Expand Down Expand Up @@ -270,12 +270,14 @@ export class ScratchyService {
* @augments ScratchyService
* @param {string} userPseudo - a user pseudo
* @param {string} userProfileImage - profile image link
* @param {[string]} rooms - Array of room where the user is
* @returns {Promise<User>} - user id
*/
async createUser(userPseudo: string, userProfileImage: string): Promise<User> {
async createUser(userPseudo: string, userProfileImage: string,rooms:[string]): Promise<User> {
const response = await axios.post(this.url + this.apiNamespace + "/user", {
pseudo: userPseudo,
profileImage: userProfileImage,
rooms : rooms,
});
this.userCache.set(response.data.id, response.data);
this.userPseudoMap.set(response.data.pseudo, response.data.id);
Expand Down Expand Up @@ -306,9 +308,10 @@ export class ScratchyService {
* @param {string} userID - user id
* @param {string} userPseudo - a user's pseudo
* @param {string} userProfileImage - profile image link
* @param {Room} Rooms - Array of room where the user is
* @returns {Promise<User>} - user information
*/
async updateUser(userID: string, userPseudo: string, userProfileImage: string): Promise<User> {
async updateUser(userID: string, userPseudo: string, userProfileImage: string,rooms: [string]): Promise<User> {
// update cache
if (this.userCache.has(userID)) {
const user = this.userCache.get(userID) as User;
Expand All @@ -318,11 +321,13 @@ export class ScratchyService {
}
user.pseudo = userPseudo;
user.profileImage = userProfileImage;
user.rooms = rooms
this.userCache.set(userID, user);
}
const response = await axios.put(this.url + this.apiNamespace + "/user/" + userID, {
pseudo: userPseudo,
profileImage: userProfileImage,
rooms: rooms
});
return response.data;
}
Expand Down Expand Up @@ -352,7 +357,7 @@ export class ScratchyService {
async getOrCreateUser(pseudo: string): Promise<User> {
let user = await this.userExistByPseudo(pseudo);
if (user === undefined) {
user = await this.createUser(pseudo, "");
user = await this.createUser(pseudo,"",[""]);
}
return user;
}
Expand Down Expand Up @@ -448,11 +453,11 @@ export class ScratchyService {
*
* @async
* @augments ScratchyService
* @param {string} id - room id , eg:60895dd62d1a706830c31f10
* @param {string} roomid - room id , eg:60895dd62d1a706830c31f10
* @returns {Promise<undefined>} -
*/
async join(roomId:string): Promise<void> {
this.socket.emit('join',roomId)
this.socket.emit('join',{ roomId :roomId });
}

/**
Expand All @@ -463,7 +468,7 @@ export class ScratchyService {
* @returns {Promise<undefined>} -
*/
async login(id:string): Promise<void> {
this.socket.volatile.emit("login", id);
this.socket.emit("login", { userId : id });
}

/**
Expand Down