diff --git a/backend/db/db.go b/backend/db/db.go index 7a15a62..a488b76 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -7,12 +7,9 @@ import ( "net/url" "os" - "codeberg.org/u1f320/pronouns.cc/backend/log" "emperror.dev/errors" "github.com/Masterminds/squirrel" "github.com/jackc/pgconn" - "github.com/jackc/pgx/v4" - "github.com/jackc/pgx/v4/log/zapadapter" "github.com/jackc/pgx/v4/pgxpool" "github.com/mediocregopher/radix/v4" "github.com/minio/minio-go/v7" @@ -38,15 +35,7 @@ type DB struct { } func New() (*DB, error) { - pgxCfg, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL")) - if err != nil { - return nil, errors.Wrap(err, "parsing config") - } - pgxCfg.ConnConfig.LogLevel = pgx.LogLevelDebug - pgxCfg.ConnConfig.Logger = zapadapter.NewLogger(log.Logger) - - pool, err := pgxpool.ConnectConfig(context.Background(), pgxCfg) - // pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL")) + pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL")) if err != nil { return nil, errors.Wrap(err, "creating postgres client") } diff --git a/backend/db/field.go b/backend/db/field.go index 4e5296f..e73577b 100644 --- a/backend/db/field.go +++ b/backend/db/field.go @@ -52,7 +52,7 @@ func (f Field) Validate() string { // UserFields returns the fields associated with the given user ID. func (db *DB) UserFields(ctx context.Context, id xid.ID) (fs []Field, err error) { - sql, args, err := sq.Select("*").From("user_fields").Where("user_id = ?", id).OrderBy("id").ToSql() + sql, args, err := sq.Select("id", "name", "entries").From("user_fields").Where("user_id = ?", id).OrderBy("id").ToSql() if err != nil { return fs, errors.Wrap(err, "building sql") } @@ -88,7 +88,7 @@ func (db *DB) SetUserFields(ctx context.Context, tx pgx.Tx, userID xid.ID, field // MemberFields returns the fields associated with the given member ID. func (db *DB) MemberFields(ctx context.Context, id xid.ID) (fs []Field, err error) { - sql, args, err := sq.Select("*").From("member_fields").Where("member_id = ?", id).OrderBy("id").ToSql() + sql, args, err := sq.Select("id", "name", "entries").From("member_fields").Where("member_id = ?", id).OrderBy("id").ToSql() if err != nil { return fs, errors.Wrap(err, "building sql") } diff --git a/backend/routes/auth/discord.go b/backend/routes/auth/discord.go index d0a9b78..38214f0 100644 --- a/backend/routes/auth/discord.go +++ b/backend/routes/auth/discord.go @@ -61,7 +61,7 @@ func (s *Server) discordCallback(w http.ResponseWriter, r *http.Request) error { } cfg := discordOAuthConfig - cfg.RedirectURL = decoded.CallbackDomain + "/login/discord" + cfg.RedirectURL = decoded.CallbackDomain + "/auth/login/discord" token, err := cfg.Exchange(r.Context(), decoded.Code) if err != nil { log.Errorf("exchanging oauth code: %v", err) diff --git a/backend/routes/auth/routes.go b/backend/routes/auth/routes.go index fcd0194..8b781a2 100644 --- a/backend/routes/auth/routes.go +++ b/backend/routes/auth/routes.go @@ -105,10 +105,10 @@ func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error { // copy Discord config and set redirect url discordCfg := discordOAuthConfig - discordCfg.RedirectURL = req.CallbackDomain + "/login/discord" + discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord" render.JSON(w, r, oauthURLsResponse{ - Discord: discordCfg.AuthCodeURL(state), + Discord: discordCfg.AuthCodeURL(state) + "&prompt=none", }) return nil } diff --git a/backend/routes/user/get_user.go b/backend/routes/user/get_user.go index a8db049..ca3f699 100644 --- a/backend/routes/user/get_user.go +++ b/backend/routes/user/get_user.go @@ -35,7 +35,9 @@ type PartialMember struct { ID xid.ID `json:"id"` Name string `json:"name"` DisplayName *string `json:"display_name"` + Bio *string `json:"bio"` AvatarURLs []string `json:"avatar_urls"` + Links []string `json:"links"` Names []db.FieldEntry `json:"names"` Pronouns []db.PronounEntry `json:"pronouns"` } @@ -59,7 +61,9 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser ID: members[i].ID, Name: members[i].Name, DisplayName: members[i].DisplayName, + Bio: members[i].Bio, AvatarURLs: db.NotNull(members[i].AvatarURLs), + Links: db.NotNull(members[i].Links), Names: db.NotNull(members[i].Names), Pronouns: db.NotNull(members[i].Pronouns), } diff --git a/backend/routes/user/patch_user.go b/backend/routes/user/patch_user.go index 2550b2e..2ad0ac6 100644 --- a/backend/routes/user/patch_user.go +++ b/backend/routes/user/patch_user.go @@ -23,7 +23,6 @@ type PatchUserRequest struct { } // patchUser parses a PatchUserRequest and updates the user with the given ID. -// TODO: could this be refactored to be less repetitive? names, pronouns, and fields are all validated in the same way func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { ctx := r.Context() diff --git a/frontend/src/lib/api/entities.ts b/frontend/src/lib/api/entities.ts index 8e31b78..dc4c2b5 100644 --- a/frontend/src/lib/api/entities.ts +++ b/frontend/src/lib/api/entities.ts @@ -3,8 +3,8 @@ export interface User { name: string; display_name: string | null; bio: string | null; - avatar_urls: string[] | null; - links: string[] | null; + avatar_urls: string[]; + links: string[]; names: FieldEntry[]; pronouns: Pronoun[]; @@ -46,15 +46,16 @@ export interface PartialMember { id: string; name: string; display_name: string | null; - avatar_urls: string[] | null; + bio: string | null; + avatar_urls: string[]; + links: string[]; + names: FieldEntry[]; + pronouns: Pronoun[]; } export interface Member extends PartialMember { - bio: string | null; - links: string | null; - names: FieldEntry[]; - pronouns: Pronoun[]; fields: Field[]; + user: MemberPartialUser; } @@ -62,7 +63,7 @@ export interface MemberPartialUser { id: string; name: string; display_name: string | null; - avatar_urls: string[] | null; + avatar_urls: string[]; } export interface APIError { diff --git a/frontend/src/lib/components/FallbackImage.svelte b/frontend/src/lib/components/FallbackImage.svelte index 3370030..e5d3c52 100644 --- a/frontend/src/lib/components/FallbackImage.svelte +++ b/frontend/src/lib/components/FallbackImage.svelte @@ -1,6 +1,7 @@ -{#if urls} +{#if urls.length > 0} {#each urls as url} - + {/each} - + {:else} - + {/if} diff --git a/frontend/src/lib/components/FieldCard.svelte b/frontend/src/lib/components/FieldCard.svelte index 814c2d6..2dd270b 100644 --- a/frontend/src/lib/components/FieldCard.svelte +++ b/frontend/src/lib/components/FieldCard.svelte @@ -9,7 +9,7 @@
-
{field.name}
+

{field.name}

{/if} -
- {#if data.names} -
-

Names

- -
- {/if} - {#if data.pronouns} -
-

Pronouns

- -
- {/if} -
- {#if data.fields} -
- {#each data.fields as field} -
- -
- {/each} -
- {/if} +
+ {#if data.names} +
+

Names

+ +
+ {/if} + {#if data.pronouns} +
+

Pronouns

+ +
+ {/if} + {#each data.fields as field} +
+ +
+ {/each} +
{#if data.members}
@@ -90,7 +86,7 @@

Members

-
+
{#each data.members as member} {/each} diff --git a/frontend/src/routes/@[username]/[memberName]/+page.svelte b/frontend/src/routes/@[username]/[memberName]/+page.svelte index 505a00a..a3626d5 100644 --- a/frontend/src/routes/@[username]/[memberName]/+page.svelte +++ b/frontend/src/routes/@[username]/[memberName]/+page.svelte @@ -49,7 +49,7 @@
{/if}
-
+
{#if data.names}

Names

@@ -73,15 +73,11 @@
{/if} -
-
- {#if data.fields} -
{#each data.fields as field}
{/each}
- {/if} + diff --git a/frontend/src/routes/login/+page.server.ts b/frontend/src/routes/auth/login/+page.server.ts similarity index 100% rename from frontend/src/routes/login/+page.server.ts rename to frontend/src/routes/auth/login/+page.server.ts diff --git a/frontend/src/routes/login/+page.svelte b/frontend/src/routes/auth/login/+page.svelte similarity index 100% rename from frontend/src/routes/login/+page.svelte rename to frontend/src/routes/auth/login/+page.svelte diff --git a/frontend/src/routes/login/discord/+page.server.ts b/frontend/src/routes/auth/login/discord/+page.server.ts similarity index 62% rename from frontend/src/routes/login/discord/+page.server.ts rename to frontend/src/routes/auth/login/discord/+page.server.ts index 98e79c7..740922e 100644 --- a/frontend/src/routes/login/discord/+page.server.ts +++ b/frontend/src/routes/auth/login/discord/+page.server.ts @@ -1,16 +1,16 @@ -import type { MeUser } from "$lib/api/entities"; +import type { APIError, MeUser } from "$lib/api/entities"; import { apiFetch } from "$lib/api/fetch"; -import type { PageServerLoad } from "./$types"; +import type { PageServerLoad, Actions } from "./$types"; import { PUBLIC_BASE_URL } from "$env/static/public"; -export const load = (async (event) => { +export const load = (async ({ locals, url }) => { try { const resp = await apiFetch("/auth/discord/callback", { method: "POST", body: { callback_domain: PUBLIC_BASE_URL, - code: event.url.searchParams.get("code"), - state: event.url.searchParams.get("state"), + code: url.searchParams.get("code"), + state: url.searchParams.get("state"), }, }); @@ -18,7 +18,9 @@ export const load = (async (event) => { ...resp, }; } catch (e) { - return { error: e }; + console.log(e); + + return { error: e as APIError }; } }) satisfies PageServerLoad; diff --git a/frontend/src/routes/auth/login/discord/+page.svelte b/frontend/src/routes/auth/login/discord/+page.svelte new file mode 100644 index 0000000..a6d186f --- /dev/null +++ b/frontend/src/routes/auth/login/discord/+page.svelte @@ -0,0 +1,95 @@ + + + + Log in with Discord - pronouns.cc + + +

Log in with Discord

+ +{#if data.error} + +

An error occurred

+ {data.error.code}: + {data.error.message} +
+{/if} +{#if data.ticket} +
+
+ + +
+
+ + +
+ {#if data.require_invite} +
+ + +
+ You currently need an invite code to sign up. You can get one from an existing user. +
+
+ {/if} +
+ By signing up, you agree to the terms of service and the + privacy policy. +
+ +
+{:else} + Loading... +{/if} diff --git a/frontend/src/routes/auth/logout/+page.svelte b/frontend/src/routes/auth/logout/+page.svelte new file mode 100644 index 0000000..0c8326b --- /dev/null +++ b/frontend/src/routes/auth/logout/+page.svelte @@ -0,0 +1,16 @@ + + +

Log out

diff --git a/frontend/src/routes/login/discord/+page.svelte b/frontend/src/routes/login/discord/+page.svelte deleted file mode 100644 index c8b2af5..0000000 --- a/frontend/src/routes/login/discord/+page.svelte +++ /dev/null @@ -1,17 +0,0 @@ - - - - Log in with Discord - pronouns.cc - - -

Log in with Discord

- -{#if data.error} - -{:else} - -{/if} \ No newline at end of file diff --git a/frontend/src/routes/nav/Navigation.svelte b/frontend/src/routes/nav/Navigation.svelte index e0c9faf..e5eecb8 100644 --- a/frontend/src/routes/nav/Navigation.svelte +++ b/frontend/src/routes/nav/Navigation.svelte @@ -60,7 +60,7 @@ }; const toggleTheme = () => { - themeStore.set(theme === "dark" ? "light" : "dark") + themeStore.set(theme === "dark" ? "light" : "dark"); }; const toggleMenu = () => { showMenu = !showMenu; @@ -78,15 +78,21 @@