2021-10-28 01:35:53 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.handmade.network/hmn/hmn/src/config"
|
|
|
|
"git.handmade.network/hmn/hmn/src/hmnurl"
|
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
|
|
|
"git.handmade.network/hmn/hmn/src/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getBaseDataAutocrumb(c *RequestContext, title string) templates.BaseData {
|
|
|
|
return getBaseData(c, title, []templates.Breadcrumb{{Name: title, Url: ""}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(asaf): If you set breadcrumbs, the breadcrumb for the current project will automatically be prepended when necessary.
|
2022-11-05 21:23:12 +00:00
|
|
|
//
|
|
|
|
// If you pass nil, no breadcrumbs will be created.
|
2021-10-28 01:35:53 +00:00
|
|
|
func getBaseData(c *RequestContext, title string, breadcrumbs []templates.Breadcrumb) templates.BaseData {
|
2021-12-08 03:37:52 +00:00
|
|
|
var project models.Project
|
2021-11-11 19:00:46 +00:00
|
|
|
if c.CurrentProject != nil {
|
|
|
|
project = *c.CurrentProject
|
|
|
|
}
|
|
|
|
|
2021-10-28 01:35:53 +00:00
|
|
|
var templateUser *templates.User
|
|
|
|
var templateSession *templates.Session
|
|
|
|
if c.CurrentUser != nil {
|
|
|
|
u := templates.UserToTemplate(c.CurrentUser, c.Theme)
|
|
|
|
s := templates.SessionToTemplate(c.CurrentSession)
|
|
|
|
templateUser = &u
|
|
|
|
templateSession = &s
|
|
|
|
}
|
|
|
|
|
|
|
|
notices := getNoticesFromCookie(c)
|
|
|
|
|
|
|
|
if len(breadcrumbs) > 0 {
|
2021-11-10 04:11:39 +00:00
|
|
|
projectUrl := c.UrlContext.BuildHomepage()
|
2021-10-28 01:35:53 +00:00
|
|
|
if breadcrumbs[0].Url != projectUrl {
|
|
|
|
rootBreadcrumb := templates.Breadcrumb{
|
2021-11-11 19:00:46 +00:00
|
|
|
Name: project.Name,
|
2021-10-28 01:35:53 +00:00
|
|
|
Url: projectUrl,
|
|
|
|
}
|
|
|
|
breadcrumbs = append([]templates.Breadcrumb{rootBreadcrumb}, breadcrumbs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseData := templates.BaseData{
|
|
|
|
Theme: c.Theme,
|
|
|
|
Title: title,
|
|
|
|
Breadcrumbs: breadcrumbs,
|
|
|
|
|
2024-05-20 22:00:03 +00:00
|
|
|
CurrentUrl: c.FullUrl(),
|
|
|
|
CurrentProjectUrl: c.UrlContext.BuildHomepage(),
|
|
|
|
LoginPageUrl: hmnurl.BuildLoginPage(c.FullUrl()),
|
|
|
|
ProjectCSSUrl: hmnurl.BuildProjectCSS(project.Color1),
|
|
|
|
DiscordInviteUrl: "https://discord.gg/hmn",
|
|
|
|
NewsletterSignupUrl: hmnurl.BuildAPINewsletterSignup(),
|
2021-10-28 01:35:53 +00:00
|
|
|
|
2021-12-08 03:37:52 +00:00
|
|
|
Project: templates.ProjectToTemplate(&project, c.UrlContext.BuildHomepage()),
|
2021-10-28 01:35:53 +00:00
|
|
|
User: templateUser,
|
|
|
|
Session: templateSession,
|
|
|
|
Notices: notices,
|
|
|
|
|
2022-06-02 01:49:19 +00:00
|
|
|
ReportIssueEmail: "team@handmade.network",
|
2021-10-28 01:35:53 +00:00
|
|
|
|
2022-02-13 20:07:09 +00:00
|
|
|
OpenGraphItems: buildDefaultOpenGraphItems(&project, c.CurrentProjectLogoUrl, title),
|
2021-10-28 01:35:53 +00:00
|
|
|
|
2021-11-11 19:00:46 +00:00
|
|
|
IsProjectPage: !project.IsHMN(),
|
2021-10-28 01:35:53 +00:00
|
|
|
Header: templates.Header{
|
Add Discord login (#106)
This leverages our existing Discord OAuth implementation. Any users with a linked Discord account will be able to log in immediately. When logging in, we request the `email` scope in addition to `identity`, so existing users will be prompted one time to accept the new permissions. On subsequent logins, Discord will skip the prompt.
When linking your Discord account to an existing HMN account, we continue to only request the `identity` scope, so we do not receive the user's Discord email.
Both login and linking go through the same Discord OAuth callback. All flows through the callback try to achieve the same end goal: a logged-in HMN user with a linked Discord account.
Linking works the same as it ever has. Login, however, is different because we do not have a session ID to use as the OAuth state. To account for this, I have added a `pending_login` table that stores a secure unique ID and the eventual destination URL. These pending logins expire after 10 minutes. When we receive the OAuth callback, we look up the pending login by the OAuth `state` and immediately delete it. The destination URL will be used to redirect the user to the right place.
If we have a `discord_user` entry for the OAuth'd Discord user, we immediately log the user into the associated HMN account. This is the typical login case. If we do not have a `discord_user`, but there is exactly one HMN user with the same email address as the Discord user, we will link the two accounts and log into the HMN account.
(It is possible for multiple HMN accounts to have the same email, because we don't have a uniqueness constraint there. We fail the login in this case rather than link to the wrong account.)
Finally, if no associated HMN user exists, a new one will be created. It will use the Discord user's username, email, and avatar. This user will have no password, but they can set or reset a password through the usual flows.
Co-authored-by: Ben Visness <bvisness@gmail.com>
Reviewed-on: https://git.handmade.network/hmn/hmn/pulls/106
2023-05-06 19:38:50 +00:00
|
|
|
AdminUrl: hmnurl.BuildAdminApprovalQueue(), // TODO(asaf): Replace with general-purpose admin page
|
|
|
|
UserSettingsUrl: hmnurl.BuildUserSettings(""),
|
|
|
|
LoginActionUrl: hmnurl.BuildLoginAction(c.FullUrl()),
|
|
|
|
LogoutActionUrl: hmnurl.BuildLogoutAction(c.FullUrl()),
|
|
|
|
ForgotPasswordUrl: hmnurl.BuildRequestPasswordReset(),
|
|
|
|
RegisterUrl: hmnurl.BuildRegister(""),
|
|
|
|
LoginWithDiscordUrl: hmnurl.BuildLoginWithDiscord(c.FullUrl()),
|
2021-10-28 01:35:53 +00:00
|
|
|
|
|
|
|
HMNHomepageUrl: hmnurl.BuildHomepage(),
|
2023-09-25 07:40:45 +00:00
|
|
|
ProjectIndexUrl: hmnurl.BuildProjectIndex(1, ""),
|
2021-10-28 01:35:53 +00:00
|
|
|
PodcastUrl: hmnurl.BuildPodcast(),
|
2022-06-12 12:45:56 +00:00
|
|
|
FishbowlUrl: hmnurl.BuildFishbowlIndex(),
|
2021-11-10 04:11:39 +00:00
|
|
|
ForumsUrl: hmnurl.HMNProjectContext.BuildForum(nil, 1),
|
2022-08-13 20:07:37 +00:00
|
|
|
ConferencesUrl: hmnurl.BuildConferences(),
|
2023-04-22 16:26:07 +00:00
|
|
|
JamsUrl: hmnurl.BuildJamsIndex(),
|
2022-09-10 16:29:57 +00:00
|
|
|
EducationUrl: hmnurl.BuildEducationIndex(),
|
2024-01-28 17:12:59 +00:00
|
|
|
CalendarUrl: hmnurl.BuildCalendarIndex(),
|
2024-05-21 02:37:28 +00:00
|
|
|
ManifestoUrl: hmnurl.BuildManifesto(),
|
|
|
|
AboutUrl: hmnurl.BuildAbout(),
|
2021-10-28 01:35:53 +00:00
|
|
|
},
|
|
|
|
Footer: templates.Footer{
|
|
|
|
HomepageUrl: hmnurl.BuildHomepage(),
|
|
|
|
AboutUrl: hmnurl.BuildAbout(),
|
|
|
|
ManifestoUrl: hmnurl.BuildManifesto(),
|
|
|
|
CommunicationGuidelinesUrl: hmnurl.BuildCommunicationGuidelines(),
|
2023-09-25 07:40:45 +00:00
|
|
|
ProjectIndexUrl: hmnurl.BuildProjectIndex(1, ""),
|
2021-10-28 01:35:53 +00:00
|
|
|
ContactUrl: hmnurl.BuildContactPage(),
|
2022-02-10 20:27:28 +00:00
|
|
|
SearchActionUrl: "https://duckduckgo.com",
|
2021-10-28 01:35:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CurrentUser != nil {
|
|
|
|
baseData.Header.UserProfileUrl = hmnurl.BuildUserProfile(c.CurrentUser.Username)
|
|
|
|
}
|
|
|
|
|
2021-11-11 19:00:46 +00:00
|
|
|
if !project.IsHMN() {
|
2021-10-28 01:35:53 +00:00
|
|
|
episodeGuideUrl := ""
|
2021-11-11 19:00:46 +00:00
|
|
|
defaultTopic, hasAnnotations := config.Config.EpisodeGuide.Projects[project.Slug]
|
2021-10-28 01:35:53 +00:00
|
|
|
if hasAnnotations {
|
2021-11-10 04:11:39 +00:00
|
|
|
episodeGuideUrl = c.UrlContext.BuildEpisodeList(defaultTopic)
|
2021-10-28 01:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
baseData.Header.Project = &templates.ProjectHeader{
|
2021-11-11 19:00:46 +00:00
|
|
|
HasForums: project.HasForums(),
|
|
|
|
HasBlog: project.HasBlog(),
|
2021-10-28 01:35:53 +00:00
|
|
|
HasEpisodeGuide: hasAnnotations,
|
2021-12-04 14:55:45 +00:00
|
|
|
CanEdit: c.CurrentUserCanEditCurrentProject,
|
2021-11-10 04:11:39 +00:00
|
|
|
ForumsUrl: c.UrlContext.BuildForum(nil, 1),
|
|
|
|
BlogUrl: c.UrlContext.BuildBlog(1),
|
2021-10-28 01:35:53 +00:00
|
|
|
EpisodeGuideUrl: episodeGuideUrl,
|
2021-12-04 14:55:45 +00:00
|
|
|
EditUrl: c.UrlContext.BuildProjectEdit(""),
|
2021-10-28 01:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseData
|
|
|
|
}
|
|
|
|
|
2022-02-13 20:07:09 +00:00
|
|
|
func buildDefaultOpenGraphItems(project *models.Project, projectLogoUrl string, title string) []templates.OpenGraphItem {
|
2021-10-28 01:35:53 +00:00
|
|
|
if title == "" {
|
|
|
|
title = "Handmade Network"
|
|
|
|
}
|
|
|
|
|
|
|
|
image := hmnurl.BuildPublic("logo.png", false)
|
|
|
|
if !project.IsHMN() {
|
2022-02-13 20:07:09 +00:00
|
|
|
image = projectLogoUrl
|
2021-10-28 01:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []templates.OpenGraphItem{
|
|
|
|
{Property: "og:title", Value: title},
|
|
|
|
{Property: "og:site_name", Value: "Handmade Network"},
|
|
|
|
{Property: "og:type", Value: "website"},
|
|
|
|
{Property: "og:image", Value: image},
|
|
|
|
}
|
|
|
|
}
|