mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-14 23:11:56 -05:00
83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import type { Prisma } from ".prisma/client";
|
|
import type { Strategy as DiscordStrategy } from "passport-discord";
|
|
import type { Serialized } from "~/utils";
|
|
import { db } from "../utils/db.server";
|
|
|
|
export async function upsertUser({
|
|
loggedInUser,
|
|
refreshToken,
|
|
}: {
|
|
loggedInUser: DiscordStrategy.Profile;
|
|
refreshToken: string;
|
|
}) {
|
|
return db.user.upsert({
|
|
create: {
|
|
discordId: loggedInUser.id,
|
|
discordName: loggedInUser.username,
|
|
discordDiscriminator: loggedInUser.discriminator,
|
|
discordAvatar: loggedInUser.avatar,
|
|
discordRefreshToken: refreshToken,
|
|
...parseConnections(loggedInUser.connections),
|
|
},
|
|
update: {
|
|
discordName: loggedInUser.username,
|
|
discordDiscriminator: loggedInUser.discriminator,
|
|
discordAvatar: loggedInUser.avatar,
|
|
discordRefreshToken: refreshToken,
|
|
...parseConnections(loggedInUser.connections),
|
|
},
|
|
where: {
|
|
discordId: loggedInUser.id,
|
|
},
|
|
});
|
|
}
|
|
|
|
function parseConnections(
|
|
connections: DiscordStrategy.ConnectionInfo[] | undefined
|
|
) {
|
|
if (!connections) return null;
|
|
|
|
const result: {
|
|
twitch?: string;
|
|
twitter?: string;
|
|
youtubeId?: string;
|
|
youtubeName?: string;
|
|
} = {};
|
|
|
|
for (const connection of connections) {
|
|
if (connection.visibility !== 1 || !connection.verified) continue;
|
|
|
|
switch (connection.type) {
|
|
case "twitch":
|
|
result.twitch = connection.name;
|
|
break;
|
|
case "twitter":
|
|
result.twitter = connection.name;
|
|
break;
|
|
case "youtube":
|
|
result.youtubeId = connection.id;
|
|
result.youtubeName = connection.name;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export type GetTrustingUsersI = Serialized<
|
|
Prisma.PromiseReturnType<typeof getTrustingUsers>
|
|
>;
|
|
|
|
export function getTrustingUsers(userId: string) {
|
|
return db.trustRelationships.findMany({
|
|
where: { trustReceiverId: userId },
|
|
select: {
|
|
trustGiver: {
|
|
select: {
|
|
id: true,
|
|
discordName: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|