diff --git a/lib/teiserver/microblog/schemas/post.ex b/lib/teiserver/microblog/schemas/post.ex index b9e353c05..e3b1447e2 100644 --- a/lib/teiserver/microblog/schemas/post.ex +++ b/lib/teiserver/microblog/schemas/post.ex @@ -9,6 +9,7 @@ defmodule Teiserver.Microblog.Post do field :summary, :string field :contents, :string field :view_count, :integer, default: 0 + field :poster_alias, :string belongs_to :discord_channel, Teiserver.Communication.DiscordChannel field :discord_post_id, :integer @@ -39,7 +40,7 @@ defmodule Teiserver.Microblog.Post do struct |> cast( params, - ~w(poster_id title summary contents view_count discord_channel_id discord_post_id)a + ~w(poster_id title summary contents view_count discord_channel_id discord_post_id poster_alias)a ) |> cast_assoc(:post_tags, tag_ids) |> validate_required(~w(poster_id title contents)a) diff --git a/lib/teiserver_web/live/microblog/admin/post/post_form_component.ex b/lib/teiserver_web/live/microblog/admin/post/post_form_component.ex index 18c30772f..e87c2cb7a 100644 --- a/lib/teiserver_web/live/microblog/admin/post/post_form_component.ex +++ b/lib/teiserver_web/live/microblog/admin/post/post_form_component.ex @@ -29,6 +29,23 @@ defmodule TeiserverWeb.Microblog.PostFormComponent do
<.input field={@form[:title]} type="text" autofocus="autofocus" phx-debounce="100" /> +
+ + + + + Edit this if your contributor name is different from your in-game name. Your user id will still be linked to this blog post in the database. + + + + <.input + id="poster_alias" + field={@form[:poster_alias]} + type="text" + phx-debounce="100" + placeholder={@current_user.name} + /> +
@@ -99,7 +116,7 @@ defmodule TeiserverWeb.Microblog.PostFormComponent do $discord in a public room and the server will send you a one time code to send the bridge. - You can still post microblog messages to discord but it will not include your name/profile in them. + You can still post microblog messages to discord but it will not include your name/profile in them. It will use your contributor alias if provided.
<% end %> @@ -330,14 +347,19 @@ defmodule TeiserverWeb.Microblog.PostFormComponent do defp create_discord_text(post) do user = Account.get_user_by_id(post.poster_id) + host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host] + create_discord_text(user, post, host) + end + + def create_discord_text(user, post, host) do discord_tag = if user.discord_id do " - Posted by <@#{user.discord_id}>" else - " - Posted by #{user.name}" + username = post.poster_alias || user.name + " - Posted by #{username}" end - host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host] url = "https://#{host}/microblog/show/#{post.id}" [ diff --git a/lib/teiserver_web/live/microblog/blog/index.html.heex b/lib/teiserver_web/live/microblog/blog/index.html.heex index 6bb1589fc..5dd7674c6 100644 --- a/lib/teiserver_web/live/microblog/blog/index.html.heex +++ b/lib/teiserver_web/live/microblog/blog/index.html.heex @@ -54,7 +54,7 @@ <%= post.title %>      - <%= post.poster.name %> + <%= post.poster_alias || post.poster.name %> Posted at <%= date_to_str(post.inserted_at, format: :ymd_hms) %> diff --git a/priv/repo/migrations/20241229013049_add_microblog_poster_alias.exs b/priv/repo/migrations/20241229013049_add_microblog_poster_alias.exs new file mode 100644 index 000000000..908c51014 --- /dev/null +++ b/priv/repo/migrations/20241229013049_add_microblog_poster_alias.exs @@ -0,0 +1,9 @@ +defmodule Teiserver.Repo.Migrations.AddMicroblogPosterAlias do + use Ecto.Migration + + def change do + alter table("microblog_posts") do + add :poster_alias, :string + end + end +end diff --git a/test/teiserver_web/live/microblog/post_form_component_test.exs b/test/teiserver_web/live/microblog/post_form_component_test.exs new file mode 100644 index 000000000..399a11113 --- /dev/null +++ b/test/teiserver_web/live/microblog/post_form_component_test.exs @@ -0,0 +1,105 @@ +defmodule TeiserverWeb.Microblog.PostFormComponentTest do + @moduledoc false + use TeiserverWeb.ConnCase + + alias TeiserverWeb.Microblog.PostFormComponent + alias Teiserver.Microblog + + test "create_post works with or without poster_alias" do + post_params = %{ + "contents" => "test", + "discord_channel_id" => "", + "poster_alias" => "", + "poster_id" => 4, + "summary" => "test", + "title" => "test" + } + + {:ok, result} = Microblog.create_post(post_params) + assert result.poster_alias == nil + + post_params = %{ + "contents" => "test", + "discord_channel_id" => "", + "poster_alias" => "Jenny", + "poster_id" => 4, + "summary" => "test", + "title" => "test" + } + + {:ok, result} = Microblog.create_post(post_params) + assert result.poster_alias == "Jenny" + end + + test "Get discord text when discord id present" do + post_params = %{ + "contents" => "test", + "discord_channel_id" => "", + "poster_alias" => "contributor alias", + "poster_id" => 4, + "summary" => "test", + "title" => "test" + } + + {:ok, post} = Microblog.create_post(post_params) + + user = %{ + discord_id: "mydiscordname", + name: "ign" + } + + host = "localhost" + + result = PostFormComponent.create_discord_text(user, post, host) + + assert String.contains?(result, "Posted by <@mydiscordname>") + end + + test "Get discord text when discord id not present and alias is present" do + post_params = %{ + "contents" => "test", + "discord_channel_id" => "", + "poster_alias" => "contributoralias", + "poster_id" => 4, + "summary" => "test", + "title" => "test" + } + + {:ok, post} = Microblog.create_post(post_params) + + user = %{ + discord_id: nil, + name: "ign" + } + + host = "localhost" + + result = PostFormComponent.create_discord_text(user, post, host) + + assert String.contains?(result, "Posted by contributoralias") + end + + test "Get discord text when discord id not present and alias not present" do + post_params = %{ + "contents" => "test", + "discord_channel_id" => "", + "poster_alias" => "", + "poster_id" => 4, + "summary" => "test", + "title" => "test" + } + + {:ok, post} = Microblog.create_post(post_params) + + user = %{ + discord_id: nil, + name: "ingame_name" + } + + host = "localhost" + + result = PostFormComponent.create_discord_text(user, post, host) + + assert String.contains?(result, "Posted by ingame_name") + end +end