Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
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
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
pick_by: 2,
integerfy: 1,
strip_struct: 1,
module_to_thread: 1,
module_to_atom: 1,
get_config: 2,
ensure: 2
]
Expand Down Expand Up @@ -333,7 +333,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end

defp add_pin_articles_ifneed(articles, querable, %{community: community} = filter) do
thread = module_to_thread(querable)
thread = module_to_atom(querable)

with true <- should_add_pin?(filter),
true <- 1 == Map.get(articles, :page_number),
Expand Down
66 changes: 46 additions & 20 deletions lib/groupher_server_web/schema/Helper/fields.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do

@article_threads get_config(:article, :threads)

@doc "general article fields for grqphql resolve fields"
defmacro general_article_fields() do
quote do
field(:id, :id)
field(:title, :string)
field(:views, :integer)
field(:is_pinned, :boolean)
field(:mark_delete, :boolean)

field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags))
field(:author, :user, resolve: dataloader(CMS, :author))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

field(:meta, :article_meta)
field(:upvotes_count, :integer)
field(:collects_count, :integer)
field(:emotions, :article_emotions)

field(:viewer_has_collected, :boolean)
field(:viewer_has_upvoted, :boolean)
field(:viewer_has_viewed, :boolean)
field(:viewer_has_reported, :boolean)
end
end

defmacro article_thread_enums do
@article_threads
|> Enum.map(fn thread ->
quote do
enum(unquote(:"#{thread}_thread"), do: value(unquote(thread)))
end
end)
end

defmacro article_values do
@article_threads
|> Enum.map(fn thread ->
quote do
value(unquote(thread))
end
end)
end

defmacro timestamp_fields(:article) do
quote do
field(:inserted_at, :datetime)
Expand Down Expand Up @@ -86,15 +130,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end)
end

defmacro viewer_has_state_fields do
quote do
field(:viewer_has_collected, :boolean)
field(:viewer_has_upvoted, :boolean)
field(:viewer_has_viewed, :boolean)
field(:viewer_has_reported, :boolean)
end
end

# TODO: remove
defmacro comments_fields do
quote do
field(:id, :id)
Expand All @@ -120,16 +156,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
middleware(M.ConvertToInt)
end

field :viewer_has_liked, :boolean do
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)

middleware(M.Authorize, :login)
# put current user into dataloader's args
middleware(M.PutCurrentUser)
resolve(dataloader(CMS, :likes))
middleware(M.ViewerDidConvert)
end

field :replies, list_of(:comment) do
arg(:filter, :members_filter)

Expand Down Expand Up @@ -157,6 +183,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

# TODO: remove
defmacro comments_counter_fields(thread) do
quote do
# @dec "total comments of the post"
Expand All @@ -170,7 +197,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
# @desc "unique participator list of a the comments"
field :comments_participators, list_of(:user) do
arg(:filter, :members_filter)
arg(:unique, :unique_type, default_value: true)

# middleware(M.ForceLoader)
middleware(M.PageSizeProof)
Expand Down
94 changes: 17 additions & 77 deletions lib/groupher_server_web/schema/cms/cms_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,36 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
"""
use Absinthe.Schema.Notation
import GroupherServerWeb.Schema.Helper.Fields
alias GroupherServer.CMS

alias CMS.Model.{Post, Job, Repo}
import Helper.Utils, only: [module_to_atom: 1]

@default_inner_page_size 5

enum(:post_thread, do: value(:post))
enum(:job_thread, do: value(:job))
enum(:repo_thread, do: value(:repo))

enum(:community_type, do: value(:community))
enum(:comment_replies_type, do: value(:comment_replies_type))

enum(:count_type, do: value(:count))
enum(:viewer_did_type, do: value(:viewer_did))

enum(:star_action, do: value(:star))
enum(:comment_action, do: value(:comment))

enum :unique_type do
value(true)
value(false)
end
@doc """
only used for reaction result, like: upvote/collect/watch ...
"""
interface :article do
# article 所包含的共同字段
field(:id, :id)
field(:title, :string)
field(:views, :integer)

enum :react_action do
value(:collect)
value(:upvote)
# value(:watch)
# 这里只是遵循 absinthe 的规范,并不是指返回以下的字段
resolve_type(fn parent_module, _ -> module_to_atom(parent_module) end)
end

enum :reactable_action do
value(:upvote)
# value(:collect)
# value(:watch)
end
article_thread_enums()

# TODO: remove
enum(:count_type, do: value(:count))
# TODO: remove
enum :cms_comment do
value(:post_comment)
end

enum :thread do
value(:post)
value(:job)
article_values()
value(:user)
value(:repo)
value(:wiki)
value(:cheatsheet)
# home community
Expand Down Expand Up @@ -266,9 +251,7 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
end

enum :report_content_type do
value(:post)
value(:job)
value(:repo)
article_values()
value(:account)
value(:article_comment)
# value(:community)
Expand All @@ -285,47 +268,4 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
# min_case_count,
# max_case_count,
end

@doc """
only used for reaction result, like: upvote/collect/watch ...
"""
interface :article do
field(:id, :id)
# field(:title, :string)

# TODO: remove the domain part
resolve_type(fn
%Post{}, _ -> :post
%Job{}, _ -> :job
%Repo{}, _ -> :repo
_, _ -> nil
end)
end

# @desc """
# The `DateTime` scalar type represents a date and time in the UTC
# timezone. The DateTime appears in a JSON response as an ISO8601 formatted
# string, including UTC timezone ("Z"). The parsed date and time string will
# be converted to UTC and any UTC offset other than 0 will be rejected.
# """
# scalar :datetime, name: "DateTime" do
# serialize &DateTime.to_iso8601/1
# parse &parse_datetime/1
# end

# @spec parse_datetime(Absinthe.Blueprint.Input.String.t) :: {:ok, DateTime.t} | :error
# @spec parse_datetime(Absinthe.Blueprint.Input.Null.t) :: {:ok, nil}
# defp parse_datetime(%Absinthe.Blueprint.Input.String{value: value}) do
# case DateTime.from_iso8601(value) do
# {:ok, datetime, 0} -> {:ok, datetime}
# {:ok, _datetime, _offset} -> :error
# _error -> :error
# end
# end
# defp parse_datetime(%Absinthe.Blueprint.Input.Null{}) do
# {:ok, nil}
# end
# defp parse_datetime(_) do
# :error
# end
end
68 changes: 14 additions & 54 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,15 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
object :post do
meta(:cache, max_age: 30)
interface(:article)
field(:id, :id)
field(:title, :string)

general_article_fields()
article_comments_fields()

field(:digest, :string)
field(:length, :integer)
field(:link_addr, :string)
field(:copy_right, :string)
field(:body, :string)
field(:views, :integer)
# NOTE: only meaningful in paged-xxx queries
field(:is_pinned, :boolean)
field(:mark_delete, :boolean)
field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags))

field(:author, :user, resolve: dataloader(CMS, :author))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

field(:meta, :article_meta)
field(:emotions, :article_emotions)

field :comments, list_of(:comment) do
arg(:filter, :members_filter)
Expand All @@ -79,8 +69,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
resolve(dataloader(CMS, :comments))
end

# comments_count
# comments_participators / paged
comments_counter_fields(:post)

@desc "totalCount of unique participator list of a the comments"
Expand All @@ -94,18 +82,18 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
end)
end

article_comments_fields()
viewer_has_state_fields()
# upvoted_count
# upvoted_count ?
# collected_count

timestamp_fields(:article)
end

object :job do
interface(:article)
field(:id, :id)
field(:title, :string)

general_article_fields()
article_comments_fields()

field(:desc, :string)
field(:company, :string)
field(:company_link, :string)
Expand All @@ -114,34 +102,19 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:link_addr, :string)
field(:copy_right, :string)
field(:body, :string)
field(:views, :integer)

field(:is_pinned, :boolean)
field(:mark_delete, :boolean)

field(:author, :user, resolve: dataloader(CMS, :author))
field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

field(:meta, :article_meta)
field(:emotions, :article_emotions)

# comments_count
# comments_participators
article_comments_fields()
viewer_has_state_fields()
timestamp_fields(:article)
end

object :repo do
# interface(:article)
field(:id, :id)
field(:title, :string)
interface(:article)

general_article_fields()
article_comments_fields()

field(:owner_name, :string)
field(:owner_url, :string)
field(:repo_url, :string)
field(:author, :user, resolve: dataloader(CMS, :author))

field(:desc, :string)
field(:homepage_url, :string)
Expand All @@ -159,21 +132,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do

field(:contributors, list_of(:repo_contributor))

field(:views, :integer)
field(:is_pinned, :boolean)
field(:mark_delete, :boolean)

field(:last_sync, :datetime)

field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

article_comments_fields()
viewer_has_state_fields()
# comments_count
# comments_participators

timestamp_fields(:article)
end

Expand Down
Loading