Files
new-phone-who-dis/resources/server/sv_photo.ts
T
Taso 1aa6a383ab refactor(server/source-handling): Altered how we handle source
These changes are made in case source is not guranteed to remain the same

Due to how JS should operate, this shouldn't be an issue but this just ensures that
2021-02-14 20:17:41 -05:00

61 lines
1.9 KiB
TypeScript

import events from '../utils/events';
import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { IPhoto } from '../../phone/src/common/typings/photo';
import { mainLogger } from './sv_logger';
const photoLogger = mainLogger.child({ module: 'photo' });
async function uploadPhoto(identifier: string, image: string) {
const query =
'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)';
await pool.query(query, [identifier, image]);
}
async function getPhotosByIdentifier(identifier: string): Promise<IPhoto[]> {
const query =
'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC';
const [results] = await pool.query(query, [identifier]);
const photos = <IPhoto[]>results;
return photos;
}
async function deletePhoto(photo: IPhoto, identifier: string) {
const query =
'DELETE FROM npwd_phone_gallery WHERE id = ? AND identifier = ?';
await pool.query(query, [photo.id, identifier]);
}
onNet(events.CAMERA_UPLOAD_PHOTO, async (image: string) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await uploadPhoto(identifier, image);
emitNet(events.CAMERA_UPLOAD_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to upload photo, ${e.message}`, {
source: _source,
});
}
});
onNet(events.CAMERA_FETCH_PHOTOS, async () => {
const _source = getSource();
const identifier = getIdentifier(_source);
const photos = await getPhotosByIdentifier(identifier);
emitNet(events.CAMERA_SEND_PHOTOS, source, photos);
});
onNet(events.CAMERA_DELETE_PHOTO, async (photo: IPhoto) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await deletePhoto(photo, identifier);
emitNet(events.CAMERA_DELETE_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
});
}
});