Skip to content

Commit

Permalink
Merge branch 'master' into action-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
slmnio authored Nov 26, 2022
2 parents cbca19e + 1bd8335 commit 9d7e023
Show file tree
Hide file tree
Showing 64 changed files with 2,350 additions and 235 deletions.
5 changes: 4 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
"nodemon": "^2.0.7"
},
"dependencies": {
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.13.0",
"@twurple/api": "^5.2.5",
"@twurple/auth": "^5.2.5",
"airtable": "^0.10.1",
"body-parser": "^1.19.0",
"chalk": "^4.1.0",
"cors": "^2.8.5",
"discord.js": "~13",
"discord.js": "^13.3.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"libsodium-wrappers": "^0.7.10",
"node-fetch": "^2.6.1",
"ora": "^5.4.0",
"sharp": "^0.30.1",
Expand Down
15 changes: 12 additions & 3 deletions server/src/action-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ function dirtyID(id) {
return id;
}

/**
* @param {Cache} Cache - Cache object
* @param {string} tableName - Airtable table name this record belongs to
* @param {object} item - Full item as requested from Cache
* @param {AnyAirtableID} item.id - Item must have its Airtable ID
* @param {*?} item.* - Item can have any other data
* @param {*} data - Data to update (can be partial)
*/
async function updateRecord(Cache, tableName, item, data) {
// see: airtable-interface.js customUpdater
console.log(`[update record] updating table=${tableName} id=${item.id}`, data);
Expand All @@ -54,7 +62,7 @@ async function updateRecord(Cache, tableName, item, data) {
if (tableName === "News" && item.slug) Cache.set(`news-${item.slug}`, slmnggData, { eager: true });

try {
await slmngg(tableName).update(item.id, data);
return await slmngg(tableName).update(item.id, data);
} catch (e) {
console.error("Airtable update failed", e);
return { error: true };
Expand All @@ -77,8 +85,9 @@ async function createRecord(Cache, tableName, records) {
newRecords.forEach(record => {
Cache.set(cleanID(record.id), deAirtable(record.fields), { eager: true });
});
console.log(newRecords.length);
console.log(newRecords);
// console.log(newRecords.length);
// console.log(newRecords);
return newRecords;
} catch (e) {
console.error("Airtable create failed", e);
return { error: true, errorMessage: e.message };
Expand Down
33 changes: 33 additions & 0 deletions server/src/actions/create-live-guest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
key: "create-live-guest",
requiredParams: [],
auth: ["user"],
/***
* @param {ActionSuccessCallback} success
* @param {ActionErrorCallback} error
* @param {object}
* @param {UserData} user
* @param {SimpleUpdateRecord} updateRecord
* @returns {Promise<void>}
*/
async handler(success, error, { }, { user }, { updateRecord, get, createRecord }) {
if (user.airtable?.live_guests?.length > 0) {
const currentLiveGuest = await get(user.airtable?.live_guests[0]);
let response = await updateRecord("Live Guests", currentLiveGuest, {
"Discord ID": user.discord.id,
"Avatar": `https://cdn.discordapp.com/avatars/${user.discord.id}/${user.discord.avatar}.webp?size=512`,
"Use Cam": true
});
return response?.error ? error("Airtable error", 500) : success();
} else {
let response = await createRecord("Live Guests", {
"Discord ID": user.discord.id,
"Avatar": `https://cdn.discordapp.com/avatars/${user.discord.id}/${user.discord.avatar}.webp?size=512`,
"Name": user.discord.username,
"Player": [user.airtable.id],
"Use Cam": true
});
return response?.error ? error("Airtable error", 500) : success();
}
}
};
2 changes: 1 addition & 1 deletion server/src/actions/start-commercial.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ module.exports = {
*/
// eslint-disable-next-line no-empty-pattern
async handler(success, error, { commercialDuration }, { client }, { get, auth }) {
if (!user.airtable?.website_settings?.includes("Full broadcast permissions")) return error("You don't have permission to start a commercial", 403);
const { channel } = getTwitchChannel(client, ["channel:edit:commercial"], { success, error });
const api = getTwitchAPIClient(channel);

try {
await api.channels.startChannelCommercial(channel.channel_id, commercialDuration);
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions server/src/airtable-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function setRebuilding(isRebuilding) {
// Starting with syncing Matches

// const tables = ["Matches", "Teams", "Themes", "Events", "Players", "Player Relationships"];
const tables = ["Broadcasts", "Clients", "Players", "Channels", "Events", "Event Series", "Teams", "Ad Reads", "Ad Read Groups", "News", "Matches", "Themes", "Socials", "Accolades", "Player Relationships", "Brackets", "Live Guests", "Headlines", "Maps", "Map Data", "Heroes", "Log Files", "Tracks", "Track Groups", "Track Group Roles"];
const tables = ["Broadcasts", "Clients", "Channels", "Discord Bots", "Players", "Events", "Event Series", "Teams", "Ad Reads", "Ad Read Groups", "News", "Matches", "Themes", "Socials", "Accolades", "Player Relationships", "Brackets", "Live Guests", "Headlines", "Maps", "Map Data", "Heroes", "Log Files", "Tracks", "Track Groups", "Track Group Roles"];
const staticTables = ["Redirects"];

function deAirtable(obj) {
Expand Down Expand Up @@ -120,7 +120,7 @@ async function getAllTableData(tableName, options = {}) {
// Airtable down
return console.error("Airtable 503");
}
console.error("Airtable error", e);
console.error(`[Airtable error] getting all table data on ${tableName}`, e);
}
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ function registerUpdater(tableName, options) {
// Airtable down
return console.error("Airtable 503");
}
console.error("Airtable error", e);
console.error(`[Airtable error] getting updates from ${tableName}`, e);
}
}
}, pollRate);
Expand Down
39 changes: 32 additions & 7 deletions server/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ async function dataUpdate(id, data, options) {
if (JSON.stringify(store.get(id)) !== JSON.stringify(data)) {
// console.log(`Data update on [${id}]`);
recents.sent++;
if (!(options && options.custom)) updateFunction(id, { oldData: store.get(id), newData: data });
if (data) data = await removeAntiLeak(id, data);
// if (options?.eager) console.log("Sending");
await broadcast(id, "data_update", id, data);
if (!(options && options.custom)) updateFunction(id, { oldData: store.get(id), newData: data });
}
}

Expand All @@ -116,9 +116,13 @@ const slmnggAttachments = {
"Teams": ["icon"]
};

function stripValidation(str) {
function generateAttachmentURL(str, filename) {
let idx = str.indexOf("ts=");
if (idx !== -1) return str.slice(0, idx -1);
if (idx !== -1) str = str.slice(0, idx -1);

if (filename && !str.split("/").pop().includes(".")) {
str += `?filename=${filename}`;
}
return str;
}

Expand All @@ -130,10 +134,10 @@ async function removeAttachmentTimestamps(data) {
tableData.forEach(key => {
if (data[key]) {
data[key].forEach(attachment => {
attachment.url = stripValidation(attachment.url);
attachment.url = generateAttachmentURL(attachment.url, attachment.filename);
for (let size in attachment.thumbnails) {
size = attachment.thumbnails[size];
size.url = stripValidation(size.url);
size.url = generateAttachmentURL(size.url, attachment.filename);
}
});
}
Expand Down Expand Up @@ -194,7 +198,13 @@ async function set(id, data, options) {

if (data?.__tableName === "Channels") {
auth.set(`channel_${id}`, data);
return; // not setting it on global requestble store
return; // not setting it on global requestable store
}

if (data?.__tableName === "Discord Bots") {
auth.set(`bot_${cleanID(id)}`, data);

return; // not setting it on global requestable store
}

if (data?.__tableName === "Events") {
Expand Down Expand Up @@ -262,6 +272,7 @@ async function set(id, data, options) {

await dataUpdate(id, data, options);
store.set(id, data);

}
function cleanID(id) {
if (!id) return null;
Expand Down Expand Up @@ -314,6 +325,18 @@ async function getPlayer(discordID) {
async function getChannel(airtableID) {
return auth.get(`channel_${cleanID(airtableID)}`);
}
async function getBot(airtableID) {
return auth.get(`bot_${cleanID(airtableID)}`);
}
async function getChannelByID(channelID) {
return (await getChannels()).find(channel => channel.channel_id === channelID);
}
async function getChannels() {
return await Promise.all(((await get("Channels"))?.ids || []).map(id => getChannel(id)));
}
async function getBots() {
return await Promise.all(((await get("Discord Bots"))?.ids || []).map(id => getBot(id)));
}

async function getTwitchAccessToken(channel) {
// get stored access token, check if it's valid
Expand All @@ -338,6 +361,8 @@ module.exports = {
getData: getAuthenticatedData,
getPlayer,
getChannel,
getTwitchAccessToken
getChannelByID,
getTwitchAccessToken,
getBots
}
};
1 change: 1 addition & 0 deletions server/src/custom-datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function tableUpdated(tableName, Cache) {
if (tableName === "Matches") matchUpdate(Cache);
if (tableName === "Broadcasts") broadcastUpdate(Cache);
if (tableName === "Players") playerList(Cache);
// TODO: maybe add discord bots here?
}
module.exports = tableUpdated;

Expand Down
Loading

0 comments on commit 9d7e023

Please sign in to comment.