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 res = await api.get(`/chat_rooms/${id}/joinable?lat=${userPosition.lat}&lng=${userPosition.lng}`);
if (res) {
const joinable = await api.get(`/chat_rooms/${id}/joinable?lat=${userPosition.lat}&lng=${userPosition.lng}`);
if (joinable) {
navigate(`/rooms/${id}`);
}
};

View File

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

View File

@ -23,18 +23,35 @@ export class ChatRoomController {
@Get('/chat_rooms')
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')
async getId(@Param('id') id: number) {
async getId(@Param('id') id: string) {
return await this.chatRoomService.findById(id);
}
@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(
(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);
}
private async authorized(jwtBody: JwtBodyDto, chatRoom: any) {
private async userCanEdit(jwtBody: JwtBodyDto, chatRoom: any) {
const user = await this.usersService.find(jwtBody.userId);
if (user.id !== chatRoom.user.id) {
return {
@ -55,10 +72,10 @@ export class ChatRoomController {
}
@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);
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;
}
chat_room.latitude = chatRoom.latitude;
@ -68,9 +85,9 @@ export class ChatRoomController {
}
@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']);
if (!(await this.authorized(jwtBody, chat_room))) {
if (!(await this.userCanEdit(jwtBody, chat_room))) {
return false;
}
return await this.chatRoomService.remove(chat_room);

View File

@ -1,16 +1,22 @@
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';
import { uniqueId } from 'lodash';
export class AddChatRoom1648605030863 implements MigrationInterface {
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(
new Table({
name: 'chat_room',
columns: [
{
name: 'id',
type: 'int',
type: 'text',
isPrimary: true,
isGenerated: true,
isUnique: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
},
{
name: 'userId',
@ -39,6 +45,7 @@ export class AddChatRoom1648605030863 implements MigrationInterface {
},
],
}),
true,
);
await queryRunner.createForeignKey(

View File

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