Files
new-phone-who-dis/resources/server/sv_photo.ts
T
Taso b5fca6b35b cleanup: Recoil State Type Safety + Misc Cleanup (#189)
* chore(phone/settings): Removed unused state

* cleanup(phone/photos): Added type safety to photo state

* tweak(phone/marketplace): Added type safety to marketplace state

* chore(phone/match): Removed unnecessary console.log

* tweak(phone/messages): Added type safety to messages state

Also fixed ESLint warning for exported anonymous hook in useMessages.ts

* tweak(phone/match): Added type safety to match state

* tweak(phone/dialer): Cleanup & type safety for state

Renamed some types to better represent their data

* cleanup(phone/gen): Added NUI event enums for phone events

* tweak(phone/contacts): Type safety for contacts state & variable rename

Renamed and type variable in setFilteredContacts to better represent its use

* tweak(phone/gen-state): Added type-safety to gen phone global state

* tweak(phone/settings): Type safety for settings state

* tweak(phone/photos): Tweaked type for photo referral

* fix(phone/notification-comp): Fix reference to old theme config

* chore(phone/hooks): Remove keyboard service

Removed as this was unused and ESLint was not happy with it

* chore(phone): Cleaned up eslint warnings

No more warnings hooray!

* tweak(phone): Set time in dev mode

* fix(phone): Remove useNuiService that appeared somehow

* fix(phone/gallery): Fixed missing import for gallery

* Revert removing the keyboard service as this is built upon in another PR

This reverts commit 82d12255a84e918da087d988b599af98ce1fe54d.

* fix(phone/keyboard): Add useKeyboardService hook back to Phone.tsx

* fix(phone/gallery): Fix and standardized GalleryPhoto type
2021-04-11 10:01:38 -04:00

63 lines
2.1 KiB
TypeScript

import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { GalleryPhoto, PhotoEvents } from '../../typings/photo';
import { mainLogger } from './sv_logger';
const photoLogger = mainLogger.child({ module: 'photo' });
async function uploadPhoto(identifier: string, image: string): Promise<IPhoto> {
const query = 'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)';
const [results] = (await pool.query(query, [identifier, image])) as any;
return { id: results.insertId, image };
}
async function getPhotosByIdentifier(identifier: string): Promise<GalleryPhoto[]> {
const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC';
const [results] = await pool.query(query, [identifier]);
return <GalleryPhoto[]>results;
}
async function deletePhoto(photo: GalleryPhoto, identifier: string) {
const query = 'DELETE FROM npwd_phone_gallery WHERE image = ? AND identifier = ?';
await pool.query(query, [photo.image, identifier]);
}
onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const photo = await uploadPhoto(identifier, image);
emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source, photo);
} catch (e) {
photoLogger.error(`Failed to upload photo, ${e.message}`, {
source: _source,
});
}
});
onNet(PhotoEvents.FETCH_PHOTOS, async () => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const photos = await getPhotosByIdentifier(identifier);
emitNet(PhotoEvents.SEND_PHOTOS, _source, photos);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
});
}
});
onNet(PhotoEvents.DELETE_PHOTO, async (photo: GalleryPhoto) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await deletePhoto(photo, identifier);
emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
});
}
});