Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Created by https://www.toptal.com/developers/gitignore/api/node,macos,windows,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,macos,windows,visualstudiocode

### Secrets ###
firebase-admin.json

### macOS ###
# General
.DS_Store
Expand Down
1,036 changes: 1,014 additions & 22 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-async-handler": "^1.2.0",
"firebase-admin": "^12.3.1",
"http-status-codes": "^2.3.0",
"jsonwebtoken": "^9.0.2",
"mysql2": "^3.10.3",
Expand Down
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export default function App() {
app.use(express.json()); // request의 본문을 json으로 해석할 수 있도록 함 (JSON 형태의 요청 body를 파싱하기 위함)
app.use(express.urlencoded({ extended: false }));

// firebase setting
const admin = require("firebase-admin");
let serviceAccount = require("../firebase-admin.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});

app.get("/", (req, res) => {
res.send(process.env.PORT);
});
Expand Down
18 changes: 18 additions & 0 deletions src/features/api-utils/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { alarmDao } from "../../../models/api-util/alarm/alarm.dao";
import { commentDao } from "../../../models/api-util/comment/comment.dao";
import { commentPostDto, commentPatchDto, commentDeleteDto } from "../../../models/api-util/comment/comment.dto";
import { getSocketIO } from "../../../utils/socket.middleware";
import { fcmDao } from "../../../models/api-util/fcm/fcm.dao";
import { fcmDto } from "../../../models/api-util/fcm/fcm.dto";
import { pushCommentAlarm } from "../../../utils/fcm.utils";

export const commentService = {
post: async (req: commentPostDto, user_id: string) => {
Expand All @@ -14,6 +17,21 @@ export const commentService = {
...status.SUCCESS.body,
result: result
};

// 댓글을 보낸 후 fcm 전송
let postData: fcmDto | null = null;
if (req.post_type.toString() === "community") {
postData = await fcmDao.getCommunityWriter(req.post_id.toString());
} else if (req.post_type.toString() === "blog") {
postData = await fcmDao.getBlogWriter(req.post_id.toString());
} else if (req.post_type.toString() === "mvp") {
postData = await fcmDao.getMVPWriter(req.post_id.toString());
}

if (postData && user_id !== postData.user_id) {
pushCommentAlarm(postData, req.post_type);
}

const message = `${req.post_type} 나의 글에 댓글이 달렸어요.`
await alarmDao.post(req, message);
const target: string | null = await redisClient.get(req.post_user_id + "_socket")
Expand Down
18 changes: 18 additions & 0 deletions src/features/api-utils/post_like/post_like.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { alarmDao } from "../../../models/api-util/alarm/alarm.dao";
import { post_likeDao } from "../../../models/api-util/post_like/post_like.dao";
import { post_likeDto } from "../../../models/api-util/post_like/post_like.dto";
import { getSocketIO } from "../../../utils/socket.middleware";
import { fcmDao } from "../../../models/api-util/fcm/fcm.dao";
import { fcmDto } from "../../../models/api-util/fcm/fcm.dto";
import { pushLikeAlarm } from "../../../utils/fcm.utils";

export const post_likeService = {
post: async (req: post_likeDto, user_id: string) => {
Expand All @@ -13,6 +16,21 @@ export const post_likeService = {
...status.SUCCESS.body,
result: result
};

// 좋아요를 보낸 후 fcm 전송
let postData: fcmDto | null = null;
if (req.post_type.toString() === "community") {
postData = await fcmDao.getCommunityWriter(req.post_id.toString());
} else if (req.post_type.toString() === "blog") {
postData = await fcmDao.getBlogWriter(req.post_id.toString());
} else if (req.post_type.toString() === "mvp") {
postData = await fcmDao.getMVPWriter(req.post_id.toString());
}

if (postData && !req.checked && user_id !== postData.user_id) {
pushLikeAlarm(postData, req.post_type);
}

const message = `${req.post_type} 게시물에 좋아요 반응이 달렸어요.`
await alarmDao.post(req, message);
const target: string | null = await redisClient.get(req.post_user_id + "_socket")
Expand Down
32 changes: 32 additions & 0 deletions src/features/api-utils/reply/reply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { alarmDao } from "../../../models/api-util/alarm/alarm.dao";
import { replyDao } from "../../../models/api-util/reply/reply.dao";
import { replyPostDto, replyPatchDto, replyDeleteDto } from "../../../models/api-util/reply/reply.dto";
import { getSocketIO } from "../../../utils/socket.middleware";
import { fcmDao } from "../../../models/api-util/fcm/fcm.dao";
import { fcmDto } from "../../../models/api-util/fcm/fcm.dto";
import { pushCommentAlarm, pushReplyAlarm } from "../../../utils/fcm.utils";

export const replyService = {
post: async (req: replyPostDto, user_id: string) => {
Expand All @@ -13,6 +16,35 @@ export const replyService = {
...status.SUCCESS.body,
result: result
};

// 대댓글 보낸 후 게시글 작성자에게 fcm 전송
let postData: fcmDto | null = null;
if (req.post_type.toString() === "community") {
postData = await fcmDao.getCommunityWriter(req.post_id.toString());
} else if (req.post_type.toString() === "blog") {
postData = await fcmDao.getBlogWriter(req.post_id.toString());
} else if (req.post_type.toString() === "mvp") {
postData = await fcmDao.getMVPWriter(req.post_id.toString());
}

if (postData && user_id !== postData.user_id) {
pushCommentAlarm(postData, req.post_type);
}

// 대댓글 보낸 후 댓글 작성자에게 fcm 전송
const commentData = await fcmDao.getCommentWriter(
req.comment_id.toString()
);
// 댓글 작성자가 본인이거나 게시글 작성자랑 같은 사람이면 보내지 않음
if (
commentData &&
postData &&
commentData.user_id !== user_id &&
commentData.user_id !== postData.user_id
) {
pushReplyAlarm(commentData, req.post_type);
}

const message = `${req.post_type} 나의 댓글에 답글이 달렸어요.`
await alarmDao.post(req, message);
const target: string | null = await redisClient.get(req.post_user_id + "_socket")
Expand Down
76 changes: 76 additions & 0 deletions src/models/api-util/fcm/fcm.dao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ResultSetHeader, RowDataPacket } from "mysql2";
import { getPool } from "../../../../config/db.pool";
import { fcmSql } from "./fcm.sql";
import { ApiError } from "../../../../config/error";
import { fcmDto } from "./fcm.dto";

export const fcmDao = {
getCommunityWriter: async (post_id: string): Promise<fcmDto | null> => {
const connection = await getPool().getConnection();
try {
const [result] = await connection.query<RowDataPacket[]>(
fcmSql.getCommunityWriter,
[post_id]
);
connection.release();
return {
user_id: result[0].user_id,
created_at: result[0].created_at,
};
} catch (error) {
console.log(error);
return null;
}
},
getBlogWriter: async (post_id: string): Promise<fcmDto | null> => {
const connection = await getPool().getConnection();
try {
const [result] = await connection.query<RowDataPacket[]>(
fcmSql.getBlogWriter,
[post_id]
);
connection.release();
return {
user_id: result[0].user_id,
created_at: result[0].created_at,
};
} catch (error) {
console.log(error);
return null;
}
},
getMVPWriter: async (post_id: string): Promise<fcmDto | null> => {
const connection = await getPool().getConnection();
try {
const [result] = await connection.query<RowDataPacket[]>(
fcmSql.getMVPWriter,
[post_id]
);
connection.release();
return {
user_id: result[0].user_id,
created_at: result[0].created_at,
};
} catch (error) {
console.log(error);
return null;
}
},
getCommentWriter: async (comment_id: string): Promise<fcmDto | null> => {
const connection = await getPool().getConnection();
try {
const [result] = await connection.query<RowDataPacket[]>(
fcmSql.getCommentWriter,
[comment_id]
);
connection.release();
return {
user_id: result[0].user_id,
created_at: result[0].created_at,
};
} catch (error) {
console.log(error);
return null;
}
},
};
4 changes: 4 additions & 0 deletions src/models/api-util/fcm/fcm.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface fcmDto {
user_id: string;
created_at: Date;
}
22 changes: 22 additions & 0 deletions src/models/api-util/fcm/fcm.sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const fcmSql = {
getCommunityWriter: `
SELECT user_id, created_at
FROM community
WHERE id = ?;
`,
getBlogWriter: `
SELECT user_id, created_at
FROM blog
WHERE id = ?;
`,
getMVPWriter: `
SELECT user_id, created_at
FROM mvp
WHERE id = ?;
`,
getCommentWriter: `
SELECT user_id, created_at
FROM comment
WHERE id = ?;
`,
};
2 changes: 1 addition & 1 deletion src/models/api-util/post_like/post_like.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export interface post_likeDto {
post_id: number,
post_user_id: string,
checked: boolean,
post_type: "blog" | "mvp" | "article"
post_type: "blog" | "mvp" | "community"
}
4 changes: 4 additions & 0 deletions src/models/device_token.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface DeviceToken {
token: string;
updated_at: Date;
}
Loading