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

Commit 38e8480

Browse files
committed
fix(reaction): clean up favorite, comment like etc
1 parent 6818e50 commit 38e8480

File tree

25 files changed

+24
-922
lines changed

25 files changed

+24
-922
lines changed

lib/groupher_server/accounts/accounts.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule GroupherServer.Accounts do
2121
defdelegate default_subscribed_communities(filter), to: Profile
2222
defdelegate subscribed_communities(user, filter), to: Profile
2323

24-
# favorite category
24+
# collect folder
2525
defdelegate list_collect_folders(user_id, filter), to: CollectFolder
2626
defdelegate list_collect_folders(user_id, filter, owner), to: CollectFolder
2727
defdelegate list_collect_folder_articles(folder_id, filter, user), to: CollectFolder

lib/groupher_server/accounts/delegates/achievements.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
2020
@follow_weight get_config(:general, :user_achieve_follow_weight)
2121

2222
@doc """
23-
inc user's achievement by inc followers_count of favorite_weight
23+
inc user's achievement by inc followers_count of collect_weight
2424
"""
2525
@spec achieve(User.t(), atom, atom) :: SpecType.done()
2626
def achieve(%User{id: user_id}, :inc, :follow) do
@@ -34,7 +34,7 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
3434
end
3535

3636
@doc """
37-
dec user's achievement by inc followers_count of favorite_weight
37+
dec user's achievement by inc followers_count of collect_weight
3838
"""
3939
def achieve(%User{id: user_id}, :dec, :follow) do
4040
with {:ok, achievement} <- ORM.findby_or_insert(Achievement, ~m(user_id)a, ~m(user_id)a) do
@@ -73,7 +73,7 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
7373
end
7474

7575
@doc """
76-
dec user's achievement by articles_collects_count of favorite_weight
76+
dec user's achievement by articles_collects_count of collect_weight
7777
"""
7878
def achieve(%User{id: user_id} = _user, :inc, :collect) do
7979
with {:ok, achievement} <- ORM.findby_or_insert(Achievement, ~m(user_id)a, ~m(user_id)a) do
@@ -86,7 +86,7 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
8686
end
8787

8888
@doc """
89-
inc user's achievement by articles_collects_count of favorite_weight
89+
inc user's achievement by articles_collects_count of collect_weight
9090
"""
9191
def achieve(%User{id: user_id} = _user, :dec, :collect) do
9292
with {:ok, achievement} <- ORM.findby_or_insert(Achievement, ~m(user_id)a, ~m(user_id)a) do

lib/groupher_server/accounts/delegates/upvoted_articles.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
22
@moduledoc """
3-
get contents(posts, jobs ...) that user reacted (star, favorite ..)
3+
get contents(posts, jobs ...) that user upvotes
44
"""
55
# import GroupherServer.CMS.Utils.Matcher
66
import Ecto.Query, warn: false

lib/groupher_server/cms/cms.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule GroupherServer.CMS do
1212
ArticleCURD,
1313
ArticleOperation,
1414
ArticleReaction,
15-
FavoritedContents,
1615
ArticleComment,
1716
CommentCURD,
1817
CommunitySync,
@@ -97,7 +96,6 @@ defmodule GroupherServer.CMS do
9796
defdelegate set_collect_folder(collect, folder), to: ArticleReaction
9897
defdelegate undo_set_collect_folder(collect, folder), to: ArticleReaction
9998

100-
defdelegate favorited_category(thread, content_id, user), to: FavoritedContents
10199
# ArticleOperation
102100
# >> set flag on article, like: pin / unpin article
103101
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
@@ -155,11 +153,6 @@ defmodule GroupherServer.CMS do
155153
# report
156154
defdelegate create_report(type, content_id, args, user), to: AbuseReport
157155

158-
# Comment Reaction
159-
# >> like / undo like
160-
defdelegate like_comment(thread, comment, user), to: CommentReaction
161-
defdelegate undo_like_comment(thread, comment, user), to: CommentReaction
162-
163156
# Passport CURD
164157
defdelegate stamp_passport(rules, user), to: PassportCURD
165158
defdelegate erase_passport(rules, user), to: PassportCURD

lib/groupher_server/cms/community.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ defmodule GroupherServer.CMS.Community do
9999
|> unique_constraint(:aka, name: :communities_aka_index)
100100

101101
# |> foreign_key_constraint(:communities_author_fkey)
102-
# |> unique_constraint(:user_id, name: :posts_favorites_user_id_post_id_index)
103102
end
104103

105104
@doc false
@@ -114,6 +113,5 @@ defmodule GroupherServer.CMS.Community do
114113
|> unique_constraint(:aka, name: :communities_aka_index)
115114

116115
# |> foreign_key_constraint(:communities_author_fkey)
117-
# |> unique_constraint(:user_id, name: :posts_favorites_user_id_post_id_index)
118116
end
119117
end

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
140140
|> update_content_result()
141141
end
142142

143-
@doc """
144-
get CMS contents
145-
post's favorites/stars/comments ...
146-
...
147-
jobs's favorites/stars/comments ...
148-
149-
with or without page info
150-
"""
151-
def reaction_users(thread, react, id, %{page: page, size: size} = filters) do
152-
with {:ok, action} <- match_action(thread, react),
153-
{:ok, where} <- dynamic_where(thread, id) do
154-
# common_filter(action.reactor)
155-
action.reactor
156-
|> where(^where)
157-
|> QueryBuilder.load_inner_users(filters)
158-
|> ORM.paginater(~m(page size)a)
159-
|> done()
160-
end
161-
end
162-
163143
@spec ensure_author_exists(User.t()) :: {:ok, User.t()}
164144
def ensure_author_exists(%User{} = user) do
165145
# unique_constraint: avoid race conditions, make sure user_id unique

lib/groupher_server/cms/delegates/article_reaction.ex

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule GroupherServer.CMS.Delegate.ArticleReaction do
22
@moduledoc """
3-
reaction[favorite, star, watch ...] on article [post, job...]
3+
reaction[upvote, collect, watch ...] on article [post, job...]
44
"""
55
import Helper.Utils, only: [done: 1, done: 2]
66

@@ -208,109 +208,4 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
208208
defp reaction_result({:error, _, result, _steps}) do
209209
{:error, result}
210210
end
211-
212-
##### #########################
213-
##### #########################
214-
##### #########################
215-
##### #########################
216-
##### #########################
217-
218-
@doc """
219-
favorite / star / watch CMS contents like post / tuts ...
220-
"""
221-
def reaction(thread, react, content_id, %User{id: user_id}) do
222-
with {:ok, action} <- match_action(thread, react),
223-
{:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]),
224-
{:ok, user} <- ORM.find(Accounts.User, user_id) do
225-
Multi.new()
226-
|> Multi.run(:create_reaction_record, fn _, _ ->
227-
create_reaction_record(action, user, thread, content)
228-
end)
229-
|> Multi.run(:add_achievement, fn _, _ ->
230-
achiever_id = content.author.user_id
231-
Accounts.achieve(%User{id: achiever_id}, :inc, react)
232-
end)
233-
|> Repo.transaction()
234-
|> old_reaction_result()
235-
end
236-
end
237-
238-
defp old_reaction_result({:ok, %{create_reaction_record: result}}), do: result |> done()
239-
240-
defp old_reaction_result({:error, :create_reaction_record, %Ecto.Changeset{} = result, _steps}) do
241-
{:error, result}
242-
end
243-
244-
defp old_reaction_result({:error, :create_reaction_record, _result, _steps}) do
245-
{:error, [message: "create reaction fails", code: ecode(:react_fails)]}
246-
end
247-
248-
defp old_reaction_result({:error, :add_achievement, _result, _steps}),
249-
do: {:error, [message: "achieve fails", code: ecode(:react_fails)]}
250-
251-
defp create_reaction_record(action, %User{id: user_id}, thread, content) do
252-
attrs = %{} |> Map.put("user_id", user_id) |> Map.put("#{thread}_id", content.id)
253-
254-
action.reactor
255-
|> ORM.create(attrs)
256-
|> done(with: content)
257-
end
258-
259-
# ------
260-
@doc """
261-
unfavorite / unstar / unwatch CMS contents like post / tuts ...
262-
"""
263-
def undo_reaction(thread, react, content_id, %User{id: user_id}) do
264-
with {:ok, action} <- match_action(thread, react),
265-
{:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]),
266-
{:ok, user} <- ORM.find(Accounts.User, user_id) do
267-
Multi.new()
268-
|> Multi.run(:delete_reaction_record, fn _, _ ->
269-
delete_reaction_record(action, user, thread, content)
270-
end)
271-
|> Multi.run(:minus_achievement, fn _, _ ->
272-
achiever_id = content.author.user_id
273-
Accounts.achieve(%User{id: achiever_id}, :dec, react)
274-
end)
275-
|> Repo.transaction()
276-
|> undo_reaction_result()
277-
end
278-
end
279-
280-
defp undo_reaction_result({:ok, %{delete_reaction_record: result}}), do: result |> done()
281-
282-
defp undo_reaction_result({:error, :delete_reaction_record, _result, _steps}) do
283-
{:error, [message: "delete reaction fails", code: ecode(:react_fails)]}
284-
end
285-
286-
defp undo_reaction_result({:error, :minus_achievement, _result, _steps}),
287-
do: {:error, [message: "achieve fails", code: ecode(:react_fails)]}
288-
289-
defp delete_reaction_record(action, %User{id: user_id}, thread, content) do
290-
user_where = dynamic([u], u.user_id == ^user_id)
291-
reaction_where = dynamic_reaction_where(thread, content.id, user_where)
292-
293-
query = from(f in action.reactor, where: ^reaction_where)
294-
295-
case Repo.one(query) do
296-
nil ->
297-
{:error, "record not found"}
298-
299-
record ->
300-
Repo.delete(record)
301-
{:ok, content}
302-
end
303-
end
304-
305-
defp dynamic_reaction_where(:post, id, user_where) do
306-
dynamic([p], p.post_id == ^id and ^user_where)
307-
end
308-
309-
defp dynamic_reaction_where(:job, id, user_where) do
310-
dynamic([p], p.job_id == ^id and ^user_where)
311-
end
312-
313-
defp dynamic_reaction_where(:repo, id, user_where) do
314-
dynamic([p], p.repo_id == ^id and ^user_where)
315-
end
316211
end

lib/groupher_server/cms/delegates/comment_reaction.ex

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,6 @@ defmodule GroupherServerWeb.Resolvers.CMS do
149149
CMS.collected_users(thread, id, filter)
150150
end
151151

152-
def reaction(_root, ~m(id thread action)a, %{context: %{cur_user: user}}) do
153-
CMS.reaction(thread, action, id, user)
154-
end
155-
156-
def undo_reaction(_root, ~m(id thread action)a, %{context: %{cur_user: user}}) do
157-
CMS.undo_reaction(thread, action, id, user)
158-
end
159-
160-
def reaction_users(_root, ~m(id action thread filter)a, _info) do
161-
CMS.reaction_users(thread, action, id, filter)
162-
end
163-
164-
def favorited_category(root, ~m(thread)a, %{context: %{cur_user: user}}) do
165-
CMS.favorited_category(thread, root.id, user)
166-
end
167-
168152
# #######################
169153
# category ..
170154
# #######################
@@ -385,14 +369,6 @@ defmodule GroupherServerWeb.Resolvers.CMS do
385369
CMS.reply_comment(thread, id, args, user)
386370
end
387371

388-
def like_comment(_root, ~m(thread id)a, %{context: %{cur_user: user}}) do
389-
CMS.like_comment(thread, id, user)
390-
end
391-
392-
def undo_like_comment(_root, ~m(thread id)a, %{context: %{cur_user: user}}) do
393-
CMS.undo_like_comment(thread, id, user)
394-
end
395-
396372
def stamp_passport(_root, ~m(user_id rules)a, %{context: %{cur_user: _user}}) do
397373
CMS.stamp_passport(rules, %User{id: user_id})
398374
end

lib/groupher_server_web/schema/account/account_mutations.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule GroupherServerWeb.Schema.Account.Mutations do
3939
resolve(&R.Accounts.undo_follow/3)
4040
end
4141

42-
@desc "create a favorites category"
42+
@desc "create a collect folder"
4343
field :create_collect_folder, :collect_folder do
4444
arg(:title, non_null(:string))
4545
arg(:private, :boolean)
@@ -49,7 +49,7 @@ defmodule GroupherServerWeb.Schema.Account.Mutations do
4949
resolve(&R.Accounts.create_collect_folder/3)
5050
end
5151

52-
@desc "update a favorites category"
52+
@desc "update a collect folder"
5353
field :update_collect_folder, :collect_folder do
5454
arg(:id, non_null(:id))
5555
arg(:title, :string)

0 commit comments

Comments
 (0)