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

refactor: apply community logic #441

Merged
merged 7 commits into from
Nov 22, 2021
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
6 changes: 6 additions & 0 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ defmodule GroupherServer.CMS do
defdelegate read_community(args, user), to: CommunityCURD
defdelegate create_community(args), to: CommunityCURD
defdelegate update_community(id, args), to: CommunityCURD
defdelegate apply_community(args), to: CommunityCURD
defdelegate approve_community_apply(id), to: CommunityCURD
defdelegate deny_community_apply(id), to: CommunityCURD
defdelegate is_community_exist?(raw), to: CommunityCURD
defdelegate has_pending_community_apply?(user), to: CommunityCURD

# >> editor ..
defdelegate update_editor(user, community, title), to: CommunityCURD
# >> geo info ..
Expand Down
16 changes: 16 additions & 0 deletions lib/groupher_server/cms/constant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@ defmodule GroupherServer.CMS.Constant do
@artiment_illegal 1
@artiment_audit_failed 2

@community_normal 0
@community_applying 1

@apply_public "PUBLIC"
@apply_city "CITY"
@apply_works "WORKS"
@apply_team "TEAM"

def pending(:legal), do: @artiment_legal
def pending(:illegal), do: @artiment_illegal
def pending(:audit_failed), do: @artiment_audit_failed

def pending(:normal), do: @community_normal
def pending(:applying), do: @community_applying

def apply_category(:public), do: @apply_public
def apply_category(:city), do: @apply_city
def apply_category(:works), do: @apply_works
def apply_category(:team), do: @apply_team
end
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/article_community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
@doc "update isEdited meta label if needed"
# TODO: diff history
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: _} = meta} = content) do
meta = meta |> strip_struct |> Map.merge(%{is_edited: true})
meta = meta |> Map.merge(%{is_edited: true})
ORM.update_meta(content, meta)
end

Expand Down
81 changes: 73 additions & 8 deletions lib/groupher_server/cms/delegates/community_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
Thread
}

alias CMS.Constant

@default_meta Embeds.CommunityMeta.default_meta()
@article_threads get_config(:article, :threads)

def read_community(clauses, user), do: read_community(clauses) |> viewer_has_states(user)
def read_community(%{id: id}), do: ORM.read(Community, id, inc: :views)
def read_community(%{raw: raw} = clauses), do: do_read_community(clauses, raw)
def read_community(%{title: title} = clauses), do: do_read_community(clauses, title)
@community_normal Constant.pending(:normal)
@community_applying Constant.pending(:applying)

@default_apply_category Constant.apply_category(:public)

def read_community(raw, user), do: read_community(raw) |> viewer_has_states(user)
def read_community(raw), do: do_read_community(raw)

@doc """
create a community
Expand All @@ -54,6 +59,59 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
end
end

@doc """
check if community exist
"""
def is_community_exist?(raw) do
case ORM.find_by(Community, raw: raw) do
{:ok, _} -> {:ok, %{exist: true}}
{:error, _} -> {:ok, %{exist: false}}
end
end

def has_pending_community_apply?(%User{} = user) do
with {:ok, paged_applies} <- paged_community_applies(user, %{page: 1, size: 1}) do
case paged_applies.total_count > 0 do
true -> {:ok, %{exist: true}}
false -> {:ok, %{exist: false}}
end
end
end

def paged_community_applies(%User{} = user, %{page: page, size: size} = _filter) do
Community
|> where([c], c.pending == ^@community_applying)
|> where([c], c.user_id == ^user.id)
|> ORM.paginator(~m(page size)a)
|> done
end

def apply_community(args) do
with {:ok, community} <- create_community(Map.merge(args, %{pending: @community_applying})) do
apply_msg = Map.get(args, :apply_msg, "")
apply_category = Map.get(args, :apply_category, @default_apply_category)

meta = community.meta |> Map.merge(~m(apply_msg apply_category)a)
ORM.update_meta(community, meta)
end
end

def approve_community_apply(id) do
# TODO: create community with thread, category and tags
with {:ok, community} <- ORM.find(Community, id) do
ORM.update(community, %{pending: @community_normal})
end
end

def deny_community_apply(id) do
with {:ok, community} <- ORM.find(Community, id) do
case community.pending == @community_applying do
true -> ORM.delete(community)
false -> {:ok, community}
end
end
end

@doc """
update editors_count of a community
"""
Expand Down Expand Up @@ -235,13 +293,20 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
end
end

defp do_read_community(clauses, aka) do
case ORM.read_by(Community, clauses, inc: :views) do
{:ok, community} -> {:ok, community}
{:error, _} -> ORM.find_by(Community, aka: aka)
defp do_read_community(raw) do
with {:ok, community} <- find_community(raw) do
community |> ORM.read(inc: :views)
end
end

defp find_community(raw) do
Community
|> where([c], c.pending == ^@community_normal)
|> where([c], c.raw == ^raw or c.aka == ^raw)
|> Repo.one()
|> done
end

defp viewer_has_states({:ok, community}, %User{id: user_id}) do
viewer_has_states = %{
viewer_has_subscribed: user_id in community.meta.subscribed_user_ids,
Expand Down
3 changes: 1 addition & 2 deletions lib/groupher_server/cms/delegates/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ defmodule GroupherServer.CMS.Delegate.Document do
CURD operation on post/job ...
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2]
import Helper.Utils, only: [thread_of: 2]

import Helper.ErrorCode
import ShortMaps

alias Helper.{ORM, Converter}
alias GroupherServer.{CMS, Repo}
Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server/cms/delegates/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ defmodule GroupherServer.CMS.Delegate.Search do

defp do_search_communities(queryable, title) do
queryable
|> where([c], ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%"))
|> where(
[c],
ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%") or ilike(c.aka, ^"%#{title}%")
)
|> ORM.paginator(page: 1, size: @search_items_count)
|> done()
end
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/models/article_document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do
use Accessible

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

@timestamps_opts [type: :utc_datetime_usec]
Expand Down
9 changes: 7 additions & 2 deletions lib/groupher_server/cms/models/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Model.Community do
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset

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

@required_fields ~w(title desc user_id logo raw)a
# @required_fields ~w(title desc user_id)a
@optional_fields ~w(label geo_info index aka contributes_digest)a
@optional_fields ~w(label geo_info index aka contributes_digest pending)a

def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread

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

field(:pending, :integer, default: 0)

field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
field(:viewer_is_editor, :boolean, default: false, virtual: true)
field(:contributes_digest, {:array, :integer}, default: [])
Expand Down Expand Up @@ -81,8 +85,9 @@ defmodule GroupherServer.CMS.Model.Community do
|> validate_required(@required_fields)
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
|> validate_length(:title, min: 1, max: 30)
|> validate_length(:raw, min: 1, max: 30)
|> foreign_key_constraint(:user_id)
|> unique_constraint(:title, name: :communities_title_index)
|> unique_constraint(:raw, name: :communities_raw_index)
|> unique_constraint(:aka, name: :communities_aka_index)

# |> foreign_key_constraint(:communities_author_fkey)
Expand Down
7 changes: 6 additions & 1 deletion lib/groupher_server/cms/models/embeds/community_meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do
@general_options %{
editors_ids: [],
subscribed_user_ids: [],
contributes_digest: []
contributes_digest: [],
apply_msg: "",
apply_category: ""
}

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

def changeset(struct, params) do
Expand Down
41 changes: 29 additions & 12 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,48 @@ defmodule GroupherServerWeb.Resolvers.CMS do
# #######################
# community ..
# #######################
def community(_root, args, %{context: %{cur_user: user}}) do
case Enum.empty?(args) do
false -> CMS.read_community(args, user)
true -> {:error, "please provide community id or title or raw"}
end
def community(_root, %{raw: raw}, %{context: %{cur_user: user}}) do
CMS.read_community(raw, user)
end

def community(_root, args, _info) do
case Enum.empty?(args) do
false -> CMS.read_community(args)
true -> {:error, "please provide community id or title or raw"}
end
def community(_root, %{raw: raw}, _info) do
CMS.read_community(raw)
end

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

def create_community(_root, args, %{context: %{cur_user: user}}) do
args = args |> Map.merge(%{user_id: user.id})
Community |> ORM.create(args)
CMS.create_community(args)
end

def update_community(_root, args, _info), do: Community |> ORM.find_update(args)
def update_community(_root, args, _info) do
CMS.update_community(args.id, args)
end

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

def apply_community(_root, args, %{context: %{cur_user: user}}) do
args = args |> Map.merge(%{user_id: user.id})
CMS.apply_community(args)
end

def approve_community_apply(_root, %{id: id}, _) do
CMS.approve_community_apply(id)
end

def deny_community_apply(_root, %{id: id}, _) do
CMS.deny_community_apply(id)
end

def is_community_exist?(_root, %{raw: raw}, _) do
CMS.is_community_exist?(raw)
end

def has_pending_community_apply?(_root, _, %{context: %{cur_user: user}}) do
CMS.has_pending_community_apply?(user)
end

# #######################
# community thread (post, job), login user should be logged
# #######################
Expand Down
19 changes: 16 additions & 3 deletions lib/groupher_server_web/schema/cms/cms_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
@desc "spec community info"
field :community, :community do
# arg(:id, non_null(:id))
arg(:id, :id)
arg(:title, :string)
arg(:raw, :string)
# arg(:title, :string)
arg(:raw, non_null(:string))
resolve(&R.CMS.community/3)
end

@desc "if use has pending apply"
field :has_pending_community_apply, :check_state do
middleware(M.Authorize, :login)
resolve(&R.CMS.has_pending_community_apply?/3)
end

@desc "if the community exist or not"
field :is_community_exist, :check_state do
arg(:raw, non_null(:string))

middleware(M.Authorize, :login)
resolve(&R.CMS.is_community_exist?/3)
end

@desc "communities with pagination info"
field :paged_communities, :paged_communities do
arg(:filter, non_null(:communities_filter))
Expand Down
9 changes: 9 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do

import_types(Schema.CMS.Metrics)

object :check_state do
field(:exist, :boolean)
end

######
# common stands for minimal info of the type
# usually used in abuse_report, feeds, etc ..
Expand All @@ -24,6 +28,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:nickname, :string)
field(:avatar, :string)
field(:bio, :string)
field(:shortbio, :string)
end

object :common_article do
Expand Down Expand Up @@ -265,6 +270,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:viewer_has_subscribed, :boolean)
field(:viewer_is_editor, :boolean)

field(:pending, :integer)

# TODO: remove
field :threads_count, :integer do
resolve(&R.CMS.threads_count/3)
Expand Down Expand Up @@ -461,5 +468,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do

object :community_meta do
threads_count_fields()
field(:apply_msg, :string)
field(:apply_category, :string)
end
end
Loading