This commit is contained in:
Logan Hunt 2022-04-01 14:16:35 -06:00
parent f62854ebaa
commit 1108970a6a
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
6 changed files with 44 additions and 26 deletions

View File

@ -28,8 +28,8 @@ export const Home = () => {
}; };
const joinRoom = async (id, userPosition) => { const joinRoom = async (id, userPosition) => {
const res = await api.get(`/chat_rooms/${id}/joinable?lat=${userPosition.lat}&lng=${userPosition.lng}`); const joinable = await api.get(`/chat_rooms/${id}/joinable?lat=${userPosition.lat}&lng=${userPosition.lng}`);
if (res) { if (joinable) {
navigate(`/rooms/${id}`); navigate(`/rooms/${id}`);
} }
}; };

View File

@ -53,17 +53,12 @@ export const Geoman = ({ user, userPos, joinRoom }) => {
let dontRedirect = true; let dontRedirect = true;
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.isEditable, icon }); const marker = new L.Marker(chatRoom.center, { pmIgnore: !chatRoom.editable, icon });
circle.setStyle( console.log(chatRoom);
chatRoom.isEditable circle.setStyle(chatRoom.editable ? editable : chatRoom.joinable ? joinable : unjoinable); // We only send the id when user is in the radius
? editable
: haversine(userPos, { lat: chatRoom.latitude, lng: chatRoom.longitude }) < chatRoom.radius
? joinable
: unjoinable,
);
marker.addEventListener('click', () => { marker.addEventListener('click', () => {
setTimeout(() => { setTimeout(() => {
if (dontRedirect) { if (!dontRedirect) {
joinRoom(chatRoom.id, userPos); joinRoom(chatRoom.id, userPos);
return; return;
} }
@ -74,7 +69,7 @@ export const Geoman = ({ user, userPos, joinRoom }) => {
marker.on('mouseover', (e) => { marker.on('mouseover', (e) => {
e.target.openPopup(); e.target.openPopup();
}); });
if (chatRoom.isEditable) { if (chatRoom.editable) {
[circle, marker].map((x) => { [circle, marker].map((x) => {
x.on('pm:edit', (e) => { x.on('pm:edit', (e) => {
const coords = e.target.getLatLng(); const coords = e.target.getLatLng();
@ -122,7 +117,6 @@ export const Geoman = ({ user, userPos, joinRoom }) => {
circleAndMarkerFromChatroom({ circleAndMarkerFromChatroom({
center: [x.latitude, x.longitude], center: [x.latitude, x.longitude],
...x, ...x,
isEditable: user && x.userId == user.id,
}); });
}); });
layersToRemove.map((x) => context.map.removeLayer(x)); layersToRemove.map((x) => context.map.removeLayer(x));

View File

@ -23,18 +23,35 @@ export class ChatRoomController {
@Get('/chat_rooms') @Get('/chat_rooms')
async get(@JwtBody() jwtBody: JwtBodyDto, @Query() query: any) { async get(@JwtBody() jwtBody: JwtBodyDto, @Query() query: any) {
return await this.chatRoomService.nearOrUserOwns({ ...query, userId: jwtBody.userId }); const user = await this.usersService.find(jwtBody.userId);
const rooms = await this.chatRoomService.nearOrUserOwns({ ...query, userId: jwtBody.userId });
return rooms.map((cr) => {
const editable = cr.userId === user.id;
const joinable = editable || haversine({ lat: cr.latitude, lng: cr.longitude }, query) <= cr.radius;
return joinable
? { ...cr, editable, joinable }
: {
name: cr.name,
latitude: cr.latitude,
longitude: cr.longitude,
radius: cr.radius,
editable,
joinable,
};
});
} }
@Get('/chat_rooms/:id') @Get('/chat_rooms/:id')
async getId(@Param('id') id: number) { async getId(@Param('id') id: string) {
return await this.chatRoomService.findById(id); return await this.chatRoomService.findById(id);
} }
@Get('/chat_rooms/:id/joinable') @Get('/chat_rooms/:id/joinable')
async joinable(@JwtBody() jwtBody, @Param('id') id: number, @Query() query: any) { async joinable(@JwtBody() jwtBody: JwtBodyDto, @Param('id') id: string, @Query() query: any) {
return !!(await this.chatRoomService.nearOrUserOwns({ ...query, userId: jwtBody.userId })).find( return !!(await this.chatRoomService.nearOrUserOwns({ ...query, userId: jwtBody.userId })).find(
(cr) => cr.id == id && haversine({ lat: cr.latitude, lng: cr.longitude }, query) < cr.radius, (cr) =>
cr.id == id &&
(haversine({ lat: cr.latitude, lng: cr.longitude }, query) <= cr.radius || cr.userId === jwtBody.userId),
); );
} }
@ -44,7 +61,7 @@ export class ChatRoomController {
return await this.chatRoomService.create(chatRoom); return await this.chatRoomService.create(chatRoom);
} }
private async authorized(jwtBody: JwtBodyDto, chatRoom: any) { private async userCanEdit(jwtBody: JwtBodyDto, chatRoom: any) {
const user = await this.usersService.find(jwtBody.userId); const user = await this.usersService.find(jwtBody.userId);
if (user.id !== chatRoom.user.id) { if (user.id !== chatRoom.user.id) {
return { return {
@ -55,10 +72,10 @@ export class ChatRoomController {
} }
@Put('/chat_rooms/:id') @Put('/chat_rooms/:id')
async update(@JwtBody() jwtBody: JwtBodyDto, @Param('id') id: number, @Body() chatRoom: any) { async update(@JwtBody() jwtBody: JwtBodyDto, @Param('id') id: string, @Body() chatRoom: any) {
console.log(id); console.log(id);
const chat_room = await this.chatRoomService.findById(id, ['user']); const chat_room = await this.chatRoomService.findById(id, ['user']);
if (!(await this.authorized(jwtBody, chat_room))) { if (!(await this.userCanEdit(jwtBody, chat_room))) {
return chat_room; return chat_room;
} }
chat_room.latitude = chatRoom.latitude; chat_room.latitude = chatRoom.latitude;
@ -68,9 +85,9 @@ export class ChatRoomController {
} }
@Delete('/chat_rooms/:id') @Delete('/chat_rooms/:id')
async delete(@JwtBody() jwtBody: JwtBodyDto, @Param('id') id: number) { async delete(@JwtBody() jwtBody: JwtBodyDto, @Param('id') id: string) {
const chat_room = await this.chatRoomService.findById(id, ['user']); const chat_room = await this.chatRoomService.findById(id, ['user']);
if (!(await this.authorized(jwtBody, chat_room))) { if (!(await this.userCanEdit(jwtBody, chat_room))) {
return false; return false;
} }
return await this.chatRoomService.remove(chat_room); return await this.chatRoomService.remove(chat_room);

View File

@ -1,16 +1,22 @@
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm'; import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';
import { uniqueId } from 'lodash';
export class AddChatRoom1648605030863 implements MigrationInterface { export class AddChatRoom1648605030863 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> { public async up(queryRunner: QueryRunner): Promise<void> {
// uuid from https://github.com/typeorm/typeorm/issues/3770
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
await queryRunner.createTable( await queryRunner.createTable(
new Table({ new Table({
name: 'chat_room', name: 'chat_room',
columns: [ columns: [
{ {
name: 'id', name: 'id',
type: 'int', type: 'text',
isPrimary: true, isPrimary: true,
isGenerated: true, isUnique: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
}, },
{ {
name: 'userId', name: 'userId',
@ -39,6 +45,7 @@ export class AddChatRoom1648605030863 implements MigrationInterface {
}, },
], ],
}), }),
true,
); );
await queryRunner.createForeignKey( await queryRunner.createForeignKey(

View File

@ -4,7 +4,7 @@ import { User } from './user.entity';
@Entity() @Entity()
export class ChatRoom { export class ChatRoom {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: string;
@Column() @Column()
latitude: number; latitude: number;

View File

@ -25,7 +25,7 @@ export class ChatRoomService {
); );
} }
findById(id: number, relations: string[] = []) { findById(id: string, relations: string[] = []) {
return this.chatRoomRepository.findOne(id, { relations }); return this.chatRoomRepository.findOne(id, { relations });
} }