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

Commit 889a562

Browse files
authored
refactor: apply community logic (#441)
* refactor(comment): add checker api for exist * refactor(pending): add operation api & tests * refactor(pending): add api endpoint * chore: more test on apply workflow * chore: more test on apply workflow * refactor(community-apply): more apply info & tests * refactor(community-apply): edge case
1 parent 66c4062 commit 889a562

26 files changed

+482
-52
lines changed

lib/groupher_server/cms/cms.ex

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ defmodule GroupherServer.CMS do
3737
defdelegate read_community(args, user), to: CommunityCURD
3838
defdelegate create_community(args), to: CommunityCURD
3939
defdelegate update_community(id, args), to: CommunityCURD
40+
defdelegate apply_community(args), to: CommunityCURD
41+
defdelegate approve_community_apply(id), to: CommunityCURD
42+
defdelegate deny_community_apply(id), to: CommunityCURD
43+
defdelegate is_community_exist?(raw), to: CommunityCURD
44+
defdelegate has_pending_community_apply?(user), to: CommunityCURD
45+
4046
# >> editor ..
4147
defdelegate update_editor(user, community, title), to: CommunityCURD
4248
# >> geo info ..

lib/groupher_server/cms/constant.ex

+16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@ defmodule GroupherServer.CMS.Constant do
88
@artiment_illegal 1
99
@artiment_audit_failed 2
1010

11+
@community_normal 0
12+
@community_applying 1
13+
14+
@apply_public "PUBLIC"
15+
@apply_city "CITY"
16+
@apply_works "WORKS"
17+
@apply_team "TEAM"
18+
1119
def pending(:legal), do: @artiment_legal
1220
def pending(:illegal), do: @artiment_illegal
1321
def pending(:audit_failed), do: @artiment_audit_failed
22+
23+
def pending(:normal), do: @community_normal
24+
def pending(:applying), do: @community_applying
25+
26+
def apply_category(:public), do: @apply_public
27+
def apply_category(:city), do: @apply_city
28+
def apply_category(:works), do: @apply_works
29+
def apply_category(:team), do: @apply_team
1430
end

lib/groupher_server/cms/delegates/article_community.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
187187
@doc "update isEdited meta label if needed"
188188
# TODO: diff history
189189
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: _} = meta} = content) do
190-
meta = meta |> strip_struct |> Map.merge(%{is_edited: true})
190+
meta = meta |> Map.merge(%{is_edited: true})
191191
ORM.update_meta(content, meta)
192192
end
193193

lib/groupher_server/cms/delegates/community_curd.ex

+73-8
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
2424
Thread
2525
}
2626

27+
alias CMS.Constant
28+
2729
@default_meta Embeds.CommunityMeta.default_meta()
2830
@article_threads get_config(:article, :threads)
2931

30-
def read_community(clauses, user), do: read_community(clauses) |> viewer_has_states(user)
31-
def read_community(%{id: id}), do: ORM.read(Community, id, inc: :views)
32-
def read_community(%{raw: raw} = clauses), do: do_read_community(clauses, raw)
33-
def read_community(%{title: title} = clauses), do: do_read_community(clauses, title)
32+
@community_normal Constant.pending(:normal)
33+
@community_applying Constant.pending(:applying)
34+
35+
@default_apply_category Constant.apply_category(:public)
36+
37+
def read_community(raw, user), do: read_community(raw) |> viewer_has_states(user)
38+
def read_community(raw), do: do_read_community(raw)
3439

3540
@doc """
3641
create a community
@@ -54,6 +59,59 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5459
end
5560
end
5661

62+
@doc """
63+
check if community exist
64+
"""
65+
def is_community_exist?(raw) do
66+
case ORM.find_by(Community, raw: raw) do
67+
{:ok, _} -> {:ok, %{exist: true}}
68+
{:error, _} -> {:ok, %{exist: false}}
69+
end
70+
end
71+
72+
def has_pending_community_apply?(%User{} = user) do
73+
with {:ok, paged_applies} <- paged_community_applies(user, %{page: 1, size: 1}) do
74+
case paged_applies.total_count > 0 do
75+
true -> {:ok, %{exist: true}}
76+
false -> {:ok, %{exist: false}}
77+
end
78+
end
79+
end
80+
81+
def paged_community_applies(%User{} = user, %{page: page, size: size} = _filter) do
82+
Community
83+
|> where([c], c.pending == ^@community_applying)
84+
|> where([c], c.user_id == ^user.id)
85+
|> ORM.paginator(~m(page size)a)
86+
|> done
87+
end
88+
89+
def apply_community(args) do
90+
with {:ok, community} <- create_community(Map.merge(args, %{pending: @community_applying})) do
91+
apply_msg = Map.get(args, :apply_msg, "")
92+
apply_category = Map.get(args, :apply_category, @default_apply_category)
93+
94+
meta = community.meta |> Map.merge(~m(apply_msg apply_category)a)
95+
ORM.update_meta(community, meta)
96+
end
97+
end
98+
99+
def approve_community_apply(id) do
100+
# TODO: create community with thread, category and tags
101+
with {:ok, community} <- ORM.find(Community, id) do
102+
ORM.update(community, %{pending: @community_normal})
103+
end
104+
end
105+
106+
def deny_community_apply(id) do
107+
with {:ok, community} <- ORM.find(Community, id) do
108+
case community.pending == @community_applying do
109+
true -> ORM.delete(community)
110+
false -> {:ok, community}
111+
end
112+
end
113+
end
114+
57115
@doc """
58116
update editors_count of a community
59117
"""
@@ -235,13 +293,20 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
235293
end
236294
end
237295

238-
defp do_read_community(clauses, aka) do
239-
case ORM.read_by(Community, clauses, inc: :views) do
240-
{:ok, community} -> {:ok, community}
241-
{:error, _} -> ORM.find_by(Community, aka: aka)
296+
defp do_read_community(raw) do
297+
with {:ok, community} <- find_community(raw) do
298+
community |> ORM.read(inc: :views)
242299
end
243300
end
244301

302+
defp find_community(raw) do
303+
Community
304+
|> where([c], c.pending == ^@community_normal)
305+
|> where([c], c.raw == ^raw or c.aka == ^raw)
306+
|> Repo.one()
307+
|> done
308+
end
309+
245310
defp viewer_has_states({:ok, community}, %User{id: user_id}) do
246311
viewer_has_states = %{
247312
viewer_has_subscribed: user_id in community.meta.subscribed_user_ids,

lib/groupher_server/cms/delegates/document.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ defmodule GroupherServer.CMS.Delegate.Document do
33
CURD operation on post/job ...
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2]
6+
import Helper.Utils, only: [thread_of: 2]
77

88
import Helper.ErrorCode
9-
import ShortMaps
109

1110
alias Helper.{ORM, Converter}
1211
alias GroupherServer.{CMS, Repo}

lib/groupher_server/cms/delegates/search.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ defmodule GroupherServer.CMS.Delegate.Search do
3030

3131
defp do_search_communities(queryable, title) do
3232
queryable
33-
|> where([c], ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%"))
33+
|> where(
34+
[c],
35+
ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%") or ilike(c.aka, ^"%#{title}%")
36+
)
3437
|> ORM.paginator(page: 1, size: @search_items_count)
3538
|> done()
3639
end

lib/groupher_server/cms/models/article_document.ex

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do
88
use Accessible
99

1010
import Ecto.Changeset
11-
import GroupherServer.CMS.Helper.Macros
1211
import Helper.Utils, only: [get_config: 2]
1312

1413
@timestamps_opts [type: :utc_datetime_usec]

lib/groupher_server/cms/models/community.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Model.Community do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
alias GroupherServer.{Accounts, CMS}
@@ -22,7 +24,7 @@ defmodule GroupherServer.CMS.Model.Community do
2224

2325
@required_fields ~w(title desc user_id logo raw)a
2426
# @required_fields ~w(title desc user_id)a
25-
@optional_fields ~w(label geo_info index aka contributes_digest)a
27+
@optional_fields ~w(label geo_info index aka contributes_digest pending)a
2628

2729
def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread
2830

@@ -51,6 +53,8 @@ defmodule GroupherServer.CMS.Model.Community do
5153
field(:article_tags_count, :integer, default: 0)
5254
field(:threads_count, :integer, default: 0)
5355

56+
field(:pending, :integer, default: 0)
57+
5458
field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
5559
field(:viewer_is_editor, :boolean, default: false, virtual: true)
5660
field(:contributes_digest, {:array, :integer}, default: [])
@@ -81,8 +85,9 @@ defmodule GroupherServer.CMS.Model.Community do
8185
|> validate_required(@required_fields)
8286
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
8387
|> validate_length(:title, min: 1, max: 30)
88+
|> validate_length(:raw, min: 1, max: 30)
8489
|> foreign_key_constraint(:user_id)
85-
|> unique_constraint(:title, name: :communities_title_index)
90+
|> unique_constraint(:raw, name: :communities_raw_index)
8691
|> unique_constraint(:aka, name: :communities_aka_index)
8792

8893
# |> foreign_key_constraint(:communities_author_fkey)

lib/groupher_server/cms/models/embeds/community_meta.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do
3131
@general_options %{
3232
editors_ids: [],
3333
subscribed_user_ids: [],
34-
contributes_digest: []
34+
contributes_digest: [],
35+
apply_msg: "",
36+
apply_category: ""
3537
}
3638

3739
@optional_fields Map.keys(@general_options) ++
@@ -53,6 +55,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do
5355
# 关注相关
5456
field(:subscribed_user_ids, {:array, :integer}, default: [])
5557
field(:contributes_digest, {:array, :integer}, default: [])
58+
# 申请信息
59+
field(:apply_msg, :string, default: "")
60+
field(:apply_category, :string, default: "")
5661
end
5762

5863
def changeset(struct, params) do

lib/groupher_server_web/resolvers/cms_resolver.ex

+29-12
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,48 @@ defmodule GroupherServerWeb.Resolvers.CMS do
1414
# #######################
1515
# community ..
1616
# #######################
17-
def community(_root, args, %{context: %{cur_user: user}}) do
18-
case Enum.empty?(args) do
19-
false -> CMS.read_community(args, user)
20-
true -> {:error, "please provide community id or title or raw"}
21-
end
17+
def community(_root, %{raw: raw}, %{context: %{cur_user: user}}) do
18+
CMS.read_community(raw, user)
2219
end
2320

24-
def community(_root, args, _info) do
25-
case Enum.empty?(args) do
26-
false -> CMS.read_community(args)
27-
true -> {:error, "please provide community id or title or raw"}
28-
end
21+
def community(_root, %{raw: raw}, _info) do
22+
CMS.read_community(raw)
2923
end
3024

3125
def paged_communities(_root, ~m(filter)a, _info), do: Community |> ORM.find_all(filter)
3226

3327
def create_community(_root, args, %{context: %{cur_user: user}}) do
3428
args = args |> Map.merge(%{user_id: user.id})
35-
Community |> ORM.create(args)
29+
CMS.create_community(args)
3630
end
3731

38-
def update_community(_root, args, _info), do: Community |> ORM.find_update(args)
32+
def update_community(_root, args, _info) do
33+
CMS.update_community(args.id, args)
34+
end
3935

4036
def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete!(id)
4137

38+
def apply_community(_root, args, %{context: %{cur_user: user}}) do
39+
args = args |> Map.merge(%{user_id: user.id})
40+
CMS.apply_community(args)
41+
end
42+
43+
def approve_community_apply(_root, %{id: id}, _) do
44+
CMS.approve_community_apply(id)
45+
end
46+
47+
def deny_community_apply(_root, %{id: id}, _) do
48+
CMS.deny_community_apply(id)
49+
end
50+
51+
def is_community_exist?(_root, %{raw: raw}, _) do
52+
CMS.is_community_exist?(raw)
53+
end
54+
55+
def has_pending_community_apply?(_root, _, %{context: %{cur_user: user}}) do
56+
CMS.has_pending_community_apply?(user)
57+
end
58+
4259
# #######################
4360
# community thread (post, job), login user should be logged
4461
# #######################

lib/groupher_server_web/schema/cms/cms_queries.ex

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
1010
@desc "spec community info"
1111
field :community, :community do
1212
# arg(:id, non_null(:id))
13-
arg(:id, :id)
14-
arg(:title, :string)
15-
arg(:raw, :string)
13+
# arg(:title, :string)
14+
arg(:raw, non_null(:string))
1615
resolve(&R.CMS.community/3)
1716
end
1817

18+
@desc "if use has pending apply"
19+
field :has_pending_community_apply, :check_state do
20+
middleware(M.Authorize, :login)
21+
resolve(&R.CMS.has_pending_community_apply?/3)
22+
end
23+
24+
@desc "if the community exist or not"
25+
field :is_community_exist, :check_state do
26+
arg(:raw, non_null(:string))
27+
28+
middleware(M.Authorize, :login)
29+
resolve(&R.CMS.is_community_exist?/3)
30+
end
31+
1932
@desc "communities with pagination info"
2033
field :paged_communities, :paged_communities do
2134
arg(:filter, non_null(:communities_filter))

lib/groupher_server_web/schema/cms/cms_types.ex

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
1515

1616
import_types(Schema.CMS.Metrics)
1717

18+
object :check_state do
19+
field(:exist, :boolean)
20+
end
21+
1822
######
1923
# common stands for minimal info of the type
2024
# usually used in abuse_report, feeds, etc ..
@@ -24,6 +28,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
2428
field(:nickname, :string)
2529
field(:avatar, :string)
2630
field(:bio, :string)
31+
field(:shortbio, :string)
2732
end
2833

2934
object :common_article do
@@ -265,6 +270,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
265270
field(:viewer_has_subscribed, :boolean)
266271
field(:viewer_is_editor, :boolean)
267272

273+
field(:pending, :integer)
274+
268275
# TODO: remove
269276
field :threads_count, :integer do
270277
resolve(&R.CMS.threads_count/3)
@@ -461,5 +468,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
461468

462469
object :community_meta do
463470
threads_count_fields()
471+
field(:apply_msg, :string)
472+
field(:apply_category, :string)
464473
end
465474
end

0 commit comments

Comments
 (0)