Skip to content

Commit eb10514

Browse files
committed
add daily post limit
1 parent 3b0fdaa commit eb10514

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎apps/codebility/app/home/feeds/_services/action.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,29 @@ export async function deletePostComment(comment_id: string) {
575575
throw error;
576576
}
577577
}
578+
579+
export const hasReachedDailyPostLimit = async (author_id: string, limit = 2) => {
580+
try {
581+
const supabase = await createClientServerComponent();
582+
583+
// Timestamp 24 hours ago
584+
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
585+
586+
const { count, error } = await supabase
587+
.from("posts")
588+
.select("id", { count: "exact", head: true }) // head: true avoids fetching rows
589+
.eq("author_id", author_id)
590+
.gte("created_at", twentyFourHoursAgo);
591+
592+
if (error) {
593+
console.error("Error checking daily post limit:", error);
594+
throw error;
595+
}
596+
597+
// Return true if count >= limit
598+
return (count ?? 0) >= limit;
599+
} catch (error) {
600+
console.error("Error in hasReachedDailyPostLimit:", error);
601+
throw error;
602+
}
603+
};

‎apps/codebility/app/home/feeds/page.tsx‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { useEffect, useState } from "react";
44
import CreatePostModal from "@/components/modals/CreatePostModal";
55
import { Button } from "@/components/ui/button";
66
import { useUserStore } from "@/store/codev-store";
7-
import { ChevronDown } from "lucide-react";
87

98
import Feed from "./_components/Feed";
109
import SocialPointsCard from "./_components/SocialPointsCard";
1110
import SortMenu from "./_components/SortMenu";
1211
import { SEARCH_DEBOUNCE_MS } from "./_constants";
13-
import { getUserRole } from "./_services/action";
12+
import { getUserRole, hasReachedDailyPostLimit } from "./_services/action";
13+
import toast from "react-hot-toast";
1414

1515
type SortField = "title" | "date" | "upvotes";
1616
type SortOrder = "asc" | "desc";
@@ -22,7 +22,6 @@ export default function FeedsPage() {
2222
const [debouncedQuery, setDebouncedQuery] = useState("");
2323
const [sortField, setSortField] = useState<SortField>("date");
2424
const [sortOrder, setSortOrder] = useState<SortOrder>("desc");
25-
const [sortMenuOpen, setSortMenuOpen] = useState(false);
2625
const { user } = useUserStore();
2726

2827
useEffect(() => {
@@ -42,7 +41,22 @@ export default function FeedsPage() {
4241
return () => clearTimeout(handler);
4342
}, [searchQuery]);
4443

45-
const openModal = () => setIsModalOpen(true);
44+
const openModal = async () => {
45+
if (!user) return;
46+
47+
try {
48+
const unallowed = await hasReachedDailyPostLimit(user.id);
49+
50+
if (unallowed) {
51+
toast.error("Daily post limit reached. You’re not allowed to create a post right now.");
52+
return;
53+
}
54+
55+
setIsModalOpen(true);
56+
} catch (error) {
57+
console.error("Failed to check posting permission", error);
58+
}
59+
};
4660
const closeModal = () => setIsModalOpen(false);
4761

4862
return (

0 commit comments

Comments
 (0)