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

Commit 37badea

Browse files
authored
chore(article-comments): missing support for repo (#359)
* chore(article-comments): missing repo tests * chore(article-comments): missing repo support in pinned comments * chore(article-comments): wip * chore(article-comments): wip * chore(article-comments): wip && clean up * chore(article-comments): wip && clean up * chore(article-comments): wip && clean up * fix(article-comments): repo pinned comment * fix(article-comments): rename pined -> pinned * fix(article-comments): rename pined -> pinned * fix(article-comments): rename pined -> pinned
1 parent adee4f2 commit 37badea

File tree

59 files changed

+1008
-338
lines changed

Some content is hidden

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

59 files changed

+1008
-338
lines changed

lib/groupher_server/cms/article_comment.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule GroupherServer.CMS.ArticleComment do
3333
@report_threshold_for_fold 5
3434

3535
# 每篇文章最多含有置顶评论的条数
36-
@pined_comment_limit 10
36+
@pinned_comment_limit 10
3737

3838
@doc "latest participators stores in article article_comment_participators field"
3939
def max_participator_count(), do: @max_participator_count
@@ -47,7 +47,7 @@ defmodule GroupherServer.CMS.ArticleComment do
4747
def delete_hint(), do: @delete_hint
4848

4949
def report_threshold_for_fold, do: @report_threshold_for_fold
50-
def pined_comment_limit, do: @pined_comment_limit
50+
def pinned_comment_limit, do: @pinned_comment_limit
5151

5252
@type t :: %ArticleComment{}
5353
schema "articles_comments" do

lib/groupher_server/cms/article_pined_comment.ex

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule GroupherServer.CMS.ArticlePinnedComment do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
use Accessible
7+
8+
import Ecto.Changeset
9+
10+
alias GroupherServer.CMS
11+
alias CMS.ArticleComment
12+
13+
# alias Helper.HTML
14+
15+
@required_fields ~w(article_comment_id)a
16+
@optional_fields ~w(post_id job_id repo_id)a
17+
18+
@type t :: %ArticlePinnedComment{}
19+
schema "articles_pinned_comments" do
20+
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
21+
belongs_to(:post, CMS.Post, foreign_key: :post_id)
22+
belongs_to(:job, CMS.Job, foreign_key: :job_id)
23+
belongs_to(:repo, CMS.Repo, foreign_key: :repo_id)
24+
25+
timestamps(type: :utc_datetime)
26+
end
27+
28+
@doc false
29+
def changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
30+
article_pined_comment
31+
|> cast(attrs, @required_fields ++ @optional_fields)
32+
|> validate_required(@required_fields)
33+
end
34+
35+
# @doc false
36+
def update_changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
37+
article_pined_comment
38+
|> cast(attrs, @required_fields ++ @optional_fields)
39+
end
40+
end

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, get_config: 2]
6+
import Helper.Utils, only: [done: 1]
77
import Helper.ErrorCode
88

99
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
@@ -15,16 +15,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
1515
alias GroupherServer.{Accounts, CMS, Repo}
1616

1717
alias Accounts.User
18-
alias CMS.{ArticleComment, ArticlePinedComment, Embeds}
18+
alias CMS.{ArticleComment, ArticlePinnedComment, Embeds}
1919
alias Ecto.Multi
2020

21-
@supported_emotions get_config(:article, :comment_supported_emotions)
2221
@max_participator_count ArticleComment.max_participator_count()
2322
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
2423
@delete_hint ArticleComment.delete_hint()
2524

2625
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
27-
@pined_comment_limit ArticleComment.pined_comment_limit()
26+
@pinned_comment_limit ArticleComment.pinned_comment_limit()
2827

2928
@doc """
3029
[timeline-mode] list paged article comments
@@ -69,7 +68,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
6968
do_paged_comment_replies(comment_id, filters, user)
7069
end
7170

72-
@spec paged_article_comments_participators(T.comment_thread(), Integer.t(), T.paged_filter()) ::
71+
@spec paged_article_comments_participators(T.article_thread(), Integer.t(), T.paged_filter()) ::
7372
{:ok, T.paged_users()}
7473
def paged_article_comments_participators(thread, article_id, filters) do
7574
%{page: page, size: size} = filters
@@ -125,7 +124,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
125124
update_article_comments_count(comment, :dec)
126125
end)
127126
|> Multi.run(:remove_pined_comment, fn _, _ ->
128-
ORM.findby_delete(ArticlePinedComment, %{article_comment_id: comment.id})
127+
ORM.findby_delete(ArticlePinnedComment, %{article_comment_id: comment.id})
129128
end)
130129
|> Multi.run(:delete_article_comment, fn _, _ ->
131130
ORM.update(comment, %{body_html: @delete_hint, is_deleted: true})
@@ -236,7 +235,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
236235
}) do
237236
with {:ok, info} <- match(thread),
238237
query <-
239-
from(p in ArticlePinedComment,
238+
from(p in ArticlePinnedComment,
240239
join: c in ArticleComment,
241240
on: p.article_comment_id == c.id,
242241
where: field(p, ^info.foreign_key) == ^article_id,
@@ -249,17 +248,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
249248

250249
_ ->
251250
preloaded_pined_comments =
252-
Enum.slice(pined_comments, 0, @pined_comment_limit)
251+
Enum.slice(pined_comments, 0, @pinned_comment_limit)
253252
|> Repo.preload(reply_to: :author)
254253

255-
updated_entries = Enum.concat(preloaded_pined_comments, entries)
256-
254+
entries = Enum.concat(preloaded_pined_comments, entries)
257255
pined_comment_count = length(pined_comments)
258256

259-
Map.merge(paged_comments, %{
260-
entries: updated_entries,
261-
total_count: paged_comments.total_count + pined_comment_count
262-
})
257+
total_count = paged_comments.total_count + pined_comment_count
258+
paged_comments |> Map.merge(%{entries: entries, total_count: total_count})
263259
end
264260
end
265261
end

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
1919

2020
alias CMS.{
2121
ArticleComment,
22-
ArticlePinedComment,
22+
ArticlePinnedComment,
2323
ArticleCommentUpvote,
2424
ArticleCommentReply,
25+
Community,
2526
# TODO: remove spec type
2627
Post,
2728
Job
2829
}
2930

3031
alias Ecto.Multi
3132

33+
@article_threads Community.article_threads()
3234
@max_parent_replies_count ArticleComment.max_parent_replies_count()
33-
@pined_comment_limit ArticleComment.pined_comment_limit()
35+
@pinned_comment_limit ArticleComment.pinned_comment_limit()
3436

3537
@spec pin_article_comment(Integer.t()) :: {:ok, ArticleComment.t()}
3638
@doc "pin a comment"
@@ -41,22 +43,25 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
4143
Multi.new()
4244
|> Multi.run(:checked_pined_comments_count, fn _, _ ->
4345
count_query =
44-
from(p in ArticlePinedComment,
46+
from(p in ArticlePinnedComment,
4547
where: field(p, ^info.foreign_key) == ^full_comment.article.id
4648
)
4749

4850
pined_comments_count = Repo.aggregate(count_query, :count)
4951

50-
case pined_comments_count >= @pined_comment_limit do
51-
true -> {:error, "only support #{@pined_comment_limit} pined comment for each article"}
52-
false -> {:ok, :pass}
52+
case pined_comments_count >= @pinned_comment_limit do
53+
true ->
54+
{:error, "only support #{@pinned_comment_limit} pinned comment for each article"}
55+
56+
false ->
57+
{:ok, :pass}
5358
end
5459
end)
5560
|> Multi.run(:update_comment_flag, fn _, _ ->
5661
ORM.update(comment, %{is_pinned: true})
5762
end)
5863
|> Multi.run(:add_pined_comment, fn _, _ ->
59-
ArticlePinedComment
64+
ArticlePinnedComment
6065
|> ORM.create(
6166
%{article_comment_id: comment.id}
6267
|> Map.put(info.foreign_key, full_comment.article.id)
@@ -74,7 +79,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
7479
ORM.update(comment, %{is_pinned: false})
7580
end)
7681
|> Multi.run(:remove_pined_comment, fn _, _ ->
77-
ORM.findby_delete(ArticlePinedComment, %{article_comment_id: comment.id})
82+
ORM.findby_delete(ArticlePinnedComment, %{article_comment_id: comment.id})
7883
end)
7984
|> Repo.transaction()
8085
|> result()
@@ -256,9 +261,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
256261
end
257262
end
258263

264+
defp get_article(%ArticleComment{repo_id: repo_id} = comment) when not is_nil(repo_id) do
265+
with {:ok, article} <- ORM.find(CMS.Repo, comment.repo_id, preload: [author: :user]) do
266+
{:repo, article}
267+
end
268+
end
269+
259270
@spec get_full_comment(String.t()) :: {:ok, T.article_info()} | {:error, nil}
260271
defp get_full_comment(comment_id) do
261-
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: :post, preload: :job)
272+
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: ^@article_threads)
262273

263274
with {:ok, comment} <- Repo.one(query) |> done() do
264275
extract_article_info(comment)
@@ -273,6 +284,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
273284
do_extract_article_info(:job, job)
274285
end
275286

287+
defp extract_article_info(%ArticleComment{repo: %CMS.Repo{} = repo}) when not is_nil(repo) do
288+
do_extract_article_info(:repo, repo)
289+
end
290+
276291
@spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()}
277292
defp do_extract_article_info(thread, article) do
278293
with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(),

lib/groupher_server/cms/delegates/article_community.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
77
import Ecto.Query, warn: false
88

99
import Helper.ErrorCode
10-
import ShortMaps
1110
import Helper.Utils, only: [strip_struct: 1, done: 1]
1211
import GroupherServer.CMS.Helper.Matcher2
1312

@@ -202,7 +201,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
202201

203202
def lock_article_comment(content), do: {:ok, content}
204203

205-
# check if the thread has aready enough pined articles
204+
# check if the thread has aready enough pinned articles
206205
defp check_pinned_article_count(community_id, thread) do
207206
thread_upcase = thread |> to_string |> String.upcase()
208207

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
305305
|> join(:inner, [p], article in assoc(p, ^thread))
306306
|> where([p, c, article], c.raw == ^community)
307307
|> select([p, c, article], article)
308-
# 10 pined articles per community/thread, at most
308+
# 10 pinned articles per community/thread, at most
309309
|> ORM.find_all(%{page: 1, size: 10}) do
310310
concat_articles(pinned_articles, articles)
311311
else
@@ -334,7 +334,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
334334

335335
normal_count = non_pinned_articles |> Map.get(:total_count)
336336

337-
# remote the pined article from normal_entries (if have)
337+
# remote the pinned article from normal_entries (if have)
338338
pind_ids = pick_by(pinned_entries, :id)
339339
normal_entries = Enum.reject(normal_entries, &(&1.id in pind_ids))
340340

lib/groupher_server/cms/post.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule GroupherServer.CMS.Post do
1313
Embeds,
1414
Author,
1515
ArticleComment,
16-
ArticlePinedComment,
1716
Community,
1817
PostComment,
1918
Tag,
@@ -63,7 +62,6 @@ defmodule GroupherServer.CMS.Post do
6362
has_many(:comments, {"posts_comments", PostComment})
6463

6564
has_many(:article_comments, {"articles_comments", ArticleComment})
66-
has_many(:article_pined_comments, {"articles_pined_comments", ArticlePinedComment})
6765
field(:article_comments_count, :integer, default: 0)
6866
field(:article_comments_participators_count, :integer, default: 0)
6967
# 评论参与者,只保留最近 5 个

lib/groupher_server/cms/repo.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ defmodule GroupherServer.CMS.Repo do
77

88
import Ecto.Changeset
99

10-
alias GroupherServer.CMS
10+
alias GroupherServer.{CMS, Accounts}
1111

1212
alias CMS.{
1313
Author,
1414
Embeds,
15+
ArticleComment,
1516
Community,
1617
RepoContributor,
1718
RepoLang,
@@ -24,7 +25,7 @@ defmodule GroupherServer.CMS.Repo do
2425

2526
@timestamps_opts [type: :utc_datetime_usec]
2627
@required_fields ~w(title owner_name owner_url repo_url desc readme star_count issues_count prs_count fork_count watch_count)a
27-
@optional_fields ~w(original_community_id last_sync homepage_url release_tag license upvotes_count collects_count mark_delete)a
28+
@optional_fields ~w(original_community_id last_sync homepage_url release_tag license upvotes_count collects_count mark_delete article_comments_count article_comments_participators_count)a
2829

2930
@type t :: %Repo{}
3031
schema "cms_repos" do
@@ -73,6 +74,12 @@ defmodule GroupherServer.CMS.Repo do
7374

7475
field(:last_sync, :utc_datetime)
7576

77+
has_many(:article_comments, {"articles_comments", ArticleComment})
78+
field(:article_comments_count, :integer, default: 0)
79+
field(:article_comments_participators_count, :integer, default: 0)
80+
# 评论参与者,只保留最近 5 个
81+
embeds_many(:article_comments_participators, Accounts.User, on_replace: :delete)
82+
7683
many_to_many(
7784
:tags,
7885
Tag,

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
178178
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
179179
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
180180

181+
article_comments_fields()
181182
viewer_has_state_fields()
182183
# comments_count
183184
# comments_participators

0 commit comments

Comments
 (0)