Skip to content

Commit

Permalink
Update sqlhandler to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jul 24, 2023
1 parent d6bac2d commit b22de11
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/render/addStickerModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const addStickerModal = {
downloadActive = false;
addStickerButton.classList.remove('loading');
addStickerButton.firstElementChild.textContent = 'check';
addStickerModal.stickerRenderer.refreshStickerPacks();
}
const data = event.data;
if (data.type === 'download-sticker-pack') {
Expand Down
2 changes: 1 addition & 1 deletion src/render/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const menuBar = {
setUpMenuBarButtons: async () => {
// close on X button
const closeButton = document.getElementById('close-button');
closeButton?.addEventListener('click', () => {
closeButton!.addEventListener('click', () => {
api.closeWindow();
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lineDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function downloadPack(storeURL: string, port: MessagePort, directory: stri
const document = dom.window.document;

const packIDMatch = storeURL.match(packIDRegex);
const packID = packIDMatch?.[1];
const packID = packIDMatch![1];
if (!packID) {
port.postMessage({
type: 'download-sticker-pack',
Expand Down
54 changes: 30 additions & 24 deletions src/utils/sqlHandler.js → src/utils/sqlHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { sqlite3, Database } from "sqlite3";

const sqlite3 = require('sqlite3').verbose();

const sqlHandler = {
db: null,
init: function (path) {
this.db = new sqlite3.Database(path, (err) => {
db: null as Database | null,
init: function (path: string) {
this.db = new sqlite3.Database(path, (err: Error) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the stickers database.');
});
if (this.db === null) {
console.error('Failed to connect to the stickers database.');
return;
}
// Create favorites table if it doesn't exist
// PackID/StickerID key and position value
this.db.run(
this.db!.run(
`CREATE TABLE IF NOT EXISTS favorites (
PackID TEXT NOT NULL,
StickerID TEXT NOT NULL,
Expand All @@ -36,7 +42,7 @@ const sqlHandler = {
*/
getFavorites: function () {
return new Promise((resolve, reject) => {
this.db.all('SELECT * FROM favorites ORDER BY position', (err, rows) => {
this.db!.all('SELECT * FROM favorites ORDER BY position', (err, rows) => {
if (err) {
reject(err);
}
Expand All @@ -49,58 +55,58 @@ const sqlHandler = {
* @param {Object<Array>} favorites Array of objects of the form {PackID, StickerID}; position is the index in the array
* @returns
*/
setFavorites: function (favorites) {
setFavorites: function (favorites: Record<string, any>[]) {
return new Promise((resolve, reject) => {
// Create a map of PackID/StickerID key and index value
favorites = favorites.reduce((acc, curr, index) => {
// Create a map of `PackID;StickerID` key and index value
const favoritesKeyMap = favorites.reduce((acc, curr, index) => {
acc[curr.PackID + ';' + curr.StickerID] = index;
return acc;
}, {});
this.db.serialize(() => {
this.db!.serialize(() => {
// Iterate through rows of db and update index accordingly, delete row if not in favorites
this.db.all('SELECT * FROM favorites', (err, rows) => {
this.db!.all('SELECT * FROM favorites', (err: Error, rows: any[]) => {
if (err) {
reject(err);
}
rows.forEach((row) => {
const key = row.PackID + ';' + row.StickerID;
if (favorites[key] === undefined) {
if (favoritesKeyMap[key] === undefined) {
// Delete row
this.db.run('DELETE FROM favorites WHERE PackID = ? AND StickerID = ?', [
this.db!.run('DELETE FROM favorites WHERE PackID = ? AND StickerID = ?', [
row.PackID,
row.StickerID,
]);
} else {
// Update row
this.db.run('UPDATE favorites SET position = ? WHERE PackID = ? AND StickerID = ?', [
favorites[key],
this.db!.run('UPDATE favorites SET position = ? WHERE PackID = ? AND StickerID = ?', [
favoritesKeyMap[key],
row.PackID,
row.StickerID,
]);
delete favorites[key];
delete favoritesKeyMap[key];
}
});
// Insert new rows
Object.keys(favorites).forEach((key) => {
Object.keys(favoritesKeyMap).forEach((key) => {
const [PackID, StickerID] = key.split(';');
this.db.run('INSERT INTO favorites (PackID, StickerID, position) VALUES (?, ?, ?)', [
this.db!.run('INSERT INTO favorites (PackID, StickerID, position) VALUES (?, ?, ?)', [
PackID,
StickerID,
favorites[key],
favoritesKeyMap[key],
]);
});
});
resolve();
resolve(true);
});
});
},
/**
* Gets the most used stickers
* @param {number} count Amount of most used stickers to return
*/
getMostUsed: function (count) {
getMostUsed: function (count: number) {
return new Promise((resolve, reject) => {
this.db.all(`SELECT * FROM stickerUses ORDER BY count DESC LIMIT ${count}`, (err, rows) => {
this.db!.all(`SELECT * FROM stickerUses ORDER BY count DESC LIMIT ${count}`, (err, rows) => {
if (err) {
reject(err);
}
Expand All @@ -112,17 +118,17 @@ const sqlHandler = {
* Increments the count of the given sticker
* @param {Object} sticker Object of the form {PackID, StickerID}
*/
useSticker: function (sticker) {
useSticker: function (sticker: { PackID: any; StickerID: any }) {
return new Promise((resolve, reject) => {
this.db.run(
this.db!.run(
`INSERT INTO stickerUses (PackID, StickerID, count) VALUES (?, ?, 1)
ON CONFLICT(PackID, StickerID) DO UPDATE SET count = count + 1`,
[sticker.PackID, sticker.StickerID],
(err) => {
if (err) {
reject(err);
}
resolve();
resolve(true);
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/stickerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function pasteStickerFromPath(

// write sticker file to clipboard if not linux
if (process.platform !== 'linux') {
clipboardEx?.writeFilePaths([tempStickerPath]);
clipboardEx!.writeFilePaths([tempStickerPath]);
} else {
// linux
clipboard.writeImage(tempStickerPath);
Expand Down

0 comments on commit b22de11

Please sign in to comment.