Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions apps/hasura/metadata/databases/masterbots/tables/public_thread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ object_relationships:
- name: modelsEnum
using:
foreign_key_constraint_on: model
- name: thread
using:
foreign_key_constraint_on: parent_thread_id
- name: user
using:
foreign_key_constraint_on: user_id
Expand All @@ -19,32 +22,42 @@ array_relationships:
table:
name: message
schema: public
- name: threads
using:
foreign_key_constraint_on:
column: parent_thread_id
table:
name: thread
schema: public
insert_permissions:
- role: moderator
permission:
check: {}
columns:
- chatbot_id
- created_at
- is_approved
- is_blocked
- is_public
- model
- chatbot_id
- created_at
- updated_at
- parent_thread_id
- thread_id
- updated_at
- user_id
comment: ""
- role: user
permission:
check:
user_id:
_eq: X-Hasura-User-Id
thread:
user_id:
_eq: X-Hasura-User-Id
columns:
- chatbot_id
- thread_id
- user_id
- is_public
- model
- parent_thread_id
- thread_id
- user_id
comment: ""
select_permissions:
- role: anonymous
Expand All @@ -63,14 +76,15 @@ select_permissions:
- role: moderator
permission:
columns:
- chatbot_id
- created_at
- is_approved
- is_blocked
- is_public
- model
- chatbot_id
- created_at
- updated_at
- parent_thread_id
- thread_id
- updated_at
- user_id
filter: {}
allow_aggregations: true
Expand All @@ -83,10 +97,14 @@ select_permissions:
- is_approved
- is_public
- model
- parent_thread_id
- thread_id
- updated_at
- user_id
filter: {}
filter:
thread:
user_id:
_eq: X-Hasura-User-Id
comment: ""
update_permissions:
- role: moderator
Expand All @@ -105,9 +123,10 @@ update_permissions:
- is_public
- model
filter:
user_id:
_eq: X-Hasura-User-Id
check: null
thread:
user_id:
_eq: X-Hasura-User-Id
check: {}
comment: ""
delete_permissions:
- role: moderator
Expand All @@ -117,6 +136,7 @@ delete_permissions:
- role: user
permission:
filter:
user_id:
_eq: X-Hasura-User-Id
thread:
user_id:
_eq: X-Hasura-User-Id
comment: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."thread" add column "parent_thread" uuid
-- null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."thread" add column "parent_thread" uuid
null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."thread" drop constraint "thread_parent_thread_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread"
add constraint "thread_parent_thread_fkey"
foreign key ("parent_thread")
references "public"."thread"
("thread_id") on update restrict on delete restrict;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread" drop constraint "thread_parent_thread_fkey",
add constraint "thread_parent_thread_fkey"
foreign key ("parent_thread")
references "public"."thread"
("thread_id") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread" drop constraint "thread_parent_thread_fkey",
add constraint "thread_parent_thread_fkey"
foreign key ("parent_thread")
references "public"."thread"
("thread_id") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread"
add constraint "thread_parent_thread_fkey"
foreign key ("parent_thread")
references "public"."thread"
("thread_id") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."thread" drop constraint "thread_parent_thread_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."thread" alter column "parent_thread" drop not null;
alter table "public"."thread" add column "parent_thread" uuid;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."thread" drop column "parent_thread" cascade;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."thread" add column "parent_thread_id" uuid
-- null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."thread" add column "parent_thread_id" uuid
null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."thread" drop constraint "thread_parent_thread_id_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread"
add constraint "thread_parent_thread_id_fkey"
foreign key ("parent_thread_id")
references "public"."thread"
("thread_id") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread" drop constraint "thread_parent_thread_id_fkey",
add constraint "thread_parent_thread_id_fkey"
foreign key ("parent_thread_id")
references "public"."thread"
("thread_id") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."thread" drop constraint "thread_parent_thread_id_fkey",
add constraint "thread_parent_thread_id_fkey"
foreign key ("parent_thread_id")
references "public"."thread"
("thread_id") on update restrict on delete restrict;
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ interface ThreadPageProps {
}
}
export default async function ThreadPage({ params }: ThreadPageProps) {


const thread = await getThread({
threadId: params.threadId,
jwt: ''
})

if(!thread){
return <div>Thread not found</div>
}
const { user, chatbot, messages } = thread

return (
<BrowseChatMessageList
user={user as User | undefined}
chatbot={chatbot}
messages={messages}
isThread
/>
user={user as User | undefined}
chatbot={chatbot}
messages={messages}
isThread
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export function BrowseChatMessageList({
setPairs(prePairs)
} else setPairs([])
}, [messages])

return (
<div>
{pairs.map((pair: MessagePair, key: number) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import React from 'react'
import BrowseChatbotDetails from '@/components/routes/browse/browse-chatbot-details'
import { BrowseChatMessageList } from '@/components/routes/browse/browse-chat-message-list'
import { getMessages } from '@/services/hasura'
import { ExternalLink } from '@/components/shared/external-link'
import { toSlug } from 'mb-lib'
import Link from 'next/link'

export type MessagePair = {
userMessage: Message
Expand All @@ -36,14 +39,20 @@ export function convertMessage(message: Message) {

export function BrowseChatMessages({
threadId,
parentThreadId,
user,
chatbot
}: {
threadId: string
parentThreadId?: string
user?: User
chatbot?: Chatbot
}) {
const [messages, setMessages] = React.useState<Message[]>([])
const [parentThreadTitle, setParentThreadTitle] = React.useState<string | null>(null)
const { name: categoryName } = chatbot?.categories[0].category || { name: '' }
const { name: chatBotName } = chatbot || { name: '' }
const parentThreadUrl = `/b/${toSlug(chatBotName)}/${parentThreadId}`

// Fetch messages for the specified thread ID
const fetchMessages = async () => {
Expand All @@ -53,11 +62,27 @@ export function BrowseChatMessages({
}
}

// Fetch parent thread info
const fetchParentThreadInfo = async () => {
if (parentThreadId) {
const parentThread = await getMessages({ threadId: parentThreadId })
const parentThreadTitle = parentThread[0]?.content
setParentThreadTitle(parentThreadTitle)
}
}

// Effect to fetch messages when the thread ID changes
React.useEffect(() => {
fetchMessages()
}, [threadId])

// Effect to fetch the parent thread info if the parentThreadId exists
React.useEffect(() => {
if (parentThreadId) {
fetchParentThreadInfo()
}
}, [parentThreadId])

return (
<div className="w-full">
{chatbot ? (
Expand All @@ -69,13 +94,19 @@ export function BrowseChatMessages({
''
)}
<div className="flex flex-col max-w-screen-lg px-4 mx-auto mt-8 gap-y-4">
{ parentThreadTitle && (
<p>This thread is an extension of the original content from the parent thread titled <Link className="text-muted-foreground hover:text-primary transition-colors underline" href={parentThreadUrl}>"{ parentThreadTitle }"</Link>. To get the full context and explore more, visit the <Link className="text-muted-foreground hover:text-primary transition-colors underline" href={parentThreadUrl}>original post</Link>.</p>
)}
<BrowseChatMessageList
user={user}
chatbot={chatbot}
messages={messages}
isThread
/>
</div>
<div className="border-t border-t-iron dark:border-t-mirage pt-6 text-center mt-44 lg:mt-96">
<ExternalLink href={`/c/${toSlug(categoryName)}/${toSlug(chatBotName)}?continuousThreadId=${threadId}`}>Continue Thread</ExternalLink>
</div>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function BrowseThread({
chatbot={thread?.chatbot}
user={thread?.user || undefined}
threadId={thread.threadId}
parentThreadId={thread?.parentThreadId}
/>
) : (
''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useSidebar } from '@/lib/hooks/use-sidebar'
import { useThread } from '@/lib/hooks/use-thread'
import { getCategory, getThreads, chatbotFollowOrUnfollow } from '@/services/hasura'
import { useSession } from 'next-auth/react'
import { useParams } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import type { SocialFollowing } from 'mb-genql'
Expand Down Expand Up @@ -59,15 +58,15 @@ export default function ChatChatbotDetails() {
followeeIdChatbot: followeeId,
chatbot: null,
createdAt: new Date().toISOString(),
userByFollowerId: null as unknown,
userByFollowerId: null as unknown,
user: null,
__typename: 'SocialFollowing'
} as SocialFollowing
} as SocialFollowing
]);
}else{
setFollowers(followers.filter(follower => !(follower.followerId === followerId && follower.followeeIdChatbot === followeeId)))
}

customSonner({type: 'success', text: follow ? `You have followed ${activeChatbot?.name} successfully` : `You have unfollowed ${activeChatbot?.name}`})
} catch (error) {
setIsFollowLoading(false)
Expand Down Expand Up @@ -132,7 +131,6 @@ export default function ChatChatbotDetails() {
console.log('Starting new chat with:', botName)
}


const sharedProps = {
botName,
avatar: activeChatbot?.avatar || randomChatbot?.avatar || '',
Expand All @@ -148,7 +146,6 @@ export default function ChatChatbotDetails() {
followers
}


return (
<>
<OnboardingChatbotDetails {...sharedProps} />
Expand Down
Loading