added event collector

This commit is contained in:
Ren Amamiya
2023-04-10 15:50:38 +07:00
parent 213d7514d2
commit fabc0e6cc2
9 changed files with 232 additions and 57 deletions

View File

@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "Chat" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"pubkey" TEXT NOT NULL,
"createdAt" INTEGER NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "Chat_pubkey_key" ON "Chat"("pubkey");
-- CreateIndex
CREATE INDEX "Chat_pubkey_idx" ON "Chat"("pubkey");

View File

@@ -0,0 +1,35 @@
/*
Warnings:
- Added the required column `accountId` to the `Chat` table without a default value. This is not possible if the table is not empty.
- Added the required column `accountId` to the `Channel` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Chat" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"pubkey" TEXT NOT NULL,
"createdAt" INTEGER NOT NULL,
"accountId" INTEGER NOT NULL,
CONSTRAINT "Chat_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Chat" ("createdAt", "id", "pubkey") SELECT "createdAt", "id", "pubkey" FROM "Chat";
DROP TABLE "Chat";
ALTER TABLE "new_Chat" RENAME TO "Chat";
CREATE UNIQUE INDEX "Chat_pubkey_key" ON "Chat"("pubkey");
CREATE INDEX "Chat_pubkey_idx" ON "Chat"("pubkey");
CREATE TABLE "new_Channel" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"eventId" TEXT NOT NULL,
"content" TEXT NOT NULL,
"accountId" INTEGER NOT NULL,
CONSTRAINT "Channel_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Channel" ("content", "eventId", "id") SELECT "content", "eventId", "id" FROM "Channel";
DROP TABLE "Channel";
ALTER TABLE "new_Channel" RENAME TO "Channel";
CREATE UNIQUE INDEX "Channel_eventId_key" ON "Channel"("eventId");
CREATE INDEX "Channel_eventId_idx" ON "Channel"("eventId");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;

View File

@@ -21,6 +21,8 @@ model Account {
plebs Pleb[]
messages Message[]
notes Note[]
chats Chat[]
channels Channel[]
@@index([pubkey])
}
@@ -66,11 +68,25 @@ model Message {
@@index([pubkey, createdAt])
}
model Chat {
id Int @id @default(autoincrement())
pubkey String @unique
createdAt Int
Account Account @relation(fields: [accountId], references: [id])
accountId Int
@@index([pubkey])
}
model Channel {
id Int @id @default(autoincrement())
eventId String @unique
content String
Account Account @relation(fields: [accountId], references: [id])
accountId Int
@@index([eventId])
}

View File

@@ -81,10 +81,23 @@ struct GetLatestNoteData {
date: i32,
}
#[derive(Deserialize, Type)]
struct CreateChatData {
pubkey: String,
created_at: i32,
account_id: i32,
}
#[derive(Deserialize, Type)]
struct GetChatData {
account_id: i32,
}
#[derive(Deserialize, Type)]
struct CreateChannelData {
event_id: String,
content: String,
account_id: i32,
}
#[tauri::command]
@@ -225,7 +238,45 @@ async fn count_total_notes(db: DbState<'_>) -> Result<i64, ()> {
#[specta::specta]
async fn create_channel(db: DbState<'_>, data: CreateChannelData) -> Result<channel::Data, ()> {
db.channel()
.create(data.event_id, data.content, vec![])
.upsert(
channel::event_id::equals(data.event_id.clone()),
channel::create(
data.event_id,
data.content,
account::id::equals(data.account_id),
vec![],
),
vec![],
)
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_chat(db: DbState<'_>, data: CreateChatData) -> Result<chat::Data, ()> {
db.chat()
.upsert(
chat::pubkey::equals(data.pubkey.clone()),
chat::create(
data.pubkey,
data.created_at,
account::id::equals(data.account_id),
vec![],
),
vec![],
)
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn get_chats(db: DbState<'_>, data: GetChatData) -> Result<Vec<chat::Data>, ()> {
db.chat()
.find_many(vec![chat::account_id::equals(data.account_id)])
.exec()
.await
.map_err(|_| ())
@@ -247,7 +298,9 @@ async fn main() {
get_notes,
get_latest_notes,
get_note_by_id,
create_channel
create_channel,
create_chat,
get_chats
],
"../src/utils/bindings.ts",
)
@@ -290,7 +343,9 @@ async fn main() {
get_latest_notes,
get_note_by_id,
count_total_notes,
create_channel
create_channel,
create_chat,
get_chats
])
.manage(Arc::new(db))
.run(tauri::generate_context!())