mirror of
https://github.com/anatolykopyl/movieroom-core.git
synced 2026-03-26 12:54:51 +00:00
More logical syncronisation
This commit is contained in:
10
src/index.ts
10
src/index.ts
@@ -31,6 +31,7 @@ router.post('/room', async (ctx) => {
|
||||
createdAt: new Date(),
|
||||
movie: torrent.name,
|
||||
position: 0,
|
||||
syncedAt: new Date(),
|
||||
};
|
||||
const doc = new RoomModel(room);
|
||||
|
||||
@@ -58,15 +59,20 @@ router.get('/room', async (ctx) => {
|
||||
});
|
||||
|
||||
router.post('/position', async (ctx) => {
|
||||
await RoomModel.updateOne({ id: ctx.request.body.id }, { position: Number(ctx.request.body.position) });
|
||||
await RoomModel.updateOne({ id: ctx.request.body.id }, {
|
||||
position: Number(ctx.request.body.position),
|
||||
syncedAt: new Date(),
|
||||
});
|
||||
ctx.body = 'success';
|
||||
});
|
||||
|
||||
router.get('/position', async (ctx) => {
|
||||
const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec();
|
||||
if (room) {
|
||||
const elapsedTime = new Date().getTime() - new Date(room.syncedAt).getTime();
|
||||
ctx.body = {
|
||||
position: room.position,
|
||||
position: room.position + elapsedTime / 1000,
|
||||
syncedAt: room.syncedAt,
|
||||
};
|
||||
} else {
|
||||
({ status: ctx.status, body: ctx.body } = errorResponse('room-00', 'Room not found'));
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface Room {
|
||||
downloaded?: boolean;
|
||||
downloadedAt?: Date;
|
||||
position: number;
|
||||
syncedAt: Date;
|
||||
}
|
||||
|
||||
export const roomSchema = new Schema<Room>({
|
||||
@@ -20,4 +21,5 @@ export const roomSchema = new Schema<Room>({
|
||||
downloaded: { type: Boolean, required: false },
|
||||
downloadedAt: { type: Date, required: false },
|
||||
position: { type: Number, required: true },
|
||||
syncedAt: { type: Date, required: true },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user