Skip to content

Commit

Permalink
Merge pull request #106 from lyve-app/feat/notifications
Browse files Browse the repository at this point in the history
refactor: added more attributes to notification
  • Loading branch information
Louis3797 authored Jun 11, 2024
2 parents edfaacb + e4fa53e commit 978d2be
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
11 changes: 8 additions & 3 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ model Notification {
type NotificationType
streamId String?
achievemntId String?
rewardId String?
userWhoFiredEvent String?
recipient User @relation(fields: [recipientId], references: [id])
message String @default("")
recipient User @relation(fields: [recipientId], references: [id])
recipientId String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}

model Achievement {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import prismaClient from "./config/prisma";
import { jwtDecode } from "jwt-decode";
import { initRabbitMQ } from "./utils/initRabbitMQ";
import { rewards } from "./utils/rewards";
import { getNotificationsMessage } from "./utils/notificationsMessages";

type Stream = {
id: string;
Expand Down Expand Up @@ -743,6 +744,8 @@ io.on("connection", (socket) => {
await prismaClient.notification.create({
data: {
type: "REWARD_RECEIVED",
rewardId: newDBReward.id,
message: getNotificationsMessage("REWARD_RECEIVED", user.dispname),
userWhoFiredEvent: user.id,
recipientId: stream.streamer.id
}
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/controller/stream.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TypedResponse
} from "../types/types";
import { createErrorObject } from "../utils/createErrorObject";
import { getNotificationsMessage } from "../utils/notificationsMessages";

export const getStreamInfo = async (
req: Request<{ id: string }>,
Expand Down Expand Up @@ -218,6 +219,10 @@ export const startStream = async (
const notifications: Prisma.NotificationCreateManyInput[] =
follower.followedBy.map((f) => ({
type: "STREAM_STARTED",
message: getNotificationsMessage(
"STREAM_STARTED",
stream.streamer.dispname
),
userWhoFiredEvent: stream.streamer.id,
streamId: stream.id,
recipientId: f.followedById
Expand Down
14 changes: 13 additions & 1 deletion apps/api/src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
User
} from "@prisma/client";
import { createErrorObject } from "../utils/createErrorObject";
import { getNotificationsMessage } from "../utils/notificationsMessages";

export const getUserInfo = async (
req: Request<{ id: string }>,
Expand Down Expand Up @@ -161,6 +162,13 @@ export const followUser = async (
data: {
followedById: ownId,
followingId: otherId
},
include: {
followedBy: {
select: {
dispname: true
}
}
}
});

Expand All @@ -169,6 +177,10 @@ export const followUser = async (
await prismaClient.notification.create({
data: {
type: "NEW_FOLLOWER",
message: getNotificationsMessage(
"NEW_FOLLOWER",
follow.followedBy.dispname
),
userWhoFiredEvent: ownId,
recipientId: otherId
}
Expand All @@ -177,7 +189,7 @@ export const followUser = async (
return res.status(httpStatus.CREATED).json({
success: true,
data: {
follow
follow: { ...follow }
},
error: []
});
Expand Down
38 changes: 38 additions & 0 deletions apps/api/src/utils/notificationsMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NotificationType } from "@prisma/client";

export const getNotificationsMessage = (
type: NotificationType,
name: string
): string => {
const notificationsMessages: { [Key in NotificationType]: string[] } = {
STREAM_STARTED: [
`Heads up! ${name} is live now. Join the stream and enjoy!`,
`Tune in now! ${name} has just started streaming. Don’t miss out!`,
`Alert! ${name} is live. Catch all the live action now!`,
`${name} is streaming live. Click to join and watch the show!`
],
REWARD_RECEIVED: [
`You've received a reward from ${name}! Thanks for creating awesome content!`,
`Amazing! ${name} just sent you a reward. Keep up the great work!`,
`Wow! ${name} has rewarded you for your stream. Thank you for your efforts!`,
`You've been rewarded by ${name}! Your content is truly appreciated.`
],
NEW_FOLLOWER: [
`${name} is now following you! Welcome them to your community.`,
`Exciting news! ${name} just followed you. Say hello!`,
`You have a new follower: ${name}. Thanks for growing your community!`,
`${name} has joined your follower list. Keep engaging with your fans!`
],
ACHIEVEMENT_RECEIVED: [
`Fantastic! You've unlocked the ${name} achievement. Well done!`,
`Achievement unlocked! You’ve earned ${name}. Keep it up!`,
`Congrats! You've achieved ${name}. Your dedication is paying off!`,
`You've earned the ${name} achievement. Great job and keep striving!`
]
};

const messages = notificationsMessages[type];

const randomIndex = Math.floor(Math.random() * messages.length);
return messages[randomIndex]!;
};

0 comments on commit 978d2be

Please sign in to comment.