mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
feat(camera): refactor to new promise event format
This commit is contained in:
1 parent
1566bb0fdb
commit
4e8c57492b
15 files changed
+274
-141
No files matched your search
@@ -2,24 +2,52 @@ import { getSource } from '../utils/miscUtils';
|
||||
import { GalleryPhoto, PhotoEvents } from '../../../typings/photo';
|
||||
import PhotoService from './photo.service';
|
||||
import { photoLogger } from './photo.utils';
|
||||
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
|
||||
|
||||
onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => {
|
||||
/*onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => {
|
||||
const src = getSource();
|
||||
PhotoService.handleUploadPhoto(src, image).catch((e) =>
|
||||
photoLogger.error(`Error occurred in upload photo event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
});*/
|
||||
|
||||
onNetPromise<string, GalleryPhoto>(PhotoEvents.UPLOAD_PHOTO, (reqObj, resp) => {
|
||||
PhotoService.handleUploadPhoto(reqObj, resp).catch((e) => {
|
||||
photoLogger.error(
|
||||
`Error occurred in upload photo event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(PhotoEvents.FETCH_PHOTOS, async () => {
|
||||
onNetPromise<void, GalleryPhoto[]>(PhotoEvents.FETCH_PHOTOS, (reqObj, resp) => {
|
||||
PhotoService.handleFetchPhotos(reqObj, resp).catch((e) => {
|
||||
photoLogger.error(
|
||||
`Error occurred in upload photo event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
/*onNet(PhotoEvents.FETCH_PHOTOS, async () => {
|
||||
const src = getSource();
|
||||
PhotoService.handleFetchPhotos(src).catch((e) =>
|
||||
photoLogger.error(`Error occurred in upload photo event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
});*/
|
||||
|
||||
onNetPromise<GalleryPhoto, void>(PhotoEvents.DELETE_PHOTO, (reqObj, resp) => {
|
||||
PhotoService.handleDeletePhoto(reqObj, resp).catch((e) => {
|
||||
photoLogger.error(
|
||||
`Error occurred in delete photo event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(PhotoEvents.DELETE_PHOTO, async (photo: GalleryPhoto) => {
|
||||
/*onNet(PhotoEvents.DELETE_PHOTO, async (photo: GalleryPhoto) => {
|
||||
const src = getSource();
|
||||
PhotoService.handleDeletePhoto(src, photo).catch((e) =>
|
||||
photoLogger.error(`Error occurred in delete photo event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
});
|
||||
});*/
|
||||
@@ -2,6 +2,7 @@ import PlayerService from '../players/player.service';
|
||||
import { GalleryPhoto, PhotoEvents } from '../../../typings/photo';
|
||||
import PhotoDB, { _PhotoDB } from './photo.db';
|
||||
import { photoLogger } from './photo.utils';
|
||||
import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types';
|
||||
|
||||
class _PhotoService {
|
||||
private readonly photoDB: _PhotoDB;
|
||||
@@ -11,39 +12,51 @@ class _PhotoService {
|
||||
photoLogger.debug('Photo service started');
|
||||
}
|
||||
|
||||
async handleUploadPhoto(src: number, image: string) {
|
||||
async handleUploadPhoto(
|
||||
reqObj: PromiseRequest<string>,
|
||||
resp: PromiseEventResp<GalleryPhoto>,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
const photo = await this.photoDB.uploadPhoto(identifier, image);
|
||||
emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, src, photo);
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
const photo = await this.photoDB.uploadPhoto(identifier, reqObj.data);
|
||||
|
||||
resp({ status: 'ok', data: photo });
|
||||
} catch (e) {
|
||||
photoLogger.error(`Failed to upload photo, ${e.message}`, {
|
||||
source: src,
|
||||
source: reqObj.source,
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleFetchPhotos(src: number) {
|
||||
async handleFetchPhotos(reqObj: PromiseRequest<void>, resp: PromiseEventResp<GalleryPhoto[]>) {
|
||||
try {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
const photos = await this.photoDB.getPhotosByIdentifier(identifier);
|
||||
emitNet(PhotoEvents.SEND_PHOTOS, src, photos);
|
||||
|
||||
resp({ status: 'ok', data: photos });
|
||||
} catch (e) {
|
||||
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
|
||||
source: src,
|
||||
source: reqObj.source,
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleDeletePhoto(src: number, photo: GalleryPhoto) {
|
||||
async handleDeletePhoto(
|
||||
reqObj: PromiseRequest<GalleryPhoto>,
|
||||
resp: PromiseEventResp<void>,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
await this.photoDB.deletePhoto(photo, identifier);
|
||||
emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, src);
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
await this.photoDB.deletePhoto(reqObj.data, identifier);
|
||||
|
||||
resp({ status: 'ok' });
|
||||
} catch (e) {
|
||||
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
|
||||
source: src,
|
||||
photoLogger.error(`Failed to delete photo, ${e.message}`, {
|
||||
source: reqObj.source,
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user