Skip to content

Commit 1b855c2

Browse files
author
robot
committed
feat: daily check
1 parent 22b142a commit 1b855c2

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

.github/workflows/schedule.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
with:
1616
node-version: "14"
1717
- run: npm install
18-
- name: daily problem
18+
- name: daily problem and daily check
1919
env:
2020
issueToken: ${{secrets.ISSUETOKEN}}
2121
run: npm run daily-schedule

database.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const MongoClient = require("mongodb").MongoClient;
2+
const { getDay } = require("./utils/day");
3+
4+
const uri = process.env.collectionString;
5+
let collection = null;
6+
const client = new MongoClient(uri, {
7+
useNewUrlParser: true,
8+
useUnifiedTopology: true,
9+
});
10+
client.connect((err, data) => {
11+
collection = client.db("91algo").collection("my-solution");
12+
});
13+
14+
const findSolution = function (name) {
15+
return new Promise((resolve, reject) => {
16+
collection.find({ name }, function (err, result) {
17+
if (err) reject(err);
18+
resolve(result);
19+
});
20+
});
21+
};
22+
23+
const dailyCheck = async function (name, data, callback) {
24+
try {
25+
const solution = await findSolution();
26+
console.log(solution);
27+
solution[getDay() - 1] = data;
28+
collection.updateOne({ name }, { $set: solution });
29+
return data;
30+
} catch (_) {
31+
const solution = Array(91);
32+
solution[getDay() - 1] = data;
33+
return new Promise((resolve, reject) => {
34+
collection.insert({ name: solution }, function (err, result) {
35+
if (err) reject(err);
36+
resolve(result);
37+
});
38+
});
39+
}
40+
};
41+
42+
module.exports = {
43+
dailyCheck,
44+
};

middleware/mockUserInfo.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = ({ whitelist = [] }) =>
44
ctx.session = {};
55
}
66
ctx.session.user = {
7-
login: "lilyzhaoyilu",
7+
// login: "lilyzhaoyilu",
8+
login: "azl397985856",
89
avatar_url: "https://avatars.githubusercontent.com/u/12479470?v=4",
910
name: "lucifer",
1011
pay: true,

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dev": "NODE_ENV=development ./node_modules/.bin/nodemon bin/www",
88
"prd": "pm2 start bin/www",
99
"test": "echo \"Error: no test specified\" && exit 1",
10-
"daily-schedule": "node ./schedule/daily-problem.js",
10+
"daily-schedule": "node ./schedule/daily-problem.js && node ./schedule/daily-check.js",
1111
"generate": "node ./static/users/generate.js && node ./static/solution/generate.js && node ./static/lectures/generate.js"
1212
},
1313
"dependencies": {
@@ -23,6 +23,7 @@
2323
"koa-router": "^7.4.0",
2424
"koa-static": "^5.0.0",
2525
"koa-views": "^6.2.0",
26+
"mongodb": "^3.6.6",
2627
"node-fetch": "^2.6.1",
2728
"pug": "^2.0.3"
2829
},

routes/github.js

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
const router = require("koa-router")();
22
const fetch = require("node-fetch");
3-
const fs = require("fs");
4-
const path = require("path");
5-
const process = require("child_process");
63

74
const { getDay } = require("../utils/day");
85
const { success, fail } = require("../utils/request");
9-
const mySolutions = require("../static/my/solutions.json");
106
const solutions = require("../static/solution/solutions.json");
7+
const mySolutions = require("../database");
118

129
router.all("/api/v1/github/content", async (ctx) => {
1310
const { url, ...params } = ctx.query;
@@ -38,24 +35,8 @@ router.all("/api/v1/github/webhook", async (ctx) => {
3835
return;
3936
}
4037
if (action === "created" && comment.body.length > 20) {
41-
mySolutions[comment.user.login][getDay() - 1] = {
42-
// title: problem.title,
43-
url: comment.html_url,
44-
body: comment.body,
45-
};
46-
47-
fs.writeFileSync(
48-
path.resolve(__dirname, "../static/my/solutions.json"),
49-
JSON.stringify(mySolutions)
50-
);
51-
52-
process.exec(
53-
"sh " + path.resolve(__dirname, "../scripts/commit.sh"),
54-
console.log
55-
);
38+
ctx.body = success("收到!");
5639
}
57-
58-
ctx.body = success(mySolutions[comment.user.login]);
5940
});
6041

6142
module.exports = router;

0 commit comments

Comments
 (0)