Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
xrystalll committed Jan 3, 2021
1 parent 95d1108 commit e5bcbf1
Show file tree
Hide file tree
Showing 24 changed files with 7,605 additions and 4,524 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ app/config/default.json
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.eslintcache
3 changes: 2 additions & 1 deletion app/config/default.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"callback_url": "/auth/twitch/callback"
},
"mongoremote": "mongodb+srv://",
"clientEndPoint": "http://localhost:1337"
"clientEndPoint": "http://localhost:1337",
"adminUsername": ""
}
3 changes: 3 additions & 0 deletions app/config/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
"valueMustBeNumber": "Value must be a number",
"unableSaveSetting": "Unable to save setting",
"unableGetGames": "Unable to get list of games",
"unableGetAllChannels": "Unable to get list of channels",
"unableGetAllInvites": "Unable to get all invites",
"unableCreateInvite": "Unable to create invite",
"unableDeleteInvite": "Unable to delete invite",
"year": "год",
"years": "года",
"manyYears": "лет",
Expand Down
2 changes: 1 addition & 1 deletion app/modules/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Mongoose = require('mongoose')
const cachegoose = require('cachegoose')

const ChannelsDB = require(path.join(__dirname, 'models', 'ChannelsDB'))

cachegoose(Mongoose)

Mongoose.connect(config.mongoremote, { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true })
Expand Down
3 changes: 3 additions & 0 deletions app/modules/models/UserDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const UserSchema = new Schema({
},
logo: {
type: String
},
admin: {
type: Boolean
}
})
UserSchema.plugin(findOrCreate)
Expand Down
80 changes: 71 additions & 9 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ router.get('/api/user', (req, res) => {
res.json({
twitchId: data.twitchId,
login: data.login,
logo: data.logo
logo: data.logo,
admin: data.admin
})
})
.catch(error => res.status(401).json({ error: Strings.accessDenied }))
Expand Down Expand Up @@ -99,7 +100,7 @@ router.get('/api/channel/mods', (req, res) => {
}),


// bot
// bot api
// join bot to chat
router.get('/api/bot/join', (req, res) => {
AuthProtect(req, res)
Expand Down Expand Up @@ -258,6 +259,7 @@ router.put('/api/commands/delete', (req, res) => {


// badWords api
// get all badWords
router.get('/api/words/all', (req, res) => {
AuthProtect(req, res)
.then(data => {
Expand Down Expand Up @@ -648,15 +650,75 @@ router.get('/api/games', (req, res) => {
}),


// invite api
router.get('/api/invite/add', (req, res) => {
const { channel } = req.query
// admin channels api
// get all channels
router.get('/api/admin/channels/all', (req, res) => {
AuthProtect(req, res)
.then(data => {
if (!data && data.login !== config.adminUsername) throw Error

ChannelsDB.find()
.cache(0, 'cache-all-channels-for-admin')
.then(data => res.json(data))
.catch(error => res.status(500).json({ error: Strings.unableGetAllChannels }))
})
.catch(error => res.status(401).json({ error: Strings.accessDenied }))
}),


// admin invites api
// get all invites
router.get('/api/admin/invite/all', (req, res) => {
AuthProtect(req, res)
.then(data => {
if (!data && data.login !== config.adminUsername) throw Error

InvitesDB.find()
.cache(0, 'cache-all-invites-for-admin')
.then(data => res.json(data))
.catch(error => res.status(500).json({ error: Strings.unableGetAllInvites }))
})
.catch(error => res.status(401).json({ error: Strings.accessDenied }))
}),

// add new invite
router.put('/api/admin/invite/add', (req, res) => {
AuthProtect(req, res)
.then(data => {
if (!data && data.login !== config.adminUsername) throw Error

const { channel } = req.body

if (!channel) return res.status(400).json({ error: Strings.emptyRequest })
if (!channel) return res.status(400).json({ error: Strings.emptyRequest })

InvitesDB.findOrCreate({ channel })
.then(data => res.json(data))
.catch(error => res.status(500).json({ error: Strings.unableCreateInvite }))
InvitesDB.findOrCreate({ channel })
.then(data => {
cachegoose.clearCache('cache-all-invites-for-admin')
res.json(data)
})
.catch(error => res.status(500).json({ error: Strings.unableCreateInvite }))
})
.catch(error => res.status(401).json({ error: Strings.accessDenied }))
}),

// delete invite
router.put('/api/admin/invite/delete', (req, res) => {
AuthProtect(req, res)
.then(data => {
if (!data && data.login !== config.adminUsername) throw Error

const { id } = req.body

if (!id) return res.status(400).json({ error: Strings.emptyRequest })

InvitesDB.deleteOne({ _id: Mongoose.Types.ObjectId(id) })
.then(data => {
cachegoose.clearCache('cache-all-invites-for-admin')
res.json(data)
})
.catch(error => res.status(500).json({ error: Strings.unableDeleteInvite }))
})
.catch(error => res.status(401).json({ error: Strings.accessDenied }))
}),


Expand Down
13 changes: 9 additions & 4 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const Strings = require(path.join(__dirname, 'config', 'strings.json'))
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io').listen(server)
const io = require('socket.io')(server, {
cors: {
origin: config.clientEndPoint,
}
})
const port = process.env.PORT || 1337

const routes = require(path.join(__dirname, 'routes'))
Expand Down Expand Up @@ -327,10 +331,10 @@ passport.use('twitch', new OAuth2Strategy({

InvitesDB.findOne({ channel })
.then(res => {
if (res && res.channel === data.login) {
if (res && res.channel === channel) {
UserDB.findOrCreate({
twitchId: data.id,
login: data.login
login: channel
})
return next(null, profile)
} else {
Expand Down Expand Up @@ -360,8 +364,9 @@ app.get(config.auth.callback_url, passport.authenticate('twitch', { failureRedir
const token = req.session.passport.user.accessToken
const hash = crypto.createHash('md5').update(req.session.passport.user.refreshToken + 'is' + req.session.passport.user.data[0].login).digest('hex')
const logo = req.session.passport.user.data[0].profile_image_url
const admin = req.session.passport.user.data[0].login === config.adminUsername

UserDB.updateOne({ twitchId: id }, { token, hash, logo })
UserDB.updateOne({ twitchId: id }, { token, hash, logo, admin })
.then(() => {
UserDB.find({ twitchId: id })
.then(data => {
Expand Down
2 changes: 1 addition & 1 deletion helpers/build_win.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const options = {
"dir": "./",
"icon": path.join(__dirname, '..', 'build', 'images', 'icon.ico'),
"name": "xlllBot",
"version": "1.4.2",
"version": "1.5.0",
"app-copyright": "xrystalll",
"out": "./releases",
"asar": true,
Expand Down
Loading

0 comments on commit e5bcbf1

Please sign in to comment.