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(),
|
createdAt: new Date(),
|
||||||
movie: torrent.name,
|
movie: torrent.name,
|
||||||
position: 0,
|
position: 0,
|
||||||
|
syncedAt: new Date(),
|
||||||
};
|
};
|
||||||
const doc = new RoomModel(room);
|
const doc = new RoomModel(room);
|
||||||
|
|
||||||
@@ -58,15 +59,20 @@ router.get('/room', async (ctx) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/position', 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';
|
ctx.body = 'success';
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/position', async (ctx) => {
|
router.get('/position', async (ctx) => {
|
||||||
const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec();
|
const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec();
|
||||||
if (room) {
|
if (room) {
|
||||||
|
const elapsedTime = new Date().getTime() - new Date(room.syncedAt).getTime();
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
position: room.position,
|
position: room.position + elapsedTime / 1000,
|
||||||
|
syncedAt: room.syncedAt,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
({ status: ctx.status, body: ctx.body } = errorResponse('room-00', 'Room not found'));
|
({ status: ctx.status, body: ctx.body } = errorResponse('room-00', 'Room not found'));
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export interface Room {
|
|||||||
downloaded?: boolean;
|
downloaded?: boolean;
|
||||||
downloadedAt?: Date;
|
downloadedAt?: Date;
|
||||||
position: number;
|
position: number;
|
||||||
|
syncedAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const roomSchema = new Schema<Room>({
|
export const roomSchema = new Schema<Room>({
|
||||||
@@ -20,4 +21,5 @@ export const roomSchema = new Schema<Room>({
|
|||||||
downloaded: { type: Boolean, required: false },
|
downloaded: { type: Boolean, required: false },
|
||||||
downloadedAt: { type: Date, required: false },
|
downloadedAt: { type: Date, required: false },
|
||||||
position: { type: Number, required: true },
|
position: { type: Number, required: true },
|
||||||
|
syncedAt: { type: Date, required: true },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user