Skip to content

Commit

Permalink
feat: async ejs support (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 authored Jul 25, 2021
1 parent 6913f53 commit 71143fd
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/server/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ module.exports = {
setSetting: (key, value) => {
return settings.set(`app.${key}`, value);
},
stateNotify(type, message, options = null) {
io().emit("app.notice", { type, message, options });
stateNotify(type, message, data = undefined) {
io().emit("app.notice", { type, message, options: { data } });
type = types.includes(type) ? type : "notice";
logger[type](message);
logger[type](message, data);
},
};
2 changes: 2 additions & 0 deletions app/server/api/twitch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const settings = require("../libs/settings");
const state = require("../libs/twitch/state");
const twitchLogin = require("../libs/twitch/login");
const getUser = require("../libs/twitch/api/getUser");
const pubsubConnect = require("../libs/twitch/pubsub");
const setEvent = require("../libs/twitch/api/setEvent");
const getEvents = require("../libs/twitch/api/getEvents");
Expand Down Expand Up @@ -60,6 +61,7 @@ module.exports = {
});
},
getEvents: () => getEvents(),
getUser: (name) => getUser(name),
setEvent: (event) => setEvent(event),
getRewardList: () => getRewardList(),
getCommandList: () => getCommandList(),
Expand Down
5 changes: 5 additions & 0 deletions app/server/libs/twitch/api/getUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const twitch = require("../index");

module.exports = async function getUser(channel) {
return await twitch.api.helix.users.getUserByName(channel);
};
16 changes: 15 additions & 1 deletion app/server/libs/twitch/chat/onCommand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const getCommandByName = require("../api/getCommandByName");
const pushActions = require("../pushActions");
const appApi = require("../../../api/app");
const getUser = require("../api/getUser");
const jsonLogic = require("../jsonLogic");
const { _ } = require("../../i18next");
const ejs = require("ejs");
const ms = require("ms");

const cooldowns = {};

const api = {
getUser,
};

function parseUsage(usage) {
return (usage || "")
.replace(/[ ,]+/g, " ")
Expand Down Expand Up @@ -89,6 +95,14 @@ module.exports = async function onCommand({
let chatMessage = (commandEntry.message || "").trim();

if (chatMessage.length) {
this.say(channel, ejs.render(chatMessage, args));
args.api = api;

chatMessage = chatMessage.replace(/[\r\n]+/g, " ");

try {
this.say(channel, await ejs.render(chatMessage, args, { async: true }));
} catch (error) {
appApi.stateNotify("error", _("errors.unable_to_render_ejs"), command);
}
}
};
3 changes: 2 additions & 1 deletion app/static/locales/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"unable_to_fetch_follows": "Unable to fetch follows list",
"unable_to_fetch_new_follows": "Unable to fetch new follows",
"unable_to_fetch_rewards_list": "Unable to fetch rewards list",
"unable_to_fetch_stream_state": "Unable to fetch subscriber state"
"unable_to_fetch_stream_state": "Unable to fetch subscriber state",
"unable_to_render_ejs": "errors.unable_to_render_ejs"
}
}
3 changes: 2 additions & 1 deletion app/static/locales/fr/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"unable_to_fetch_follows": "Impossible de récuperer la list des follows",
"unable_to_fetch_new_follows": "Impossible de récuperer les derrniers follows",
"unable_to_fetch_rewards_list": "Impossible de récuperer la liste des récompenses",
"unable_to_fetch_stream_state": "Impossible de récupérer l'état de l'abonné"
"unable_to_fetch_stream_state": "Impossible de récupérer l'état de l'abonné",
"unable_to_render_ejs": "errors.unable_to_render_ejs"
}
}
1 change: 1 addition & 0 deletions front-src/client/api/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { emit, on } from "@/libs/socket.io";
export default {
getState: () => emit("twitch.getState"),
getEvents: () => emit("twitch.getEvents"),
getUser: (name) => emit("twitch.getUser", name),
setEvent: (event) => emit("twitch.setEvent", event),
getRewardList: () => emit("twitch.getRewardList"),
getCommandList: () => emit("twitch.getCommandList"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
import ejs from "../../../../../../../libs/ejs";
import { fetchText } from "../../../libs/utils";
import api from "../../../../../../../libs/api";
import getStyle from "../../../libs/getStyle";
import getTrans from "../../../libs/getTrans";
import { getContext } from "svelte";
Expand All @@ -21,7 +22,8 @@
Object.keys(data).forEach((tag) => {
html = html.replace(`$${tag}`, data[tag]);
});
html = ejs.render(html, data);
data.api = api;
html = await ejs.render(html, data, { async: true });
} catch (error) {
html = `<pre>${error}</pre>`;
}
Expand Down
8 changes: 8 additions & 0 deletions front-src/libs/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import api from "../client/api/twitch";

export default {
async getUser(name) {
const { _data } = await api.getUser(name);
return _data;
},
};
11 changes: 9 additions & 2 deletions front-src/overlay/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { loadUsedFonts, loadFont } from "../libs/fonts";
import { on } from "./libs/socket.io";
import animejs from "animejs";
import ejs from "../libs/ejs";
import api from "../libs/api";

(async () => await loadUsedFonts(false))();

Expand All @@ -26,7 +27,7 @@ function runAnime(action, cb) {

action.data.forEach((item) => {
promises.push(
createElementFromTarget(item.target).then((element) => {
createElementFromTarget(item.target).then(async (element) => {
const style = getStyle(item.target.style);
const trans = getTrans(item.target.trans);

Expand Down Expand Up @@ -69,7 +70,13 @@ function runAnime(action, cb) {
const regexp = new RegExp(`\\$${key}`, "g");
element.innerText = element.innerText.replace(regexp, val);
});
element.innerHTML = ejs.render(element.innerText, action.eventProps);
element.innerHTML = await ejs.render(
element.innerText,
{ ...action.eventProps, api },
{
async: true,
}
);
}

return element;
Expand Down

0 comments on commit 71143fd

Please sign in to comment.