Skip to content

Commit

Permalink
chore: reduce unnecessary page apis
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Jan 15, 2025
1 parent 34d1a33 commit a0bbaf2
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 655 deletions.
7 changes: 2 additions & 5 deletions ee/tabby-db/migrations/0042_page-section.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE pages(
author_id INTEGER NOT NULL,

title TEXT,
summary TEXT,
content TEXT,

created_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),
Expand All @@ -17,14 +17,11 @@ CREATE TABLE page_sections(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
page_id INTEGER NOT NULL,

position INTEGER NOT NULL,

title TEXT NOT NULL,
content TEXT NOT NULL,

created_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),

FOREIGN KEY(page_id) REFERENCES pages(id) ON DELETE CASCADE,
CONSTRAINT `page_id_position` UNIQUE(page_id, position)
FOREIGN KEY(page_id) REFERENCES pages(id) ON DELETE CASCADE
);
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
6 changes: 2 additions & 4 deletions ee/tabby-db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,17 @@ CREATE TABLE pages(
-- The user who created the page
author_id INTEGER NOT NULL,
title TEXT,
summary TEXT,
content TEXT,
created_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE page_sections(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
page_id INTEGER NOT NULL,
position INTEGER NOT NULL,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(page_id) REFERENCES pages(id) ON DELETE CASCADE,
CONSTRAINT `page_id_position` UNIQUE(page_id, position)
FOREIGN KEY(page_id) REFERENCES pages(id) ON DELETE CASCADE
);
1,020 changes: 508 additions & 512 deletions ee/tabby-db/schema/schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ee/tabby-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use job_runs::JobRunDAO;
pub use ldap_credential::LdapCredentialDAO;
pub use notifications::NotificationDAO;
pub use oauth_credential::OAuthCredentialDAO;
pub use pages::PageDAO;
pub use pages::{PageDAO, PageSectionDAO};
pub use provided_repositories::ProvidedRepositoryDAO;
pub use repositories::RepositoryDAO;
pub use server_setting::ServerSettingDAO;
Expand Down
80 changes: 52 additions & 28 deletions ee/tabby-db/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct PageDAO {
pub id: i64,
pub author_id: i64,
pub title: Option<String>,
pub summary: Option<String>,
pub content: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
Expand All @@ -20,7 +20,6 @@ pub struct PageSectionDAO {
pub id: i64,
pub page_id: i64,

pub position: i64,
pub title: String,
pub content: String,

Expand All @@ -37,26 +36,6 @@ impl DbConn {
Ok(res.last_insert_rowid())
}

Check warning on line 37 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L36-L37

Added lines #L36 - L37 were not covered by tests

pub async fn create_page_section(
&self,
page_id: i64,
position: i64,
title: &str,
content: &str,
) -> Result<i64> {
let res = query!(
"INSERT INTO page_sections(page_id, position, title, content) VALUES (?, ?, ?, ?)",
page_id,
position,
title,
content
)
.execute(&self.pool)
.await?;

Ok(res.last_insert_rowid())
}

pub async fn update_page_title(&self, page_id: i64, title: &str) -> Result<()> {
query!(
"UPDATE pages SET title = ?, updated_at = DATETIME('now') WHERE id = ?",
Expand All @@ -69,10 +48,10 @@ impl DbConn {
Ok(())
}

Check warning on line 49 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L48-L49

Added lines #L48 - L49 were not covered by tests

pub async fn update_page_summary(&self, page_id: i64, summary: &str) -> Result<()> {
pub async fn update_page_content(&self, page_id: i64, content: &str) -> Result<()> {
query!(
"UPDATE pages SET summary = ?, updated_at = DATETIME('now') WHERE id = ?",
summary,
"UPDATE pages SET content = ?, updated_at = DATETIME('now') WHERE id = ?",
content,
page_id
)
.execute(&self.pool)
Expand Down Expand Up @@ -106,7 +85,7 @@ impl DbConn {
"id",
"author_id",
"title",
"summary",
"content",
"created_at" as "created_at: DateTime<Utc>",
"updated_at" as "updated_at: DateTime<Utc>"
],
Expand Down Expand Up @@ -136,7 +115,7 @@ impl DbConn {
id,
author_id,
title,
summary,
content,
created_at as "created_at: DateTime<Utc>",
updated_at as "updated_at: DateTime<Utc>"
FROM pages
Expand All @@ -163,7 +142,6 @@ impl DbConn {
[
"id",
"page_id",
"position",
"title",
"content",
"created_at" as "created_at: DateTime<Utc>",
Expand All @@ -179,4 +157,50 @@ impl DbConn {

Ok(sections)
}

Check warning on line 159 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L158-L159

Added lines #L158 - L159 were not covered by tests

pub async fn get_page_section(&self, id: i64) -> Result<Option<PageSectionDAO>> {
let section = query_as!(
PageSectionDAO,
r#"SELECT
id,
page_id,
title,
content,
created_at as "created_at: DateTime<Utc>",
updated_at as "updated_at: DateTime<Utc>"
FROM page_sections
WHERE id = ?"#,
id
)
.fetch_optional(&self.pool)
.await?;

Check warning on line 176 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L161-L176

Added lines #L161 - L176 were not covered by tests

Ok(section)
}

Check warning on line 179 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L178-L179

Added lines #L178 - L179 were not covered by tests

pub async fn create_page_section(
&self,
page_id: i64,
title: &str,
content: &str,
) -> Result<i64> {
let res = query!(
"INSERT INTO page_sections(page_id, title, content) VALUES (?, ?, ?)",
page_id,
title,
content
)
.execute(&self.pool)
.await?;

Check warning on line 194 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L181-L194

Added lines #L181 - L194 were not covered by tests

Ok(res.last_insert_rowid())
}

Check warning on line 197 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L196-L197

Added lines #L196 - L197 were not covered by tests

pub async fn delete_page_section(&self, id: i64) -> Result<()> {
query!("DELETE FROM page_sections WHERE id = ?", id,)
.execute(&self.pool)
.await?;

Check warning on line 202 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L199-L202

Added lines #L199 - L202 were not covered by tests

Ok(())
}

Check warning on line 205 in ee/tabby-db/src/pages.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/pages.rs#L204-L205

Added lines #L204 - L205 were not covered by tests
}
43 changes: 23 additions & 20 deletions ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ enum Role {
ASSISTANT
}

input AddPageSectionInput {
pageId: ID!
title: String!
}

input CodeQueryInput {
filepath: String
language: String
Expand Down Expand Up @@ -206,12 +211,6 @@ input PasswordResetInput {
password2: String!
}

input ReorderSectionInput {
pageId: ID!
id: ID!
position: Int!
}

input RequestInvitationInput {
email: String!
}
Expand Down Expand Up @@ -275,13 +274,6 @@ input UpdateOAuthCredentialInput {
clientSecret: String
}

input UpdateSectionInput {
pageId: ID!
id: ID!
title: String!
regenerate: Boolean!
}

input UpsertUserGroupMembershipInput {
userGroupId: ID!
userId: ID!
Expand Down Expand Up @@ -665,13 +657,24 @@ type Mutation {
"Turn on persisted status for a thread."
setThreadPersisted(threadId: ID!): Boolean!
updateThreadMessage(input: UpdateMessageInput!): Boolean!
"""
Utilize an existing thread and its messages to create a page.
This will automatically generate the page title and a summary of the content.
Every two messages will be converted into a page section.
The user's message will serve as the title of the section.
The assistant's message will become the content of the section.
"""
convertThreadToPage(threadId: ID!): ID!
generatePageTitle(id: ID!): String!
generatePageSummary(id: ID!): String!
"delete a page and all its sections."
deletePage(id: ID!): Boolean!
updatePageSection(input: UpdateSectionInput!): Section!
reorderPageSections(input: ReorderSectionInput!): Boolean!
deletePageSection(pageId: ID!, sectionId: ID!): Boolean!
"""
Creates a new page section.
Only the title is required; the answer will be generated as the content.
"""
addPageSection(input: AddPageSectionInput!): ID!
"delete a single page section."
deletePageSection(sectionId: ID!): Boolean!
createCustomDocument(input: CreateCustomDocumentInput!): ID!
deleteCustomDocument(id: ID!): Boolean!
setPresetDocumentActive(input: SetPresetDocumentActiveInput!): Boolean!
Expand Down Expand Up @@ -706,7 +709,7 @@ type Page {
id: ID!
authorId: ID!
title: String
summary: String
content: String
createdAt: DateTime!
updatedAt: DateTime!
}
Expand Down Expand Up @@ -827,6 +830,7 @@ type Query {
Thread is public within an instance, so no need to check for ownership.
"""
threadMessages(threadId: ID!, after: String, before: String, first: Int, last: Int): MessageConnection!
"Read pages by page IDs."
pages(ids: [ID!], after: String, before: String, first: Int, last: Int): PageConnection!
pageSections(pageId: ID!, after: String, before: String, first: Int, last: Int): SectionConnection!
customWebDocuments(ids: [ID!], after: String, before: String, first: Int, last: Int): CustomDocumentConnection!
Expand Down Expand Up @@ -878,7 +882,6 @@ type RepositoryGrepOutput {
type Section {
id: ID!
pageId: ID!
position: Int!
title: String!
content: String!
createdAt: DateTime!
Expand Down
22 changes: 18 additions & 4 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use hash_ids::HashIds;
use lazy_static::lazy_static;
use tabby_db::{
EmailSettingDAO, IntegrationDAO, InvitationDAO, JobRunDAO, LdapCredentialDAO, NotificationDAO,
OAuthCredentialDAO, PageDAO, ServerSettingDAO, ThreadDAO, ThreadMessageAttachmentClientCode,
ThreadMessageAttachmentCode, ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc,
ThreadMessageAttachmentPullDoc, ThreadMessageAttachmentWebDoc, UserEventDAO,
OAuthCredentialDAO, PageDAO, PageSectionDAO, ServerSettingDAO, ThreadDAO,
ThreadMessageAttachmentClientCode, ThreadMessageAttachmentCode, ThreadMessageAttachmentDoc,
ThreadMessageAttachmentIssueDoc, ThreadMessageAttachmentPullDoc, ThreadMessageAttachmentWebDoc,
UserEventDAO,
};

use crate::{
Expand Down Expand Up @@ -354,7 +355,20 @@ impl From<PageDAO> for page::Page {
id: value.id.as_id(),
author_id: value.author_id.as_id(),
title: value.title,
summary: value.summary,
content: value.content,
created_at: value.created_at,
updated_at: value.updated_at,
}
}

Check warning on line 362 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L353-L362

Added lines #L353 - L362 were not covered by tests
}

impl From<PageSectionDAO> for page::Section {
fn from(value: PageSectionDAO) -> Self {
Self {
id: value.id.as_id(),
page_id: value.page_id.as_id(),
title: value.title,
content: value.content,
created_at: value.created_at,
updated_at: value.updated_at,
}
Expand Down
Loading

0 comments on commit a0bbaf2

Please sign in to comment.