Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created April 25, 2018 09:59
Show Gist options
  • Save mayfer/d30adaaf5b64df79199e86915ba2d5fd to your computer and use it in GitHub Desktop.
Save mayfer/d30adaaf5b64df79199e86915ba2d5fd to your computer and use it in GitHub Desktop.
class CashorbitEvent {
constructor() {
this.version = 0;
}
validate(payload) {
if (!event_hub || (!this.version && this.version !== 0) || !this.vote_schema) {
throw new Error('Spiel_feedback requires event_hub, vote_type, version.');
}
let {error} = Joi.validate(payload, this.vote_schema);
if (error) {
console.error('Spiel_feedback validation error: ', error);
throw error;
}
}
async emit({payload, responders}) {
this.validate(payload)
return await event_hub.add_event({type: this.event_name, version: this.version, payload, responders});
}
// only use this if you need a response/confirmation back
async emit_and_respond({payload, responders}) {
if(!responders || responders.length == 0) {
responders = this.default_responders;
}
return await this.emit({payload, responders})
}
}
class SpielFeedbackReason extends CashorbitEvent {
constructor() {
super()
this.event_name = event_registry.spiel_feedback.reason;
this.default_responders = ['spiel_feedback_reason'];
this.vote_schema = Joi.object().keys({
user_id: Joi.number().required(),
spiel_id: Joi.string().required(),
reason_id: Joi.number().required(),
type: Joi.string().required(),
from_channel: Joi.string().optional(),
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment