HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/coauthor/convex/user.ts
import { v } from "convex/values";
import { mutation, query } from "./_generated/server";

export const createUser = mutation({
    args: {
        userId: v.string(), 
        pfpUrl: v.string(), 
        email: v.string(), 
        name: v.string(),
    },
    handler: async (context, args) => {
        const identity = await context.auth.getUserIdentity(); 

        if (!identity) {
            throw new Error("Not Authenticated");
        }

        const existingUser = await context.db
        .query("user")
        .filter(q => q.eq(q.field("userId"), args.userId))
        .first();  

        if (existingUser) { 
            console.log("User already exists");
            return null; 
        }

        const document = await context.db.insert("user",
        {
            userId: args.userId, 
            pfpUrl: args.pfpUrl,
            email: args.email,
            name: args.name 
        });

        return document; 
    }
})


export const getUserById = query({
    args: { convexId: v.id("user") },
    handler: async (ctx, args) => {
        const identity = await ctx.auth.getUserIdentity();
        if (!identity) {
            throw new Error("No Auth");
        }

        const user = await ctx.db
        .query("user")
        .filter((q) => q.eq(q.field("_id"), args.convexId))
        .first();

        let userData;
        
        if (user?.userId === undefined) {
            return undefined; 
        } else { 
            userData = {
                userId: user.userId,
                pfpUrl: user.pfpUrl,
                email: user.email,
                name: user.name
            }
        }
        return userData;
    }
});


export const getByUserId = query({
    args: { userId: v.string() },
    handler: async (ctx, args) => {
        const identity = await ctx.auth.getUserIdentity();
        if (!identity) {
            throw new Error("No Auth");
        }

        const user = await ctx.db
        .query("user")
        .filter((q) => q.eq(q.field("userId"), args.userId))
        .first();

        let userData;
        
        if (user?.userId === undefined) {
            return undefined; 
        } else { 
            userData = {
                userId: user.userId,
                pfpUrl: user.pfpUrl,
                email: user.email,
                name: user.name
            }
        }
        return userData;
    }
});


export const getByEmail = query({
    args: { email: v.string() },
    handler: async (ctx, args) => {
        const identity = await ctx.auth.getUserIdentity();
        if (!identity) {
            throw new Error("No Auth");
        }

        const user = await ctx.db
        .query("user")
        .filter((q) => q.eq(q.field("email"), args.email))
        .first();

        
        const userData = {
            userId: user?.userId,
            pfpUrl: user?.pfpUrl,
            email: user?.email,
            name: user?.name
        }
        return userData;
    }
})



export const getAllUsersForSearch = query({
    args: {},
    handler: async (ctx, args) => {
        const identity = await ctx.auth.getUserIdentity();
        if (!identity) {
            throw new Error("No Auth");
        }

        const users = await ctx.db
        .query("user")
        .collect();

        let userData = users.map(user => {
            return {
                _id: user._id,
                userId: user.userId,
                pfpUrl: user.pfpUrl,
                email: user.email,
                name: user.name
            }
        })
        return userData;
    }
})