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

Commit 0733508

Browse files
committed
refactor(delivery): enhence test
1 parent 7f1332b commit 0733508

File tree

5 files changed

+416
-2
lines changed

5 files changed

+416
-2
lines changed

lib/groupher_server/cms/delegates/comment_action.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
139139
|> Ecto.Changeset.put_assoc(:reply_to, replying_comment)
140140
|> Repo.update()
141141
end)
142+
|> Multi.run(:after_hooks, fn _, %{create_reply_comment: replyed_comment} ->
143+
Later.run({Hooks.Notify, :handle, [:reply, replyed_comment, user]})
144+
end)
142145
|> Repo.transaction()
143146
|> result()
144147
else

lib/groupher_server/cms/delegates/hooks/notify.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do
2929
Delivery.send(:notify, notify_attrs, from_user)
3030
end
3131

32+
# 回复评论是特殊情况,单独处理
33+
def handle(:reply, %Comment{} = reply_comment, %User{} = from_user) do
34+
{:ok, article} = parent_article_of(reply_comment)
35+
{:ok, article} = preload_author(article)
36+
{:ok, thread} = thread_of_article(article)
37+
38+
notify_attrs = %{
39+
action: :reply,
40+
thread: thread,
41+
article_id: article.id,
42+
title: article.title,
43+
comment_id: reply_comment.id,
44+
# NOTE: 这里是提醒该评论的作者,不提醒文章作者了
45+
user_id: reply_comment.reply_to.author_id
46+
}
47+
48+
Delivery.send(:notify, notify_attrs, from_user)
49+
end
50+
3251
def handle(action, %Comment{} = comment, %User{} = from_user) do
3352
{:ok, article} = parent_article_of(comment)
3453
{:ok, thread} = thread_of_article(article)
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
2+
use GroupherServer.TestTools
3+
4+
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1]
5+
6+
alias GroupherServer.{CMS, Delivery}
7+
alias CMS.Delegate.Hooks
8+
9+
setup do
10+
{:ok, user} = db_insert(:user)
11+
{:ok, user2} = db_insert(:user)
12+
{:ok, user3} = db_insert(:user)
13+
14+
{:ok, community} = db_insert(:community)
15+
16+
blog_attrs = mock_attrs(:blog, %{community_id: community.id})
17+
{:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user)
18+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
19+
20+
{:ok, ~m(user2 user3 blog comment)a}
21+
end
22+
23+
describe "[upvote notify]" do
24+
@tag :wip2
25+
test "upvote hook should work on blog", ~m(user2 blog)a do
26+
{:ok, blog} = preload_author(blog)
27+
28+
{:ok, article} = CMS.upvote_article(:blog, blog.id, user2)
29+
Hooks.Notify.handle(:upvote, article, user2)
30+
31+
{:ok, notifications} =
32+
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
33+
34+
assert notifications.total_count == 1
35+
36+
notify = notifications.entries |> List.first()
37+
assert notify.action == "UPVOTE"
38+
assert notify.article_id == blog.id
39+
assert notify.thread == "BLOG"
40+
assert notify.user_id == blog.author.user.id
41+
assert user_exist_in?(user2, notify.from_users)
42+
end
43+
44+
@tag :wip2
45+
test "upvote hook should work on blog comment", ~m(user2 blog comment)a do
46+
{:ok, comment} = CMS.upvote_comment(comment.id, user2)
47+
{:ok, comment} = preload_author(comment)
48+
49+
Hooks.Notify.handle(:upvote, comment, user2)
50+
51+
{:ok, notifications} =
52+
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
53+
54+
assert notifications.total_count == 1
55+
56+
notify = notifications.entries |> List.first()
57+
assert notify.action == "UPVOTE"
58+
assert notify.article_id == blog.id
59+
assert notify.thread == "BLOG"
60+
assert notify.user_id == comment.author.id
61+
assert notify.comment_id == comment.id
62+
assert user_exist_in?(user2, notify.from_users)
63+
end
64+
65+
@tag :wip2
66+
test "undo upvote hook should work on blog", ~m(user2 blog)a do
67+
{:ok, blog} = preload_author(blog)
68+
69+
{:ok, article} = CMS.upvote_article(:blog, blog.id, user2)
70+
Hooks.Notify.handle(:upvote, article, user2)
71+
72+
{:ok, article} = CMS.undo_upvote_article(:blog, blog.id, user2)
73+
Hooks.Notify.handle(:undo, :upvote, article, user2)
74+
75+
{:ok, notifications} =
76+
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
77+
78+
assert notifications.total_count == 0
79+
end
80+
81+
@tag :wip2
82+
test "undo upvote hook should work on blog comment", ~m(user2 comment)a do
83+
{:ok, comment} = CMS.upvote_comment(comment.id, user2)
84+
85+
Hooks.Notify.handle(:upvote, comment, user2)
86+
87+
{:ok, comment} = CMS.undo_upvote_comment(comment.id, user2)
88+
Hooks.Notify.handle(:undo, :upvote, comment, user2)
89+
90+
{:ok, comment} = preload_author(comment)
91+
92+
{:ok, notifications} =
93+
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
94+
95+
assert notifications.total_count == 0
96+
end
97+
end
98+
99+
describe "[collect notify]" do
100+
@tag :wip2
101+
test "collect hook should work on blog", ~m(user2 blog)a do
102+
{:ok, blog} = preload_author(blog)
103+
104+
{:ok, _} = CMS.collect_article(:blog, blog.id, user2)
105+
Hooks.Notify.handle(:collect, blog, user2)
106+
107+
{:ok, notifications} =
108+
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
109+
110+
assert notifications.total_count == 1
111+
112+
notify = notifications.entries |> List.first()
113+
assert notify.action == "COLLECT"
114+
assert notify.article_id == blog.id
115+
assert notify.thread == "BLOG"
116+
assert notify.user_id == blog.author.user.id
117+
assert user_exist_in?(user2, notify.from_users)
118+
end
119+
120+
@tag :wip2
121+
test "undo collect hook should work on blog", ~m(user2 blog)a do
122+
{:ok, blog} = preload_author(blog)
123+
124+
{:ok, _} = CMS.upvote_article(:blog, blog.id, user2)
125+
Hooks.Notify.handle(:collect, blog, user2)
126+
127+
{:ok, _} = CMS.undo_upvote_article(:blog, blog.id, user2)
128+
Hooks.Notify.handle(:undo, :collect, blog, user2)
129+
130+
{:ok, notifications} =
131+
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
132+
133+
assert notifications.total_count == 0
134+
end
135+
end
136+
137+
describe "[comment notify]" do
138+
@tag :wip
139+
test "blog author should get notify after some one comment on it", ~m(user2 blog)a do
140+
{:ok, blog} = preload_author(blog)
141+
142+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user2)
143+
Hooks.Notify.handle(:comment, comment, user2)
144+
145+
{:ok, notifications} =
146+
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
147+
148+
assert notifications.total_count == 1
149+
150+
notify = notifications.entries |> List.first()
151+
assert notify.action == "COMMENT"
152+
assert notify.thread == "BLOG"
153+
assert notify.article_id == blog.id
154+
assert notify.user_id == blog.author.user.id
155+
assert user_exist_in?(user2, notify.from_users)
156+
end
157+
158+
@tag :wip
159+
test "blog comment author should get notify after some one reply it", ~m(user2 user3 blog)a do
160+
{:ok, blog} = preload_author(blog)
161+
162+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user2)
163+
{:ok, replyed_comment} = CMS.reply_comment(comment.id, mock_comment(), user3)
164+
165+
Hooks.Notify.handle(:reply, replyed_comment, user3)
166+
167+
{:ok, notifications} =
168+
Delivery.fetch(:notification, comment.author_id, %{page: 1, size: 20})
169+
170+
assert notifications.total_count == 1
171+
172+
notify = notifications.entries |> List.first()
173+
174+
assert notify.action == "REPLY"
175+
assert notify.thread == "BLOG"
176+
assert notify.article_id == blog.id
177+
assert notify.comment_id == replyed_comment.id
178+
179+
assert notify.user_id == comment.author_id
180+
assert user_exist_in?(user3, notify.from_users)
181+
end
182+
end
183+
end

0 commit comments

Comments
 (0)