Skip to content

Commit 5e2cd49

Browse files
committed
feat: add Message and Settings models
1 parent 3b049db commit 5e2cd49

10 files changed

Lines changed: 152 additions & 6 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- CreateTable
2+
CREATE TABLE `Message` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
`mid` VARCHAR(191) NOT NULL,
5+
`gid` INTEGER NULL,
6+
`lid` INTEGER NULL,
7+
`content` VARCHAR(191) NOT NULL,
8+
`type` VARCHAR(191) NOT NULL DEFAULT 'send',
9+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
10+
`updatedAt` DATETIME(3) NOT NULL,
11+
12+
UNIQUE INDEX `Message_mid_key`(`mid`),
13+
PRIMARY KEY (`id`)
14+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
15+
16+
-- AddForeignKey
17+
ALTER TABLE `Message` ADD CONSTRAINT `Message_gid_fkey` FOREIGN KEY (`gid`) REFERENCES `Group`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
18+
19+
-- AddForeignKey
20+
ALTER TABLE `Message` ADD CONSTRAINT `Message_lid_fkey` FOREIGN KEY (`lid`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `gid` on the `Message` table. All the data in the column will be lost.
5+
- You are about to drop the column `mid` on the `Message` table. All the data in the column will be lost.
6+
- Made the column `lid` on table `Message` required. This step will fail if there are existing NULL values in that column.
7+
8+
*/
9+
-- DropForeignKey
10+
ALTER TABLE `Message` DROP FOREIGN KEY `Message_gid_fkey`;
11+
12+
-- DropForeignKey
13+
ALTER TABLE `Message` DROP FOREIGN KEY `Message_lid_fkey`;
14+
15+
-- DropIndex
16+
DROP INDEX `Message_gid_fkey` ON `Message`;
17+
18+
-- DropIndex
19+
DROP INDEX `Message_lid_fkey` ON `Message`;
20+
21+
-- DropIndex
22+
DROP INDEX `Message_mid_key` ON `Message`;
23+
24+
-- AlterTable
25+
ALTER TABLE `Message` DROP COLUMN `gid`,
26+
DROP COLUMN `mid`,
27+
MODIFY `lid` VARCHAR(191) NOT NULL;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- CreateTable
2+
CREATE TABLE `Settings` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
`name` VARCHAR(191) NOT NULL,
5+
`value` VARCHAR(191) NOT NULL,
6+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
7+
`updatedAt` DATETIME(3) NOT NULL,
8+
9+
UNIQUE INDEX `Settings_name_key`(`name`),
10+
PRIMARY KEY (`id`)
11+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

prisma/schema.prisma

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ model Group {
4545
createdAt DateTime @default(now())
4646
updatedAt DateTime @updatedAt
4747
}
48+
49+
model Message {
50+
id Int @id @default(autoincrement())
51+
lid String
52+
content String
53+
type String @default("send") // "send", "edit", "delete"
54+
createdAt DateTime @default(now())
55+
updatedAt DateTime @updatedAt
56+
}
57+
58+
model Settings {
59+
id Int @id @default(autoincrement())
60+
name String @unique
61+
value String
62+
createdAt DateTime @default(now())
63+
updatedAt DateTime @updatedAt
64+
}

src/components/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ client.on("message_reaction", async (react: Reaction) =>
4141
client.on("message_create", async (msg: Message) => messageEvent(msg));
4242
client.on(
4343
"message_edit",
44-
async (msg: Message, newBody: String, prevBody: String) =>
44+
async (msg: Message, newBody: string, prevBody: string) =>
4545
messageEdit(msg, newBody, prevBody)
4646
);
4747
client.on(
@@ -50,7 +50,7 @@ client.on(
5050
);
5151
client.on("group_join", async (notif: GroupNotification) => groupJoin(notif));
5252
client.on("group_leave", async (notif: GroupNotification) => groupLeave(notif));
53-
client.on("auth_failure", (msg: String) =>
53+
client.on("auth_failure", (msg: string) =>
5454
log.error("Auth", "Authentication failed. Please try again.")
5555
);
5656

src/components/events/edit.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Message } from "whatsapp-web.js";
22
import { getUserbyLid } from "../services/user";
3+
import { addMessage } from "../services/message";
34

45
export default async function (
56
msg: Message,
6-
_newBody: String,
7-
prevBody: String
7+
_newBody: string,
8+
prevBody: string
89
) {
910
if (msg.fromMe) return;
1011
const isGroup = !!msg.author;
1112
const user = await getUserbyLid(msg.from) || "Your";
13+
await addMessage(msg, prevBody, "edit");
1214
await msg.reply(
1315
`${isGroup ? user : "Your"} message was edited from "${prevBody}".`
1416
);

src/components/events/revoke.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Message } from "whatsapp-web.js";
22
import { getUserbyLid } from "../services/user";
3+
import { addMessage } from "../services/message";
34

45
export default async function (msg: Message, revoked_msg?: Message) {
56
if (msg.fromMe || !revoked_msg) return;
67
const isGroup = !!msg.author;
7-
const user = await getUserbyLid(msg.from) || "Your";
8+
const user = (await getUserbyLid(msg.from)) || "Your";
9+
await addMessage(msg, revoked_msg?.body, "revoke");
810
await msg.reply(
911
`${isGroup ? user : "Your"} message "${revoked_msg?.body}" was deleted.`
1012
);

src/components/services/message.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { prisma } from "../prisma";
2+
import log from "../../components/utils/log";
3+
import { Message } from "whatsapp-web.js";
4+
5+
export async function addMessage(
6+
msg: Message,
7+
body: string,
8+
type: string
9+
): Promise<void> {
10+
try {
11+
12+
const lid = msg.id.remote.split("@")[0];
13+
await prisma.message.create({
14+
data: {
15+
lid,
16+
content: body,
17+
type: type,
18+
},
19+
});
20+
} catch (error) {
21+
log.error("Database", "Failed to add message.", error);
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { prisma } from "../prisma";
2+
import log from "../../components/utils/log";
3+
import { Message } from "whatsapp-web.js";
4+
5+
export async function saveSetting(name: string, value: string): Promise<void> {
6+
try {
7+
await prisma.settings.upsert({
8+
where: { name },
9+
update: {
10+
value,
11+
},
12+
create: {
13+
name,
14+
value,
15+
},
16+
});
17+
} catch (error) {
18+
log.error("Database", "Failed to add message.", error);
19+
}
20+
}
21+
22+
export async function getSetting(name: string): Promise<string | null> {
23+
try {
24+
const setting = await prisma.settings.findUnique({
25+
where: { name },
26+
});
27+
return setting ? setting.value : null;
28+
} catch (error) {
29+
log.error("Database", `Failed to get setting: ${name}`, error);
30+
return null;
31+
}
32+
}
33+
34+
export async function getAllSettings(): Promise<Record<string, string>> {
35+
try {
36+
const settings = await prisma.settings.findMany();
37+
return settings.reduce((acc, setting) => {
38+
acc[setting.name] = setting.value;
39+
return acc;
40+
}, {} as Record<string, string>);
41+
} catch (error) {
42+
log.error("Database", "Failed to get all settings.", error);
43+
return {};
44+
}
45+
}

src/components/services/user.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { prisma } from "../prisma";
22
import log from "../../components/utils/log";
33
import { Message } from "whatsapp-web.js";
4-
import { info } from "console";
54

65
export async function findOrCreateUser(msg: Message): Promise<boolean> {
76
try {

0 commit comments

Comments
 (0)