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

refactor(post-meta): use build-in embed schema #313

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/groupher_server/cms/article_meta.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule GroupherServer.CMS.ArticleMeta do
@moduledoc """
general article meta info for article-like content, like post, job, works ...
"""
use Ecto.Schema

@default_meta %{
is_edited: false,
forbid_comment: false,
is_reported: false
# linkedPostsCount: 0,
# linkedJobsCount: 0,
# linkedWorksCount: 0,
# reaction: %{
# rocketCount: 0,
# heartCount: 0,
# }
}

@doc "for test usage"
def default_meta(), do: @default_meta

embedded_schema do
field(:is_edited, :boolean, default: false)
field(:forbid_comment, :boolean, default: false)
field(:is_reported, :boolean, default: false)
end
end
38 changes: 18 additions & 20 deletions lib/groupher_server/cms/delegates/article_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
alias Helper.ORM

alias GroupherServer.CMS.{
ArticleMeta,
Community,
Post,
PostCommunityFlag,
Expand All @@ -29,22 +30,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
alias GroupherServer.CMS.Repo, as: CMSRepo
alias GroupherServer.Repo

@default_article_meta %{
isEdited: false,
forbidComment: false,
isReported: false
# linkedPostsCount: 0,
# linkedJobsCount: 0,
# linkedWorksCount: 0,
# reaction: %{
# rocketCount: 0,
# heartCount: 0,
# }
}

@doc "for test usage"
def default_article_meta(), do: @default_article_meta

def pin_content(%Post{id: post_id}, %Community{id: community_id}, topic) do
with {:ok, %{id: topic_id}} <- ORM.find_by(Topic, %{raw: topic}),
{:ok, pined} <-
Expand Down Expand Up @@ -294,19 +279,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
def set_topic(_topic, _thread, _content_id), do: {:ok, :pass}

@doc "set meta info"
# embeds_one do not have default option, so we init it with empty map mannully
# see: https://github.com/elixir-ecto/ecto/issues/2634
def set_meta(:post, content_id) do
ORM.update_by(Post, [id: content_id], %{meta: @default_article_meta})
ORM.update_by(Post, [id: content_id], %{meta: %{}})
end

def set_meta(_, _), do: {:ok, :pass}

@doc "update isEdited meta label if needed"
def update_meta(%Post{meta: %{"isEdited" => false} = meta} = content, :is_edited) do
ORM.update(content, %{meta: Map.merge(meta, %{"isEdited" => true})})
def update_meta(%Post{meta: %ArticleMeta{is_edited: false} = meta} = content, :is_edited) do
new_meta = meta |> Map.from_struct() |> Map.delete(:id) |> Map.merge(%{is_edited: true})

content
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:meta, new_meta)
|> Repo.update()
end

# for test or exsiting articles
def update_meta(%Post{meta: nil} = content, :is_edited) do
ORM.update(content, %{meta: Map.merge(@default_article_meta, %{"isEdited" => true})})
new_meta = ArticleMeta.default_meta() |> Map.merge(%{is_edited: true})

content
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:meta, new_meta)
|> Repo.update()
end

def update_meta(content, _), do: {:ok, content}
Expand Down
8 changes: 6 additions & 2 deletions lib/groupher_server/cms/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Post do
alias GroupherServer.CMS

alias CMS.{
ArticleMeta,
Author,
Community,
PostComment,
Expand All @@ -23,7 +24,8 @@ defmodule GroupherServer.CMS.Post do

@timestamps_opts [type: :utc_datetime_usec]
@required_fields ~w(title body digest length)a
@optional_fields ~w(origial_community_id link_addr copy_right link_addr link_icon meta)a
@optional_fields ~w(origial_community_id link_addr copy_right link_addr link_icon)a
@embed_fileds ~w(meta)a

@type t :: %Post{}
schema "cms_posts" do
Expand All @@ -36,7 +38,9 @@ defmodule GroupherServer.CMS.Post do
field(:length, :integer)
field(:views, :integer, default: 0)

field(:meta, :map)
embeds_one(:meta, ArticleMeta, on_replace: :update)

# field(:meta, :map)

has_many(:community_flags, {"posts_communities_flags", PostCommunityFlag})

Expand Down
19 changes: 0 additions & 19 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ defmodule GroupherServerWeb.Resolvers.CMS do
alias Helper.ORM
alias Helper.Utils

@default_article_meta CMS.Delegate.ArticleOperation.default_article_meta()

# #######################
# community ..
# #######################
Expand Down Expand Up @@ -444,21 +442,4 @@ defmodule GroupherServerWeb.Resolvers.CMS do
def tags_count(root, _, _) do
CMS.count(%Community{id: root.id}, :tags)
end

@doc """
covert normal map to absinthe fmt
e.g:
%{"exampleKey" => false } -> %{example_key: false }
"""
def get_article_meta(root, _, _) do
# if meta is nil , means exsit article or test env (like: db_insert)
meta = if is_nil(root.meta), do: @default_article_meta, else: root.meta

fmt_meta =
meta
|> Utils.snake_map_key()
|> Utils.keys_to_atoms()

{:ok, fmt_meta}
end
end
6 changes: 1 addition & 5 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
# field(:topic)

# article meta info
field(:meta, :article_meta) do
# NOTE: absinthe has issue with :map resolving, do it mannulay
resolve(&R.CMS.get_article_meta/3)
end
field(:meta, :article_meta)

# field :meta, :article_meta do
# resolve(&R.CMS.get_meta/3)
Expand Down
21 changes: 11 additions & 10 deletions test/groupher_server/cms/post_meta_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule GroupherServer.Test.CMS.PostMeta do
@moduledoc false
use GroupherServer.TestTools

alias Helper.ORM
alias GroupherServer.CMS
alias Helper.Utils

@default_article_meta CMS.Delegate.ArticleOperation.default_article_meta()
alias CMS.{ArticleMeta, Author, Post}

@default_article_meta ArticleMeta.default_meta()

setup do
{:ok, user} = db_insert(:user)
Expand All @@ -18,29 +20,28 @@ defmodule GroupherServer.Test.CMS.PostMeta do
end

describe "[cms post meta info]" do
alias CMS.{Author, Post}

@tag :wip
@tag :wip2
test "can get default meta info", ~m(user community post_attrs)a do
assert {:error, _} = ORM.find_by(Author, user_id: user.id)

{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
{:ok, post} = ORM.find_by(Post, id: post.id)
meta = post.meta |> Map.from_struct() |> Map.delete(:id)

assert @default_article_meta == Utils.keys_to_atoms(post.meta)
assert @default_article_meta == meta
end

@tag :wip
test "isEdited flag should set to true after post updated", ~m(user community post_attrs)a do
@tag :wip2
test "is_edited flag should set to true after post updated", ~m(user community post_attrs)a do
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
{:ok, post} = ORM.find_by(Post, id: post.id)

assert post.meta["isEdited"] == false
assert post.meta.is_edited == false

{:ok, _} = CMS.update_content(post, %{"title" => "new title"})
{:ok, post} = ORM.find_by(Post, id: post.id)

assert post.meta["isEdited"] == true
assert post.meta.is_edited == true
end

# test "post with image should have imageCount in meta" do
Expand Down
3 changes: 2 additions & 1 deletion test/groupher_server_web/mutation/cms/post_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ defmodule GroupherServer.Test.Mutation.Post do
assert updated_post["copyRight"] == variables.copyRight
end

test "update post with valid attrs should have isEdited meta info update",
@tag :wip2
test "update post with valid attrs should have is_edited meta info update",
~m(owner_conn post)a do
unique_num = System.unique_integer([:positive, :monotonic])

Expand Down
4 changes: 2 additions & 2 deletions test/groupher_server_web/query/cms/post_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule GroupherServer.Test.Query.Post do
}
}
"""
# @tag :wip2
@tag :wip2
test "basic graphql query on post with logined user",
~m(user_conn community user post_attrs)a do
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
Expand All @@ -43,7 +43,7 @@ defmodule GroupherServer.Test.Query.Post do
assert length(Map.keys(results)) == 4
end

@tag :wip2
# @tag :wip2
test "basic graphql query on post with stranger(unloged user)", ~m(guest_conn post)a do
variables = %{id: post.id}
results = guest_conn |> query_result(@query, variables, "post")
Expand Down