From 6bc29f93da8c14f715996887acddc36f9adcae6c Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Mon, 20 Nov 2023 18:25:52 +0100 Subject: [PATCH] fix: better room name generation See #173 --- router/router.go | 2 ++ ui/package.json | 1 - ui/src/RoomManage.tsx | 3 +-- ui/src/message.ts | 1 + ui/src/name.ts | 8 -------- ui/src/useConfig.ts | 1 + ui/yarn.lock | 5 ----- util/sillyname.go | 34 +++++++++++++++++++++++++--------- ws/rooms.go | 6 +++++- 9 files changed, 35 insertions(+), 26 deletions(-) delete mode 100644 ui/src/name.ts diff --git a/router/router.go b/router/router.go index 5b0d8758..2cbd54d3 100644 --- a/router/router.go +++ b/router/router.go @@ -21,6 +21,7 @@ type UIConfig struct { User string `json:"user"` LoggedIn bool `json:"loggedIn"` Version string `json:"version"` + RoomName string `json:"roomName"` CloseRoomWhenOwnerLeaves bool `json:"closeRoomWhenOwnerLeaves"` } @@ -42,6 +43,7 @@ func Router(conf config.Config, rooms *ws.Rooms, users *auth.Users, version stri LoggedIn: loggedIn, User: user, Version: version, + RoomName: rooms.RandRoomName(), CloseRoomWhenOwnerLeaves: conf.CloseRoomWhenOwnerLeaves, }) }) diff --git a/ui/package.json b/ui/package.json index d93be6e1..725dc849 100644 --- a/ui/package.json +++ b/ui/package.json @@ -18,7 +18,6 @@ "react-dom": "^18.2.0", "react-hotkeys-hook": "^4.4.1", "typescript": "~5.2.2", - "unique-names-generator": "^4.7.1", "use-http": "^1.0.28", "vite": "^4.4.9", "vite-plugin-svgr": "^4.0.0", diff --git a/ui/src/RoomManage.tsx b/ui/src/RoomManage.tsx index 765ef70e..d47388ba 100644 --- a/ui/src/RoomManage.tsx +++ b/ui/src/RoomManage.tsx @@ -13,13 +13,12 @@ import { } from '@mui/material'; import {FCreateRoom, UseRoom} from './useRoom'; import {UIConfig} from './message'; -import {randomRoomName} from './name'; import {getRoomFromURL} from './useRoomID'; import {authModeToRoomMode, UseConfig} from './useConfig'; import {LoginForm} from './LoginForm'; const CreateRoom = ({room, config}: Pick & {config: UIConfig}) => { - const [id, setId] = React.useState(() => getRoomFromURL() ?? randomRoomName()); + const [id, setId] = React.useState(() => getRoomFromURL() ?? config.roomName); const mode = authModeToRoomMode(config.authMode, config.loggedIn); const [ownerLeave, setOwnerLeave] = React.useState(config.closeRoomWhenOwnerLeaves); const submit = () => diff --git a/ui/src/message.ts b/ui/src/message.ts index e12b30dd..c15f1c7b 100644 --- a/ui/src/message.ts +++ b/ui/src/message.ts @@ -10,6 +10,7 @@ export interface UIConfig { user: string; loggedIn: boolean; version: string; + roomName: string; closeRoomWhenOwnerLeaves: boolean; } diff --git a/ui/src/name.ts b/ui/src/name.ts deleted file mode 100644 index 682fcf31..00000000 --- a/ui/src/name.ts +++ /dev/null @@ -1,8 +0,0 @@ -import * as gen from 'unique-names-generator'; - -const roomConfig: gen.Config = { - dictionaries: [gen.adjectives, gen.colors, gen.animals], - length: 3, - separator: '-', -}; -export const randomRoomName = () => gen.uniqueNamesGenerator(roomConfig); diff --git a/ui/src/useConfig.ts b/ui/src/useConfig.ts index e015a7ec..4a028f8c 100644 --- a/ui/src/useConfig.ts +++ b/ui/src/useConfig.ts @@ -18,6 +18,7 @@ export const useConfig = (): UseConfig => { loggedIn: false, loading: true, version: 'unknown', + roomName: 'unknown', closeRoomWhenOwnerLeaves: true, }); diff --git a/ui/yarn.lock b/ui/yarn.lock index b1fd8232..7a5b5db3 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -1456,11 +1456,6 @@ typescript@~5.2.2: resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78" integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w== -unique-names-generator@^4.7.1: - version "4.7.1" - resolved "https://registry.yarnpkg.com/unique-names-generator/-/unique-names-generator-4.7.1.tgz#966407b12ba97f618928f77322cfac8c80df5597" - integrity sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow== - update-browserslist-db@^1.0.11: version "1.0.13" resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz#3c5e4f5c083661bd38ef64b6328c26ed6c8248c4" diff --git a/util/sillyname.go b/util/sillyname.go index 6790f84c..a219746a 100644 --- a/util/sillyname.go +++ b/util/sillyname.go @@ -20,19 +20,35 @@ var adjectives = []string{ } var animals = []string{ - "Dog", "Puppy", "Turtle", "Rabbit", "Parrot", "Cat", "Kitten", "Goldfish", - "Mouse", "Hamster", "Fish", "Cow", "Rabbit", "Duck", "Shrimp", "Pig", - "Goat", "Crab", "Deer", "Bee", "Sheep", "Fish", "Turkey", "Dove", - "Chicken", "Horse", "Squirrel", "Dog", "Chimpanzee", "Ox", "Lion", "Panda", - "Walrus", "Otter", "Mouse", "Kangaroo", "Goat", "Horse", "Monkey", "Cow", - "Koala", "Mole", "Elephant", "Leopard", "Hippopotamus", "Giraffe", "Fox", - "Coyote", "Hedgehong", "Sheep", "Deer", + "dog", "puppy", "turtle", "rabbit", "parrot", "cat", "kitten", "goldfish", + "mouse", "hamster", "fish", "cow", "rabbit", "duck", "shrimp", "pig", + "goat", "crab", "deer", "bee", "sheep", "fish", "turkey", "dove", + "chicken", "horse", "squirrel", "dog", "chimpanzee", "ox", "lion", "panda", + "walrus", "otter", "mouse", "kangaroo", "goat", "horse", "monkey", "cow", + "koala", "mole", "elephant", "leopard", "hippopotamus", "giraffe", "fox", + "coyote", "hedgehong", "sheep", "deer", +} + +var colors = []string{ + "amaranth", "amber", "amethyst", "apricot", "aqua", "aquamarine", "azure", + "beige", "black", "blue", "blush", "bronze", "brown", "chocolate", + "coffee", "copper", "coral", "crimson", "cyan", "emerald", "fuchsia", + "gold", "gray", "green", "harlequin", "indigo", "ivory", "jade", + "lavender", "lime", "magenta", "maroon", "moccasin", "olive", "orange", + "peach", "pink", "plum", "purple", "red", "rose", "salmon", "sapphire", + "scarlet", "silver", "tan", "teal", "tomato", "turquoise", "violet", + "white", "yellow", } func r(r *rand.Rand, l []string) string { return l[r.Intn(len(l)-1)] } -func NewName(s *rand.Rand) string { - return cases.Title(language.English).String(r(s, adjectives) + " " + r(s, animals)) +func NewUserName(s *rand.Rand) string { + title := cases.Title(language.English) + return title.String(r(s, adjectives)) + " " + title.String(r(s, animals)) +} + +func NewRoomName(s *rand.Rand) string { + return r(s, adjectives) + "-" + r(s, colors) + "-" + r(s, animals) } diff --git a/ws/rooms.go b/ws/rooms.go index 1dec306e..20aee1d7 100644 --- a/ws/rooms.go +++ b/ws/rooms.go @@ -52,7 +52,11 @@ type Rooms struct { } func (r *Rooms) RandUserName() string { - return util.NewName(r.r) + return util.NewUserName(r.r) +} + +func (r *Rooms) RandRoomName() string { + return util.NewRoomName(r.r) } func (r *Rooms) Upgrade(w http.ResponseWriter, req *http.Request) {