Skip to content

Commit e2c9202

Browse files
committed
add get new (check&return new contents of a forum
1 parent 9ffe42f commit e2c9202

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

Back/model/post.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Request, Response } from "express";
33
import { prisma } from "../generated/prisma-client";
44
import { pagesize } from "./consts";
55

6+
67
export const postDeleteHard = async function(req: Request, res: Response) {
78
try {
89
const authObj = verifyJWT(req.header("Authorization"));

Back/model/thread.ts

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ import {
3232
} from "./filter";
3333
import { atProcess } from "./at";
3434

35+
var lastModifies :{[key:string]:Date} = {};
36+
37+
export const initlastModifies = async function() {
38+
try {
39+
(await prisma.forums()).forEach(async item => {
40+
let fid = item.id;
41+
lastModifies[fid] = new Date();
42+
});
43+
44+
} catch (e) {
45+
console.log(e);
46+
}
47+
}
48+
3549
export const threadList = async function(req: Request, res: Response) {
3650
try {
3751
const authObj = verifyJWT(req.header("Authorization"));
@@ -605,6 +619,8 @@ export const threadReply = async function(req: Request, res: Response) {
605619
last: 1
606620
});
607621

622+
lastModifies[forumInfo.id] = new Date();
623+
608624
forumLastPostUpdate(forumInfo.id, resultThread[0].id);
609625
let havePushMessage = 0;
610626
if (uid !== authorInfo.id) {
@@ -673,7 +689,11 @@ export const threadDelete = async function(req: Request, res: Response) {
673689
id: tid
674690
})
675691
.user();
676-
692+
const forumInfo = await prisma
693+
.thread({
694+
id: tid
695+
})
696+
.forum();
677697
if (threadAuthInfo.id !== uid && !authObj.isAdmin) {
678698
res.json({ code: -1, msg: "您无权操作此帖子!" });
679699
return;
@@ -686,7 +706,7 @@ export const threadDelete = async function(req: Request, res: Response) {
686706
active: false
687707
}
688708
});
689-
709+
lastModifies[forumInfo.id] = new Date();
690710
res.json({ code: 1 });
691711
}
692712
} catch (e) {
@@ -749,7 +769,12 @@ export const threadDiamond = async function(req: Request, res: Response) {
749769
diamond: setDiamond === "1"
750770
}
751771
});
752-
772+
const forumInfo = await prisma
773+
.thread({
774+
id: tid
775+
})
776+
.forum();
777+
lastModifies[forumInfo.id] = new Date();
753778
const threadAuthor = await prisma
754779
.thread({
755780
id: tid
@@ -794,6 +819,12 @@ export const threadTop = async function(req: Request, res: Response) {
794819
top: Number.parseInt(setTop)
795820
}
796821
});
822+
const forumInfo = await prisma
823+
.thread({
824+
id: tid
825+
})
826+
.forum();
827+
lastModifies[forumInfo.id] = new Date();
797828
res.json({ code: 1 });
798829
}
799830
} catch (e) {
@@ -818,6 +849,12 @@ export const threadClose = async function(req: Request, res: Response) {
818849
closed: setClose === "1"
819850
}
820851
});
852+
const forumInfo = await prisma
853+
.thread({
854+
id: tid
855+
})
856+
.forum();
857+
lastModifies[forumInfo.id] = new Date();
821858
res.json({ code: 1 });
822859
}
823860
} catch (e) {
@@ -841,7 +878,12 @@ export const threadRecovery = async function(req: Request, res: Response) {
841878
active: true
842879
}
843880
});
844-
881+
const forumInfo = await prisma
882+
.thread({
883+
id: tid
884+
})
885+
.forum();
886+
lastModifies[forumInfo.id] = new Date();
845887
await prisma.updateManyPosts({
846888
where: {
847889
thread: {
@@ -996,6 +1038,69 @@ export const threadUpdate = async function(req: Request, res: Response) {
9961038
}
9971039
};
9981040

1041+
export const threadGetNew = async function(req: Request, res: Response) {
1042+
try {
1043+
let { fid } = req.params;
1044+
const authObj = verifyJWT(req.header("Authorization"));
1045+
const { timestamp } = req.body;
1046+
if (parseInt(timestamp) < lastModifies[fid].getTime()) {
1047+
res.json({code: 1, msg: {haveModified: true}});
1048+
return;
1049+
}
1050+
1051+
1052+
const whereArr = {
1053+
forum: {
1054+
id: fid
1055+
},
1056+
active: authObj.isAdmin ? undefined : true,
1057+
top: 0
1058+
};
1059+
1060+
1061+
let page = 1;
1062+
let okFlag = false;
1063+
let resultArr = [];
1064+
for (;okFlag === false;) {
1065+
const resultArrRaw: Array<Thread> = await prisma.threads({
1066+
where: whereArr,
1067+
skip: (page-1)*pagesize,
1068+
first: pagesize,
1069+
orderBy: "lastDate_DESC"
1070+
});
1071+
1072+
let i = 0;
1073+
for (; i < resultArrRaw.length; i++) {
1074+
let item = resultArrRaw[i];
1075+
if ((new Date(item.lastDate)) < (new Date(timestamp))) {
1076+
okFlag = true;
1077+
break;
1078+
}
1079+
resultArr.push({
1080+
thread: item,
1081+
user: filterUserInfo(
1082+
await prisma.thread({ id: item.id }).user()
1083+
),
1084+
lastReply: await prisma.posts({
1085+
where: {
1086+
thread: {
1087+
id: item.id,
1088+
active: authObj.isAdmin ? undefined : true
1089+
}
1090+
},
1091+
orderBy: "createDate_DESC",
1092+
first: 1
1093+
})
1094+
})
1095+
}
1096+
if (okFlag === true || i === resultArrRaw.length) break;
1097+
}
1098+
res.json({ code: 1, msg: { list: resultArr } });
1099+
} catch (e) {
1100+
res.json({ code: -1 });
1101+
}
1102+
}
1103+
9991104
export const threadMove = async function(req: Request, res: Response) {
10001105
try {
10011106
const { uid, isAdmin } = verifyJWT(req.header("Authorization"));
@@ -1058,6 +1163,8 @@ export const threadMove = async function(req: Request, res: Response) {
10581163
}
10591164
}
10601165
});
1166+
lastModifies[previousForum.id] = new Date();
1167+
lastModifies[newForum.id] = new Date();
10611168
res.json({ code: 1 });
10621169
} catch (e) {
10631170
res.json({ code: -1, msg: e.message });

Back/server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ import {
4747
threadUpdate,
4848
threadMove,
4949
threadSearch,
50-
threadRuntime
50+
threadRuntime,
51+
threadGetNew,
52+
initlastModifies,
5153
} from "./model/thread";
5254
import {
5355
postDeleteHard,
@@ -195,6 +197,7 @@ app.get("/forum/runtime", forumRuntime);
195197
app.get("/forum/listSimple", forumListSimple);
196198

197199
//Thread
200+
198201
app.get("/thread/top3/:fid", threadGetTop3);
199202
app.get("/thread/list/:fid/:page", threadList);
200203
app.get("/thread/info/:tid/:page", threadInfo);
@@ -210,6 +213,7 @@ app.post("/thread/search", threadSearch);
210213
app.post("/thread/delete/:tid", threadDelete);
211214
app.post("/thread/deleteHard/:tid", threadDeleteHard);
212215
app.post("/thread/recovery/:tid", threadRecovery);
216+
app.post("/thread/getNew/:fid", threadGetNew);
213217

214218
//Post
215219
app.get("/post/info/:pid", postInfo);
@@ -266,5 +270,6 @@ server.listen(7010, () => {
266270
console.log(
267271
`Rabbit WebServer / ${SERVER_VERSION} is running on port 7010.\nRedis:6379 , MySQL:3306 , graphQL:4466`
268272
);
273+
setTimeout(initlastModifies, 2000);
269274
});
270275

0 commit comments

Comments
 (0)