Skip to content

Commit f2ee0be

Browse files
committed
Store user IP in database
Fix migration mechanism.
1 parent 9ee4962 commit f2ee0be

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

README.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,9 @@ Some things we could do include:
536536
Some spam events:
537537

538538
* 2021-12-22 https://archive.ph/EVcUw spammed a bunch of pro Chinese government messages, see also: https://github.com/cirosantilli/china-dictatorship and https://cirosantilli.com/china-dictatorship/backlinks
539+
+
540+
Did a bit of updating with `./heroku.sh psql`:
541+
+
542+
....
543+
UPDATE "Tag" SET name = REPLACE(name, '', '六四事件法轮功新疆再教育營')
544+
....

api/users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ router.post('/users', async function(req, res, next) {
7878
}
7979
user.username = req.body.user.username
8080
user.email = req.body.user.email
81+
user.ip = req.headers['x-forwarded-for']
8182
req.app.get('sequelize').models.User.setPassword(user, req.body.user.password)
8283
await user.save()
8384
await lib.deleteOldestForDemo(req.app.get('sequelize').models.User)

bin/sync-db.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99
(async () => {
1010
const path = require('path')
1111
const child_process = require('child_process');
12+
const { DatabaseError } = require('sequelize')
1213
const models = require('../models')
1314

1415
const sequelize = models.getSequelize(path.dirname(__dirname));
15-
let dbExists = await models.sync(sequelize)
16-
if (!dbExists) {
16+
let dbEmpty = true;
17+
try {
18+
await sequelize.models.SequelizeMeta.findOne()
19+
dbEmpty = false
20+
} catch(e) {
21+
if (e instanceof DatabaseError) {
22+
await models.sync(sequelize)
23+
}
24+
}
25+
if (!dbEmpty) {
1726
out = child_process.spawnSync('npx', ['sequelize-cli', 'db:migrate'])
1827
console.error(out.stdout.toString());
1928
console.error(out.stderr.toString());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
up: async (queryInterface, Sequelize) => queryInterface.sequelize.transaction(async transaction => {
3+
await queryInterface.addColumn('User', 'ip',
4+
{
5+
type: Sequelize.STRING,
6+
defaultValue: '',
7+
},
8+
{transaction},
9+
)
10+
}),
11+
down: async (queryInterface, Sequelize) => {
12+
await queryInterface.removeColumn('User', 'displayName')
13+
}
14+
};

models/user.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ module.exports = (sequelize) => {
5454
bio: DataTypes.STRING,
5555
image: DataTypes.STRING,
5656
hash: DataTypes.STRING(1024),
57-
salt: DataTypes.STRING
57+
salt: DataTypes.STRING,
58+
ip: {
59+
type: DataTypes.STRING,
60+
default: '',
61+
}
5862
},
5963
{
6064
indexes: [{ fields: ['username'] }, { fields: ['email'] }]

0 commit comments

Comments
 (0)