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

Commit 60bef5a

Browse files
committed
refactor(delivery): rename type -> thread in notification
1 parent b657180 commit 60bef5a

File tree

11 files changed

+116
-329
lines changed

11 files changed

+116
-329
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ config :groupher_server, :general,
5151
# others
5252
# 在这个时间段内,多条提醒消息将被合并为一条
5353
notify_group_interval_hour: 1,
54-
nofity_types: [:upvote, :comment, :reply, :collect, :follow]
54+
nofity_actions: [:upvote, :comment, :reply, :collect, :follow]
5555

5656
config :groupher_server, :customization,
5757
theme: "cyan",

lib/groupher_server/cms/delegates/comment_curd.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
114114
false -> CMS.update_active_timestamp(thread, article)
115115
end
116116
end)
117-
|> Multi.run(:after_hooks, fn _, %{create_comment: create_comment} ->
118-
Later.run({Hooks.Cite, :handle, [create_comment]})
117+
|> Multi.run(:after_hooks, fn _, %{create_comment: comment} ->
118+
Later.run({Hooks.Cite, :handle, [comment]})
119+
Later.run({Hooks.Notify, :handle, [:comment, comment, user]})
119120
end)
120121
|> Repo.transaction()
121122
|> result()

lib/groupher_server/cms/delegates/helper.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ defmodule GroupherServer.CMS.Delegate.Helper do
4141
|> done
4242
end
4343

44+
@doc "get parent article of a comment"
45+
def parent_article_of(%Comment{} = comment) do
46+
article_thread = comment.thread |> String.downcase() |> String.to_atom()
47+
48+
comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done
49+
end
50+
51+
def parent_article_of(_), do: {:error, "only support comment"}
52+
4453
#######
4554
# emotion related
4655
#######

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

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,90 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do
33
notify hooks, for upvote, collect, comment, reply
44
"""
55
import Helper.Utils, only: [thread_of_article: 1]
6-
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1]
6+
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, parent_article_of: 1]
77

8-
alias GroupherServer.{Accounts, CMS, Delivery, Repo}
8+
alias GroupherServer.{Accounts, CMS, Delivery}
99

1010
alias Accounts.Model.User
1111
alias CMS.Model.Comment
1212

13-
def handle(:undo, action, %Comment{} = comment, %User{} = from_user) do
14-
article_thread = comment.thread |> String.downcase() |> String.to_atom()
15-
article = comment |> Repo.preload(article_thread) |> Map.get(article_thread)
13+
# 发布评论是特殊情况,单独处理
14+
def handle(:comment, %Comment{} = comment, %User{} = from_user) do
15+
{:ok, article} = parent_article_of(comment)
16+
{:ok, article} = preload_author(article)
17+
{:ok, thread} = thread_of_article(article)
1618

1719
notify_attrs = %{
18-
action: action,
19-
type: :comment,
20+
action: :comment,
21+
thread: thread,
2022
article_id: article.id,
2123
title: article.title,
2224
comment_id: comment.id,
23-
user_id: comment.author_id
25+
# NOTE: 这里是提醒该评论文章的作者,不是评论本身的作者
26+
user_id: article.author.user.id
2427
}
2528

26-
Delivery.revoke(:notify, notify_attrs, from_user)
29+
Delivery.send(:notify, notify_attrs, from_user)
2730
end
2831

29-
def handle(:undo, action, article, %User{} = from_user) do
32+
def handle(action, %Comment{} = comment, %User{} = from_user) do
33+
{:ok, article} = parent_article_of(comment)
34+
{:ok, thread} = thread_of_article(article)
35+
36+
notify_attrs = %{
37+
action: action,
38+
thread: thread,
39+
article_id: article.id,
40+
title: article.title,
41+
user_id: comment.author_id,
42+
comment_id: comment.id
43+
}
44+
45+
Delivery.send(:notify, notify_attrs, from_user)
46+
end
47+
48+
def handle(action, article, %User{} = from_user) do
3049
{:ok, article} = preload_author(article)
3150
{:ok, thread} = thread_of_article(article)
3251

3352
notify_attrs = %{
3453
action: action,
35-
type: thread,
54+
thread: thread,
3655
article_id: article.id,
56+
title: article.title,
3757
user_id: article.author.user.id
3858
}
3959

40-
Delivery.revoke(:notify, notify_attrs, from_user)
60+
Delivery.send(:notify, notify_attrs, from_user)
4161
end
4262

43-
def handle(action, %Comment{} = comment, %User{} = from_user) do
44-
article_thread = comment.thread |> String.downcase() |> String.to_atom()
45-
article = comment |> Repo.preload(article_thread) |> Map.get(article_thread)
63+
def handle(:undo, action, %Comment{} = comment, %User{} = from_user) do
64+
{:ok, article} = parent_article_of(comment)
65+
{:ok, thread} = thread_of_article(article)
4666

4767
notify_attrs = %{
4868
action: action,
49-
type: :comment,
69+
thread: thread,
5070
article_id: article.id,
5171
title: article.title,
5272
comment_id: comment.id,
5373
user_id: comment.author_id
5474
}
5575

56-
Delivery.send(:notify, notify_attrs, from_user)
76+
Delivery.revoke(:notify, notify_attrs, from_user)
5777
end
5878

59-
def handle(action, article, %User{} = from_user) do
79+
def handle(:undo, action, article, %User{} = from_user) do
6080
{:ok, article} = preload_author(article)
6181
{:ok, thread} = thread_of_article(article)
6282

6383
notify_attrs = %{
6484
action: action,
65-
type: thread,
85+
thread: thread,
6686
article_id: article.id,
67-
title: article.title,
6887
user_id: article.author.user.id
6988
}
7089

71-
Delivery.send(:notify, notify_attrs, from_user)
90+
Delivery.revoke(:notify, notify_attrs, from_user)
7291
end
7392
end

lib/groupher_server/delivery/delegates/notification.ex

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
1414
alias Accounts.Model.User
1515
alias Helper.ORM
1616

17-
@supported_notify_type get_config(:general, :nofity_types)
17+
@notify_actions get_config(:general, :nofity_actions)
1818
@notify_group_interval_hour get_config(:general, :notify_group_interval_hour)
1919

2020
def handle(%{action: action, user_id: user_id} = attrs, %User{} = from_user) do
21-
with true <- action in @supported_notify_type,
21+
with true <- action in @notify_actions,
2222
true <- is_valid?(attrs),
2323
true <- user_id !== from_user.id do
2424
from_user = from_user |> Map.take([:login, :nickname]) |> Map.put(:user_id, from_user.id)
@@ -90,18 +90,21 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
9090

9191
defp find_exist_notify(%{comment_id: comment_id} = attrs, opt)
9292
when not is_nil(comment_id) do
93-
~m(type article_id comment_id)a = atom_values_to_upcase(attrs)
93+
~m(thread article_id comment_id)a = atom_values_to_upcase(attrs)
9494

9595
Notification
96-
|> where([n], n.type == ^type and n.article_id == ^article_id and n.comment_id == ^comment_id)
96+
|> where(
97+
[n],
98+
n.thread == ^thread and n.article_id == ^article_id and n.comment_id == ^comment_id
99+
)
97100
|> do_find_exist_notify(attrs, opt)
98101
end
99102

100103
defp find_exist_notify(attrs, opt) do
101-
~m(type article_id)a = atom_values_to_upcase(attrs)
104+
~m(thread article_id)a = atom_values_to_upcase(attrs)
102105

103106
Notification
104-
|> where([n], n.type == ^type and n.article_id == ^article_id)
107+
|> where([n], n.thread == ^thread and n.article_id == ^article_id)
105108
|> do_find_exist_notify(attrs, opt)
106109
end
107110

@@ -139,29 +142,24 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
139142
|> done
140143
end
141144

142-
# [:upvote, :comment, :reply, :collect, :follow]
143-
defp is_valid?(%{action: :upvote, type: :comment} = attrs) do
144-
attrs |> all_exist?([:article_id, :type, :title, :comment_id, :user_id])
145-
end
145+
defp is_valid?(%{action: :follow} = attrs), do: attrs |> all_exist?([:user_id])
146146

147147
defp is_valid?(%{action: :upvote} = attrs) do
148-
attrs |> all_exist?([:article_id, :type, :title, :user_id])
148+
attrs |> all_exist?([:article_id, :thread, :title, :user_id])
149149
end
150150

151-
defp is_valid?(%{action: :comment} = attrs) do
152-
attrs |> all_exist?([:article_id, :type, :title, :comment_id, :user_id])
151+
defp is_valid?(%{action: :collect} = attrs) do
152+
attrs |> all_exist?([:article_id, :thread, :title, :user_id])
153153
end
154154

155-
defp is_valid?(%{action: :reply} = attrs) do
156-
attrs |> all_exist?([:article_id, :type, :title, :comment_id, :user_id])
155+
defp is_valid?(%{action: :comment} = attrs) do
156+
attrs |> all_exist?([:article_id, :thread, :title, :comment_id, :user_id])
157157
end
158158

159-
defp is_valid?(%{action: :collect} = attrs) do
160-
attrs |> all_exist?([:article_id, :type, :title, :user_id])
159+
defp is_valid?(%{action: :reply} = attrs) do
160+
attrs |> all_exist?([:article_id, :thread, :title, :comment_id, :user_id])
161161
end
162162

163-
defp is_valid?(%{action: :follow} = attrs), do: attrs |> all_exist?([:user_id])
164-
165163
defp is_valid?(_), do: false
166164

167165
# 确保 key 存在,并且不为 nil

lib/groupher_server/delivery/models/notification.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ defmodule GroupherServer.Delivery.Model.Notification do
1010
alias CMS.Model.Embeds
1111

1212
@required_fields ~w(user_id action)a
13-
@optional_fields ~w(type article_id comment_id title read)a
13+
@optional_fields ~w(thread article_id comment_id title read)a
1414

1515
@type t :: %Notification{}
1616
schema "notifications" do
1717
belongs_to(:user, User)
1818
# article or comment
19-
field(:type, :string)
19+
field(:thread, :string)
2020
field(:article_id, :id)
2121
field(:title, :string)
2222
# optional comment id
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule GroupherServer.Repo.Migrations.RenameNotificationsTypeToThread do
2+
use Ecto.Migration
3+
4+
def change do
5+
rename(table(:notifications), :type, to: :thread)
6+
end
7+
end

test/groupher_server/cms/hooks/notify_blog_test.exs

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

0 commit comments

Comments
 (0)