Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
5 changes: 3 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ config :groupher_server, :article,
min_length: 10,
max_length: 20_000,
# NOTE: do not change unless you know what you are doing
threads: [:post, :job, :repo, :blog],
threads: [:post, :job, :repo, :blog, :works],
# in this period, paged articles will sort front if non-article-author commented
# 在此时间段内,一旦有非文章作者的用户评论,该文章就会排到前面
active_period_days: %{
post: 10,
job: 10,
repo: 10,
blog: 10
blog: 10,
works: 10
},

# NOTE: if you want to add/remove emotion, just edit the list below
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/hooks/cite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
end
end

def handle(%{document: document} = article) do
def handle(%{document: _document} = article) do
body = Repo.preload(article, :document) |> get_in([:document, :body])
article = article |> Map.put(:body, body)
handle(article)
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/hooks/mention.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
end
end

def handle(%{document: document} = article) do
def handle(%{document: _document} = article) do
body = Repo.preload(article, :document) |> get_in([:document, :body])
article = article |> Map.put(:body, body)
handle(article)
Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/helper/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
@moduledoc """
macros for define article related fields in CMS models
"""
import Helper.Utils, only: [get_config: 2]
import Helper.Utils, only: [get_config: 2, plural: 1]

alias GroupherServer.{CMS, Accounts}

Expand Down Expand Up @@ -220,7 +220,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
many_to_many(
:communities,
Community,
join_through: unquote("communities_join_#{to_string(thread)}s"),
join_through: unquote("communities_join_#{plural(thread)}"),
on_replace: :delete
)
end
Expand Down
48 changes: 48 additions & 0 deletions lib/groupher_server/cms/models/works.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule GroupherServer.CMS.Model.Works do
@moduledoc false
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros

alias GroupherServer.CMS
alias CMS.Model.Embeds

@timestamps_opts [type: :utc_datetime_usec]

@required_fields ~w(title digest)a
@article_cast_fields general_article_cast_fields()
@optional_fields @article_cast_fields

@type t :: %Works{}
schema "cms_works" do
article_tags_field(:works)
article_communities_field(:works)
general_article_fields(:works)
end

@doc false
def changeset(%Works{} = works, attrs) do
works
|> cast(attrs, @optional_fields ++ @required_fields)
|> validate_required(@required_fields)
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
|> generl_changeset
end

@doc false
def update_changeset(%Works{} = works, attrs) do
works
|> cast(attrs, @optional_fields ++ @required_fields)
|> generl_changeset
end

defp generl_changeset(changeset) do
changeset
|> validate_length(:title, min: 3, max: 50)
|> cast_embed(:emotions, with: &Embeds.ArticleEmotion.changeset/2)
end
end
47 changes: 47 additions & 0 deletions lib/groupher_server/cms/models/works_document.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule GroupherServer.CMS.Model.WorksDocument do
@moduledoc """
mainly for full-text search
"""
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset
import Helper.Utils, only: [get_config: 2]

alias GroupherServer.CMS
alias CMS.Model.Works

@timestamps_opts [type: :utc_datetime_usec]

@max_body_length get_config(:article, :max_length)
@min_body_length get_config(:article, :min_length)

@required_fields ~w(body body_html works_id)a
@optional_fields []

@type t :: %WorksDocument{}
schema "works_documents" do
belongs_to(:works, Works, foreign_key: :works_id)

field(:body, :string)
field(:body_html, :string)
field(:toc, :map)
end

@doc false
def changeset(%WorksDocument{} = works, attrs) do
works
|> cast(attrs, @optional_fields ++ @required_fields)
|> validate_required(@required_fields)
|> validate_length(:body, min: @min_body_length, max: @max_body_length)
end

@doc false
def update_changeset(%WorksDocument{} = works, attrs) do
works
|> cast(attrs, @optional_fields ++ @required_fields)
|> validate_length(:body, min: @min_body_length, max: @max_body_length)
end
end
4 changes: 2 additions & 2 deletions lib/groupher_server_web/schema/Helper/objects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GroupherServerWeb.Schema.Helper.Objects do
@moduledoc """
general fields used in schema definition
"""
import Helper.Utils, only: [get_config: 2]
import Helper.Utils, only: [get_config: 2, plural: 1]

@article_threads get_config(:article, :threads)

Expand All @@ -19,7 +19,7 @@ defmodule GroupherServerWeb.Schema.Helper.Objects do
@article_threads
|> Enum.map(
&quote do
object unquote(:"paged_#{&1}s") do
object unquote(:"paged_#{plural(&1)}") do
field(:entries, list_of(unquote(&1)))
pagination_fields()
end
Expand Down
42 changes: 23 additions & 19 deletions lib/groupher_server_web/schema/Helper/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
@moduledoc """
common fields
"""
import Helper.Utils, only: [get_config: 2]
import Helper.Utils, only: [get_config: 2, plural: 1]

alias GroupherServerWeb.Middleware, as: M
alias GroupherServerWeb.Resolvers, as: R
Expand All @@ -14,8 +14,9 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
@article_threads
|> Enum.map(fn thread ->
quote do
@desc unquote("paged published #{thread}s")
field unquote(:"paged_published_#{thread}s"), unquote(:"paged_#{thread}s") do
@desc unquote("paged published #{plural(thread)}")
field unquote(:"paged_published_#{plural(thread)}"),
unquote(:"paged_#{plural(thread)}") do
arg(:login, non_null(:string))
arg(:filter, non_null(:paged_filter))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
Expand All @@ -32,7 +33,7 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
|> Enum.map(fn thread ->
quote do
@desc unquote("get #{thread} by id")
field unquote(:"search_#{thread}s"), unquote(:"paged_#{thread}s") do
field unquote(:"search_#{plural(thread)}"), unquote(:"paged_#{plural(thread)}") do
arg(:title, non_null(:string))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

Expand All @@ -47,25 +48,28 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do

post, page_posts ...
"""
defmacro article_queries(thread) do
quote do
@desc unquote("get #{thread} by id")
field unquote(thread), non_null(unquote(thread)) do
arg(:id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
defmacro article_queries() do
@article_threads
|> Enum.map(fn thread ->
quote do
@desc unquote("get #{thread} by id")
field unquote(thread), non_null(unquote(thread)) do
arg(:id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

resolve(&R.CMS.read_article/3)
end
resolve(&R.CMS.read_article/3)
end

@desc unquote("get paged #{thread}s")
field unquote(:"paged_#{thread}s"), unquote(:"paged_#{thread}s") do
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
arg(:filter, non_null(unquote(:"paged_#{thread}s_filter")))
@desc unquote("get paged #{plural(thread)}")
field unquote(:"paged_#{plural(thread)}"), unquote(:"paged_#{plural(thread)}") do
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
arg(:filter, non_null(unquote(:"paged_#{plural(thread)}_filter")))

middleware(M.PageSizeProof, default_sort: :desc_active)
resolve(&R.CMS.paged_articles/3)
middleware(M.PageSizeProof, default_sort: :desc_active)
resolve(&R.CMS.paged_articles/3)
end
end
end
end)
end

defmacro article_reacted_users_query(action, resolver) do
Expand Down
9 changes: 9 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,21 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
field(:sort, :sort_enum)
end

@desc "blog_filter doc"
input_object :paged_blogs_filter do
pagination_args()
article_filter_fields()
field(:sort, :sort_enum)
end

@desc "works_filter doc"
# TODO:
input_object :paged_works_filter do
pagination_args()
article_filter_fields()
field(:sort, :sort_enum)
end

@desc "article_filter doc"
input_object :paged_repos_filter do
@desc "limit of records (default 20), if first > 30, only return 30 at most"
Expand Down
5 changes: 1 addition & 4 deletions lib/groupher_server_web/schema/cms/cms_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
article_reacted_users_query(:upvot, &R.CMS.upvoted_users/3)
article_reacted_users_query(:collect, &R.CMS.collected_users/3)

article_queries(:post)
article_queries(:job)
article_queries(:blog)
article_queries(:repo)
article_queries()
end
end
12 changes: 12 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
timestamp_fields(:article)
end

object :works do
interface(:article)

general_article_fields()
comments_fields()

field(:length, :integer)
field(:link_addr, :string)

timestamp_fields(:article)
end

object :repo do
interface(:article)

Expand Down
2 changes: 0 additions & 2 deletions lib/groupher_server_web/schema/cms/mutations/blog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do
arg(:length, :integer)
arg(:link_addr, :string)

arg(:company, :string)
arg(:company_link, :string)
arg(:article_tags, list_of(:id))

# ...
Expand Down
52 changes: 52 additions & 0 deletions lib/groupher_server_web/schema/cms/mutations/works.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule GroupherServerWeb.Schema.CMS.Mutations.Works do
@moduledoc """
CMS mutations for works
"""
use Helper.GqlSchemaSuite
import GroupherServerWeb.Schema.Helper.Mutations

object :cms_works_mutations do
@desc "create a works"
field :create_works, :works do
arg(:title, non_null(:string))
arg(:body, non_null(:string))
arg(:digest, non_null(:string))
arg(:community_id, non_null(:id))
arg(:thread, :thread, default_value: :works)
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
middleware(M.PublishThrottle)
resolve(&R.CMS.create_article/3)
middleware(M.Statistics.MakeContribute, for: [:user, :community])
end

@desc "update a cms/works"
field :update_works, :works do
arg(:id, non_null(:id))
arg(:title, :string)
arg(:body, :string)
arg(:digest, :string)

arg(:article_tags, list_of(:id))
# ...

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :works)
middleware(M.Passport, claim: "owner;cms->c?->works.edit")

resolve(&R.CMS.update_article/3)
end

#############
article_upvote_mutation(:works)
article_pin_mutation(:works)
article_mark_delete_mutation(:works)
article_delete_mutation(:works)
article_emotion_mutation(:works)
article_report_mutation(:works)
article_sink_mutation(:works)
article_lock_comment_mutation(:works)
#############
end
end
6 changes: 6 additions & 0 deletions lib/helper/utils/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ defmodule Helper.Utils do
end
end

@doc """
plural version of the thread
"""
def plural(:works), do: :works
def plural(thread), do: :"#{thread}s"

@doc """
like || in javascript
"""
Expand Down
Loading