summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/components/home/_home.jsx4
-rw-r--r--client/components/map/chat_room_geoman.jsx16
-rw-r--r--server/controllers/chat_room.controller.ts35
-rw-r--r--server/database/migrations/1648605030863-AddChatRoom.ts11
-rw-r--r--server/entities/chat_room.entity.ts2
-rw-r--r--server/providers/services/chat_room.service.ts2
6 files changed, 44 insertions, 26 deletions
diff --git a/client/components/home/_home.jsx b/client/components/home/_home.jsx
index 213d43e..a31fd63 100644
--- a/client/components/home/_home.jsx
+++ b/client/components/home/_home.jsx
@@ -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}`);
}
};
diff --git a/client/components/map/chat_room_geoman.jsx b/client/components/map/chat_room_geoman.jsx
index c4655ee..a39b45a 100644
--- a/client/components/map/chat_room_geoman.jsx
+++ b/client/components/map/chat_room_geoman.jsx
@@ -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));
diff --git a/server/controllers/chat_room.controller.ts b/server/controllers/chat_room.controller.ts
index 668a686..3917e54 100644
--- a/server/controllers/chat_room.controller.ts
+++ b/server/controllers/chat_room.controller.ts
@@ -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);
diff --git a/server/database/migrations/1648605030863-AddChatRoom.ts b/server/database/migrations/1648605030863-AddChatRoom.ts
index 4a6c156..747cdd3 100644
--- a/server/database/migrations/1648605030863-AddChatRoom.ts
+++ b/server/database/migrations/1648605030863-AddChatRoom.ts
@@ -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(
diff --git a/server/entities/chat_room.entity.ts b/server/entities/chat_room.entity.ts
index 29619be..2981589 100644
--- a/server/entities/chat_room.entity.ts
+++ b/server/entities/chat_room.entity.ts
@@ -4,7 +4,7 @@ import { User } from './user.entity';
@Entity()
export class ChatRoom {
@PrimaryGeneratedColumn()
- id: number;
+ id: string;
@Column()
latitude: number;
diff --git a/server/providers/services/chat_room.service.ts b/server/providers/services/chat_room.service.ts
index ed75ba4..6d804e6 100644
--- a/server/providers/services/chat_room.service.ts
+++ b/server/providers/services/chat_room.service.ts
@@ -25,7 +25,7 @@ export class ChatRoomService {
);
}
- findById(id: number, relations: string[] = []) {
+ findById(id: string, relations: string[] = []) {
return this.chatRoomRepository.findOne(id, { relations });
}