Skip to content

Commit

Permalink
feat: Add Pajbot banned phrases check for bio.
Browse files Browse the repository at this point in the history
  • Loading branch information
KararTY committed Aug 17, 2021
1 parent 0b73275 commit 945b6fd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
20 changes: 19 additions & 1 deletion app/Controllers/Http/ProfilesController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { rules, schema } from '@ioc:Adonis/Core/Validator'
import Database from '@ioc:Adonis/Lucid/Database'
import PajbotAPI from '@ioc:Befriendlier-Shared/PajbotAPI'
import Profile from 'App/Models/Profile'
import User from 'App/Models/User'
import { DateTime } from 'luxon'
Expand Down Expand Up @@ -174,8 +175,25 @@ export default class ProfilesController {
profile.color = validated.color
await profile.save()

// Sometimes PerspectiveAPI is unavailable, so that's why we have multiple saving. At least color can be set.
profile.bio = validated.bio

const chatOwnerUser = await User.find(profile.chatUserId) as User
const pajbotCheck = await PajbotAPI.check(chatOwnerUser.name, profile.bio)
if (pajbotCheck && pajbotCheck.banned) {
// banphrase_data appears on banned === true
const banphraseData = pajbotCheck.banphrase_data as { phrase: string }
session.flash('message', {
error: 'Error: There are banned phrases in your bio!',

})
session.flash('errors', {
bio: [`🦆 Please remove: ${banphraseData.phrase}`]
})
session.flash('bio', profile.bio)
return response.redirect(`/profile/${id}`)
}

// Sometimes PerspectiveAPI or Pajbot is unavailable, so that's why we have multiple saving. At least color can be set.
await profile.save()

session.flash('message', { message: 'Successfully updated your profile.' })
Expand Down
22 changes: 22 additions & 0 deletions config/pajbot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const PajbotConfig = {
/**
* Enable Pajbot checking.
*/
enabled: true,

/**
* Custom pajbot channels - If length === 0 / unset, gets default values.
* [See default values here.](https://github.com/KararTY/BeFriendlier-Shared/blob/master/src/pajbotList.ts)
*/
channels: [],

/**
* HTTP request headers.
*/
headers: {
'content-type': 'application/json',
'user-agent': 'befriendlierapp (https://github.com/kararty/befriendlier-web)',
}
}

export default PajbotConfig
6 changes: 6 additions & 0 deletions contracts/pajbot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module '@ioc:Befriendlier-Shared/PajbotAPI' {
import { PajbotAPI } from 'befriendlier-shared'

const pajbotAPI: PajbotAPI
export default pajbotAPI
}
10 changes: 9 additions & 1 deletion providers/AppProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import feather from 'feather-icons'
import { TwitchAuth, PerspectiveAPI } from 'befriendlier-shared'
import { TwitchAuth, PerspectiveAPI, PajbotAPI } from 'befriendlier-shared'

export default class AppProvider {
public static needsApplication = true
Expand Down Expand Up @@ -29,6 +29,14 @@ export default class AppProvider {
headers: config.get('perspective.headers'),
}, Logger.level)
})

this.app.container.singleton('Befriendlier-Shared/PajbotAPI', () => {
return new PajbotAPI({
enabled: config.get('pajbot.enabled'),
channels: config.get('pajbot.channels'),
headers: config.get('pajbot.headers'),
}, Logger.level)
})
}

public async boot () {
Expand Down

0 comments on commit 945b6fd

Please sign in to comment.