plebbit / plebbit-js

A Javascript API to build applications using plebbit
GNU General Public License v2.0
41 stars 7 forks source link

Implement the challenges API (subplebbit.settings.challenges) #9

Closed estebanabaroa closed 1 year ago

estebanabaroa commented 1 year ago

The code for the challenges API is in src/challenges. It needs to be integrated into the rest of the app. The types and how to use it is in src/challenges/README.md

It is pretty easy to use, it's only 1 function:

const {getChallengeVerification} = require('./challenges')

const getChallengeAnswers = async (challenges) => {
  // ...get challenge answers from user
  return challengeAnswers
}

let challengeVerification
try {
  challengeVerification = await getChallengeVerification(challengeRequest, subplebbit, getChallengeAnswers)
}
// getChallengeVerification will throw if one of the getChallenge function throws, which indicates a bug with the challenge script
catch (e) {
  // notify the sub owner that that one of his challenge is misconfigured via an error event
  subplebbit.emit('error', e.message)

  // notify the author that his publication wasn't published because the subplebbit is misconfigured
  challengeVerification = {
    challengeSuccess: false,
    reason: `One of the subplebbit challenges is misconfigured: ${e.message}`
  }
}

It is somewhat high priority to implement because once implemented we can start not requiring captchas for users with karma in the default subs.

The challenges included with plebbit-js are in src/challenges/plebbit-js-challenges. With the challenges API, it is possible to point to an arbitrary node.js file and use it for challenges. I don't know how this will work with native-functions. We might need a native function to require those challenges.

The challenges API already have their own tests, but one test I had to comment out was the one using the captcha library because I couldn't figure out how to make it work in the browser tests. The test is probably not needed as it doesn't test much of the logic. It does test the API for the 'captcha-canvas-v3' challenge that is included with plebbit-js so it could be good to figure out how to add it back.