-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto refresh on person thumbnail
- Loading branch information
Showing
9 changed files
with
122 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
import { CommunicationEvent } from '@app/domain'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { CommunicationGateway } from '../communication.gateway'; | ||
import { AuthService, CommunicationEvent, ICommunicationRepository, serverVersion } from '@app/domain'; | ||
import { Logger } from '@nestjs/common'; | ||
import { OnGatewayConnection, OnGatewayDisconnect, WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; | ||
import { Server, Socket } from 'socket.io'; | ||
|
||
@Injectable() | ||
export class CommunicationRepository { | ||
constructor(private ws: CommunicationGateway) {} | ||
@WebSocketGateway({ cors: true }) | ||
export class CommunicationRepository implements OnGatewayConnection, OnGatewayDisconnect, ICommunicationRepository { | ||
private logger = new Logger(CommunicationRepository.name); | ||
|
||
constructor(private authService: AuthService) {} | ||
|
||
@WebSocketServer() server!: Server; | ||
|
||
async handleConnection(client: Socket) { | ||
try { | ||
this.logger.log(`New websocket connection: ${client.id}`); | ||
const user = await this.authService.validate(client.request.headers, {}); | ||
if (user) { | ||
client.join(user.id); | ||
this.send(CommunicationEvent.SERVER_VERSION, user.id, serverVersion); | ||
} else { | ||
client.emit('error', 'unauthorized'); | ||
client.disconnect(); | ||
} | ||
} catch (e) { | ||
client.emit('error', 'unauthorized'); | ||
client.disconnect(); | ||
} | ||
} | ||
|
||
handleDisconnect(client: Socket) { | ||
client.leave(client.nsp.name); | ||
this.logger.log(`Client ${client.id} disconnected from Websocket`); | ||
} | ||
|
||
send(event: CommunicationEvent, userId: string, data: any) { | ||
this.ws.server.to(userId).emit(event, JSON.stringify(data)); | ||
this.server.to(userId).emit(event, JSON.stringify(data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
import { io, Socket } from 'socket.io-client'; | ||
import type { AssetResponseDto } from '../../api/open-api'; | ||
import type { AssetResponseDto, ServerVersionResponseDto } from '@api'; | ||
import { io } from 'socket.io-client'; | ||
import { writable } from 'svelte/store'; | ||
|
||
let websocket: Socket; | ||
|
||
function initWebsocketStore() { | ||
const onUploadSuccess = writable<AssetResponseDto>(); | ||
const onAssetDelete = writable<string>(); | ||
|
||
return { | ||
onUploadSuccess, | ||
onAssetDelete, | ||
}; | ||
interface PersonFace { | ||
assetId: string; | ||
personId: string; | ||
} | ||
|
||
export const websocketStore = initWebsocketStore(); | ||
export const websocketStore = { | ||
onUploadSuccess: writable<AssetResponseDto>(), | ||
onAssetDelete: writable<string>(), | ||
onPersonThumbnail: writable<PersonFace>(), | ||
serverVersion: writable<ServerVersionResponseDto>(), | ||
connected: writable<boolean>(false), | ||
}; | ||
|
||
export const openWebsocketConnection = () => { | ||
try { | ||
websocket = io('', { | ||
const websocket = io('', { | ||
path: '/api/socket.io', | ||
transports: ['polling'], | ||
reconnection: true, | ||
forceNew: true, | ||
autoConnect: true, | ||
}); | ||
|
||
listenToEvent(websocket); | ||
websocket | ||
.on('connect', () => websocketStore.connected.set(true)) | ||
.on('disconnect', () => websocketStore.connected.set(false)) | ||
.on('on_upload_success', (data) => websocketStore.onUploadSuccess.set(JSON.parse(data) as AssetResponseDto)) | ||
.on('on_asset_delete', (data) => websocketStore.onAssetDelete.set(JSON.parse(data) as string)) | ||
.on('on_person_thumbnail', (data) => { | ||
console.log(data); | ||
websocketStore.onPersonThumbnail.set(JSON.parse(data) as PersonFace); | ||
}) | ||
.on('on_server_version', (data) => websocketStore.serverVersion.set(JSON.parse(data) as ServerVersionResponseDto)) | ||
.on('error', (e) => console.log('Websocket Error', e)); | ||
|
||
return () => websocket?.close(); | ||
} catch (e) { | ||
console.log('Cannot connect to websocket ', e); | ||
} | ||
}; | ||
|
||
const listenToEvent = async (socket: Socket) => { | ||
socket.on('on_upload_success', (data) => websocketStore.onUploadSuccess.set(JSON.parse(data) as AssetResponseDto)); | ||
socket.on('on_asset_delete', (data) => websocketStore.onAssetDelete.set(JSON.parse(data) as string)); | ||
socket.on('error', (e) => console.log('Websocket Error', e)); | ||
}; | ||
|
||
export const closeWebsocketConnection = () => { | ||
websocket?.close(); | ||
}; |
Oops, something went wrong.