Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e5c7665

Browse files
committed
refactor(article): add thread to meta when create
1 parent b2e5b23 commit e5c7665

File tree

7 files changed

+26
-4
lines changed

7 files changed

+26
-4
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
66

77
import GroupherServer.CMS.Helper.Matcher
88

9-
import Helper.Utils, only: [done: 1, pick_by: 2, module_to_atom: 1, get_config: 2, ensure: 2]
9+
import Helper.Utils,
10+
only: [done: 1, pick_by: 2, module_to_atom: 1, get_config: 2, ensure: 2, module_to_upcase: 1]
1011

1112
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
1213
import Helper.ErrorCode
@@ -384,6 +385,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
384385
defp do_create_article(model, attrs, %Author{id: author_id}, %Community{id: community_id}) do
385386
# special article like Repo do not have :body, assign it with default-empty rich text
386387
body = Map.get(attrs, :body, Converter.Article.default_rich_text())
388+
meta = @default_article_meta |> Map.merge(%{thread: module_to_upcase(model)})
387389
attrs = attrs |> Map.merge(%{body: body})
388390

389391
with {:ok, attrs} <- add_rich_text_attrs(attrs) do
@@ -392,7 +394,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
392394
|> Ecto.Changeset.put_change(:emotions, @default_emotions)
393395
|> Ecto.Changeset.put_change(:author_id, author_id)
394396
|> Ecto.Changeset.put_change(:original_community_id, community_id)
395-
|> Ecto.Changeset.put_embed(:meta, @default_article_meta)
397+
|> Ecto.Changeset.put_embed(:meta, meta)
396398
|> Repo.insert()
397399
end
398400
end

lib/groupher_server/cms/models/embeds/article_meta.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
66
use Accessible
77
import Ecto.Changeset
88

9-
@optional_fields ~w(is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count is_sinked can_undo_sink last_active_at)a
9+
@optional_fields ~w(thread is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count is_sinked can_undo_sink last_active_at)a
1010

1111
@doc "for test usage"
1212
def default_meta() do
1313
%{
14+
thread: "POST",
1415
is_edited: false,
1516
is_comment_locked: false,
1617
upvoted_user_ids: [],
@@ -25,6 +26,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
2526
end
2627

2728
embedded_schema do
29+
field(:thread, :string)
2830
field(:is_edited, :boolean, default: false)
2931
field(:is_comment_locked, :boolean, default: false)
3032
# reaction history

lib/helper/utils/utils.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ defmodule Helper.Utils do
182182

183183
def strip_struct(map) when is_map(map), do: map
184184

185+
@doc """
186+
get upcase name of a module, most used for store thread in DB
187+
"""
188+
def module_to_upcase(module) do
189+
module |> Module.split() |> List.last() |> String.upcase()
190+
end
191+
185192
@doc """
186193
get atom name of a module
187194
"""

test/groupher_server/cms/articles/blog_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ defmodule GroupherServer.Test.Articles.Blog do
2222
end
2323

2424
describe "[cms blogs curd]" do
25+
@tag :wip
2526
test "can create blog with valid attrs", ~m(user community blog_attrs)a do
2627
assert {:error, _} = ORM.find_by(Author, user_id: user.id)
2728
{:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user)
2829

2930
body_map = Jason.decode!(blog.body)
3031

32+
assert blog.meta.thread == "BLOG"
33+
3134
assert blog.title == blog_attrs.title
3235
assert body_map |> Validator.is_valid()
3336
assert blog.body_html |> String.contains?(~s(<div class="#{@root_class["viewer"]}">))

test/groupher_server/cms/articles/job_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ defmodule GroupherServer.Test.Articles.Job do
2323
end
2424

2525
describe "[cms jobs curd]" do
26+
@tag :wip
2627
test "can create job with valid attrs", ~m(user community job_attrs)a do
2728
assert {:error, _} = ORM.find_by(Author, user_id: user.id)
2829
{:ok, job} = CMS.create_article(community, :job, job_attrs, user)
2930

3031
body_map = Jason.decode!(job.body)
3132

33+
assert job.meta.thread == "JOB"
34+
3235
assert job.title == job_attrs.title
3336
assert body_map |> Validator.is_valid()
3437
assert job.body_html |> String.contains?(~s(<div class="#{@root_class["viewer"]}">))

test/groupher_server/cms/articles/post_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ defmodule GroupherServer.Test.CMS.Articles.Post do
2323
end
2424

2525
describe "[cms post curd]" do
26+
@tag :wip
2627
test "can create post with valid attrs", ~m(user community post_attrs)a do
2728
assert {:error, _} = ORM.find_by(Author, user_id: user.id)
2829
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
2930

3031
body_map = Jason.decode!(post.body)
3132

33+
assert post.meta.thread == "POST"
34+
3235
assert post.title == post_attrs.title
3336
assert body_map |> Validator.is_valid()
3437
assert post.body_html |> String.contains?(~s(<div class="#{@root_class["viewer"]}">))

test/groupher_server/cms/articles/repo_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ defmodule GroupherServer.Test.Articles.Repo do
2323
end
2424

2525
describe "[cms repo curd]" do
26+
@tag :wip
2627
test "can create repo with valid attrs", ~m(user community repo_attrs)a do
2728
assert {:error, _} = ORM.find_by(Author, user_id: user.id)
2829
{:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user)
2930

3031
body_map = Jason.decode!(repo.body)
3132

33+
assert repo.meta.thread == "REPO"
34+
3235
assert repo.title == repo_attrs.title
3336
assert body_map |> Validator.is_valid()
3437
assert repo.body_html |> String.contains?(~s(<div class="#{@root_class["viewer"]}">))
@@ -63,7 +66,6 @@ defmodule GroupherServer.Test.Articles.Repo do
6366
assert user2.id in created.meta.viewed_user_ids
6467
end
6568

66-
@tag :wip
6769
test "read repo should contains viewer_has_xxx state", ~m(repo_attrs community user user2)a do
6870
{:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user)
6971
{:ok, repo} = CMS.read_article(:repo, repo.id, user)

0 commit comments

Comments
 (0)