diff --git a/islands/Me.tsx b/islands/Me.tsx index 28ae68d..44649a9 100644 --- a/islands/Me.tsx +++ b/islands/Me.tsx @@ -6,11 +6,15 @@ import { fetchFriends, friends } from "~/state/friends.ts"; import Post from "~/islands/Post.tsx"; import IconArrowBigLeft from "icons/arrow-big-left.tsx"; import IconArrowBigRight from "icons/arrow-big-right.tsx"; +import IconCircleCheck from "icons/circle-check.tsx"; import { type SelfPost } from "~/types.ts"; +import { getAuth } from "~/state/auth.ts"; export default function Me() { const [index, setIndex] = useState(0); const [post, setPost] = useState(null); + const [active, setActive] = useState(false); + const [reactionId, setReactionId] = useState(""); const user = me.value; const feedList = feed.value; @@ -122,12 +126,39 @@ export default function Me() { ) => (
{ + if (active) { + setActive(false); + setReactionId(realmoji.id); + fetch("/api/reactAll", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + emoji: realmoji.emoji, + access_token: getAuth()?.access_token, + }), + }); + } + }} + className={`p-2 rounded-lg bg-white dark:bg-gray-800 relative ${ + active ? "hover:bg-gray-800 hover:dark:bg-white" : "" + }`} > +
+ +
Realmoji { (d.target as HTMLImageElement).src = "/raven.png"; }} @@ -136,6 +167,18 @@ export default function Me() {
))} +
+ +
)} diff --git a/routes/api/react.ts b/routes/api/react.ts new file mode 100644 index 0000000..106b852 --- /dev/null +++ b/routes/api/react.ts @@ -0,0 +1,60 @@ +import { FreshContext } from "$fresh/server.ts"; + +export const handler = async ( + req: Request, + _ctx: FreshContext, +): Promise => { + if (req.method !== "POST") { + return new Response(null, { status: 405 }); + } + + const { emoji, access_token, postId, postUserId } = await req.json() as { + emoji?: string; + access_token?: string; + postId?: string; + postUserId?: string; + }; + + if (!emoji || !access_token || !postId || !postUserId) { + return new Response(null, { status: 400 }); + } + + const reactResponse = await fetch( + `https://mobile.bereal.com/api/content/realmojis?postId=${postId}&postUserId=${postUserId}`, + { + headers: { + authorization: `Bearer ${access_token}`, + }, + method: "POST", + body: JSON.stringify({ + emoji, + }), + }, + ); + + if (!reactResponse.ok) { + return new Response( + JSON.stringify( + await reactResponse.json(), + ), + { + status: 400, + headers: { + "Content-Type": "application/json", + }, + }, + ); + } + + return new Response( + JSON.stringify( + await reactResponse.json(), + ), + { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }, + ); +}; diff --git a/routes/api/reactAll.ts b/routes/api/reactAll.ts new file mode 100644 index 0000000..86620cf --- /dev/null +++ b/routes/api/reactAll.ts @@ -0,0 +1,77 @@ +import { FreshContext } from "$fresh/server.ts"; +import { FeedResp } from "~/types.ts"; + +export const handler = async ( + req: Request, + _ctx: FreshContext, +): Promise => { + if (req.method !== "POST") { + return new Response(null, { status: 405 }); + } + + const { access_token, emoji } = await req.json() as { + access_token?: string; + emoji?: string; + }; + + if (!access_token || !emoji) { + return new Response(null, { status: 400 }); + } + + const initialFeedResponse = await fetch( + "https://mobile.bereal.com/api/feeds/friends-v1", + { + headers: { + authorization: `Bearer ${access_token}`, + }, + }, + ); + + if (!initialFeedResponse.ok) { + return new Response( + JSON.stringify( + await initialFeedResponse.json(), + ), + { + status: 400, + headers: { + "Content-Type": "application/json", + }, + }, + ); + } + + const feed: FeedResp = await initialFeedResponse.json() as FeedResp; + + feed.friendsPosts.reduce(async (prms, batch) => { + await prms; + await batch.posts.reduce(async (prms, post) => { + await prms; + await fetch( + `https://mobile.bereal.com/api/content/realmojis?postId=${post.id}&postUserId=${batch.user.id}`, + { + headers: { + authorization: `Bearer ${access_token}`, + "content-type": "application/json", + "accept": "application/json", + }, + method: "PUT", + body: JSON.stringify({ emoji: `${emoji}` }), + }, + ); + await new Promise((resolve) => setTimeout(resolve, 300)); + }, Promise.resolve()); + }, Promise.resolve()); + + return new Response( + JSON.stringify( + { success: true }, + ), + { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }, + ); +};