Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions apps/discord-bot/src/handlers/onCommentCreateWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const onCommentCreateWeb = async (
client: Client,
threadId: string,
comment: string,
discordUserName: string
discordUserName: string,
didSession: string
): Promise<Response> => {
const existhreaingThread = await prisma.thread
.findUnique({
Expand All @@ -41,7 +42,7 @@ export const onCommentCreateWeb = async (
},
});

if (user == null || !user.didSession) {
if (user == null || !user.didSession || user.didSession!==didSession) {
return {
result: false,
value: "user not signed in from discord or did session has expired",
Expand Down Expand Up @@ -104,7 +105,7 @@ export const onCommentCreateWeb = async (
},
create: {
discordId: message.id,
streamId: composeResponse.data.createComment.document.id,
streamId: composeResponse.data.createComment?.document.id,
createdAt: message.createdAt,
discordAuthor: discordUserName,
text: comment,
Expand Down
5 changes: 3 additions & 2 deletions apps/discord-bot/src/handlers/onThreadCreateWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const onThreadCreateWeb = async (
client: Client,
threadTitle: string,
community: string,
discordUserName: string
discordUserName: string,
didSession: string
): Promise<Response> => {
let guild = await client.guilds.cache.get(community);

Expand All @@ -47,7 +48,7 @@ export const onThreadCreateWeb = async (
},
});

if (user == null || !user.didSession) {
if (user == null || !user.didSession || user.didSession!==didSession) {
return {
result: false,
value: "user not signed in from discord or did session has expired",
Expand Down
10 changes: 6 additions & 4 deletions apps/discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ const updateCommunities = async () => {

// apis
app.post("/webcomment", async (req, res) => {
const { threadId, comment, discordUserName } = req.body;
const { threadId, comment, discordUserName, didSession } = req.body;
console.log({ threadId: threadId, comment: comment });
const response = await onCommentCreateWeb(
client,
threadId,
comment,
discordUserName
discordUserName,
didSession
);
console.log(response);
if (response.result) {
Expand All @@ -131,7 +132,7 @@ app.post("/webcomment", async (req, res) => {
});

app.post("/webthread", async (req, res) => {
const { threadTitle, community, discordUserName } = req.body;
const { threadTitle, community, discordUserName, didSession } = req.body;
console.log({
threadTitle: threadTitle,
community: community,
Expand All @@ -141,7 +142,8 @@ app.post("/webthread", async (req, res) => {
client,
threadTitle,
community,
discordUserName
discordUserName,
didSession
);
console.log(response);
if (response.result) {
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prettier-plugin-tailwindcss": "^0.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-toastify": "^9.1.1",
"siwe": "^1.1.6",
"superjson": "1.12.1",
"wagmi": "^0.10.2",
Expand Down
10 changes: 7 additions & 3 deletions apps/web/src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { NavBar } from "../NavBar";

const Layout = ({ children }) => {
const Layout = (props) => {

return (
<div className="flex min-h-screen flex-col">
<NavBar />
<NavBar
handleDiscordUser = {props.handleDiscordUser}
handleDidSession = {props.handleDidSession}
/>
<div className="relative mx-auto w-full max-w-7xl grow px-5 lg:px-0">
{children}
{props.children}
</div>
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/NavBar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function classNames(...classes: string[]) {

const navigation = [{ name: "Ask a question", href: "#", current: true }];

const NavBar = () => {
const NavBar = (props) => {
const { disconnectAsync } = useDisconnect();
const { connectors, connectAsync } = useConnect();
const { address, isConnected } = useAccount();
Expand All @@ -34,6 +34,7 @@ const NavBar = () => {
});
const discordUserName = authorDiscord.data?.discordUsername;

discordUserName && props.handleDiscordUser(true);
const connectWallet = async () => {
await Promise.all(
connectors.map(async (connector) => {
Expand Down Expand Up @@ -64,7 +65,7 @@ const NavBar = () => {
);

setDidSession(session.serialize());

props.handleDidSession(true);
setDid(session.did.id);
};

Expand Down
28 changes: 1 addition & 27 deletions apps/web/src/components/Thread/CommentInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const CommentInput = (props: { threadId: string; refresh: () => void }) => {
threadId: props.threadId,
comment: String(comment),
discordUserName: String(discordUserName),
didSession:String(didSession)
}),
method: "POST",
headers: {
Expand All @@ -58,33 +59,6 @@ const CommentInput = (props: { threadId: string; refresh: () => void }) => {
});
};

if (!isConnected)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to publish comments.
</div>
</div>
);

if (!didSession)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please create a DID session
</div>
</div>
);

if (!isDiscordUser)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to Discord
</div>
</div>
);

return (
<div className="block w-full bg-white p-6">
<form
Expand Down
42 changes: 9 additions & 33 deletions apps/web/src/components/Thread/NewThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { useEffect, useState } from "react";
import { useAccount } from "wagmi";
import useLocalStorage from "../../hooks/useLocalStorage";
import { trpc } from "../../utils/trpc";
import { toast } from 'react-toastify';

const NewThread = (props: { refresh: () => void }) => {
const NewThread = (props) => {
const { isConnected } = useAccount();
const [did, setDid] = useState("");
const [didSession] = useLocalStorage("didSession", "");
const [community, setCommunity] = useState("");

const allCommunities = trpc.public.getAllCommunities.useQuery();

const [thread, setThread] = useState("");

useEffect(() => {
const getData = async () => {
if (!didSession || !isConnected) return;
Expand All @@ -27,7 +27,6 @@ const NewThread = (props: { refresh: () => void }) => {
didSession: didSession,
});

const isDiscordUser = authorDiscord.data?.discordUsername;
const discordUserName = authorDiscord.data?.discordUsername ?? "Anonymous";

const onThreadSumbit = async () => {
Expand All @@ -36,44 +35,21 @@ const NewThread = (props: { refresh: () => void }) => {
threadTitle: thread,
community: community,
discordUserName: discordUserName,
didSession: String(didSession)
}),
method: "POST",
headers: {
"Content-Type": "application/json",
},
}).then(() => {
}).then(async (response) => {
if(!response.ok){
toast.error("Community missing or Api failed");
}
setThread("");
props.refresh();
});
})
};

if (!isConnected)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to publish comments.
</div>
</div>
);

if (!didSession)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please create a DID session
</div>
</div>
);

if (!isDiscordUser)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to Discord
</div>
</div>
);

return (
<div className="block w-full bg-white p-6">
<form
Expand Down
53 changes: 50 additions & 3 deletions apps/web/src/pages/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Layout } from "../components/Layout";
import { useState } from "react";
import Link from "next/link";
import Image from "next/image";
import { useRouter } from "next/router";
Expand All @@ -7,6 +8,8 @@ import Comment from "../components/Comment";
import { trpc } from "../utils/trpc";
import ThreadInformation from "../components/Thread/ThreadInformation";
import CommentInput from "../components/Thread/CommentInput";
import { useAccount } from "wagmi";
import useLocalStorage from "../hooks/useLocalStorage";

const QuestionPage = () => {
const router = useRouter();
Expand All @@ -15,6 +18,11 @@ const QuestionPage = () => {
const allThreads = trpc.public.getAllThreads.useQuery();
const allComments = trpc.public.getAllComments.useQuery();

const [didSession] = useLocalStorage("didSession","");
const { isConnected } = useAccount();
const [isDidSession, setDidSession] = useState(didSession?true:false);
const [isDiscordUser, setDiscordUser] = useState(false);

if (!allComments.data || !allThreads.data) return <div>Loading</div>;

const thisThread = allThreads.data.filter((thread) => thread.node.id == id)[0]
Expand All @@ -23,8 +31,47 @@ const QuestionPage = () => {
.filter((comment) => comment.node.threadID == id)
.map((comment) => comment.node);

const handleDidSession = (value) =>{
setDidSession(value)
}
const handleDiscordUser = (value) => {
setDiscordUser(value)
}

const checkConnected = () =>{
if (!isConnected)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to publish comments.
</div>
</div>
);

if (!isDidSession)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please create a DID session
</div>
</div>
);

if (!isDiscordUser)
return (
<div className="flex w-full justify-center bg-white py-6">
<div className=" bg-white text-base font-normal text-gray-700">
Please connect to Discord
</div>
</div>
);
}

return (
<Layout>
<Layout
handleDiscordUser = {handleDiscordUser}
handleDidSession = {handleDidSession}
>
<main className="absolute inset-0 m-5 lg:m-0 lg:flex lg:gap-[50px]">
<div className="pt-[50px] lg:max-w-[75%] lg:border-r-[1px]">
<Link href="/" legacyBehavior>
Expand Down Expand Up @@ -57,13 +104,13 @@ const QuestionPage = () => {
</div>
</div>
<div className="border-b border-gray-200 pb-5 sm:pb-0"></div>
<CommentInput
{isConnected && isDidSession && isDiscordUser ?<CommentInput
threadId={id}
refresh={() => {
allThreads.refetch();
allComments.refetch();
}}
/>
/> : checkConnected()}
</div>
</div>

Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { Session } from "next-auth";
import { configureChains, createClient, WagmiConfig } from "wagmi";
import { mainnet, optimism } from "wagmi/chains";

import { ToastContainer } from 'react-toastify';

import 'react-toastify/dist/ReactToastify.css';

const { chains, provider, webSocketProvider } = configureChains(
[mainnet],
[publicProvider()]
Expand All @@ -35,6 +39,18 @@ const client = createClient({
const MyApp: AppType = ({ Component, pageProps }) => {
return (
<WagmiConfig client={client}>
<ToastContainer
position="top-right"
autoClose={5000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
theme="light"
/>
<Component {...pageProps} />
</WagmiConfig>
);
Expand Down
Loading