Skip to content

Commit

Permalink
Auto-embed Youtube channel on user profile #624
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalam-alami committed Mar 21, 2021
1 parent 79d0e35 commit 6a7b129
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
11 changes: 8 additions & 3 deletions server/user/user-profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import entryService from "server/entry/entry.service";
import highScoreService from "server/entry/highscore/highscore.service";
import eventService from "server/event/event.service";
import twitchService from "server/event/streamers/twitch.service";
import youtubeService from "server/event/streamers/youtube.service";
import likeService from "server/post/like/like.service";
import postService from "server/post/post.service";
import { CustomRequest, CustomResponse } from "server/types";
Expand All @@ -19,11 +20,12 @@ export async function userProfile(req: CustomRequest, res: CustomResponse<Common
res.locals.pageTitle = profileUser.title;
res.locals.pageDescription = forms.markdownToText(profileUser.details.body);

const [entries, posts, scores, isTwitchLive] = await Promise.all([
const [entries, posts, scores, isTwitchLive, isYoutubeLive] = await Promise.all([
entryService.findUserEntries(profileUser),
postService.findPosts({ userId: profileUser.id }),
highScoreService.findUserScores(profileUser.id, { sortBy: "ranking" }),
twitchService.isLive(profileUser)
twitchService.isLive(profileUser),
youtubeService.isLive(profileUser)
]);

const alakajamEntries = [];
Expand All @@ -41,6 +43,8 @@ export async function userProfile(req: CustomRequest, res: CustomResponse<Common
}
});

const liveYoutubeUrl = isYoutubeLive ? youtubeService.getEmbedUrl(profileUser) : undefined;

res.render<CommonLocals>("user/user-profile", {
...res.locals,
profileUser,
Expand All @@ -51,7 +55,8 @@ export async function userProfile(req: CustomRequest, res: CustomResponse<Common
userScores: scores.models,
medals: scores.countBy((userScore: BookshelfModel) => userScore.get("ranking")),
userLikes: await likeService.findUserLikeInfo(posts.models as PostBookshelfModel[], res.locals.user),
isTwitchLive
isTwitchLive,
liveYoutubeUrl
});
} else {
res.errorPage(404, "No user exists with name " + req.params.name);
Expand Down
9 changes: 8 additions & 1 deletion server/user/user-profile.template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import * as postMacros from "server/post/post.macros";
import * as userMacros from "server/user/user.macros";

export default function render(context: CommonLocals): JSX.Element {
const { profileUser, user, isTwitchLive, alakajamEntries, externalEntries, otherEntries, posts, userScores, userLikes, medals } = context;
const { profileUser, user, isTwitchLive, liveYoutubeUrl, alakajamEntries, externalEntries, otherEntries,
posts, userScores, userLikes, medals } = context;
const socialLinks: UserSocialLinks = profileUser.related("details").get("social_links") || {};
const totalMedals = (medals[1] || 0) + (medals[2] || 0) + (medals[3] || 0);
const hasAvatar = Boolean(profileUser.get("avatar"));
Expand Down Expand Up @@ -93,6 +94,12 @@ export default function render(context: CommonLocals): JSX.Element {
</div>
)}

{ifSet(liveYoutubeUrl, () =>
<div class="mb-3">
{userMacros.youtubeEmbed(liveYoutubeUrl, { height: 500 })}
</div>
)}

{ifTrue(profileUser.related("details").get("body"), () =>
<div>
<h2>Bio</h2>
Expand Down
11 changes: 11 additions & 0 deletions server/user/user.macros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ export function twitchEmbed(twitchUsername: string, options: { height?: number;
}
}

export function youtubeEmbed(src: string, options: { height?: number } = {}): JSX.Element {
return <iframe
width="100%"
height={options.height || 200}
src={src}
title="YouTube video player"
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen></iframe>;
}

export function registerTwitchEmbedScripts(context: CommonLocals): void {
context.scripts.push("https://embed.twitch.tv/embed/v1.js");
}
2 changes: 2 additions & 0 deletions types/preact/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare module "preact" {
namespace JSX {
interface IntrinsicElements {
["jsx-wrapper"]: HTMLAttributes<HTMLElement>;
["iframe"]: HTMLAttributes<HTMLElement>;
}

interface HTMLAttributes<RefType extends EventTarget = EventTarget>
Expand Down Expand Up @@ -36,6 +37,7 @@ declare module "preact" {
enctype?: string;
formnovalidate?: boolean;
charset?: string;
allow?: string;
}
}
}

0 comments on commit 6a7b129

Please sign in to comment.