Fix some bugs
This commit is contained in:
parent
dbb9eea25f
commit
638b3e0750
@ -11,7 +11,7 @@ import { generateGruvboxFromString } from '../../utils/generate_gruvbox';
|
|||||||
export const ChatRoom = () => {
|
export const ChatRoom = () => {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
const [chatRoom, setChatRoom] = useState('');
|
const [chatRoom, setChatRoom] = useState('');
|
||||||
const [connections, messages, sendMessage] = useChat(chatRoom);
|
const [active, connections, messages, sendMessage] = useChat(chatRoom);
|
||||||
const [message, setMessage] = useState('');
|
const [message, setMessage] = useState('');
|
||||||
const [color, setColor] = useState(generateGruvboxFromString('placeholder'));
|
const [color, setColor] = useState(generateGruvboxFromString('placeholder'));
|
||||||
const [user, setUser] = useState({});
|
const [user, setUser] = useState({});
|
||||||
@ -78,17 +78,25 @@ export const ChatRoom = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<textarea
|
{active ? (
|
||||||
placeholder={'Message'}
|
<>
|
||||||
className="input"
|
<textarea
|
||||||
onChange={(e) => setMessage(e.target.value)}
|
placeholder={'Message'}
|
||||||
value={message}
|
className="input"
|
||||||
rows={1}
|
onChange={(e) => setMessage(e.target.value)}
|
||||||
cols={30}
|
value={message}
|
||||||
></textarea>
|
rows={1}
|
||||||
<div className="button" onClick={sendThisMessage}>
|
cols={30}
|
||||||
Send
|
></textarea>
|
||||||
</div>
|
<div className="button" onClick={sendThisMessage}>
|
||||||
|
Send
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
<p>This room has been marked inactive and has been deleted.</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className="button">
|
<div className="button">
|
||||||
<Link to="/">Back to map</Link>
|
<Link to="/">Back to map</Link>
|
||||||
</div>
|
</div>
|
||||||
|
@ -51,7 +51,7 @@ const haversine = (p1, p2) => {
|
|||||||
export const Geoman = ({ user, userPos, joinRoom }) => {
|
export const Geoman = ({ user, userPos, joinRoom }) => {
|
||||||
const context = useLeafletContext();
|
const context = useLeafletContext();
|
||||||
const api = useContext(ApiContext);
|
const api = useContext(ApiContext);
|
||||||
let dontRedirect = true;
|
let dontRedirect = false;
|
||||||
const circleAndMarkerFromChatroom = (chatRoom) => {
|
const circleAndMarkerFromChatroom = (chatRoom) => {
|
||||||
const circle = new L.Circle(chatRoom.center, chatRoom.radius);
|
const circle = new L.Circle(chatRoom.center, chatRoom.radius);
|
||||||
const marker = new L.Marker(chatRoom.center, { pmIgnore: !chatRoom.editable, icon });
|
const marker = new L.Marker(chatRoom.center, { pmIgnore: !chatRoom.editable, icon });
|
||||||
@ -64,7 +64,7 @@ export const Geoman = ({ user, userPos, joinRoom }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
dontRedirect = false;
|
dontRedirect = false;
|
||||||
}, 500);
|
}, 200);
|
||||||
});
|
});
|
||||||
marker.bindPopup(chatRoom.name || `Chat Room ${chatRoom.id}`);
|
marker.bindPopup(chatRoom.name || `Chat Room ${chatRoom.id}`);
|
||||||
marker.on('mouseover', (e) => {
|
marker.on('mouseover', (e) => {
|
||||||
@ -158,7 +158,7 @@ export const Geoman = ({ user, userPos, joinRoom }) => {
|
|||||||
|
|
||||||
const { lat: latitude, lng: longitude } = shape.layer.getLatLng();
|
const { lat: latitude, lng: longitude } = shape.layer.getLatLng();
|
||||||
const chatRoom = await api.post('/chat_rooms', {
|
const chatRoom = await api.post('/chat_rooms', {
|
||||||
name: prompt("What's the name of the chat room?"),
|
name: prompt("What's the name of the chat room?\n(Chat rooms are deleted after 2 hours of inactivity)"),
|
||||||
latitude,
|
latitude,
|
||||||
longitude,
|
longitude,
|
||||||
radius: shape.layer.getRadius(),
|
radius: shape.layer.getRadius(),
|
||||||
|
@ -5,8 +5,8 @@ import { io } from 'socket.io-client';
|
|||||||
export const useChat = (chatRoom) => {
|
export const useChat = (chatRoom) => {
|
||||||
const [messages, setMessages] = useState([]);
|
const [messages, setMessages] = useState([]);
|
||||||
const [connections, setConnections] = useState([]);
|
const [connections, setConnections] = useState([]);
|
||||||
|
const [active, setActive] = useState(true);
|
||||||
const messageRef = useRef([]);
|
const messageRef = useRef([]);
|
||||||
const connectionsRef = useRef([]);
|
|
||||||
const [socket, setSocket] = useState({});
|
const [socket, setSocket] = useState({});
|
||||||
const [authToken] = useContext(AuthContext);
|
const [authToken] = useContext(AuthContext);
|
||||||
|
|
||||||
@ -28,8 +28,10 @@ export const useChat = (chatRoom) => {
|
|||||||
setMessages([...messageRef.current]);
|
setMessages([...messageRef.current]);
|
||||||
});
|
});
|
||||||
socket.on('userlist', ({ users }) => {
|
socket.on('userlist', ({ users }) => {
|
||||||
connectionsRef.current = users;
|
setConnections([...users]);
|
||||||
setConnections([...connectionsRef.current]);
|
});
|
||||||
|
socket.on('inactive', (id) => {
|
||||||
|
setActive(false);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
socket.off('new-message');
|
socket.off('new-message');
|
||||||
@ -44,5 +46,5 @@ export const useChat = (chatRoom) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return [connections, messages, sendMessage];
|
return [active, connections, messages, sendMessage];
|
||||||
};
|
};
|
||||||
|
@ -19,11 +19,7 @@ const haversine = (p1, p2) => {
|
|||||||
};
|
};
|
||||||
@Controller()
|
@Controller()
|
||||||
export class ChatRoomController {
|
export class ChatRoomController {
|
||||||
constructor(private chatRoomService: ChatRoomService, private usersService: UsersService) {
|
constructor(private chatRoomService: ChatRoomService, private usersService: UsersService) {}
|
||||||
setInterval(() => {
|
|
||||||
console.log('Hello');
|
|
||||||
}, 60 * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get('/chat_rooms')
|
@Get('/chat_rooms')
|
||||||
async get(@JwtBody() jwtBody: JwtBodyDto, @Query() query: any) {
|
async get(@JwtBody() jwtBody: JwtBodyDto, @Query() query: any) {
|
||||||
|
@ -44,7 +44,7 @@ export class AddChatRoom1648605030863 implements MigrationInterface {
|
|||||||
isNullable: true,
|
isNullable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'lastConnection',
|
name: 'lastModified',
|
||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
default: 'now()',
|
default: 'now()',
|
||||||
},
|
},
|
||||||
|
@ -20,7 +20,7 @@ export class ChatRoom {
|
|||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
lastConnection: Date;
|
lastModified: Date;
|
||||||
|
|
||||||
@ManyToOne(() => User, (user) => user.chatRooms)
|
@ManyToOne(() => User, (user) => user.chatRooms)
|
||||||
user: User;
|
user: User;
|
||||||
|
@ -28,7 +28,15 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
|
|||||||
private jwtService: JwtService,
|
private jwtService: JwtService,
|
||||||
private userService: UsersService,
|
private userService: UsersService,
|
||||||
private chatRoomService: ChatRoomService,
|
private chatRoomService: ChatRoomService,
|
||||||
) {}
|
) {
|
||||||
|
setInterval(async () => {
|
||||||
|
const inactiveRooms = await chatRoomService.inactiveRooms();
|
||||||
|
inactiveRooms.forEach((room) => {
|
||||||
|
this.server.to(room.id).emit('inactive', room.id);
|
||||||
|
chatRoomService.remove(room);
|
||||||
|
});
|
||||||
|
}, 5 * (1000 * 60));
|
||||||
|
}
|
||||||
|
|
||||||
afterInit(server: Server) {
|
afterInit(server: Server) {
|
||||||
console.log('Sockets initialized');
|
console.log('Sockets initialized');
|
||||||
@ -38,12 +46,18 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
|
|||||||
try {
|
try {
|
||||||
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
|
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
|
||||||
const user = await this.userService.find(jwtBody.userId);
|
const user = await this.userService.find(jwtBody.userId);
|
||||||
|
|
||||||
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
|
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
|
||||||
await this.chatRoomService.connectUser(chatRoom, user);
|
if (chatRoom) {
|
||||||
client.join(chatRoom.id);
|
chatRoom.lastModified = new Date();
|
||||||
this.server.to(chatRoom.id).emit('userlist', {
|
this.chatRoomService.save(chatRoom);
|
||||||
users: await this.chatRoomService.connectedUsers(chatRoom),
|
|
||||||
});
|
await this.chatRoomService.connectUser(chatRoom, user);
|
||||||
|
client.join(chatRoom.id);
|
||||||
|
this.server.to(chatRoom.id).emit('userlist', {
|
||||||
|
users: await this.chatRoomService.connectedUsers(chatRoom),
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new WsException(e.message);
|
throw new WsException(e.message);
|
||||||
}
|
}
|
||||||
@ -53,11 +67,14 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
|
|||||||
console.log('Client Disconnected');
|
console.log('Client Disconnected');
|
||||||
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
|
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
|
||||||
const user = await this.userService.find(jwtBody.userId);
|
const user = await this.userService.find(jwtBody.userId);
|
||||||
|
|
||||||
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
|
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
|
||||||
await this.chatRoomService.disconnectUser(chatRoom, user);
|
if (chatRoom) {
|
||||||
this.server.to(chatRoom.id).emit('userlist', {
|
await this.chatRoomService.disconnectUser(chatRoom, user);
|
||||||
users: await this.chatRoomService.connectedUsers(chatRoom),
|
this.server.to(chatRoom.id).emit('userlist', {
|
||||||
});
|
users: await this.chatRoomService.connectedUsers(chatRoom),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('message')
|
@SubscribeMessage('message')
|
||||||
@ -67,11 +84,18 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
|
|||||||
@GatewayJwtBody() jwtBody: JwtBodyDto,
|
@GatewayJwtBody() jwtBody: JwtBodyDto,
|
||||||
) {
|
) {
|
||||||
const user = await this.userService.find(jwtBody.userId);
|
const user = await this.userService.find(jwtBody.userId);
|
||||||
this.server.to(client.handshake.query.chatRoomId).emit('new-message', {
|
|
||||||
id: user.id * Math.random() * Math.pow(2, 16) * Date.now(),
|
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
|
||||||
content: data,
|
if (chatRoom) {
|
||||||
userName: `${user.firstName} ${user.lastName}`,
|
chatRoom.lastModified = new Date();
|
||||||
userId: user.id,
|
this.chatRoomService.save(chatRoom);
|
||||||
});
|
|
||||||
|
this.server.to(chatRoom.id).emit('new-message', {
|
||||||
|
id: user.id * Math.random() * Math.pow(2, 16) * Date.now(),
|
||||||
|
content: data,
|
||||||
|
userName: `${user.firstName} ${user.lastName}`,
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository, LessThan } from 'typeorm';
|
||||||
import { ChatRoom } from 'server/entities/chat_room.entity';
|
import { ChatRoom } from 'server/entities/chat_room.entity';
|
||||||
import { User } from 'server/entities/user.entity';
|
import { User } from 'server/entities/user.entity';
|
||||||
import { ChatRoomConnection } from 'server/entities/chat_room_connection.entity';
|
import { ChatRoomConnection } from 'server/entities/chat_room_connection.entity';
|
||||||
@ -73,6 +73,17 @@ export class ChatRoomService {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async inactiveRooms() {
|
||||||
|
const inactiveRooms = await this.chatRoomRepository.find({
|
||||||
|
where: {
|
||||||
|
lastModified: LessThan(new Date(Date.now() - 2 * 60 * (1000 * 60))),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return inactiveRooms.filter(async (room) => {
|
||||||
|
return !(await this.connectedUsers(room)).length;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
save(chatRoom: ChatRoom) {
|
save(chatRoom: ChatRoom) {
|
||||||
return this.chatRoomRepository.save(chatRoom);
|
return this.chatRoomRepository.save(chatRoom);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user