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

Commit 32316c4

Browse files
committed
refactor(viewer-upvote): comment wip
1 parent 2c40202 commit 32316c4

File tree

9 files changed

+64
-57
lines changed

9 files changed

+64
-57
lines changed

lib/groupher_server/accounts/delegates/collect_folder.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
208208
folder
209209
|> Ecto.Changeset.change(%{total_count: total_count, last_updated: last_updated})
210210
|> Ecto.Changeset.put_embed(:collects, collects)
211-
|> Ecto.Changeset.put_embed(:meta, meta)
212-
|> Repo.update()
211+
|> ORM.update_meta(meta)
213212
end
214213

215214
# check if the article is already in this folder

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 19 additions & 1 deletion
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]
6+
import Helper.Utils, only: [done: 1, strip_struct: 1]
77
import Helper.ErrorCode
88

99
import GroupherServer.CMS.Utils.Matcher2
@@ -363,11 +363,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
363363
def upvote_article_comment(comment_id, %User{id: user_id}) do
364364
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
365365
false <- comment.is_deleted do
366+
# TODO: is user upvoted before?
366367
# IO.inspect(comment, label: "the comment")
367368
Multi.new()
368369
|> Multi.run(:create_comment_upvote, fn _, _ ->
369370
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
370371
end)
372+
|> Multi.run(:add_upvoted_user, fn _, _ ->
373+
update_upvoted_user_list(comment, user_id, :add)
374+
end)
371375
|> Multi.run(:inc_upvotes_count, fn _, _ ->
372376
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment.id)
373377
upvotes_count = Repo.aggregate(count_query, :count)
@@ -381,6 +385,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
381385
end
382386
end
383387

388+
defp update_upvoted_user_list(comment, user_id, opt) do
389+
IO.inspect(comment.meta, label: "update_upvoted_user_list meta")
390+
cur_user_ids = get_in(comment, [:meta, :upvoted_user_ids])
391+
392+
user_ids =
393+
case opt do
394+
:add -> [user_id] ++ cur_user_ids
395+
:remove -> cur_user_ids -- [user_id]
396+
end
397+
398+
meta = comment.meta |> Map.merge(%{upvoted_user_ids: user_ids}) |> strip_struct
399+
ORM.update_meta(comment, meta)
400+
end
401+
384402
@doc "upvote a comment"
385403
def undo_upvote_article_comment(comment_id, %User{id: user_id}) do
386404
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
77
import GroupherServer.CMS.Utils.Matcher2
88
import GroupherServer.CMS.Utils.Matcher, only: [match_action: 2]
99

10-
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1]
10+
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1]
1111
import Helper.ErrorCode
1212

1313
alias Helper.{Later, ORM}
@@ -443,16 +443,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
443443

444444
defp exec_update_tags(_content, _), do: {:ok, :pass}
445445

446-
# TODO: move to utils
447-
defp strip_struct(struct) do
448-
struct |> Map.from_struct() |> Map.delete(:id)
449-
end
450-
451446
defp update_viewed_user_list(%{meta: nil} = article, user_id) do
452447
new_ids = Enum.uniq([user_id] ++ @default_article_meta.viewed_user_ids)
453448
meta = @default_article_meta |> Map.merge(%{viewed_user_ids: new_ids})
454449

455-
do_update_viewed_user_list(article, meta)
450+
ORM.update_meta(article, meta)
456451
end
457452

458453
defp update_viewed_user_list(%{meta: meta} = article, user_id) do
@@ -462,20 +457,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
462457
true ->
463458
new_ids = Enum.uniq([user_id] ++ meta.viewed_user_ids)
464459
meta = meta |> Map.merge(%{viewed_user_ids: new_ids}) |> strip_struct
465-
do_update_viewed_user_list(article, meta)
460+
ORM.update_meta(article, meta)
466461

467462
false ->
468463
{:ok, :pass}
469464
end
470465
end
471466

472-
defp do_update_viewed_user_list(article, meta) do
473-
article
474-
|> Ecto.Changeset.change()
475-
|> Ecto.Changeset.put_embed(:meta, meta)
476-
|> Repo.update()
477-
end
478-
479467
defp read_result({:ok, %{inc_views: result}}), do: result |> done()
480468

481469
defp read_result({:error, _, result, _steps}) do

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
77

88
import Helper.ErrorCode
99
import ShortMaps
10+
import Helper.Utils, only: [strip_struct: 1]
1011
import GroupherServer.CMS.Utils.Matcher2
1112

1213
alias Helper.Types, as: T
@@ -163,20 +164,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
163164
@doc "update isEdited meta label if needed"
164165
# TODO: diff history
165166
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content) do
166-
new_meta =
167-
meta
168-
|> Map.from_struct()
169-
|> Map.delete(:id)
170-
|> Map.merge(%{is_edited: true})
171-
172-
do_update_meta(content, new_meta)
167+
meta = meta |> strip_struct |> Map.merge(%{is_edited: true})
168+
ORM.update_meta(content, meta)
173169
end
174170

175171
# for test or exsiting articles
176172
def update_edit_status(%{meta: nil} = content) do
177-
new_meta = Embeds.ArticleMeta.default_meta() |> Map.merge(%{is_edited: true})
173+
meta = Embeds.ArticleMeta.default_meta() |> Map.merge(%{is_edited: true})
178174

179-
do_update_meta(content, new_meta)
175+
ORM.update_meta(content, meta)
180176
end
181177

182178
def update_edit_status(content, _), do: {:ok, content}
@@ -186,25 +182,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
186182
def lock_article_comment(
187183
%{meta: %Embeds.ArticleMeta{is_comment_locked: false} = meta} = content
188184
) do
189-
new_meta =
185+
meta =
190186
meta
191187
|> Map.from_struct()
192188
|> Map.delete(:id)
193189
|> Map.merge(%{is_comment_locked: true})
194190

195-
do_update_meta(content, new_meta)
191+
ORM.update_meta(content, meta)
196192
end
197193

198194
def lock_article_comment(content), do: {:ok, content}
199195

200-
# TODO: put it into ORM helper
201-
defp do_update_meta(%{meta: _} = content, meta_params) do
202-
content
203-
|> Ecto.Changeset.change()
204-
|> Ecto.Changeset.put_embed(:meta, meta_params)
205-
|> Repo.update()
206-
end
207-
208196
# check if the thread has aready enough pined articles
209197
defp check_pinned_article_count(community_id, thread) do
210198
thread_upcase = thread |> to_string |> String.upcase()

lib/groupher_server/cms/delegates/article_reaction.ex

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
22
@moduledoc """
33
reaction[upvote, collect, watch ...] on article [post, job...]
44
"""
5-
import Helper.Utils, only: [done: 1]
6-
75
import GroupherServer.CMS.Utils.Matcher2
86
import Ecto.Query, warn: false
7+
import Helper.Utils, only: [done: 1, strip_struct: 1]
98
# import Helper.ErrorCode
109
import ShortMaps
1110

@@ -232,9 +231,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
232231
:remove -> cur_user_ids -- [user_id]
233232
end
234233

235-
updated_meta = @default_article_meta |> Map.merge(%{"#{action}ed_user_ids": updated_user_ids})
236-
237-
do_update_article_meta(article, updated_meta)
234+
meta = @default_article_meta |> Map.merge(%{"#{action}ed_user_ids": updated_user_ids})
235+
ORM.update_meta(article, meta)
238236
end
239237

240238
defp update_article_reaction_user_list(action, article, user_id, opt) do
@@ -246,20 +244,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
246244
:remove -> cur_user_ids -- [user_id]
247245
end
248246

249-
meta =
250-
article.meta
251-
|> Map.merge(%{"#{action}ed_user_ids": updated_user_ids})
252-
|> Map.from_struct()
253-
|> Map.delete(:id)
254-
255-
do_update_article_meta(article, meta)
256-
end
257-
258-
defp do_update_article_meta(article, meta) do
259-
article
260-
|> Ecto.Changeset.change()
261-
|> Ecto.Changeset.put_embed(:meta, meta)
262-
|> Repo.update()
247+
meta = article.meta |> Map.merge(%{"#{action}ed_user_ids": updated_user_ids}) |> strip_struct
248+
ORM.update_meta(article, meta)
263249
end
264250

265251
defp reaction_result({:ok, %{create_upvote: result}}), do: result |> done()

lib/groupher_server/cms/embeds/article_comment_meta.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
1111
is_article_author_upvoted: false,
1212
is_solution: false,
1313
is_reply_to_others: false,
14-
report_count: 0
14+
report_count: 0,
15+
upvoted_user_ids: []
1516
}
1617

1718
@doc "for test usage"
@@ -24,6 +25,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
2425
# 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示)
2526
field(:is_reply_to_others, :boolean, default: false)
2627
field(:report_count, :integer, default: 0)
28+
29+
field(:upvoted_user_ids, {:array, :integer}, default: [])
2730
end
2831

2932
def changeset(struct, params) do

lib/helper/orm.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ defmodule Helper.ORM do
262262
queryable |> count() |> add()
263263
end
264264

265+
@doc """
266+
update meta info for article / comment
267+
"""
268+
def update_meta(queryable, meta) do
269+
queryable
270+
|> Ecto.Changeset.change()
271+
|> Ecto.Changeset.put_embed(:meta, meta)
272+
|> Repo.update()
273+
end
274+
265275
@doc "extract common articles info"
266276
@spec extract_articles(T.paged_data(), [Atom.t()]) :: T.paged_article_common()
267277
def extract_articles(%{entries: entries} = paged_articles, supported_threads) do

lib/helper/utils/utils.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ defmodule Helper.Utils do
166166
value < target
167167
end
168168

169+
@doc """
170+
convert struct to normal map and remove :id field
171+
"""
172+
def strip_struct(struct) when is_struct(struct) do
173+
struct |> Map.from_struct() |> Map.delete(:id)
174+
end
175+
169176
@doc "html uniq id generator for editorjs"
170177
@spec uid(:html, map) :: String.t()
171178
def uid(:html, %{"id" => id}) when g_none_empty_str(id), do: id

test/groupher_server/cms/article_comment_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
130130
assert comment.meta.is_article_author_upvoted
131131
end
132132

133+
@tag :wip2
134+
test "article author upvote post comment will update upvoted_user_ids", ~m(post user)a do
135+
comment = "post_comment"
136+
{:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user)
137+
CMS.upvote_article_comment(comment.id, user)
138+
# IO.inspect(hello, label: "the hello")
139+
end
140+
133141
@tag :wip
134142
test "user can upvote a job comment", ~m(user job)a do
135143
comment = "job_comment"

0 commit comments

Comments
 (0)