mirror of
https://github.com/wassname/talk.git
synced 2026-08-16 11:29:31 +08:00
replaced eslint:recommended with prettier
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import {t} from 'plugin-api/beta/client/services';
|
||||
import { t } from 'plugin-api/beta/client/services';
|
||||
|
||||
/**
|
||||
* CheckToxicityHook adds hooks to the `commentBox`
|
||||
* that handles checking a comment for toxicity.
|
||||
*/
|
||||
export default class CheckToxicityHook extends React.Component {
|
||||
|
||||
// checked signifies if we already sent a request with the `checkToxicity` set to true.
|
||||
checked = false;
|
||||
|
||||
componentDidMount() {
|
||||
this.toxicityPreHook = this.props.registerHook('preSubmit', (input) => {
|
||||
|
||||
this.toxicityPreHook = this.props.registerHook('preSubmit', input => {
|
||||
// If we haven't check the toxicity yet, make sure to include `checkToxicity=true` in the mutation.
|
||||
// Otherwise post comment without checking the toxicity.
|
||||
if (!this.checked) {
|
||||
@@ -22,9 +20,15 @@ export default class CheckToxicityHook extends React.Component {
|
||||
}
|
||||
});
|
||||
|
||||
this.toxicityPostHook = this.props.registerHook('postSubmit', (result) => {
|
||||
this.toxicityPostHook = this.props.registerHook('postSubmit', result => {
|
||||
const actions = result.createComment.actions;
|
||||
if (actions && actions.some(({__typename, reason}) => __typename === 'FlagAction' && reason === 'TOXIC_COMMENT')) {
|
||||
if (
|
||||
actions &&
|
||||
actions.some(
|
||||
({ __typename, reason }) =>
|
||||
__typename === 'FlagAction' && reason === 'TOXIC_COMMENT'
|
||||
)
|
||||
) {
|
||||
this.props.notify('error', t('talk-plugin-toxic-comments.still_toxic'));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,33 @@
|
||||
import React from 'react';
|
||||
import {CommentDetail} from 'plugin-api/beta/client/components';
|
||||
import {isToxic} from '../utils';
|
||||
import { CommentDetail } from 'plugin-api/beta/client/components';
|
||||
import { isToxic } from '../utils';
|
||||
import styles from './ToxicDetail.css';
|
||||
import cn from 'classnames';
|
||||
import PropTypes from 'prop-types';
|
||||
import {t} from 'plugin-api/beta/client/services';
|
||||
import { t } from 'plugin-api/beta/client/services';
|
||||
|
||||
const getInfo = (toxicity, actions) => {
|
||||
const toxic = isToxic(actions);
|
||||
let text = t('talk-plugin-toxic-comments.unlikely');
|
||||
if (toxicity > 0.8) {
|
||||
text = t('talk-plugin-toxic-comments.highly_likely');
|
||||
}
|
||||
else if (toxicity >= 0.5) {
|
||||
} else if (toxicity >= 0.5) {
|
||||
text = t('talk-plugin-toxic-comments.possibly');
|
||||
}
|
||||
else if (toxicity >= 0.7) {
|
||||
} else if (toxicity >= 0.7) {
|
||||
text = t('talk-plugin-toxic-comments.likely');
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{text}
|
||||
<span className={cn(styles.info, {[styles.toxic]: toxic})}>
|
||||
<span className={cn(styles.info, { [styles.toxic]: toxic })}>
|
||||
{Math.round(toxicity * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const ToxicLabel = ({comment: {actions, toxicity}}) => (
|
||||
const ToxicLabel = ({ comment: { actions, toxicity } }) => (
|
||||
<CommentDetail
|
||||
icon={'add_box'}
|
||||
header={t('talk-plugin-toxic-comments.toxic_comment')}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import React from 'react';
|
||||
import {FlagLabel} from 'plugin-api/beta/client/components/ui';
|
||||
import { FlagLabel } from 'plugin-api/beta/client/components/ui';
|
||||
|
||||
const ToxicLabel = () => (
|
||||
<FlagLabel iconName="add_box">Toxic</FlagLabel>
|
||||
);
|
||||
const ToxicLabel = () => <FlagLabel iconName="add_box">Toxic</FlagLabel>;
|
||||
|
||||
export default ToxicLabel;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import {bindActionCreators} from 'redux';
|
||||
import {connect} from 'plugin-api/beta/client/hocs';
|
||||
import {notify} from 'plugin-api/beta/client/actions/notification';
|
||||
import { bindActionCreators } from 'redux';
|
||||
import { connect } from 'plugin-api/beta/client/hocs';
|
||||
import { notify } from 'plugin-api/beta/client/actions/notification';
|
||||
import CheckToxicityHook from '../components/CheckToxicityHook';
|
||||
|
||||
const mapDispatchToProps = (dispatch) =>
|
||||
bindActionCreators({notify}, dispatch);
|
||||
const mapDispatchToProps = dispatch => bindActionCreators({ notify }, dispatch);
|
||||
|
||||
export default connect(null, mapDispatchToProps)(CheckToxicityHook);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import {compose} from 'react-apollo';
|
||||
import {excludeIf} from 'plugin-api/beta/client/hocs';
|
||||
import { compose } from 'react-apollo';
|
||||
import { excludeIf } from 'plugin-api/beta/client/hocs';
|
||||
import ToxicDetail from './ToxicDetail';
|
||||
import {isToxic} from '../utils';
|
||||
import { isToxic } from '../utils';
|
||||
|
||||
const enhance = compose(
|
||||
excludeIf(({comment: {toxicity, actions}}) => toxicity === null || isToxic(actions)),
|
||||
excludeIf(
|
||||
({ comment: { toxicity, actions } }) =>
|
||||
toxicity === null || isToxic(actions)
|
||||
)
|
||||
);
|
||||
|
||||
export default enhance(ToxicDetail);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import {compose} from 'react-apollo';
|
||||
import {excludeIf} from 'plugin-api/beta/client/hocs';
|
||||
import { compose } from 'react-apollo';
|
||||
import { excludeIf } from 'plugin-api/beta/client/hocs';
|
||||
import ToxicDetail from './ToxicDetail';
|
||||
import {isToxic} from '../utils';
|
||||
import { isToxic } from '../utils';
|
||||
|
||||
const enhance = compose(
|
||||
excludeIf(({comment: {toxicity, actions}}) => toxicity === null || !isToxic(actions)),
|
||||
excludeIf(
|
||||
({ comment: { toxicity, actions } }) =>
|
||||
toxicity === null || !isToxic(actions)
|
||||
)
|
||||
);
|
||||
|
||||
export default enhance(ToxicDetail);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {compose, gql} from 'react-apollo';
|
||||
import {withFragments} from 'plugin-api/beta/client/hocs';
|
||||
import { compose, gql } from 'react-apollo';
|
||||
import { withFragments } from 'plugin-api/beta/client/hocs';
|
||||
import ToxicDetail from '../components/ToxicDetail';
|
||||
|
||||
const enhance = compose(
|
||||
@@ -13,8 +13,9 @@ const enhance = compose(
|
||||
reason
|
||||
}
|
||||
}
|
||||
}`,
|
||||
}),
|
||||
}
|
||||
`,
|
||||
})
|
||||
);
|
||||
|
||||
export default enhance(ToxicDetail);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {compose, gql} from 'react-apollo';
|
||||
import {withFragments, excludeIf} from 'plugin-api/beta/client/hocs';
|
||||
import { compose, gql } from 'react-apollo';
|
||||
import { withFragments, excludeIf } from 'plugin-api/beta/client/hocs';
|
||||
import ToxicLabel from '../components/ToxicLabel';
|
||||
import {isToxic} from '../utils';
|
||||
import { isToxic } from '../utils';
|
||||
|
||||
const enhance = compose(
|
||||
withFragments({
|
||||
@@ -13,9 +13,10 @@ const enhance = compose(
|
||||
reason
|
||||
}
|
||||
}
|
||||
}`,
|
||||
}
|
||||
`,
|
||||
}),
|
||||
excludeIf(({comment: {actions}}) => !isToxic(actions)),
|
||||
excludeIf(({ comment: { actions } }) => !isToxic(actions))
|
||||
);
|
||||
|
||||
export default enhance(ToxicLabel);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export function isToxic(actions) {
|
||||
return actions.some((action) => action.__typename === 'FlagAction' && action.reason === 'TOXIC_COMMENT');
|
||||
return actions.some(
|
||||
action =>
|
||||
action.__typename === 'FlagAction' && action.reason === 'TOXIC_COMMENT'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
const {readFileSync} = require('fs');
|
||||
const { readFileSync } = require('fs');
|
||||
const path = require('path');
|
||||
const hooks = require('./server/hooks');
|
||||
const resolvers = require('./server/resolvers');
|
||||
|
||||
module.exports = {
|
||||
typeDefs: readFileSync(path.join(__dirname, 'server/typeDefs.graphql'), 'utf8'),
|
||||
typeDefs: readFileSync(
|
||||
path.join(__dirname, 'server/typeDefs.graphql'),
|
||||
'utf8'
|
||||
),
|
||||
hooks,
|
||||
resolvers,
|
||||
};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const ms = require('ms');
|
||||
|
||||
const config = {
|
||||
API_ENDPOINT: process.env.TALK_PERSPECTIVE_API_ENDPOINT || 'https://commentanalyzer.googleapis.com/v1alpha1',
|
||||
API_ENDPOINT:
|
||||
process.env.TALK_PERSPECTIVE_API_ENDPOINT ||
|
||||
'https://commentanalyzer.googleapis.com/v1alpha1',
|
||||
API_KEY: process.env.TALK_PERSPECTIVE_API_KEY,
|
||||
THRESHOLD: process.env.TALK_TOXICITY_THRESHOLD || 0.8,
|
||||
API_TIMEOUT: ms(process.env.TALK_PERSPECTIVE_TIMEOUT || '300ms'),
|
||||
@@ -9,7 +11,9 @@ const config = {
|
||||
};
|
||||
|
||||
if (process.env.NODE_ENV !== 'test' && !config.API_KEY) {
|
||||
throw new Error('Please set the TALK_PERSPECTIVE_API_KEY environment variable to use the toxic-comments plugin. Visit https://www.perspectiveapi.com/ to request API access.');
|
||||
throw new Error(
|
||||
'Please set the TALK_PERSPECTIVE_API_KEY environment variable to use the toxic-comments plugin. Visit https://www.perspectiveapi.com/ to request API access.'
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = config;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const {APIError} = require('errors');
|
||||
const { APIError } = require('errors');
|
||||
|
||||
// ErrToxic is sent during a `CreateComment` mutation where
|
||||
// `input.checkToxicity` is set to true and the comment contains
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const {getScores, isToxic} = require('./perspective');
|
||||
const {ErrToxic} = require('./errors');
|
||||
const { getScores, isToxic } = require('./perspective');
|
||||
const { ErrToxic } = require('./errors');
|
||||
|
||||
// We don't add the hooks during _test_ as the perspective API is not available.
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
@@ -9,14 +9,12 @@ if (process.env.NODE_ENV === 'test') {
|
||||
module.exports = {
|
||||
RootMutation: {
|
||||
createComment: {
|
||||
async pre(_, {input}, _context, _info) {
|
||||
|
||||
async pre(_, { input }, _context, _info) {
|
||||
// Try getting scores.
|
||||
let scores;
|
||||
try {
|
||||
scores = await getScores(input.body);
|
||||
} catch(err) {
|
||||
|
||||
} catch (err) {
|
||||
// Warn and let mutation pass.
|
||||
console.trace(err);
|
||||
return;
|
||||
@@ -33,12 +31,13 @@ module.exports = {
|
||||
}
|
||||
|
||||
input.status = 'SYSTEM_WITHHELD';
|
||||
input.actions = input.actions && input.actions.length >= 0 ? input.actions : [];
|
||||
input.actions =
|
||||
input.actions && input.actions.length >= 0 ? input.actions : [];
|
||||
input.actions.push({
|
||||
action_type: 'FLAG',
|
||||
user_id: null,
|
||||
group_id: 'TOXIC_COMMENT',
|
||||
metadata: {}
|
||||
metadata: {},
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
const fetch = require('node-fetch');
|
||||
const {API_ENDPOINT, API_KEY, THRESHOLD, API_TIMEOUT, DO_NOT_STORE} = require('./config');
|
||||
const {
|
||||
API_ENDPOINT,
|
||||
API_KEY,
|
||||
THRESHOLD,
|
||||
API_TIMEOUT,
|
||||
DO_NOT_STORE,
|
||||
} = require('./config');
|
||||
|
||||
/**
|
||||
* Get scores from the perspective api
|
||||
@@ -7,33 +13,36 @@ const {API_ENDPOINT, API_KEY, THRESHOLD, API_TIMEOUT, DO_NOT_STORE} = require('.
|
||||
* @return {object} object containing toxicity scores
|
||||
*/
|
||||
async function getScores(text) {
|
||||
const response = await fetch(`${API_ENDPOINT}/comments:analyze?key=${API_KEY}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
timeout: API_TIMEOUT,
|
||||
body: JSON.stringify({
|
||||
comment: {
|
||||
text,
|
||||
const response = await fetch(
|
||||
`${API_ENDPOINT}/comments:analyze?key=${API_KEY}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
timeout: API_TIMEOUT,
|
||||
body: JSON.stringify({
|
||||
comment: {
|
||||
text,
|
||||
},
|
||||
|
||||
// TODO: support other languages.
|
||||
languages: ['en'],
|
||||
doNotStore: DO_NOT_STORE,
|
||||
requestedAttributes: {
|
||||
TOXICITY: {},
|
||||
SEVERE_TOXICITY: {},
|
||||
}
|
||||
}),
|
||||
});
|
||||
// TODO: support other languages.
|
||||
languages: ['en'],
|
||||
doNotStore: DO_NOT_STORE,
|
||||
requestedAttributes: {
|
||||
TOXICITY: {},
|
||||
SEVERE_TOXICITY: {},
|
||||
},
|
||||
}),
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
return {
|
||||
TOXICITY: {
|
||||
summaryScore: data.attributeScores.TOXICITY.summaryScore.value
|
||||
summaryScore: data.attributeScores.TOXICITY.summaryScore.value,
|
||||
},
|
||||
SEVERE_TOXICITY: {
|
||||
summaryScore: data.attributeScores.SEVERE_TOXICITY.summaryScore.value
|
||||
summaryScore: data.attributeScores.SEVERE_TOXICITY.summaryScore.value,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -53,9 +62,10 @@ function getProbability(scores) {
|
||||
* @return {boolean}
|
||||
*/
|
||||
function isToxic(scoresOrProbability) {
|
||||
const probability = typeof scoresOrProbability === 'object'
|
||||
? getProbability(scoresOrProbability)
|
||||
: scoresOrProbability;
|
||||
const probability =
|
||||
typeof scoresOrProbability === 'object'
|
||||
? getProbability(scoresOrProbability)
|
||||
: scoresOrProbability;
|
||||
return probability > THRESHOLD;
|
||||
}
|
||||
|
||||
@@ -69,8 +79,7 @@ function maskKeyInError(fn) {
|
||||
return async (...args) => {
|
||||
try {
|
||||
return await fn(...args);
|
||||
}
|
||||
catch(err) {
|
||||
} catch (err) {
|
||||
if (err.message) {
|
||||
err.message = err.message.replace(API_KEY, '***');
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ const get = require('lodash/get');
|
||||
|
||||
module.exports = {
|
||||
Comment: {
|
||||
toxicity: (comment) => get(comment, 'metadata.perspective.SEVERE_TOXICITY.summaryScore'),
|
||||
}
|
||||
toxicity: comment =>
|
||||
get(comment, 'metadata.perspective.SEVERE_TOXICITY.summaryScore'),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user