Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
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
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ config :groupher_server, :customization,
sidebar_communities_index: %{}

config :groupher_server, :article,
emotionable_threads: [:post, :job],
# NOTE: if you want to add/remove emotion, just edit the list below
# and migrate the field to table "articles_users_emotions"
supported_emotions: [
Expand Down
16 changes: 5 additions & 11 deletions lib/groupher_server/accounts/delegates/collect_folder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
CMS.set_collect_folder(article_collect, folder)
end)
|> Repo.transaction()
|> upsert_collect_folder_result()
|> result()
end
end

Expand Down Expand Up @@ -183,7 +183,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
CMS.undo_set_collect_folder(article_collect, folder)
end)
|> Repo.transaction()
|> upsert_collect_folder_result()
|> result()
end
end

Expand Down Expand Up @@ -249,13 +249,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do

defp filter_thread_ifneed(query, _), do: query

defp upsert_collect_folder_result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result}

defp upsert_collect_folder_result({:ok, %{rm_from_collect_folder: result}}) do
{:ok, result}
end

defp upsert_collect_folder_result({:error, _, result, _steps}) do
{:error, result}
end
defp result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result}
defp result({:ok, %{rm_from_collect_folder: result}}), do: {:ok, result}
defp result({:error, _, result, _steps}), do: {:error, result}
end
20 changes: 13 additions & 7 deletions lib/groupher_server/cms/abuse_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ defmodule GroupherServer.CMS.AbuseReport do
use Ecto.Schema
use Accessible
import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}
alias CMS.{ArticleComment, Embeds, Post, Job, Repo}
alias CMS.{ArticleComment, Embeds}

@article_threads CMS.Community.article_threads()

# @required_fields ~w(article_comment_id user_id recived_user_id)a
@optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with report_cases_count)a
@optional_fields ~w(article_comment_id account_id operate_user_id deal_with report_cases_count)a
@update_fields ~w(operate_user_id deal_with report_cases_count)a

@article_fields @article_threads |> Enum.map(&:"#{&1}_id")

@type t :: %AbuseReport{}
schema "abuse_reports" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:repo, Repo, foreign_key: :repo_id)
belongs_to(:account, Accounts.User, foreign_key: :account_id)

embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)
Expand All @@ -28,19 +31,22 @@ defmodule GroupherServer.CMS.AbuseReport do

field(:deal_with, :string)

article_belongs_to()
timestamps(type: :utc_datetime)
end

@doc false
def changeset(%AbuseReport{} = struct, attrs) do
struct
|> cast(attrs, @optional_fields)
|> cast(attrs, @optional_fields ++ @article_fields)
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
|> articles_foreign_key_constraint
end

def update_changeset(%AbuseReport{} = struct, attrs) do
struct
|> cast(attrs, @update_fields)
|> cast(attrs, @update_fields ++ @article_fields)
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
|> articles_foreign_key_constraint
end
end
22 changes: 10 additions & 12 deletions lib/groupher_server/cms/article_collect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,36 @@ defmodule GroupherServer.CMS.ArticleCollect do

use Ecto.Schema
import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}

alias Accounts.{User, CollectFolder}
alias CMS.{Post, Job, Repo}

@article_threads CMS.Community.article_threads()

@required_fields ~w(user_id)a
@optional_fields ~w(thread post_id job_id repo_id)a
@optional_fields ~w(thread)a

@article_fields @article_threads |> Enum.map(&:"#{&1}_id")

@type t :: %ArticleCollect{}
schema "article_collects" do
field(:thread, :string)

belongs_to(:user, User, foreign_key: :user_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:repo, Repo, foreign_key: :repo_id)

embeds_many(:collect_folders, CollectFolder, on_replace: :delete)

article_belongs_to()
timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleCollect{} = article_collect, attrs) do
article_collect
|> cast(attrs, @optional_fields ++ @required_fields)
|> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields)
|> validate_required(@required_fields)
|> cast_embed(:collect_folders, with: &CollectFolder.changeset/2)
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:post_id)
|> foreign_key_constraint(:job_id)
|> foreign_key_constraint(:repo_id)
|> articles_foreign_key_constraint
end
end
26 changes: 11 additions & 15 deletions lib/groupher_server/cms/article_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ defmodule GroupherServer.CMS.ArticleComment do
use Accessible

import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}

alias CMS.{
Post,
Job,
Repo,
Embeds,
ArticleCommentUpvote
}
alias CMS.{Embeds, ArticleCommentUpvote}

# alias Helper.HTML
@article_threads CMS.Community.article_threads()

@required_fields ~w(body_html author_id)a
@optional_fields ~w(post_id job_id repo_id reply_to_id replies_count is_folded is_deleted floor is_article_author)a
@optional_fields ~w(reply_to_id replies_count is_folded is_deleted floor is_article_author)a
@updatable_fields ~w(is_folded is_deleted floor upvotes_count is_pinned)a

@article_fields @article_threads |> Enum.map(&:"#{&1}_id")

@max_participator_count 5
@max_parent_replies_count 3

Expand Down Expand Up @@ -69,10 +67,6 @@ defmodule GroupherServer.CMS.ArticleComment do
field(:is_pinned, :boolean, default: false)
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)

belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:repo, Repo, foreign_key: :repo_id)

belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)

embeds_many(:replies, ArticleComment, on_replace: :delete)
Expand All @@ -83,13 +77,14 @@ defmodule GroupherServer.CMS.ArticleComment do

has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote})

article_belongs_to()
timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleComment{} = article_comment, attrs) do
article_comment
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast(attrs, @required_fields ++ @optional_fields ++ @article_fields)
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
|> validate_required(@required_fields)
Expand All @@ -99,14 +94,15 @@ defmodule GroupherServer.CMS.ArticleComment do
# @doc false
def update_changeset(%ArticleComment{} = article_comment, attrs) do
article_comment
|> cast(attrs, @required_fields ++ @updatable_fields)
|> cast(attrs, @required_fields ++ @updatable_fields ++ @article_fields)
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
|> generl_changeset
end

defp generl_changeset(content) do
content
|> foreign_key_constraint(:author_id)
|> articles_foreign_key_constraint

# |> validate_length(:body_html, min: 3, max: 2000)
# |> HTML.safe_string(:body_html)
Expand Down
17 changes: 11 additions & 6 deletions lib/groupher_server/cms/article_pinned_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,40 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do
use Accessible

import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.CMS
alias CMS.ArticleComment

# alias Helper.HTML
@article_threads CMS.Community.article_threads()

@required_fields ~w(article_comment_id)a
@optional_fields ~w(post_id job_id repo_id)a
# @optional_fields ~w(post_id job_id repo_id)a

@article_fields @article_threads |> Enum.map(&:"#{&1}_id")

@type t :: %ArticlePinnedComment{}
schema "articles_pinned_comments" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:post, CMS.Post, foreign_key: :post_id)
belongs_to(:job, CMS.Job, foreign_key: :job_id)
belongs_to(:repo, CMS.Repo, foreign_key: :repo_id)

article_belongs_to()
timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
article_pined_comment
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast(attrs, @required_fields ++ @article_fields)
|> validate_required(@required_fields)
|> articles_foreign_key_constraint
end

# @doc false
def update_changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
article_pined_comment
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast(attrs, @required_fields ++ @article_fields)
|> articles_foreign_key_constraint
end
end
20 changes: 9 additions & 11 deletions lib/groupher_server/cms/article_upvote.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@ defmodule GroupherServer.CMS.ArticleUpvote do

use Ecto.Schema
import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}

alias Accounts.User
alias CMS.{Post, Job, Repo}

@article_threads CMS.Community.article_threads()

@required_fields ~w(user_id)a
@optional_fields ~w(thread post_id job_id repo_id)a
@optional_fields ~w(thread)a
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")

@type t :: %ArticleUpvote{}
schema "article_upvotes" do
# for user-center to filter
field(:thread, :string)

belongs_to(:user, User, foreign_key: :user_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:repo, Repo, foreign_key: :repo_id)

article_belongs_to()
timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleUpvote{} = article_upvote, attrs) do
article_upvote
|> cast(attrs, @optional_fields ++ @required_fields)
|> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:post_id)
|> foreign_key_constraint(:job_id)
|> foreign_key_constraint(:repo_id)
|> articles_foreign_key_constraint
end
end
16 changes: 7 additions & 9 deletions lib/groupher_server/cms/article_user_emotion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
import Ecto.Changeset
import GroupherServer.CMS.ArticleUserEmotion.Macros
import Helper.Utils, only: [get_config: 2]
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}
alias CMS.{Post, Job}

@supported_emotions get_config(:article, :supported_emotions)
@supported_threads get_config(:article, :emotionable_threads)
@article_threads CMS.Community.article_threads()

@required_fields ~w(user_id recived_user_id)a
@optional_fields Enum.map(@supported_threads, &:"#{&1}_id") ++
@optional_fields Enum.map(@article_threads, &:"#{&1}_id") ++
Enum.map(@supported_emotions, &:"#{&1}")

@type t :: %ArticleUserEmotion{}
schema "articles_users_emotions" do
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id)
belongs_to(:user, Accounts.User, foreign_key: :user_id)

emotion_fields()
article_belongs_to()
timestamps(type: :utc_datetime)
end

Expand All @@ -50,19 +50,17 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
struct
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:post_id)
|> foreign_key_constraint(:job_id)
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:recived_user_id)
|> articles_foreign_key_constraint
end

def update_changeset(%ArticleUserEmotion{} = struct, attrs) do
struct
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:post_id)
|> foreign_key_constraint(:job_id)
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:recived_user_id)
|> articles_foreign_key_constraint
end
end
30 changes: 30 additions & 0 deletions lib/groupher_server/cms/helper/macros.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule GroupherServer.CMS.Helper.Macros do
@moduledoc """
macros for define article related fields in CMS models
"""

alias GroupherServer.CMS

@article_threads CMS.Community.article_threads()

@doc """
generate belongs to fields for given thread

e.g:
belongs_to(:post, Post, foreign_key: :post_id)

NOTE: should do migration to DB manually
"""
defmacro article_belongs_to() do
@article_threads
|> Enum.map(fn thread ->
quote do
belongs_to(
unquote(thread),
Module.concat(CMS, unquote(thread) |> to_string |> Recase.to_pascal()),
foreign_key: unquote(:"#{thread}_id")
)
end
end)
end
end
Loading