-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
273 lines (254 loc) · 8.26 KB
/
server.js
File metadata and controls
273 lines (254 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const express = require("express");
const { createServer } = require("node:http");
const { join } = require("node:path");
const { Server } = require("socket.io");
const multer = require("multer");
const upload = multer();
const app = express();
app.use("/assets", express.static("node_modules/socket.io/client-dist"));
const server = createServer(app);
const io = new Server(server);
// モックデータ(本番環境ではデータベースを使用)
const comments = [
{
id: "1",
userId: "1",
postTime: new Date(2024, 7, 23, 8, 1, 59),
updateTime: new Date(2024, 7, 23, 8, 1, 59),
parentCommentId: "",
content: "新入社員の松沢と申します。どうぞよろしくお願いいたします。",
},
{
id: "2",
userId: "0",
postTime: new Date(2024, 7, 23, 8, 1, 59),
updateTime: new Date(2024, 7, 23, 8, 1, 59),
parentCommentId: "1",
content:
"どうぞよろしくお願いいたします。",
}
];
const users = [
{
id: "0",
name: "森上 偉久馬",
avatar:
"https://demo.mescius.jp/inputmanjs/demos/ja/samples/comment/commentMode/threadMode/img/avatar1.png",
},
{
id: "1",
name: "葛城 孝史",
avatar:
"https://demo.mescius.jp/inputmanjs/demos/ja/samples/comment/commentMode/threadMode/img/avatar2.png",
}
];
const reactions = [
{
userId: "0",
commentId: "0",
reactionChar: "👍",
},
{
userId: "1",
commentId: "0",
reactionChar: "🔥",
}
];
// User取得
function getUserInfo(userId) {
return users.find((u) => u.id === userId);
}
// Comment削除
function deleteCommentAndChildren(commentId) {
const index = comments.findIndex((c) => c.id === commentId);
if (index >= 0) {
comments.splice(index, 1);
comments
.filter((c) => c.parentCommentId === commentId)
.forEach((childComment) => deleteCommentAndChildren(childComment.id));
}
}
// Comment変更を送信者以外にのみ通知します
function sendCommentChange(socketId, info) {
const socket = io.sockets.sockets.get(socketId);
socket.broadcast.emit("commentupdated", info);
}
// Reaction変更を送信者以外にのみ通知します
function sendReactionChange(socketId, info) {
const socket = io.sockets.sockets.get(socketId);
socket.broadcast.emit("reactionupdated", info);
}
function getReactionInfo(commentId, curUserId) {
const reactionMap = new Map();
reactions.forEach((reaction) => {
if (reaction.commentId === commentId) {
if (!reactionMap.has(reaction.reactionChar)) {
reactionMap.set(reaction.reactionChar, {
reactionChar: reaction.reactionChar,
count: 0,
currentUserReacted: false,
});
}
const reactionInfo = reactionMap.get(reaction.reactionChar);
reactionInfo.count++;
if (reaction.userId === curUserId) {
reactionInfo.currentUserReacted = true;
}
}
});
return Array.from(reactionMap.values());
}
// comment
function commentResponse(req, res) {
const method = req.method;
if (method === "GET") {
res.json({
hasMore: false,
comments: comments,
});
} else if (method === "POST") {
const { content, userId, parentId, socketId } = req.body;
const now = new Date();
const newComment = {
id: new Date().getTime().toString(),
userId: userId,
postTime: now,
updateTime: now,
parentCommentId: parentId,
content: content,
};
comments.push(newComment);
res.json(newComment);
// Comment変更を送信者以外にのみ通知します
sendCommentChange(socketId, {
type: "add",
comment: {
id: newComment.id,
userInfo: getUserInfo(newComment.userId),
content: newComment.content,
postTime: newComment.postTime,
updateTime: newComment.updateTime,
parentCommentId: newComment.parentCommentId,
},
});
} else if (method === "PUT") {
const { id, content, newContent, socketId } = req.body;
const comment = comments.find((c) => c.id === id);
if (comment) {
comment.content = newContent || content;
comment.updateTime = new Date();
}
res.json(comment);
// Comment変更を送信者以外にのみ通知します
sendCommentChange(socketId, {
type: "update",
comment: {
id: comment.id,
content: comment.content,
updateTime: comment.updateTime,
},
});
} else if (method === "DELETE") {
const id = req.query.commentId;
const socketId = req.query.socketId;
deleteCommentAndChildren(id);
res.end();
// Comment変更を送信者以外にのみ通知します
sendCommentChange(socketId, {
type: "delete",
comment: {
id: id,
},
});
}
}
// reaction
function reactionResponse(req, res) {
const method = req.method;
if (method === "GET") {
const commentId = req.query.commentId;
const userId = req.query.userId;
const reactionMap = new Map();
reactions.forEach((reaction) => {
if (reaction.commentId === commentId) {
if (!reactionMap.has(reaction.reactionChar)) {
reactionMap.set(reaction.reactionChar, {
reactionChar: reaction.reactionChar,
count: 0,
currentUserReacted: false,
});
}
const reactionInfo = reactionMap.get(reaction.reactionChar);
reactionInfo.count += 1;
if (reaction.userId === userId) {
reactionInfo.currentUserReacted = true;
}
}
});
res.json(Array.from(reactionMap.values()));
} else if (method === "POST") {
const { userId, commentId, reactChar, socketId } = req.body;
reactions.push({
userId: userId,
commentId: commentId,
reactionChar: reactChar,
});
res.send(true);
// Reaction変更を送信者以外にのみ通知します
sendReactionChange(socketId, {
commentId,
reactionInfo: getReactionInfo(commentId, userId),
});
} else if (method === "DELETE") {
const userId = req.query.userId;
const commentId = req.query.commentId;
const reactChar = req.query.reactChar;
const socketId = req.query.socketId;
const index = reactions.findIndex(
(c) =>
c.userId === userId &&
c.commentId === commentId &&
c.reactionChar === reactChar
);
if (index !== -1) {
reactions.splice(index, 1);
}
res.send(true);
// Reaction変更を送信者以外にのみ通知します
sendReactionChange(socketId, {
commentId,
reactionInfo: getReactionInfo(commentId, userId),
});
}
res.end();
}
// User
function userResponse(req, res) {
const method = req.method;
const { filterText, id } = req.query;
if (method === "GET") {
if (id) {
res.json(users.find((u) => u.id === id));
} else if (filterText) {
res.json(users.filter((u) => u.name.includes(filterText)));
}
}
res.end();
}
//クライアント用ファイルを開く
app.get("/", (req, res) => {
res.sendFile(join(__dirname, "index.html"));
});
// 3005ポートでサーバを起動
server.listen(3005, () => {
console.log("server running at http://localhost:3005");
});
app.all("/comments", upload.none(), (req, res) => {
commentResponse(req, res);
});
app.all("/users", upload.none(), (req, res) => {
userResponse(req, res);
});
app.all("/reactions", upload.none(), (req, res) => {
reactionResponse(req, res);
});