Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action to start commercial #146

Merged
merged 3 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions server/src/actions/set-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
/***
* @param {ActionSuccessCallback} success
* @param {ActionErrorCallback} error
* @param {PredictionAction} predictionAction
* @param {number?} autoLockAfter
* @param {ClientData} client
* @param {CacheGetFunction} get
Expand All @@ -15,8 +14,7 @@ module.exports = {
* @returns {Promise<void>}
*/
// eslint-disable-next-line no-empty-pattern
async handler(success, error, { predictionAction, autoLockAfter = 120 }, { client }, { get, auth }) {

async handler(success, error, { }, { client }, { get, auth }) {
const broadcast = await get(client?.broadcast?.[0]);
if (!broadcast) return error("No broadcast associated");
if (!broadcast.channel) return error("No channel associated with broadcast");
Expand Down
44 changes: 44 additions & 0 deletions server/src/actions/start-commercial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { ApiClient } = require("@twurple/api");
const { StaticAuthProvider } = require("@twurple/auth");
module.exports = {
key: "start-commercial",
auth: ["client"],
requiredParams: ["commercialDuration"],
/***
* @param {ActionSuccessCallback} success
* @param {ActionErrorCallback} error
* @param {30 | 60 | 90 | 120 | 150 | 180} commercialDuration
* @param {ClientData} client
* @param {CacheGetFunction} get
* @param {CacheAuthFunctions} auth
* @param {SimpleUpdateRecord} updateRecord
* @returns {Promise<void>}
*/
// eslint-disable-next-line no-empty-pattern
async handler(success, error, { commercialDuration }, { client, user }, { get, auth }) {
if (!user.airtable?.website_settings?.includes("Full broadcast permissions")) return error("You don't have permission to start a commercial", 403);

const broadcast = await get(client?.broadcast?.[0]);
if (!broadcast) return error("No broadcast associated");
if (!broadcast.channel) return error("No channel associated with broadcast");

const channel = await auth.getChannel(broadcast?.channel?.[0]);
if (!channel.twitch_refresh_token) return error("No twitch auth token associated with channel");
if (!channel.channel_id || !channel.name || !channel.twitch_scopes) return error("Invalid channel data");
let scopes = channel.twitch_scopes.split(" ");
if (!["channel:edit:commercial"].every(scope => scopes.includes(scope))) return error("Token doesn't have the required scopes");

const accessToken = await auth.getTwitchAccessToken(channel);

const authProvider = new StaticAuthProvider(process.env.TWITCH_CLIENT_ID, accessToken);
const api = new ApiClient({authProvider});

try {
await api.channels.startChannelCommercial(channel.channel_id, commercialDuration);
} catch (e) {
console.log(e);
return error("Failed to start commercial");
}
return success();
}
};
36 changes: 36 additions & 0 deletions website/src/components/website/dashboard/Commercials.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<div class="mt-2">
<b-button-group>
<b-button class="label-button"><i class="fas fa-fw fa-dollar-sign"></i> Start Commercial</b-button>
<b-button @click="commercial(30)">30s</b-button>
<b-button @click="commercial(60)">1m</b-button>
<b-button @click="commercial(90)">1m30</b-button>
<b-button @click="commercial(120)">2m</b-button>
<b-button @click="commercial(150)">2m30</b-button>
<b-button @click="commercial(180)">3m</b-button>
</b-button-group>

</div>
</template>

<script>
import { startCommercial } from "@/utils/dashboard";
import { BButton, BButtonGroup } from "bootstrap-vue";

export default {
name: "Commercials",
props: ["client"],
components: { BButton, BButtonGroup },
methods: {
async commercial(commercialDuration) {
await startCommercial(this.$root.auth, "self", commercialDuration);
}
}
};
</script>

<style scoped>
.label-button {
pointer-events: none;
}
</style>
8 changes: 8 additions & 0 deletions website/src/utils/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export async function managePred(auth, client, predictionAction) {
});
}

export async function startCommercial(auth, client, commercialDuration) {
if (!auth?.user) return { error: true, errorMessage: "Not authenticated" };
return await authenticatedRequest(auth, "actions/start-commercial", {
client: client.id || client,
commercialDuration
});
}

export async function updateAutomaticTitle(auth, client) {
if (!auth?.user) return { error: true, errorMessage: "Not authenticated" };
return await authenticatedRequest(auth, "actions/set-title", {
Expand Down
8 changes: 6 additions & 2 deletions website/src/views/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<MatchEditor :match="liveMatch"></MatchEditor>
</div>
<Predictions v-if="liveMatch" :client="client"/>
<Commercials v-if="hasPermission('Full broadcast permissions')" :client="client" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a quick check to see if people have full broadcast perms - I think a full look at permissions might be needed in the future, but I'm happy with this for now

<b-button class="mt-2" variant="secondary" @click="updateTitle">
<i class="fal fa-fw fa-wand-magic mr-1"></i>Update title
</b-button>
Expand All @@ -28,10 +29,11 @@ import MatchEditor from "@/components/website/dashboard/MatchEditor";
import { BButton, BFormCheckbox } from "bootstrap-vue";
import { togglePlayerCams, updateAutomaticTitle } from "@/utils/dashboard";
import Predictions from "@/components/website/dashboard/Predictions";
import Commercials from "@/components/website/dashboard/Commercials";

export default {
name: "Dashboard",
components: { Predictions, MatchEditor, MatchThumbnail, BroadcastSwitcher, BFormCheckbox, BButton },
components: { Commercials, Predictions, MatchEditor, MatchThumbnail, BroadcastSwitcher, BFormCheckbox, BButton },
computed: {
user() {
if (!this.$root.auth.user?.airtableID) return {};
Expand Down Expand Up @@ -74,9 +76,11 @@ export default {
methods: {
url,
togglePlayerCams,

async updateTitle() {
await updateAutomaticTitle(this.$root.auth, "self", "create");
},
hasPermission(permission) {
return (this.user.website_settings || []).includes(permission);
}
}
};
Expand Down