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

Commit f9771d4

Browse files
authored
feat(works-thread): basic workflow (#414)
* chore: remove warning * chore(works): create migrate tables * chore(works): basic setup & test * chore(works): missing type * chore(works): missing schema * chore(works): plural issue * chore(works): plural issue * chore(works): tests * chore(works): pural issue * chore(works): meta thread fix * chore(works): adjust article_queries macros * chore(works): fix test
1 parent fc17d57 commit f9771d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4517
-33
lines changed

config/config.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ config :groupher_server, :article,
6969
min_length: 10,
7070
max_length: 20_000,
7171
# NOTE: do not change unless you know what you are doing
72-
threads: [:post, :job, :repo, :blog],
72+
threads: [:post, :job, :repo, :blog, :works],
7373
# in this period, paged articles will sort front if non-article-author commented
7474
# 在此时间段内,一旦有非文章作者的用户评论,该文章就会排到前面
7575
active_period_days: %{
7676
post: 10,
7777
job: 10,
7878
repo: 10,
79-
blog: 10
79+
blog: 10,
80+
works: 10
8081
},
8182

8283
# NOTE: if you want to add/remove emotion, just edit the list below

lib/groupher_server/cms/delegates/hooks/cite.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
6666
end
6767
end
6868

69-
def handle(%{document: document} = article) do
69+
def handle(%{document: _document} = article) do
7070
body = Repo.preload(article, :document) |> get_in([:document, :body])
7171
article = article |> Map.put(:body, body)
7272
handle(article)

lib/groupher_server/cms/delegates/hooks/mention.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
2727
end
2828
end
2929

30-
def handle(%{document: document} = article) do
30+
def handle(%{document: _document} = article) do
3131
body = Repo.preload(article, :document) |> get_in([:document, :body])
3232
article = article |> Map.put(:body, body)
3333
handle(article)

lib/groupher_server/cms/helper/macros.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
22
@moduledoc """
33
macros for define article related fields in CMS models
44
"""
5-
import Helper.Utils, only: [get_config: 2]
5+
import Helper.Utils, only: [get_config: 2, plural: 1]
66

77
alias GroupherServer.{CMS, Accounts}
88

@@ -220,7 +220,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
220220
many_to_many(
221221
:communities,
222222
Community,
223-
join_through: unquote("communities_join_#{to_string(thread)}s"),
223+
join_through: unquote("communities_join_#{plural(thread)}"),
224224
on_replace: :delete
225225
)
226226
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule GroupherServer.CMS.Model.Works do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
use Accessible
7+
8+
import Ecto.Changeset
9+
import GroupherServer.CMS.Helper.Macros
10+
11+
alias GroupherServer.CMS
12+
alias CMS.Model.Embeds
13+
14+
@timestamps_opts [type: :utc_datetime_usec]
15+
16+
@required_fields ~w(title digest)a
17+
@article_cast_fields general_article_cast_fields()
18+
@optional_fields @article_cast_fields
19+
20+
@type t :: %Works{}
21+
schema "cms_works" do
22+
article_tags_field(:works)
23+
article_communities_field(:works)
24+
general_article_fields(:works)
25+
end
26+
27+
@doc false
28+
def changeset(%Works{} = works, attrs) do
29+
works
30+
|> cast(attrs, @optional_fields ++ @required_fields)
31+
|> validate_required(@required_fields)
32+
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
33+
|> generl_changeset
34+
end
35+
36+
@doc false
37+
def update_changeset(%Works{} = works, attrs) do
38+
works
39+
|> cast(attrs, @optional_fields ++ @required_fields)
40+
|> generl_changeset
41+
end
42+
43+
defp generl_changeset(changeset) do
44+
changeset
45+
|> validate_length(:title, min: 3, max: 50)
46+
|> cast_embed(:emotions, with: &Embeds.ArticleEmotion.changeset/2)
47+
end
48+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule GroupherServer.CMS.Model.WorksDocument do
2+
@moduledoc """
3+
mainly for full-text search
4+
"""
5+
alias __MODULE__
6+
7+
use Ecto.Schema
8+
use Accessible
9+
10+
import Ecto.Changeset
11+
import Helper.Utils, only: [get_config: 2]
12+
13+
alias GroupherServer.CMS
14+
alias CMS.Model.Works
15+
16+
@timestamps_opts [type: :utc_datetime_usec]
17+
18+
@max_body_length get_config(:article, :max_length)
19+
@min_body_length get_config(:article, :min_length)
20+
21+
@required_fields ~w(body body_html works_id)a
22+
@optional_fields []
23+
24+
@type t :: %WorksDocument{}
25+
schema "works_documents" do
26+
belongs_to(:works, Works, foreign_key: :works_id)
27+
28+
field(:body, :string)
29+
field(:body_html, :string)
30+
field(:toc, :map)
31+
end
32+
33+
@doc false
34+
def changeset(%WorksDocument{} = works, attrs) do
35+
works
36+
|> cast(attrs, @optional_fields ++ @required_fields)
37+
|> validate_required(@required_fields)
38+
|> validate_length(:body, min: @min_body_length, max: @max_body_length)
39+
end
40+
41+
@doc false
42+
def update_changeset(%WorksDocument{} = works, attrs) do
43+
works
44+
|> cast(attrs, @optional_fields ++ @required_fields)
45+
|> validate_length(:body, min: @min_body_length, max: @max_body_length)
46+
end
47+
end

lib/groupher_server_web/schema/Helper/objects.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule GroupherServerWeb.Schema.Helper.Objects do
22
@moduledoc """
33
general fields used in schema definition
44
"""
5-
import Helper.Utils, only: [get_config: 2]
5+
import Helper.Utils, only: [get_config: 2, plural: 1]
66

77
@article_threads get_config(:article, :threads)
88

@@ -19,7 +19,7 @@ defmodule GroupherServerWeb.Schema.Helper.Objects do
1919
@article_threads
2020
|> Enum.map(
2121
&quote do
22-
object unquote(:"paged_#{&1}s") do
22+
object unquote(:"paged_#{plural(&1)}") do
2323
field(:entries, list_of(unquote(&1)))
2424
pagination_fields()
2525
end

lib/groupher_server_web/schema/Helper/queries.ex

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
22
@moduledoc """
33
common fields
44
"""
5-
import Helper.Utils, only: [get_config: 2]
5+
import Helper.Utils, only: [get_config: 2, plural: 1]
66

77
alias GroupherServerWeb.Middleware, as: M
88
alias GroupherServerWeb.Resolvers, as: R
@@ -14,8 +14,9 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
1414
@article_threads
1515
|> Enum.map(fn thread ->
1616
quote do
17-
@desc unquote("paged published #{thread}s")
18-
field unquote(:"paged_published_#{thread}s"), unquote(:"paged_#{thread}s") do
17+
@desc unquote("paged published #{plural(thread)}")
18+
field unquote(:"paged_published_#{plural(thread)}"),
19+
unquote(:"paged_#{plural(thread)}") do
1920
arg(:login, non_null(:string))
2021
arg(:filter, non_null(:paged_filter))
2122
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
@@ -32,7 +33,7 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
3233
|> Enum.map(fn thread ->
3334
quote do
3435
@desc unquote("get #{thread} by id")
35-
field unquote(:"search_#{thread}s"), unquote(:"paged_#{thread}s") do
36+
field unquote(:"search_#{plural(thread)}"), unquote(:"paged_#{plural(thread)}") do
3637
arg(:title, non_null(:string))
3738
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
3839

@@ -47,25 +48,28 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
4748
4849
post, page_posts ...
4950
"""
50-
defmacro article_queries(thread) do
51-
quote do
52-
@desc unquote("get #{thread} by id")
53-
field unquote(thread), non_null(unquote(thread)) do
54-
arg(:id, non_null(:id))
55-
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
51+
defmacro article_queries() do
52+
@article_threads
53+
|> Enum.map(fn thread ->
54+
quote do
55+
@desc unquote("get #{thread} by id")
56+
field unquote(thread), non_null(unquote(thread)) do
57+
arg(:id, non_null(:id))
58+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
5659

57-
resolve(&R.CMS.read_article/3)
58-
end
60+
resolve(&R.CMS.read_article/3)
61+
end
5962

60-
@desc unquote("get paged #{thread}s")
61-
field unquote(:"paged_#{thread}s"), unquote(:"paged_#{thread}s") do
62-
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
63-
arg(:filter, non_null(unquote(:"paged_#{thread}s_filter")))
63+
@desc unquote("get paged #{plural(thread)}")
64+
field unquote(:"paged_#{plural(thread)}"), unquote(:"paged_#{plural(thread)}") do
65+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
66+
arg(:filter, non_null(unquote(:"paged_#{plural(thread)}_filter")))
6467

65-
middleware(M.PageSizeProof, default_sort: :desc_active)
66-
resolve(&R.CMS.paged_articles/3)
68+
middleware(M.PageSizeProof, default_sort: :desc_active)
69+
resolve(&R.CMS.paged_articles/3)
70+
end
6771
end
68-
end
72+
end)
6973
end
7074

7175
defmacro article_reacted_users_query(action, resolver) do

lib/groupher_server_web/schema/cms/cms_metrics.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,21 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
191191
field(:sort, :sort_enum)
192192
end
193193

194+
@desc "blog_filter doc"
194195
input_object :paged_blogs_filter do
195196
pagination_args()
196197
article_filter_fields()
197198
field(:sort, :sort_enum)
198199
end
199200

201+
@desc "works_filter doc"
202+
# TODO:
203+
input_object :paged_works_filter do
204+
pagination_args()
205+
article_filter_fields()
206+
field(:sort, :sort_enum)
207+
end
208+
200209
@desc "article_filter doc"
201210
input_object :paged_repos_filter do
202211
@desc "limit of records (default 20), if first > 30, only return 30 at most"

lib/groupher_server_web/schema/cms/cms_queries.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
145145
article_reacted_users_query(:upvot, &R.CMS.upvoted_users/3)
146146
article_reacted_users_query(:collect, &R.CMS.collected_users/3)
147147

148-
article_queries(:post)
149-
article_queries(:job)
150-
article_queries(:blog)
151-
article_queries(:repo)
148+
article_queries()
152149
end
153150
end

0 commit comments

Comments
 (0)