Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Extended Bleeter Features #1648

Merged
merged 7 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- AlterTable
ALTER TABLE "BleeterPost" ADD COLUMN "creatorId" TEXT;

-- CreateTable
CREATE TABLE "BleeterProfile" (
"id" TEXT NOT NULL,
"name" VARCHAR(255) NOT NULL,
"handle" VARCHAR(255) NOT NULL,
"isVerified" BOOLEAN DEFAULT false,
"bio" TEXT,
"userId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "BleeterProfile_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "BleeterProfileFollow" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"followerProfileId" TEXT,
"followingProfileId" TEXT,

CONSTRAINT "BleeterProfileFollow_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "BleeterProfile_handle_key" ON "BleeterProfile"("handle");

-- CreateIndex
CREATE UNIQUE INDEX "BleeterProfile_userId_key" ON "BleeterProfile"("userId");

-- AddForeignKey
ALTER TABLE "BleeterProfile" ADD CONSTRAINT "BleeterProfile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BleeterProfileFollow" ADD CONSTRAINT "BleeterProfileFollow_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BleeterProfileFollow" ADD CONSTRAINT "BleeterProfileFollow_followerProfileId_fkey" FOREIGN KEY ("followerProfileId") REFERENCES "BleeterProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BleeterProfileFollow" ADD CONSTRAINT "BleeterProfileFollow_followingProfileId_fkey" FOREIGN KEY ("followingProfileId") REFERENCES "BleeterProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BleeterPost" ADD CONSTRAINT "BleeterPost_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "BleeterProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;
46 changes: 39 additions & 7 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ model User {
vehicles RegisteredVehicle[]
weapons Weapon[]
notifications Notification[]
executedNotifictions Notification[] @relation("executor")
executedNotifictions Notification[] @relation("executor")
medicalRecords MedicalRecord[]
bleeterPosts BleeterPost[]
businesses Business[]
Expand All @@ -292,6 +292,8 @@ model User {
ActiveTone ActiveTone[]
AuditLog AuditLog[]
DoctorVisit DoctorVisit[]
BleeterProfile BleeterProfile?
BleeterProfileFollow BleeterProfileFollow[]
}

model UserSession {
Expand Down Expand Up @@ -798,15 +800,45 @@ model Notification {
}

// bleeter
model BleeterProfile {
id String @id @default(cuid())
name String @db.VarChar(255)
handle String @unique @db.VarChar(255)
isVerified Boolean? @default(false)
bio String? @db.Text
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
followers BleeterProfileFollow[] @relation("followers")
following BleeterProfileFollow[] @relation("following")
posts BleeterPost[]
}

model BleeterProfileFollow {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt

followerProfile BleeterProfile? @relation(name: "followers", fields: [followerProfileId], references: [id], onDelete: Cascade)
followerProfileId String?
followingProfile BleeterProfile? @relation(name: "following", fields: [followingProfileId], references: [id], onDelete: Cascade)
followingProfileId String?
}

model BleeterPost {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
id String @id @default(uuid())
creator BleeterProfile? @relation(fields: [creatorId], references: [id], onDelete: Cascade)
creatorId String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
title String @db.VarChar(255)
body String? @db.Text
title String @db.VarChar(255)
body String? @db.Text
bodyData Json?
imageId String? @db.VarChar(255)
imageBlurData String? @db.Text
imageId String? @db.VarChar(255)
imageBlurData String? @db.Text

createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down
87 changes: 74 additions & 13 deletions apps/api/src/controllers/bleeter/bleeter-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs/promises";
import { AllowedFileExtension, allowedFileExtensions } from "@snailycad/config";
import { BLEETER_SCHEMA } from "@snailycad/schemas";
import { BLEETER_PROFILE_SCHEMA, BLEETER_SCHEMA } from "@snailycad/schemas";
import {
Controller,
Get,
Expand Down Expand Up @@ -31,25 +31,34 @@ import generateBlurPlaceholder from "lib/images/generate-image-blur-data";
export class BleeterController {
@Get("/")
@Description("Get **all** bleeter posts, ordered by `createdAt`")
async getBleeterPosts() {
const posts = await prisma.bleeterPost.findMany({
orderBy: { createdAt: "desc" },
include: {
user: {
select: { username: true },
async getBleeterPosts(@Context("user") user: User) {
const [posts, totalCount] = await prisma.$transaction([
prisma.bleeterPost.findMany({
orderBy: { createdAt: "desc" },
include: {
user: { select: { username: true } },
creator: true,
},
},
}),
prisma.bleeterPost.count(),
]);

const userBleeterProfile = await prisma.bleeterProfile.findUnique({
where: { userId: user.id },
});

return posts;
return { posts, totalCount, userBleeterProfile };
}

@Get("/:id")
@Description("Get a bleeter post by its id")
async getPostById(@PathParams("id") postId: string): Promise<APITypes.GetBleeterByIdData> {
const post = await prisma.bleeterPost.findUnique({
where: { id: postId },
include: { user: { select: { username: true } } },
include: {
user: { select: { username: true } },
creator: true,
},
});

if (!post) {
Expand All @@ -64,17 +73,25 @@ export class BleeterController {
async createPost(
@BodyParams() body: unknown,
@Context("user") user: User,
): Promise<APITypes.PostBleeterByIdData> {
): Promise<APITypes.PostBleeterData> {
const data = validateSchema(BLEETER_SCHEMA, body);

const userProfile = await prisma.bleeterProfile.findUnique({
where: { userId: user.id },
});

const post = await prisma.bleeterPost.create({
data: {
title: data.title,
body: data.body,
bodyData: data.bodyData,
userId: user.id,
creatorId: userProfile?.id,
},
include: {
user: { select: { username: true } },
creator: true,
},
include: { user: { select: { username: true } } },
});

return post;
Expand Down Expand Up @@ -108,7 +125,10 @@ export class BleeterController {
body: data.body,
bodyData: data.bodyData,
},
include: { user: { select: { username: true } } },
include: {
user: { select: { username: true } },
creator: true,
},
});

return updated;
Expand Down Expand Up @@ -185,4 +205,45 @@ export class BleeterController {

return true;
}

@Post("/new-experience/profile")
@Description("Create a new bleeter profile")
async createBleeterProfile(
@Context("user") user: User,
@BodyParams() body: unknown,
): Promise<APITypes.PostNewExperienceProfileData> {
const data = validateSchema(BLEETER_PROFILE_SCHEMA, body);

const existingUserProfile = await prisma.bleeterProfile.findUnique({
where: { userId: user.id },
});

const existingProfileWithHandle = await prisma.bleeterProfile.findUnique({
where: { handle: data.handle.toLowerCase() },
});

if (existingProfileWithHandle && existingProfileWithHandle.id !== existingUserProfile?.id) {
throw new BadRequest("handleTaken");
}

const profile = await prisma.bleeterProfile.upsert({
where: { userId: user.id },
update: { bio: data.bio, name: data.name, handle: data.handle.toLowerCase() },
create: {
handle: data.handle.toLowerCase(),
name: data.name,
bio: data.bio,
userId: user.id,
},
});

await prisma.bleeterPost.updateMany({
where: { userId: user.id, creatorId: { equals: null } },
data: {
creatorId: profile.id,
},
});

return profile;
}
}
Loading