slax/priv/repo/seeds.exs
Joao P Dubas b7de53bab0
feat(chat): validate username on registration
Also, update seeds to register with usernames.
2025-02-15 22:15:21 +00:00

112 lines
4.1 KiB
Elixir

# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Slax.Repo.insert!(%Slax.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias Slax.Accounts
alias Slax.Chat.Message
alias Slax.Chat.Room
alias Slax.Chat.RoomMembership
alias Slax.Repo
names = [
"Aragorn",
"Boromir",
"Elrond",
"Frodo",
"Gandalf",
"Gimli",
"Legolas"
]
pw = "TheFellowship"
for name <- names do
email = "#{String.downcase(name)}@fellowship.me"
Accounts.register_user(%{username: name, email: email, password: pw, password_confirmation: pw})
end
aragorn = Accounts.get_user_by_email("aragorn@fellowship.me")
boromir = Accounts.get_user_by_email("boromir@fellowship.me")
elrond = Accounts.get_user_by_email("elrond@fellowship.me")
frodo = Accounts.get_user_by_email("frodo@fellowship.me")
gandalf = Accounts.get_user_by_email("gandalf@fellowship.me")
gimli = Accounts.get_user_by_email("gimli@fellowship.me")
legolas = Accounts.get_user_by_email("legolas@fellowship.me")
rooms_and_messages = [
# Council of Elrond Room
%{
room: %{name: "council-of-elrond", topic: "What to do with this ring?"},
messages: [
{elrond,
"Strangers from distant lands, friends of old. You have beehn summoned here to answer the threat of Mordor. Middle-Earth stands upon the brink of destruction. None can escape it. You will unite or you will fall. Each race is bound to this fate - this one doom."},
{elrond, "Bring forth the Ring, Frodo."},
{boromir, "So it is true..."},
{boromir,
"It is a gift. A gift to the foes of Mordor. Why not use this Ring? Long has my father, the Stward fo Gondor, kept the forces of Mordor at bay. By the blood of our people are your lands kept safe! Give Gondor the weapon of the Enemy. Let us use it against him!"},
{aragorn,
"You cannot yield! None of us can. The One Ring answers to Sauron alone. It has no other master."},
{boromir, "And what would a ranger know of this matter?"}
]
},
# The Shire Room
%{
room: %{name: "the-shire", topic: "A peaceful place for hobbits"},
messages: [
{frodo, "What news of the outside world? Tell me everything!"},
{gandalf,
"What can I tell you? Life in the wide world goes on much as it has this past Age..."},
{gandalf,
"Full of its own comings and goings, scarcely aware of the existence of Hobbits..."},
{frodo, "You're late!"},
{gandalf,
"A wizard is never late, Frodo Baggins. Nor is he early. He arrives precisely when he means to."}
]
},
# Moria Room
%{
room: %{name: "mines-of-moria", topic: "Speak friend and enter"},
messages: [
{gandalf, "The walls of Moria... Dwarf doors are invisible when closed."},
{gimli, "Their own masters cannot find them if their secrets are forgotten!"},
{gandalf, "Let's see... Ithildin. It mirrors only starlight and moonlight."},
{frodo, "What does the writing say?"},
{gandalf, "It reads 'The Doors of Durin, Lord of Moria. Speak, friend, and enter.'"},
{legolas, "I do not feel comfortable in here. This is no mine, it's a tomb!"}
]
},
# Helm's Deep Room
%{
room: %{name: "helms-deep", topic: "The fortress of Rohan"},
messages: [
{aragorn, "By nightfall these hills will be swarming with Orcs!"},
{legolas, "This is no rabble of mindless Orcs. These are Uruk-hai."},
{gimli, "Whatever luck you live by, let's hope it lasts the night."},
{legolas, "Your friends are with you, Aragorn."},
{gimli, "Let them come! There is one Dwarf yet in Moria who still draws breath!"}
]
}
]
for %{room: room, messages: messages} <- rooms_and_messages do
room = Room |> struct(room) |> Repo.insert!()
users =
for {user, message} <- messages do
Repo.insert!(%Message{user: user, room: room, body: message})
user
end
for user <- MapSet.new(users) do
Repo.insert!(%RoomMembership{user: user, room: room})
end
end