-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to start recording anonymous users so we can store relevant data such as a fake name, fake avatar, and when their chatroom was last viewed by an admin. We generate the relevant migrations and add the fields we need for an `anonymous_user`. Since the frontend generates a UUID for every anonymous user, the UUID would be perfect as `id` for our AnonymousUser records. We use the `uuid` type for the `id` column of our AnonymousUser and set it to not autogenerate an id. That way, we use the UUID passed to us from the frontend as `id` every time we create a new AnonymousUser record. Now that we have an AnonymousUser, we can associate it with Message so we can easily get all the messages sent by a user. Note that there are a few extra steps for this because we are using a `:uuid` type as `id` instead of the default `:integer`.
- Loading branch information
Showing
6 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
priv/repo/migrations/20160915111446_create_anonymous_users.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule PhoenixChat.Repo.Migrations.CreateAnonymousUsers do | ||
use Ecto.Migration | ||
|
||
def change do | ||
# We want to use a `uuid` as primary key so we need to set `primary_key: false`. | ||
create table(:anonymous_users, primary_key: false) do | ||
# We add the `:id` column manually with a type of `uuid` and set | ||
# it as `primary_key`. | ||
add :id, :uuid, primary_key: true | ||
add :name, :string | ||
add :avatar, :string | ||
add :public_key, :string | ||
add :last_viewed_by_admin_at, :datetime | ||
|
||
timestamps | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
priv/repo/migrations/20160915111609_message_belongs_to_anonymous_user.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule PhoenixChat.Repo.Migrations.MessageBelongsToAnonymousUser do | ||
use Ecto.Migration | ||
|
||
def up do | ||
alter table(:messages) do | ||
# We need to set `type` as `uuid` so it does not default to `integer`. | ||
add :anonymous_user_id, references(:anonymous_users, on_delete: :nilify_all, type: :uuid) | ||
remove :from | ||
end | ||
end | ||
|
||
def down do | ||
alter table(:messages) do | ||
remove :anonymous_user_id | ||
add :from, :string | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule PhoenixChat.AnonymousUser do | ||
use PhoenixChat.Web, :model | ||
|
||
alias PhoenixChat.Message | ||
|
||
# Since we provide the `id` for our AnonymousUser record, we will need to set | ||
# the primary key to not autogenerate it. | ||
@primary_key {:id, :binary_id, autogenerate: false} | ||
# We need to set `@foreign_key_type` below since it defaults to `:integer`. | ||
# We are using a UUID as `id` so we need to set type as `:binary_id`. | ||
@foreign_key_type :binary_id | ||
|
||
schema "anonymous_users" do | ||
field :name | ||
field :avatar | ||
field :public_key | ||
field :last_viewed_by_admin_at, PhoenixChat.DateTime | ||
has_many :messages, Message | ||
|
||
timestamps | ||
end | ||
|
||
def changeset(model, params \\ :empty) do | ||
model | ||
|> cast(params, ~w(public_key id), ~w()) | ||
|> put_avatar | ||
|> put_name | ||
end | ||
|
||
def last_viewed_changeset(model) do | ||
params = %{last_viewed_by_admin_at: System.system_time(:milliseconds)} | ||
model | ||
|> cast(params, ~w(last_viewed_by_admin_at), []) | ||
end | ||
|
||
@doc """ | ||
This query returns all users and the respective last messages they | ||
have sent. | ||
Once the query is run, the return value is a tuple of two elements: | ||
`{user, message}` | ||
""" | ||
def by_public_key(public_key, limit \\ 20) do | ||
from u in __MODULE__, | ||
join: m in Message, on: m.anonymous_user_id == u.id, | ||
where: u.public_key == ^public_key, | ||
limit: ^limit, | ||
distinct: u.id, | ||
order_by: [desc: m.inserted_at], | ||
select: {u, m} | ||
end | ||
|
||
# Set a fake name for our anonymous user every time we create one | ||
defp put_name(changeset) do | ||
name = (Faker.Color.fancy_name <> " " <> Faker.Company.buzzword()) |> String.downcase | ||
changeset | ||
|> put_change(:name, name) | ||
end | ||
|
||
# Set a fake avatar for our anonymous user every time we create one | ||
defp put_avatar(changeset) do | ||
changeset | ||
|> put_change(:avatar, Faker.Avatar.image_url(25, 25)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters