feat: add timezone settings

This commit is contained in:
sam 2023-08-02 23:24:38 +02:00
parent e10db2fa09
commit 3e3ccd971b
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
6 changed files with 85 additions and 4 deletions

View file

@ -555,9 +555,10 @@ func (db *DB) UpdateUser(
memberTitle *string, listPrivate *bool, memberTitle *string, listPrivate *bool,
links *[]string, links *[]string,
avatar *string, avatar *string,
timezone *string,
customPreferences *CustomPreferences, customPreferences *CustomPreferences,
) (u User, err error) { ) (u User, err error) {
if displayName == nil && bio == nil && links == nil && avatar == nil && memberTitle == nil && listPrivate == nil && customPreferences == nil { if displayName == nil && bio == nil && links == nil && avatar == nil && memberTitle == nil && listPrivate == nil && timezone == nil && customPreferences == nil {
sql, args, err := sq.Select("*").From("users").Where("id = ?", id).ToSql() sql, args, err := sq.Select("*").From("users").Where("id = ?", id).ToSql()
if err != nil { if err != nil {
return u, errors.Wrap(err, "building sql") return u, errors.Wrap(err, "building sql")
@ -593,6 +594,13 @@ func (db *DB) UpdateUser(
builder = builder.Set("member_title", *memberTitle) builder = builder.Set("member_title", *memberTitle)
} }
} }
if timezone != nil {
if *timezone == "" {
builder = builder.Set("timezone", nil)
} else {
builder = builder.Set("timezone", *timezone)
}
}
if links != nil { if links != nil {
builder = builder.Set("links", *links) builder = builder.Set("links", *links)
} }

View file

@ -25,6 +25,7 @@ type PatchUserRequest struct {
Pronouns *[]db.PronounEntry `json:"pronouns"` Pronouns *[]db.PronounEntry `json:"pronouns"`
Fields *[]db.Field `json:"fields"` Fields *[]db.Field `json:"fields"`
Avatar *string `json:"avatar"` Avatar *string `json:"avatar"`
Timezone *string `json:"timezone"`
ListPrivate *bool `json:"list_private"` ListPrivate *bool `json:"list_private"`
CustomPreferences *db.CustomPreferences `json:"custom_preferences"` CustomPreferences *db.CustomPreferences `json:"custom_preferences"`
Flags *[]xid.ID `json:"flags"` Flags *[]xid.ID `json:"flags"`
@ -91,6 +92,19 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
} }
} }
// validate timezone
if req.Timezone != nil {
if *req.Timezone != "" {
_, err := time.LoadLocation(*req.Timezone)
if err != nil {
return server.APIError{
Code: server.ErrBadRequest,
Details: fmt.Sprintf("%q is not a valid timezone", *req.Timezone),
}
}
}
}
// validate links // validate links
if req.Links != nil { if req.Links != nil {
if len(*req.Links) > db.MaxUserLinksLength { if len(*req.Links) > db.MaxUserLinksLength {
@ -224,7 +238,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
} }
} }
u, err = s.DB.UpdateUser(ctx, tx, claims.UserID, req.DisplayName, req.Bio, req.MemberTitle, req.ListPrivate, req.Links, avatarHash, req.CustomPreferences) u, err = s.DB.UpdateUser(ctx, tx, claims.UserID, req.DisplayName, req.Bio, req.MemberTitle, req.ListPrivate, req.Links, avatarHash, req.Timezone, req.CustomPreferences)
if err != nil && errors.Cause(err) != db.ErrNothingToUpdate { if err != nil && errors.Cause(err) != db.ErrNothingToUpdate {
log.Errorf("updating user: %v", err) log.Errorf("updating user: %v", err)
return err return err

View file

@ -57,6 +57,7 @@ export interface MeUser extends User {
fediverse_instance: string | null; fediverse_instance: string | null;
list_private: boolean; list_private: boolean;
last_sid_reroll: string; last_sid_reroll: string;
timezone: string | null;
} }
export interface Field { export interface Field {

View file

@ -21,14 +21,16 @@
CardBody, CardBody,
CardHeader, CardHeader,
FormGroup, FormGroup,
InputGroup,
Icon, Icon,
Input, Input,
Popover, Popover,
TabContent, TabContent,
TabPane, TabPane,
InputGroupText,
} from "sveltestrap"; } from "sveltestrap";
import { encode } from "base64-arraybuffer"; import { encode } from "base64-arraybuffer";
import { DateTime } from "luxon"; import { DateTime, FixedOffsetZone } from "luxon";
import { apiFetchClient } from "$lib/api/fetch"; import { apiFetchClient } from "$lib/api/fetch";
import { PUBLIC_SHORT_BASE } from "$env/static/public"; import { PUBLIC_SHORT_BASE } from "$env/static/public";
import IconButton from "$lib/components/IconButton.svelte"; import IconButton from "$lib/components/IconButton.svelte";
@ -60,6 +62,7 @@
let flags: PrideFlag[] = window.structuredClone(data.user.flags); let flags: PrideFlag[] = window.structuredClone(data.user.flags);
let list_private = data.user.list_private; let list_private = data.user.list_private;
let custom_preferences = window.structuredClone(data.user.custom_preferences); let custom_preferences = window.structuredClone(data.user.custom_preferences);
let timezone = data.user.timezone;
let avatar: string | null; let avatar: string | null;
let avatar_files: FileList | null; let avatar_files: FileList | null;
@ -98,6 +101,7 @@
member_title, member_title,
list_private, list_private,
custom_preferences, custom_preferences,
timezone,
); );
$: getAvatar(avatar_files).then((b64) => (avatar = b64)); $: getAvatar(avatar_files).then((b64) => (avatar = b64));
@ -114,6 +118,7 @@
member_title: string, member_title: string,
list_private: boolean, list_private: boolean,
custom_preferences: CustomPreferences, custom_preferences: CustomPreferences,
timezone: string | null,
) => { ) => {
if (bio !== (user.bio || "")) return true; if (bio !== (user.bio || "")) return true;
if (display_name !== (user.display_name || "")) return true; if (display_name !== (user.display_name || "")) return true;
@ -126,6 +131,7 @@
if (!customPreferencesEqual(custom_preferences, user.custom_preferences)) return true; if (!customPreferencesEqual(custom_preferences, user.custom_preferences)) return true;
if (avatar !== null) return true; if (avatar !== null) return true;
if (list_private !== user.list_private) return true; if (list_private !== user.list_private) return true;
if (timezone !== user.timezone) return true;
return false; return false;
}; };
@ -208,6 +214,24 @@
return uri; return uri;
}; };
let currentTime = "";
let displayTimezone = "";
$: setTime(timezone);
const setTime = (timezone: string | null) => {
if (!timezone) {
currentTime = "";
displayTimezone = "";
return;
}
const offset = DateTime.now().setZone(timezone).offset;
const zone = FixedOffsetZone.instance(offset);
currentTime = now.setZone(zone).toLocaleString(DateTime.TIME_SIMPLE);
displayTimezone = zone.formatOffset(now.toUnixInteger(), "narrow");
};
const moveName = (index: number, up: boolean) => { const moveName = (index: number, up: boolean) => {
if (up && index == 0) return; if (up && index == 0) return;
if (!up && index == names.length - 1) return; if (!up && index == names.length - 1) return;
@ -361,6 +385,7 @@
fields, fields,
member_title, member_title,
list_private, list_private,
timezone: timezone || "",
custom_preferences, custom_preferences,
flags: flags.map((flag) => flag.id), flags: flags.map((flag) => flag.id),
}); });
@ -403,6 +428,10 @@
addToast({ body: "Copied the short link to your clipboard!", duration: 2000 }); addToast({ body: "Copied the short link to your clipboard!", duration: 2000 });
}; };
const detectTimezone = () => {
timezone = DateTime.local().zoneName;
};
interface SnapshotData { interface SnapshotData {
bio: string; bio: string;
display_name: string; display_name: string;
@ -413,6 +442,7 @@
fields: Field[]; fields: Field[];
flags: PrideFlag[]; flags: PrideFlag[];
list_private: boolean; list_private: boolean;
timezone: string | null;
custom_preferences: CustomPreferences; custom_preferences: CustomPreferences;
avatar: string | null; avatar: string | null;
@ -432,6 +462,7 @@
fields, fields,
flags, flags,
list_private, list_private,
timezone,
custom_preferences, custom_preferences,
avatar, avatar,
newName, newName,
@ -448,6 +479,7 @@
fields = value.fields; fields = value.fields;
flags = value.flags; flags = value.flags;
list_private = value.list_private; list_private = value.list_private;
timezone = value.timezone;
custom_preferences = value.custom_preferences; custom_preferences = value.custom_preferences;
avatar = value.avatar; avatar = value.avatar;
newName = value.newName; newName = value.newName;
@ -787,6 +819,31 @@
<code class="text-nowrap">pronouns.cc/@{data.user.name}/[member-name]</code>. <code class="text-nowrap">pronouns.cc/@{data.user.name}/[member-name]</code>.
</strong> </strong>
</p> </p>
<hr />
<div class="m-1">
<p class="mt-1 my-2">
You can optionally set your timezone, which will show your current local time on your
profile.
</p>
<InputGroup>
<InputGroupText><Icon name="clock" aria-hidden /></InputGroupText>
<Input disabled value={timezone !== null ? timezone : "Unset"} />
<Button on:click={detectTimezone}>Detect</Button>
<Button on:click={() => (timezone = null)}>Reset</Button>
</InputGroup>
<p class="mt-2">
{#if timezone}
This will show up on your profile like this:
<Icon name="clock" aria-hidden />
{currentTime} <span class="text-body-secondary">(UTC{displayTimezone})</span>
<br />
{/if}
<span class="text-muted">
Your timezone is never shared directly, only the difference between UTC and your
current timezone is.
</span>
</p>
</div>
</div> </div>
</div> </div>
<div> <div>

View file

@ -5,6 +5,7 @@ import { plugin as markdown, Mode as MarkdownMode } from "vite-plugin-markdown";
export default defineConfig({ export default defineConfig({
plugins: [sveltekit(), markdown({ mode: [MarkdownMode.HTML] })], plugins: [sveltekit(), markdown({ mode: [MarkdownMode.HTML] })],
server: { server: {
host: "127.0.0.1",
proxy: { proxy: {
"/api": { "/api": {
target: "http://localhost:8080", target: "http://localhost:8080",

View file

@ -48,7 +48,7 @@ func run(c *cli.Context) error {
return err return err
} }
_, err = pg.UpdateUser(ctx, tx, u.ID, ptr("testing"), ptr("This is a bio!"), nil, ptr(false), &[]string{"https://pronouns.cc"}, nil, nil) _, err = pg.UpdateUser(ctx, tx, u.ID, ptr("testing"), ptr("This is a bio!"), nil, ptr(false), &[]string{"https://pronouns.cc"}, nil, nil, nil)
if err != nil { if err != nil {
fmt.Println("error setting user info:", err) fmt.Println("error setting user info:", err)
return err return err