Skip to content

Commit

Permalink
adding is premium to run AI only for certain users
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Feb 2, 2024
1 parent 331a6f3 commit 0a16486
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
FunctionReference,
} from "convex/server";
import type * as clerk from "../clerk.js";
import type * as constants from "../constants.js";
import type * as files from "../files.js";
import type * as follows from "../follows.js";
import type * as http from "../http.js";
Expand All @@ -34,6 +35,7 @@ import type * as vision from "../vision.js";
*/
declare const fullApi: ApiFromModules<{
clerk: typeof clerk;
constants: typeof constants;
files: typeof files;
follows: typeof follows;
http: typeof http;
Expand Down
1 change: 1 addition & 0 deletions convex/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const AI_PROFILE_NAME = "AI Reviewer";
1 change: 1 addition & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default defineSchema({
name: v.optional(v.string()),
isAdmin: v.optional(v.boolean()),
profileImage: v.optional(v.string()),
isPremium: v.optional(v.boolean()),
})
.index("by_userId", ["userId"])
.index("by_subscriptionId", ["subscriptionId"]),
Expand Down
15 changes: 9 additions & 6 deletions convex/thumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { paginationOptsValidator } from "convex/server";
import { adminAuthMutation, authAction, authMutation, authQuery } from "./util";
import { internal } from "./_generated/api";
import { Id } from "./_generated/dataModel";
import { AI_PROFILE_NAME } from "./constants";

export const createThumbnail = internalMutation({
args: {
Expand Down Expand Up @@ -47,11 +48,13 @@ export const createThumbnailAction = authAction({
}
);

await ctx.scheduler.runAfter(0, internal.vision.generateAIComment, {
imageIds: args.images,
thumbnailId: thumbnailId,
userId: ctx.user._id,
});
if (ctx.user.isPremium) {
await ctx.scheduler.runAfter(0, internal.vision.generateAIComment, {
imageIds: args.images,
thumbnailId: thumbnailId,
userId: ctx.user._id,
});
}

return thumbnailId;
},
Expand Down Expand Up @@ -107,7 +110,7 @@ export const addCommentInternal = internalMutation({
text: args.text,
userId: args.userId,
thumbnailId: args.thumbnailId,
name: "GPT4 Vision",
name: AI_PROFILE_NAME,
profileUrl: "",
});
},
Expand Down
12 changes: 11 additions & 1 deletion convex/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { query } from "./_generated/server";
import { ConvexError } from "convex/values";
import { internal } from "./_generated/api";
import { Id } from "./_generated/dataModel";

export const authQuery = customQuery(
query,
Expand Down Expand Up @@ -43,7 +44,16 @@ export const authAction = customAction(
throw new ConvexError("user not found");
}

return { user };
const _id: Id<"users"> = user._id;
const isPremium: boolean = user.isPremium;

return {
user: {
_id,
userId,
isPremium,
},
};
})
);

Expand Down
Binary file added public/robot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion src/app/thumbnails/[thumbnailId]/comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useIsSubscribed } from "@/hooks/useIsSubscribed";
import { UpgradeButton } from "@/components/upgrade-button";
import { TrashIcon } from "lucide-react";
import { useSession } from "@/lib/utils";
import { AI_PROFILE_NAME } from "../../../../convex/constants";

const formSchema = z.object({
text: z.string().min(1).max(500),
Expand Down Expand Up @@ -88,7 +89,13 @@ export function Comments({ thumbnail }: { thumbnail: Doc<"thumbnails"> }) {
>
<div className="flex gap-4">
<Avatar>
<AvatarImage src={comment.profileUrl} />
<AvatarImage
src={
comment.name === AI_PROFILE_NAME
? "/robot.png"
: comment.profileUrl
}
/>
<AvatarFallback>CN</AvatarFallback>
</Avatar>
<div className="flex justify-between w-full">
Expand Down

0 comments on commit 0a16486

Please sign in to comment.