Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(settings): add battle notifier settings #616

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for adding this? it's already in .eslintrc.json

"printWidth": 80,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "avoid"
}
107 changes: 106 additions & 1 deletion src/api/player.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import { Op } from 'sequelize';
import { like, searchLimit, searchOffset } from '#utils/database';
import { authContext } from '#utils/auth';
import { authContext, authDiscord } from '#utils/auth';
import { pick, omit } from 'lodash-es';
import {
Team,
Expand All @@ -11,6 +11,7 @@ import {
LevelStats,
Level,
Setting,
BnKuskiRule,
} from '../data/models';

const router = express.Router();
Expand Down Expand Up @@ -88,6 +89,82 @@ const ChangeSettings = async data => {
return 1;
};

const BnKuskiRuleAttributes = [
'BattleTypes',
'Designers',
'LevelPatterns',
'BattleAttributes',
'MinDuration',
'MaxDuration',
'IgnoreList',
];

const BnSettings = async DiscordId => {
const get = await Setting.findOne({
where: { DiscordId },
attributes: ['KuskiIndex', 'DiscordId', 'BnEnabled'],
include: [
{
model: BnKuskiRule,
as: 'BnKuskiRules',
attributes: BnKuskiRuleAttributes,
},
],
});
return get;
};

const AllActiveBnSettings = async () => {
const get = await Setting.findAll({
attributes: ['KuskiIndex', 'DiscordId', 'BnEnabled'],
include: [
{
model: BnKuskiRule,
as: 'BnKuskiRules',
attributes: BnKuskiRuleAttributes,
required: true,
},
],
});
return get;
};

const ChangeBnEnabledSetting = async data => {
const setting = await Setting.findOne({
where: { DiscordId: data.DiscordId },
attributes: ['KuskiIndex'],
});
if (setting?.KuskiIndex) {
await Setting.update(
{ BnEnabled: data.BnEnabled },
{ where: { KuskiIndex: setting.KuskiIndex } },
);
}
return 1;
};

const ChangeBnSettings = async data => {
const setting = await Setting.findOne({
where: { DiscordId: data.DiscordId },
attributes: ['KuskiIndex'],
});
const KuskiIndex = setting?.KuskiIndex;
if (KuskiIndex && data.BnKuskiRules.length > 0) {
// first time setup, turn notification on by default
const currentRules = await BnKuskiRule.findAll({ where: { KuskiIndex } });
const isFirstTimeSetup = currentRules.length === 0;
if (isFirstTimeSetup) {
await Setting.update({ BnEnabled: 1 }, { where: { KuskiIndex } });
}

// override existing rules with new ones
await BnKuskiRule.destroy({ where: { KuskiIndex } });
const newRules = data.BnKuskiRules.map(rule => ({ ...rule, KuskiIndex }));
await BnKuskiRule.bulkCreate(newRules);
}
return 1;
};

const Player = async (IdentifierType, KuskiIdentifier, currentUser) => {
const query = {
where: {},
Expand Down Expand Up @@ -310,6 +387,34 @@ router
res.sendStatus(401);
}
})
.get('/bn/:DiscordId/linked', authDiscord, async (req, res) => {
const data = await Setting.findOne({
where: { DiscordId: req.params.DiscordId },
});
res.json(Boolean(data));
})
.get('/bn/:DiscordId', authDiscord, async (req, res) => {
const data = await BnSettings(req.params.DiscordId);
res.json(data);
})
.get('/bn', authDiscord, async (req, res) => {
const data = await AllActiveBnSettings();
res.json(data);
})
.post('/bn/:DiscordId/toggle/:BnEnabled', authDiscord, async (req, res) => {
const data = await ChangeBnEnabledSetting({
DiscordId: req.params.DiscordId,
BnEnabled: Number(req.params.BnEnabled),
});
res.json(data);
})
.post('/bn/:DiscordId', authDiscord, async (req, res) => {
const data = await ChangeBnSettings({
...req.body,
DiscordId: req.params.DiscordId,
});
res.json(data);
})
.post('/ignore/:Kuski', async (req, res) => {
const auth = authContext(req);
if (auth.auth) {
Expand Down
6 changes: 4 additions & 2 deletions src/config.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default {

// auth
// eslint-disable-next-line prettier/prettier
jwtSecret: 'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
jwtSecret:
'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
Comment on lines +52 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has eslint ignore above, something about your settings or the new .prettierrc.json forcing it anyway?

jwtAlgo: 'HS256',
recaptcha: {
client: '6Le-n9QUAAAAAG-3bYyysXddxwD6I6iJeDBTHf2r',
Expand All @@ -76,6 +77,7 @@ export default {
admin: 'admin',
},
apiAuth: '', // Authorization header sent by game events
url: 'https://test.elma.online/', // url used in discord messages
url: 'https://test.elma.online/', // url used in discord messages,
bnAuth: 'e5f13420-cf17-4fd5-8cc9-c96959d1048f', // Authorization header sent from BN
},
};
53 changes: 53 additions & 0 deletions src/data/models/BnKuskiRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import DataType from 'sequelize';
import Model from '../sequelize';

const Setting = Model.define('bn_kuski_rule', {
BnKuskiRuleIndex: {
type: DataType.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
KuskiIndex: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
BattleTypes: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
Designers: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
LevelPatterns: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
BattleAttributes: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
MinDuration: {
type: DataType.INTEGER,
allowNull: true,
defaultValue: 0,
},
MaxDuration: {
type: DataType.INTEGER,
allowNull: true,
defaultValue: 0,
},
IgnoreList: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
});

export default Setting;
5 changes: 5 additions & 0 deletions src/data/models/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const Setting = Model.define(
allowNull: false,
defaultValue: 1,
},
BnEnabled: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
},
{
indexes: [
Expand Down
15 changes: 15 additions & 0 deletions src/data/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import MultiTimeFile from './MultiTimeFile';
import Crippled from './Crippled';
import Recap from './Recap';
import ReplayLog from './ReplayLog';
import BnKuskiRule from './BnKuskiRule';

Replay.belongsTo(Kuski, {
foreignKey: 'DrivenBy',
Expand Down Expand Up @@ -511,6 +512,18 @@ Recap.belongsTo(Replay, {
as: 'ReplayData',
});

Setting.hasMany(BnKuskiRule, {
foreignKey: 'KuskiIndex',
sourceKey: 'KuskiIndex',
as: 'BnKuskiRules',
});

BnKuskiRule.belongsTo(Setting, {
foreignKey: 'KuskiIndex',
targetKey: 'KuskiIndex',
as: 'SettingData',
});

function sync(...args) {
return sequelize.sync(...args);
}
Expand Down Expand Up @@ -576,4 +589,6 @@ export {
Crippled,
Recap,
ReplayLog,
ReplayTags,
BnKuskiRule,
}; // add the data model here as well so it exports
12 changes: 8 additions & 4 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ export const auth = async body => {
};
}
if (kuskiData.dataValues.Password) {
const md5 = crypto
.createHash('md5')
.update(password)
.digest('hex');
const md5 = crypto.createHash('md5').update(password).digest('hex');
if (md5 === kuskiData.dataValues.Password) {
if (kuskiData.dataValues.Salt) {
await addSha3(
Expand Down Expand Up @@ -124,3 +121,10 @@ export function authContext(req) {
}
return { auth: false, userid: 0 };
}

export function authDiscord(req, res, next) {
if (req.header('Authorization') === config.discord.bnAuth) {
return next();
}
return res.sendStatus(401);
}