moves bio to settings

This commit is contained in:
Christian Beutel
2025-01-20 16:58:43 +01:00
parent f6a0281e6e
commit 9d8a8e2ebb
9 changed files with 252 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ import (
func init() { func init() {
m.Register(func(db dbx.Builder) error { m.Register(func(db dbx.Builder) error {
dao := daos.New(db); dao := daos.New(db)
collection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa") collection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa")
if err != nil { if err != nil {
@@ -48,7 +48,7 @@ func init() {
return dao.SaveCollection(collection) return dao.SaveCollection(collection)
}, func(db dbx.Builder) error { }, func(db dbx.Builder) error {
dao := daos.New(db); dao := daos.New(db)
collection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa") collection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa")
if err != nil { if err != nil {

View File

@@ -0,0 +1,185 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db)
// remove bio from users_anonymous to prevent ambiguity
uaCollection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa")
if err != nil {
return err
}
options := map[string]any{}
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, username, avatar, users.created, CAST(COALESCE(json_extract(privacy, '$.account') = 'private', false) as BOOL) as private FROM users LEFT JOIN settings ON settings.user = users.id"
}`), &options); err != nil {
return err
}
uaCollection.SetOptions(options)
err = dao.SaveCollection(uaCollection)
if err != nil {
return err
}
// add bio field to settings
collection, err := dao.FindCollectionByNameOrId("settings")
if err != nil {
return err
}
new_bio := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "pd2cq8sq",
"name": "bio",
"type": "text",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": 10000,
"pattern": ""
}
}`), new_bio); err != nil {
return err
}
collection.Schema.AddField(new_bio)
err = dao.SaveCollection(collection)
if err != nil {
return err
}
// migrate existing bios
users, err := dao.FindRecordsByFilter("_pb_users_auth_", "bio != null", "", -1, 0)
if err != nil {
return nil
}
for _, user := range users {
bio := user.GetString(("bio"))
userSettings, err := dao.FindRecordsByFilter("settings", "user = {:userId}", "", 1, 0, dbx.Params{"userId": user.Id})
if err != nil {
return err
}
userSettings[0].Set("bio", bio)
if err := dao.SaveRecord(userSettings[0]); err != nil {
return err
}
}
// remove bio from users
uCollection, err := dao.FindCollectionByNameOrId("_pb_users_auth_")
if err != nil {
return err
}
uCollection.Schema.RemoveField("pd2cq8sq")
err = dao.SaveCollection(uCollection)
if err != nil {
return err
}
// add bio back to users_anaonymous
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, username, avatar, bio, users.created, CAST(COALESCE(json_extract(privacy, '$.account') = 'private', false) as BOOL) as private FROM users LEFT JOIN settings ON settings.user = users.id"
}`), &options); err != nil {
return err
}
uaCollection.SetOptions(options)
err = dao.SaveCollection(uaCollection)
if err != nil {
return err
}
return nil
}, func(db dbx.Builder) error {
dao := daos.New(db)
uaCollection, err := dao.FindCollectionByNameOrId("xku110v5a5xbufa")
if err != nil {
return err
}
options := map[string]any{}
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, username, avatar, users.created, CAST(COALESCE(json_extract(privacy, '$.account') = 'private', false) as BOOL) as private FROM users LEFT JOIN settings ON settings.user = users.id"
}`), &options); err != nil {
return err
}
uaCollection.SetOptions(options)
err = dao.SaveCollection(uaCollection)
if err != nil {
return err
}
collection, err := dao.FindCollectionByNameOrId("settings")
if err != nil {
return err
}
// remove
collection.Schema.RemoveField("pd2cq8sq")
err = dao.SaveCollection(collection)
if err != nil {
return err
}
uCollection, err := dao.FindCollectionByNameOrId("_pb_users_auth_")
if err != nil {
return err
}
// add
new_bio := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "pd2cq8sq",
"name": "bio",
"type": "text",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": 10000,
"pattern": ""
}
}`), new_bio); err != nil {
return err
}
uCollection.Schema.AddField(new_bio)
err = dao.SaveCollection(uCollection)
if err != nil {
return err
}
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, username, avatar, bio, users.created, CAST(COALESCE(json_extract(privacy, '$.account') = 'private', false) as BOOL) as private FROM users LEFT JOIN settings ON settings.user = users.id"
}`), &options); err != nil {
return err
}
uaCollection.SetOptions(options)
return dao.SaveCollection(uaCollection)
})
}

View File

@@ -0,0 +1,55 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("uavt73rsqcn1n13")
if err != nil {
return err
}
// add
new_bio := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "pd2cq8sq",
"name": "bio",
"type": "text",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": 10000,
"pattern": ""
}
}`), new_bio); err != nil {
return err
}
collection.Schema.AddField(new_bio)
return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("uavt73rsqcn1n13")
if err != nil {
return err
}
// remove
collection.Schema.RemoveField("pd2cq8sq")
return dao.SaveCollection(collection)
})
}

View File

@@ -5,6 +5,7 @@ import type { Settings } from "../settings";
const SettingsCreateSchema = z.object({ const SettingsCreateSchema = z.object({
unit: z.enum(["metric", "imperial"]).optional(), unit: z.enum(["metric", "imperial"]).optional(),
language: z.enum(["en", "de", "es", "fr", "hu", "it", "nl", "pl", "pt", "zh"]).optional(), language: z.enum(["en", "de", "es", "fr", "hu", "it", "nl", "pl", "pt", "zh"]).optional(),
bio: z.string().optional(),
mapFocus: z.enum(["trails", "location"]).optional(), mapFocus: z.enum(["trails", "location"]).optional(),
location: z.object({ location: z.object({
name: z.string(), name: z.string(),

View File

@@ -18,7 +18,6 @@ const UserUpdateSchema = (z.object({
password: z.string().min(8, "must-be-at-least-n-characters-long").optional(), password: z.string().min(8, "must-be-at-least-n-characters-long").optional(),
oldPassword: z.string().min(8, "must-be-at-least-n-characters-long").optional(), oldPassword: z.string().min(8, "must-be-at-least-n-characters-long").optional(),
passwordConfirm: z.string().min(8, "must-be-at-least-n-characters-long").optional(), passwordConfirm: z.string().min(8, "must-be-at-least-n-characters-long").optional(),
bio: z.string().optional()
}) satisfies ZodType<Partial<User>>).refine((data) => data.password === data.passwordConfirm, { }) satisfies ZodType<Partial<User>>).refine((data) => data.password === data.passwordConfirm, {
message: "passwords-must-match", message: "passwords-must-match",
path: ["passwordConfirm"], path: ["passwordConfirm"],

View File

@@ -4,6 +4,7 @@ class Settings {
id?: string; id?: string;
unit?: "metric" | "imperial"; unit?: "metric" | "imperial";
language?: "en" | "de" | "es" | "fr" | "hu" | "it" | "nl" | "pl" | "pt" | "zh"; language?: "en" | "de" | "es" | "fr" | "hu" | "it" | "nl" | "pl" | "pt" | "zh";
bio?: string;
mapFocus?: "trails" | "location"; mapFocus?: "trails" | "location";
location?: { name: string, lat: number, lon: number }; location?: { name: string, lat: number, lon: number };
category?: string; category?: string;

View File

@@ -14,7 +14,6 @@ export type UserAnonymous = {
id: string, id: string,
username?: string, username?: string,
avatar?: string; avatar?: string;
bio?: string;
created?: string; created?: string;
private: boolean; private: boolean;
} }

View File

@@ -46,14 +46,14 @@
<h4 class="text-xl font-semibold"> <h4 class="text-xl font-semibold">
{$_("about")} {$_("about")}
{data.user.username} {data.user.username}
{#if data.isOwnProfile && data.user.bio?.length} {#if data.isOwnProfile && data.settings.bio?.length}
<a class="ml-4" href="/settings/profile" <a class="ml-4" href="/settings/profile"
><i class="fa fa-pen text-base"></i></a ><i class="fa fa-pen text-base"></i></a
> >
{/if} {/if}
</h4> </h4>
{#if data.user.bio?.length} {#if data.settings.bio?.length}
<p class="whitespace-pre-wrap text-sm">{data.user.bio}</p> <p class="whitespace-pre-wrap text-sm">{data.settings.bio}</p>
{:else if data.isOwnProfile} {:else if data.isOwnProfile}
<a class="btn-primary inline-block" href="/settings/profile" <a class="btn-primary inline-block" href="/settings/profile"
>+ {$_("add-bio")}</a >+ {$_("add-bio")}</a

View File

@@ -16,7 +16,7 @@
let selectedCategory = let selectedCategory =
$page.data.settings?.category || data.categories[0].id; $page.data.settings?.category || data.categories[0].id;
let bio = $currentUser?.bio ?? ""; let bio = data.settings?.bio ?? "";
const categoryItems: SelectItem[] = data.categories.map((c: Category) => ({ const categoryItems: SelectItem[] = data.categories.map((c: Category) => ({
text: $_(c.name), text: $_(c.name),
@@ -43,12 +43,12 @@
} }
async function handleBioSave() { async function handleBioSave() {
if (!$currentUser) { if (!data.settings) {
return; return;
} }
try { try {
$currentUser.bio = bio; data.settings.bio = bio;
await users_update($currentUser); await settings_update(data.settings);
} catch (e) { } catch (e) {
show_toast({ show_toast({
type: "error", type: "error",
@@ -111,7 +111,7 @@
<Button <Button
on:click={() => handleBioSave()} on:click={() => handleBioSave()}
primary primary
disabled={$currentUser.bio === bio}>{$_("save")}</Button disabled={data.settings.bio === bio}>{$_("save")}</Button
> >
</div> </div>
</div> </div>