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

Commit 188c18a

Browse files
authored
feat(radar-thread): basic workflow (#415)
* chore(radar): add migration tables * chore(radar): setup basic model & test react macros * chore(radar): fix used value * chore(radar): fix macro * chore(radar): use article_react_mutations * chore(radar): use article_react_mutations * chore(radar): add tests * chore(radar): missing radar model * chore(radar): fix test
1 parent f9771d4 commit 188c18a

Some content is hidden

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

47 files changed

+6865
-58
lines changed

config/config.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ 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, :works],
72+
threads: [:post, :job, :repo, :blog, :works, :radar],
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,
7979
blog: 10,
80-
works: 10
80+
works: 10,
81+
radar: 10
8182
},
8283

8384
# NOTE: if you want to add/remove emotion, just edit the list below
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule GroupherServer.CMS.Model.Radar 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 :: %Radar{}
21+
schema "cms_radars" do
22+
article_tags_field(:radar)
23+
article_communities_field(:radar)
24+
general_article_fields(:radar)
25+
end
26+
27+
@doc false
28+
def changeset(%Radar{} = radar, attrs) do
29+
radar
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(%Radar{} = radar, attrs) do
38+
radar
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.RadarDocument 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.Radar
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 radar_id)a
22+
@optional_fields []
23+
24+
@type t :: %RadarDocument{}
25+
schema "radar_documents" do
26+
belongs_to(:radar, Radar, foreign_key: :radar_id)
27+
28+
field(:body, :string)
29+
field(:body_html, :string)
30+
field(:toc, :map)
31+
end
32+
33+
@doc false
34+
def changeset(%RadarDocument{} = radar, attrs) do
35+
radar
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(%RadarDocument{} = radar, attrs) do
43+
radar
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/mutations.ex

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
22
@moduledoc """
33
general mutations used for articles
44
5+
can not dedefine private macros, see:
6+
https://github.com/elixir-lang/elixir/issues/3887
7+
58
e.g:
69
in schema/cms/mutation/post.ex
710
811
add following:
9-
article_upvote_mutation(:post)
10-
11-
post will have two mutation endpoint:
12-
13-
upvote_post
14-
unto_emotion_post
12+
article_react_mutations(:post, [:upvote, :pin, :mark_delete, :delete, :emotion, :report, :sink, :lock_comment])
13+
14+
it will expand as
15+
article_upvote_mutation(:radar)
16+
article_pin_mutation(:radar)
17+
article_mark_delete_mutation(:radar)
18+
article_delete_mutation(:radar)
19+
article_emotion_mutation(:radar)
20+
article_report_mutation(:radar)
21+
article_sink_mutation(:radar)
22+
article_lock_comment_mutation(:radar)
1523
1624
same for the job/repo .. article thread
1725
"""
1826
alias GroupherServerWeb.Middleware, as: M
1927
alias GroupherServerWeb.Resolvers, as: R
2028

29+
@doc """
30+
add basic mutation reactions to article
31+
"""
32+
defmacro article_react_mutations(thread, reactions) do
33+
reactions
34+
|> Enum.map(
35+
&quote do
36+
unquote(:"article_#{&1}_mutation")(unquote(thread))
37+
end
38+
)
39+
end
40+
2141
@doc """
2242
upvote mutation for article
2343

lib/groupher_server_web/schema/cms/cms_metrics.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,19 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
199199
end
200200

201201
@desc "works_filter doc"
202-
# TODO:
203202
input_object :paged_works_filter do
204203
pagination_args()
205204
article_filter_fields()
206205
field(:sort, :sort_enum)
207206
end
208207

208+
@desc "radar_filter doc"
209+
input_object :paged_radars_filter do
210+
pagination_args()
211+
article_filter_fields()
212+
field(:sort, :sort_enum)
213+
end
214+
209215
@desc "article_filter doc"
210216
input_object :paged_repos_filter do
211217
@desc "limit of records (default 20), if first > 30, only return 30 at most"

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
105105
timestamp_fields(:article)
106106
end
107107

108+
object :radar do
109+
interface(:article)
110+
111+
general_article_fields()
112+
comments_fields()
113+
114+
field(:length, :integer)
115+
field(:link_addr, :string)
116+
117+
timestamp_fields(:article)
118+
end
119+
108120
object :repo do
109121
interface(:article)
110122

lib/groupher_server_web/schema/cms/mutations/blog.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do
4343
resolve(&R.CMS.update_article/3)
4444
end
4545

46-
#############
47-
article_upvote_mutation(:blog)
48-
article_pin_mutation(:blog)
49-
article_mark_delete_mutation(:blog)
50-
article_delete_mutation(:blog)
51-
article_emotion_mutation(:blog)
52-
article_report_mutation(:blog)
53-
article_sink_mutation(:blog)
54-
article_lock_comment_mutation(:blog)
55-
#############
46+
article_react_mutations(:blog, [
47+
:upvote,
48+
:pin,
49+
:mark_delete,
50+
:delete,
51+
:emotion,
52+
:report,
53+
:sink,
54+
:lock_comment
55+
])
5656
end
5757
end

lib/groupher_server_web/schema/cms/mutations/job.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
5353
resolve(&R.CMS.update_article/3)
5454
end
5555

56-
#############
57-
article_upvote_mutation(:job)
58-
article_pin_mutation(:job)
59-
article_mark_delete_mutation(:job)
60-
article_delete_mutation(:job)
61-
article_emotion_mutation(:job)
62-
article_report_mutation(:job)
63-
article_sink_mutation(:job)
64-
article_lock_comment_mutation(:job)
65-
#############
56+
article_react_mutations(:job, [
57+
:upvote,
58+
:pin,
59+
:mark_delete,
60+
:delete,
61+
:emotion,
62+
:report,
63+
:sink,
64+
:lock_comment
65+
])
6666
end
6767
end

lib/groupher_server_web/schema/cms/mutations/post.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
4343
resolve(&R.CMS.update_article/3)
4444
end
4545

46-
#############
47-
article_upvote_mutation(:post)
48-
article_pin_mutation(:post)
49-
article_mark_delete_mutation(:post)
50-
article_delete_mutation(:post)
51-
article_emotion_mutation(:post)
52-
article_report_mutation(:post)
53-
article_sink_mutation(:post)
54-
article_lock_comment_mutation(:post)
55-
#############
46+
article_react_mutations(:post, [
47+
:upvote,
48+
:pin,
49+
:mark_delete,
50+
:delete,
51+
:emotion,
52+
:report,
53+
:sink,
54+
:lock_comment
55+
])
5656
end
5757
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
defmodule GroupherServerWeb.Schema.CMS.Mutations.Radar do
2+
@moduledoc """
3+
CMS mutations for radar
4+
"""
5+
use Helper.GqlSchemaSuite
6+
import GroupherServerWeb.Schema.Helper.Mutations
7+
8+
object :cms_radar_mutations do
9+
@desc "create a radar"
10+
field :create_radar, :radar do
11+
arg(:title, non_null(:string))
12+
arg(:body, non_null(:string))
13+
arg(:digest, non_null(:string))
14+
arg(:community_id, non_null(:id))
15+
arg(:thread, :thread, default_value: :radar)
16+
arg(:article_tags, list_of(:id))
17+
18+
middleware(M.Authorize, :login)
19+
middleware(M.PublishThrottle)
20+
resolve(&R.CMS.create_article/3)
21+
middleware(M.Statistics.MakeContribute, for: [:user, :community])
22+
end
23+
24+
@desc "update a cms/radar"
25+
field :update_radar, :radar do
26+
arg(:id, non_null(:id))
27+
arg(:title, :string)
28+
arg(:body, :string)
29+
arg(:digest, :string)
30+
31+
arg(:article_tags, list_of(:id))
32+
# ...
33+
34+
middleware(M.Authorize, :login)
35+
middleware(M.PassportLoader, source: :radar)
36+
middleware(M.Passport, claim: "owner;cms->c?->radar.edit")
37+
38+
resolve(&R.CMS.update_article/3)
39+
end
40+
41+
article_react_mutations(:radar, [
42+
:upvote,
43+
:pin,
44+
:mark_delete,
45+
:delete,
46+
:emotion,
47+
:report,
48+
:sink,
49+
:lock_comment
50+
])
51+
end
52+
end

0 commit comments

Comments
 (0)