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

Commit 076815e

Browse files
authored
feat(viewer-upvoted): add to comment && re-org codebase (#338)
* refactor(viewer-upvote): comment wip * refactor(viewer-upvote): gq-workflow done * chore: clean up
1 parent 2c40202 commit 076815e

File tree

13 files changed

+112
-92
lines changed

13 files changed

+112
-92
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/article_comment.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ defmodule GroupherServer.CMS.ArticleComment do
6969

7070
# 是否置顶
7171
field(:is_pinned, :boolean, default: false)
72+
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)
7273

7374
belongs_to(:author, Accounts.User, foreign_key: :author_id)
7475
belongs_to(:post, Post, foreign_key: :post_id)

lib/groupher_server/cms/delegates/article_comment.ex

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

99
import GroupherServer.CMS.Utils.Matcher2
@@ -281,13 +281,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
281281
end)
282282
|> Repo.transaction()
283283
|> upsert_comment_result
284-
285-
# is not work this way, why?
286-
# updated_emotions =
287-
# Map.merge(comment.emotions, %{
288-
# downvote_count: comment.emotions.downvote_count + Enum.random([1, 2, 3]),
289-
# tada_count: comment.emotions.tada_count + Enum.random([1, 2, 3])
290-
# })
291284
end
292285
end
293286

@@ -363,12 +356,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
363356
def upvote_article_comment(comment_id, %User{id: user_id}) do
364357
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
365358
false <- comment.is_deleted do
366-
# IO.inspect(comment, label: "the comment")
359+
# TODO: is user upvoted before?
367360
Multi.new()
368361
|> Multi.run(:create_comment_upvote, fn _, _ ->
369362
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
370363
end)
371-
|> Multi.run(:inc_upvotes_count, fn _, _ ->
364+
|> Multi.run(:add_upvoted_user, fn _, _ ->
365+
update_upvoted_user_list(comment, user_id, :add)
366+
end)
367+
|> Multi.run(:inc_upvotes_count, fn _, %{add_upvoted_user: comment} ->
372368
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment.id)
373369
upvotes_count = Repo.aggregate(count_query, :count)
374370
ORM.update(comment, %{upvotes_count: upvotes_count})
@@ -392,7 +388,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
392388
user_id: user_id
393389
})
394390
end)
395-
|> Multi.run(:desc_upvotes_count, fn _, _ ->
391+
|> Multi.run(:remove_upvoted_user, fn _, _ ->
392+
update_upvoted_user_list(comment, user_id, :remove)
393+
end)
394+
|> Multi.run(:desc_upvotes_count, fn _, %{remove_upvoted_user: comment} ->
396395
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)
397396
upvotes_count = Repo.aggregate(count_query, :count)
398397

@@ -419,6 +418,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
419418
|> ORM.paginater(~m(page size)a)
420419
|> set_viewer_emotion_ifneed(user)
421420
|> add_pined_comments_ifneed(thread, article_id, filters)
421+
|> mark_viewer_has_upvoted(user)
422422
|> done()
423423
end
424424
end
@@ -435,6 +435,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
435435
|> QueryBuilder.filter_pack(filters)
436436
|> ORM.paginater(~m(page size)a)
437437
|> set_viewer_emotion_ifneed(user)
438+
|> mark_viewer_has_upvoted(user)
438439
|> done()
439440
end
440441

@@ -607,6 +608,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
607608
%{paged_comments | entries: new_entries}
608609
end
609610

611+
defp mark_viewer_has_upvoted(paged_comments, nil), do: paged_comments
612+
613+
defp mark_viewer_has_upvoted(%{entries: entries} = paged_comments, %User{} = user) do
614+
entries =
615+
Enum.map(
616+
entries,
617+
&Map.merge(&1, %{viewer_has_upvoted: Enum.member?(&1.meta.upvoted_user_ids, user.id)})
618+
)
619+
620+
Map.merge(paged_comments, %{entries: entries})
621+
end
622+
610623
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
611624
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
612625
{:post, article}
@@ -674,6 +687,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
674687
end
675688
end
676689

690+
defp update_upvoted_user_list(comment, user_id, opt) do
691+
cur_user_ids = get_in(comment, [:meta, :upvoted_user_ids])
692+
693+
user_ids =
694+
case opt do
695+
:add -> [user_id] ++ cur_user_ids
696+
:remove -> cur_user_ids -- [user_id]
697+
end
698+
699+
meta = comment.meta |> Map.merge(%{upvoted_user_ids: user_ids}) |> strip_struct
700+
ORM.update_meta(comment, meta)
701+
end
702+
677703
defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
678704
defp upsert_comment_result({:ok, %{add_reply_to: result}}), do: {:ok, result}
679705
defp upsert_comment_result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
33
general article comment meta info
44
"""
55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others)a
@@ -11,7 +13,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
1113
is_article_author_upvoted: false,
1214
is_solution: false,
1315
is_reply_to_others: false,
14-
report_count: 0
16+
report_count: 0,
17+
upvoted_user_ids: []
1518
}
1619

1720
@doc "for test usage"
@@ -24,6 +27,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
2427
# 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示)
2528
field(:is_reply_to_others, :boolean, default: false)
2629
field(:report_count, :integer, default: 0)
30+
31+
field(:upvoted_user_ids, {:array, :integer}, default: [])
2732
end
2833

2934
def changeset(struct, params) do

lib/groupher_server/cms/utils/loader.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule GroupherServer.CMS.Utils.Loader do
99

1010
alias CMS.{
1111
Author,
12-
ArticleCommentUpvote,
1312
CommunityEditor,
1413
CommunitySubscriber,
1514
CommunityThread,
@@ -165,12 +164,12 @@ defmodule GroupherServer.CMS.Utils.Loader do
165164
|> QueryBuilder.members_pack(args)
166165
end
167166

168-
def query({"articles_comments_upvotes", ArticleCommentUpvote}, %{
169-
viewer_did: _,
170-
cur_user: cur_user
171-
}) do
172-
ArticleCommentUpvote |> where([f], f.user_id == ^cur_user.id)
173-
end
167+
# def query({"articles_comments_upvotes", ArticleCommentUpvote}, %{
168+
# viewer_did: _,
169+
# cur_user: cur_user
170+
# }) do
171+
# ArticleCommentUpvote |> where([f], f.user_id == ^cur_user.id)
172+
# end
174173

175174
# default loader
176175
def query(queryable, _args) do

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
339339
field(:meta, :article_comment_meta)
340340
field(:replies_count, :integer)
341341
field(:reply_to, :article_comment_reply)
342-
343-
field :viewer_has_upvoted, :boolean do
344-
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)
345-
346-
middleware(M.Authorize, :login)
347-
middleware(M.PutCurrentUser)
348-
resolve(dataloader(CMS, :upvotes))
349-
middleware(M.ViewerDidConvert)
350-
end
342+
field(:viewer_has_upvoted, :boolean)
351343

352344
timestamp_fields()
353345
end
@@ -365,15 +357,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
365357
field(:reply_to, :article_comment_reply)
366358
field(:replies, list_of(:article_comment_reply))
367359
field(:replies_count, :integer)
368-
# field(:viewer_has_upvoted, :boolean, resolve: dataloader(CMS, :))
369-
field :viewer_has_upvoted, :boolean do
370-
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)
371360

372-
middleware(M.Authorize, :login)
373-
middleware(M.PutCurrentUser)
374-
resolve(dataloader(CMS, :upvotes))
375-
middleware(M.ViewerDidConvert)
376-
end
361+
field(:viewer_has_upvoted, :boolean)
377362

378363
timestamp_fields()
379364
end

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

0 commit comments

Comments
 (0)