a cache for slack profile pictures and emojis

feat: fix case bug breaking cache

dunkirk.sh dad07bbc 9304bfca

verified
Changed files
+20 -10
src
+17 -7
src/cache.ts
···
*/
async purgeUserCache(userId: string): Promise<boolean> {
try {
-
const result = this.db.run("DELETE FROM users WHERE userId = ?", [userId]);
+
const result = this.db.run("DELETE FROM users WHERE userId = ?", [
+
userId.toUpperCase(),
+
]);
return result.changes > 0;
} catch (error) {
console.error("Error purging user cache:", error);
···
DO UPDATE SET imageUrl = ?, expiration = ?`,
[
id,
-
userId,
+
userId.toUpperCase(),
displayName,
pronouns,
imageUrl,
···
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(name)
DO UPDATE SET imageUrl = ?, expiration = ?`,
-
[id, name, alias, imageUrl, expiration, imageUrl, expiration],
+
[
+
id,
+
name.toLowerCase(),
+
alias?.toLowerCase() || null,
+
imageUrl,
+
expiration,
+
imageUrl,
+
expiration,
+
],
);
return true;
} catch (error) {
···
DO UPDATE SET imageUrl = ?, expiration = ?`,
[
id,
-
emoji.name,
-
emoji.alias,
+
emoji.name.toLowerCase(),
+
emoji.alias?.toLowerCase() || null,
emoji.imageUrl,
expiration,
emoji.imageUrl,
···
async getUser(userId: string): Promise<User | null> {
const result = this.db
.query("SELECT * FROM users WHERE userId = ?")
-
.get(userId) as User;
+
.get(userId.toUpperCase()) as User;
if (!result) {
return null;
···
async getEmoji(name: string): Promise<Emoji | null> {
const result = this.db
.query("SELECT * FROM emojis WHERE name = ? AND expiration > ?")
-
.get(name, Date.now()) as Emoji;
+
.get(name.toLowerCase(), Date.now()) as Emoji;
return result
? {
+3 -3
src/index.ts
···
.get(
"/users/:user",
async ({ params, error, request }) => {
-
const user = await cache.getUser(params.user.toLowerCase());
+
const user = await cache.getUser(params.user);
// if not found then check slack first
if (!user || !user.imageUrl) {
···
.get(
"/emojis/:emoji",
async ({ params, error }) => {
-
const emoji = await cache.getEmoji(params.emoji.toLowerCase());
+
const emoji = await cache.getEmoji(params.emoji);
if (!emoji) return error(404, { message: "Emoji not found" });
···
.get(
"/emojis/:emoji/r",
async ({ params, error, redirect }) => {
-
const emoji = await cache.getEmoji(params.emoji.toLowerCase());
+
const emoji = await cache.getEmoji(params.emoji);
if (!emoji) return error(404, { message: "Emoji not found" });