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

Commit c9d52a3

Browse files
committed
feat(article-emotions): setup && re-org config
1 parent 9b5139b commit c9d52a3

File tree

9 files changed

+115
-14
lines changed

9 files changed

+115
-14
lines changed

config/config.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ config :groupher_server, :customization,
6060
display_density: "20",
6161
sidebar_communities_index: %{}
6262

63+
config :groupher_server, :article,
64+
supported_emotions: [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill, :popcorn],
65+
# NOTE: if you want to add/remove emotion, just edit the list below
66+
# and migrate the field to table "articles_comments_users_emotions"
67+
comment_supported_emotions: [
68+
:downvote,
69+
:beer,
70+
:heart,
71+
:biceps,
72+
:orz,
73+
:confused,
74+
:pill,
75+
:popcorn
76+
]
77+
6378
config :groupher_server, GroupherServerWeb.Gettext, default_locale: "zh_CN", locales: ~w(en zh_CN)
6479

6580
config :groupher_server, :cloud_assets,

lib/groupher_server/cms/article_comment.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ defmodule GroupherServer.CMS.ArticleComment do
2525
@max_participator_count 5
2626
@max_parent_replies_count 3
2727

28-
# NOTE: if you want to add/remove emotion, just edit the list below
29-
# and migrate the field to table "articles_comments_users_emotions"
30-
@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill, :popcorn]
3128
@max_latest_emotion_users_count 5
3229

3330
@delete_hint "this comment is deleted"

lib/groupher_server/cms/article_comment_user_emotion.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
defmodule GroupherServer.CMS.ArticleCommentUserEmotion.Macros do
2+
import Helper.Utils, only: [get_config: 2]
3+
24
alias GroupherServer.CMS
35
alias CMS.ArticleComment
46

5-
@supported_emotions ArticleComment.supported_emotions()
7+
@supported_emotions get_config(:article, :comment_supported_emotions)
68

79
defmacro emotion_fields() do
810
@supported_emotions
@@ -21,11 +23,12 @@ defmodule GroupherServer.CMS.ArticleCommentUserEmotion do
2123
use Ecto.Schema
2224
import Ecto.Changeset
2325
import GroupherServer.CMS.ArticleCommentUserEmotion.Macros
26+
import Helper.Utils, only: [get_config: 2]
2427

2528
alias GroupherServer.{Accounts, CMS}
2629
alias CMS.ArticleComment
2730

28-
@supported_emotions ArticleComment.supported_emotions()
31+
@supported_emotions get_config(:article, :comment_supported_emotions)
2932

3033
@required_fields ~w(article_comment_id user_id recived_user_id)a
3134
# @optional_fields ~w(downvote beer heart biceps orz confused pill)a

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
88

99
import GroupherServer.CMS.Helper.Matcher2
1010
import ShortMaps
11+
import Helper.Utils, only: [get_config: 2]
1112

1213
alias Helper.Types, as: T
1314
alias Helper.{ORM, QueryBuilder}
@@ -17,9 +18,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
1718
alias CMS.{ArticleComment, ArticlePinedComment, Embeds}
1819
alias Ecto.Multi
1920

21+
@supported_emotions get_config(:article, :comment_supported_emotions)
2022
@max_participator_count ArticleComment.max_participator_count()
2123
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
22-
@supported_emotions ArticleComment.supported_emotions()
2324
@delete_hint ArticleComment.delete_hint()
2425

2526
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()

lib/groupher_server/cms/embeds/article_comment_emotion.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentEmotion.Macros do
88
field(:viewer_has_beered, :boolean, default: false, virtual: true)
99
embeds_many(:latest_beer_users, Embeds.User, on_replace: :delete)
1010
"""
11+
import Helper.Utils, only: [get_config: 2]
12+
1113
alias GroupherServer.CMS
12-
alias CMS.{ArticleComment, Embeds}
14+
alias CMS.Embeds
1315

14-
@supported_emotions ArticleComment.supported_emotions()
16+
@supported_emotions get_config(:article, :comment_supported_emotions)
1517

1618
defmacro emotion_fields() do
1719
@supported_emotions
@@ -35,11 +37,9 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentEmotion do
3537

3638
import Ecto.Changeset
3739
import GroupherServer.CMS.Embeds.ArticleCommentEmotion.Macros
40+
import Helper.Utils, only: [get_config: 2]
3841

39-
alias GroupherServer.CMS.ArticleComment
40-
41-
@supported_emotions ArticleComment.supported_emotions()
42-
42+
@supported_emotions get_config(:article, :comment_supported_emotions)
4343
@optional_fields Enum.map(@supported_emotions, &:"#{&1}_count") ++
4444
Enum.map(@supported_emotions, &:"#{&1}_user_logins")
4545

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
defmodule GroupherServer.CMS.Embeds.ArticleEmotion.Macros do
2+
@moduledoc """
3+
general fields for each emotion
4+
5+
e.g:
6+
field(:beer_count, :integer, default: 0)
7+
field(:beer_user_logins, :string)
8+
field(:viewer_has_beered, :boolean, default: false, virtual: true)
9+
embeds_many(:latest_beer_users, Embeds.User, on_replace: :delete)
10+
"""
11+
import Helper.Utils, only: [get_config: 2]
12+
13+
alias GroupherServer.CMS
14+
alias CMS.Embeds
15+
16+
@supported_emotions get_config(:article, :supported_emotions)
17+
18+
defmacro emotion_fields() do
19+
@supported_emotions
20+
|> Enum.map(fn emotion ->
21+
quote do
22+
field(unquote(:"#{emotion}_count"), :integer, default: 0)
23+
field(unquote(:"#{emotion}_user_logins"), {:array, :string}, default: [])
24+
field(unquote(:"viewer_has_#{emotion}ed"), :boolean, default: false, virtual: true)
25+
embeds_many(unquote(:"latest_#{emotion}_users"), Embeds.User, on_replace: :delete)
26+
end
27+
end)
28+
end
29+
end
30+
31+
defmodule GroupherServer.CMS.Embeds.ArticleEmotion do
32+
@moduledoc """
33+
general article meta info for article-like content, like post, job, works ...
34+
"""
35+
use Ecto.Schema
36+
use Accessible
37+
38+
import Ecto.Changeset
39+
import GroupherServer.CMS.Embeds.ArticleEmotion.Macros
40+
import Helper.Utils, only: [get_config: 2]
41+
42+
@supported_emotions get_config(:article, :supported_emotions)
43+
44+
@optional_fields Enum.map(@supported_emotions, &:"#{&1}_count") ++
45+
Enum.map(@supported_emotions, &:"#{&1}_user_logins")
46+
47+
@doc "default emotion status for article comment"
48+
# for create comment and test usage
49+
def default_emotions() do
50+
@supported_emotions
51+
|> Enum.reduce([], fn emotion, acc ->
52+
acc ++
53+
[
54+
"#{emotion}_count": 0,
55+
"latest_#{emotion}_users": [],
56+
"#{emotion}_user_logins": [],
57+
"viewer_has_#{emotion}ed": false
58+
]
59+
end)
60+
|> Enum.into(%{})
61+
end
62+
63+
embedded_schema do
64+
emotion_fields()
65+
end
66+
67+
def changeset(struct, params) do
68+
struct
69+
|> cast(params, @optional_fields)
70+
71+
# |> cast_embed(:latest_downvote_users, required: false, with: &Embeds.User.changeset/2)
72+
# |> ...
73+
end
74+
end

lib/groupher_server/cms/post.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ defmodule GroupherServer.CMS.Post do
7272
# 评论参与者,只保留最近 5 个
7373
embeds_many(:article_comments_participators, Accounts.User, on_replace: :delete)
7474

75+
#
76+
embeds_one(:emotions, Embeds.ArticleEmotion, on_replace: :update)
77+
7578
# The keys are inflected from the schema names!
7679
# see https://hexdocs.pm/ecto/Ecto.Schema.html
7780
many_to_many(

lib/groupher_server_web/schema/Helper/fields.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
55
import Helper.Utils, only: [get_config: 2]
66

77
alias GroupherServer.{Accounts, CMS}
8-
alias CMS.{ArticleComment}
98

109
@page_size get_config(:general, :page_size)
11-
@supported_emotions ArticleComment.supported_emotions()
10+
@supported_emotions get_config(:article, :comment_supported_emotions)
1211
@supported_collect_folder_threads Accounts.CollectFolder.supported_threads()
1312

1413
defmacro timestamp_fields do
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddEmotionsToPost do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:cms_posts) do
6+
add(:emotions, :map)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)