Skip to content

Commit

Permalink
added user id snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Sep 18, 2024
1 parent 2a2cd51 commit 45f9962
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
12 changes: 6 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ model Minecraft {
valid Boolean @default(true)
slim Boolean @default(false)
user User? @relation(fields: [userId], references: [id])
userId Int? @unique
user User? @relation(fields: [userId], references: [id])
userId String? @unique
}

model User {
id Int @id @default(autoincrement())
id String @id
username String @default("")
reserved_name String?
name String @default("")
Expand All @@ -44,7 +44,7 @@ model User {
model UserSettings {
id Int @id @default(autoincrement())
User User @relation(fields: [userId], references: [id])
userId Int @unique
userId String @unique
banned Boolean @default(false)
profile_theme Int @default(0)
autoload Boolean @default(true)
Expand All @@ -63,7 +63,7 @@ model Sessions {
sessionId String @unique @default("")
User_Agent String @default("")
User User @relation(fields: [userId], references: [id])
userId Int
userId String
}

model Bandage {
Expand All @@ -75,7 +75,7 @@ model Bandage {
base64_slim String @default("")
split_type Boolean @default(false)
User User? @relation("UserBandages", fields: [userId], references: [id])
userId Int?
userId String?
creationDate DateTime @default(now())
stars User[] @relation("UserStars")
categories Category[] @relation("Categories")
Expand Down
3 changes: 2 additions & 1 deletion src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class NotificationService {
return { data: notifications, total_count: count };
}

async createNotification(userId: number, notification: Notifications) {
async createNotification(userId: string | null, notification: Notifications) {
if (!userId) return;
const notification_db = await this.prisma.notifications.create({
data: {
users: { connect: { id: userId } },
Expand Down
10 changes: 10 additions & 0 deletions src/oauth/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { UserService } from 'src/user/user.service';

const discord_url = "https://discord.com/api/v10";
const pwgood = "447699225078136832"; // pwgood server id
const EPOCH = 1672531200000n;

interface DiscordResponse {
token_type: string,
Expand Down Expand Up @@ -78,6 +79,12 @@ export const hasAccess = (user: UserFull | undefined, level: number) => {
return user_roles.includes(level) || user_roles.includes(RolesEnum.SuperAdmin);
}

const generateSnowflake = (increment = 0n) => {
const timestamp = BigInt(Date.now()) - EPOCH;
const snowflake = (timestamp << 22n) | increment;
return snowflake.toString();
}


@Injectable()
export class OauthService {
Expand Down Expand Up @@ -159,9 +166,12 @@ export class OauthService {
return { message: "You are not on ppl", statusCode: 403 };
}

const users_count = await this.prisma.user.count();

const user_db = await this.prisma.user.upsert({
where: { discordId: ds_user.id },
create: {
id: generateSnowflake(BigInt(users_count)),
discordId: ds_user.id,
username: ds_user.username,
name: ds_user.global_name || ds_user.username,
Expand Down
4 changes: 2 additions & 2 deletions src/workshop/bandage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,14 @@ export class BandageService {
const difference = bandage_categories.filter((element) => !validated_categories.includes(element));
const difference_after = validated_categories.filter((element) => !bandage_categories.includes(element));
if (difference_after.includes(moderation_id[1])) {
this.notifications.createNotification(bandage.userId as number, {
this.notifications.createNotification(bandage.userId, {
content: `Повязка <a href="/workshop/${bandage.externalId}"><b>${bandage.title}</b></a> была отклонена. Пожалуйста, свяжитесь с <a href="/contacts"><b>администрацией</b></a> для уточнения причин.`,
type: 2
});
}

else if (moderation_id.some(element => difference.includes(element))) {
this.notifications.createNotification(bandage.userId as number, {
this.notifications.createNotification(bandage.userId, {
content: `Повязка <a href="/workshop/${bandage.externalId}"><b>${bandage.title}</b></a> успешно прошла проверку и теперь доступна остальным в <a href="/workshop"><b>мастерской</b></a>!`,
type: 1
});
Expand Down

0 comments on commit 45f9962

Please sign in to comment.