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

Commit 2cfc57e

Browse files
committed
refactor(mailbox): mark read test & re-org
1 parent 61fcd0e commit 2cfc57e

File tree

16 files changed

+189
-213
lines changed

16 files changed

+189
-213
lines changed

lib/groupher_server/accounts/accounts.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ defmodule GroupherServer.Accounts do
6464

6565
defdelegate mailbox_status(user), to: Mailbox
6666
defdelegate update_mailbox_status(user_id), to: Mailbox
67+
defdelegate mark_read(type, ids, user_id), to: Mailbox
68+
defdelegate mark_read_all(tyoe, user_id), to: Mailbox
6769

6870
# customization
6971
defdelegate get_customization(user), to: Customization

lib/groupher_server/accounts/delegates/mailbox.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ defmodule GroupherServer.Accounts.Delegate.Mailbox do
1313
def mailbox_status(%User{mailbox: nil}), do: @default_mailbox_status |> done
1414
def mailbox_status(%User{mailbox: mailbox}), do: mailbox |> done
1515

16+
def mark_read(type, ids, %User{} = user) do
17+
Delivery.mark_read(type, ids, user)
18+
end
19+
20+
def mark_read_all(type, %User{} = user), do: Delivery.mark_read_all(type, user)
21+
1622
@doc "update messages count in mailbox"
1723
def update_mailbox_status(user_id) do
1824
with {:ok, user} <- ORM.find(User, user_id),

lib/groupher_server/delivery/delegates/notification.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
7878
end
7979
end
8080

81-
def paged_notifications(user_id, %{page: page, size: size} = filter) do
81+
def paged_notifications(%User{} = user, %{page: page, size: size} = filter) do
8282
read = Map.get(filter, :read, false)
8383

8484
Notification
85-
|> where([n], n.user_id == ^user_id and n.read == ^read)
85+
|> where([n], n.user_id == ^user.id and n.read == ^read)
8686
|> ORM.paginater(~m(page size)a)
8787
|> done
8888
end

lib/groupher_server_web/resolvers/accounts_resolver.ex

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,39 +220,20 @@ defmodule GroupherServerWeb.Resolvers.Accounts do
220220
Accounts.paged_editable_communities(cur_user, filter)
221221
end
222222

223+
# mailbox
223224
def mailbox_status(_root, _args, %{context: %{cur_user: cur_user}}) do
224225
Accounts.mailbox_status(cur_user)
225226
end
226227

227-
# mentions
228-
def fetch_mentions(_root, %{filter: filter}, %{context: %{cur_user: cur_user}}) do
229-
Accounts.fetch_mentions(cur_user, filter)
228+
def mark_read(_root, ~m(type ids)a, %{context: %{cur_user: cur_user}}) do
229+
Accounts.mark_read(type, ids, cur_user)
230230
end
231231

232-
def mark_mention_read(_root, %{id: id}, %{context: %{cur_user: cur_user}}) do
233-
Accounts.mark_mail_read(%MentionMail{id: id}, cur_user)
232+
def mark_read_all(_root, ~m(type)a, %{context: %{cur_user: cur_user}}) do
233+
Accounts.mark_read_all(type, cur_user)
234234
end
235235

236-
def mark_mention_read_all(_root, _args, %{context: %{cur_user: cur_user}}) do
237-
Accounts.mark_mail_read_all(cur_user, :mention)
238-
end
239-
240-
# notification
241-
def fetch_notifications(_root, %{filter: filter}, %{context: %{cur_user: cur_user}}) do
242-
Accounts.fetch_notifications(cur_user, filter)
243-
end
244-
245-
def mark_notification_read(_root, %{id: id}, %{context: %{cur_user: cur_user}}) do
246-
Accounts.mark_mail_read(%NotificationMail{id: id}, cur_user)
247-
end
248-
249-
def mark_notification_read_all(_root, _args, %{context: %{cur_user: cur_user}}) do
250-
Accounts.mark_mail_read_all(cur_user, :notification)
251-
end
252-
253-
def mark_sys_notification_read(_root, %{id: id}, %{context: %{cur_user: cur_user}}) do
254-
Accounts.mark_mail_read(%SysNotificationMail{id: id}, cur_user)
255-
end
236+
# mailbox end
256237

257238
# for check other users subscribed_communities
258239
def subscribed_communities(_root, %{login: login, filter: filter}, _info) do

lib/groupher_server_web/schema/account/account_mutations.ex

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ defmodule GroupherServerWeb.Schema.Account.Mutations do
2323
resolve(&R.Accounts.github_signin/3)
2424
end
2525

26-
@doc "follow a user"
26+
@desc "follow a user"
2727
field :follow, :user do
2828
arg(:login, non_null(:string))
2929

3030
middleware(M.Authorize, :login)
3131
resolve(&R.Accounts.follow/3)
3232
end
3333

34-
@doc "undo follow to a user"
34+
@desc "unfollow a user"
3535
field :undo_follow, :user do
3636
arg(:login, non_null(:string))
3737

@@ -97,40 +97,13 @@ defmodule GroupherServerWeb.Schema.Account.Mutations do
9797
resolve(&R.Accounts.set_customization/3)
9898
end
9999

100-
@desc "mark a mention as read"
101-
field :mark_mention_read, :status do
102-
arg(:id, non_null(:id))
103-
104-
middleware(M.Authorize, :login)
105-
resolve(&R.Accounts.mark_mention_read/3)
106-
end
107-
108-
@desc "mark a all unread mention as read"
109-
field :mark_mention_read_all, :status do
110-
middleware(M.Authorize, :login)
111-
resolve(&R.Accounts.mark_mention_read_all/3)
112-
end
113-
114-
@desc "mark a notification as read"
115-
field :mark_notification_read, :status do
116-
arg(:id, non_null(:id))
117-
118-
middleware(M.Authorize, :login)
119-
resolve(&R.Accounts.mark_notification_read/3)
120-
end
121-
122-
@desc "mark a all unread notifications as read"
123-
field :mark_notification_read_all, :status do
124-
middleware(M.Authorize, :login)
125-
resolve(&R.Accounts.mark_notification_read_all/3)
126-
end
127-
128-
@desc "mark a system notification as read"
129-
field :mark_sys_notification_read, :status do
130-
arg(:id, non_null(:id))
100+
@desc "mark a message as read"
101+
field :mark_read, :done do
102+
arg(:ids, list_of(:id))
103+
arg(:type, :mailbox_type, default_value: :mention)
131104

132105
middleware(M.Authorize, :login)
133-
resolve(&R.Accounts.mark_sys_notification_read/3)
106+
resolve(&R.Accounts.mark_read/3)
134107
end
135108
end
136109
end

lib/groupher_server_web/schema/account/account_types.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,9 @@ defmodule GroupherServerWeb.Schema.Account.Types do
199199
field(:entries, list_of(:user))
200200
pagination_fields()
201201
end
202+
203+
enum :mailbox_type do
204+
value(:mention)
205+
value(:notification)
206+
end
202207
end

lib/helper/utils/utils.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ defmodule Helper.Utils do
5757
"""
5858
def done(false), do: {:error, false}
5959
def done(true), do: {:ok, true}
60+
def done(nil), do: {:error, "record not found."}
6061
def done(nil, :boolean), do: {:ok, false}
6162
def done(_, :boolean), do: {:ok, true}
6263
def done(nil, err_msg), do: {:error, err_msg}
@@ -69,11 +70,12 @@ defmodule Helper.Utils do
6970
def done(nil, queryable, id), do: {:error, not_found_formater(queryable, id)}
7071
def done(result, _, _), do: {:ok, result}
7172

72-
def done(nil), do: {:error, "record not found."}
73-
7473
# for delete_all, update_all
7574
# see: https://groups.google.com/forum/#!topic/elixir-ecto/1g5Pp6ceqFE
75+
# def done({0, nil}), do: {:error, %{done: false}}
7676
def done({n, nil}) when is_integer(n), do: {:ok, %{done: true}}
77+
# def done({n, nil}, extra: extra) when is_integer(n), do: {:ok, %{done: true}}
78+
7779
def done(result), do: {:ok, result}
7880

7981
def done_and_cache(result, pool, scope, expire_sec: expire_sec) do

test/groupher_server/accounts/hooks/notify_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule GroupherServer.Test.Accounts.Hooks.Notify do
1818
{:ok, _} = Accounts.follow(user, user2)
1919
Hooks.Notify.handle(:follow, user, user2)
2020

21-
{:ok, notifications} = Delivery.fetch(:notification, user.id, %{page: 1, size: 20})
21+
{:ok, notifications} = Delivery.fetch(:notification, user, %{page: 1, size: 20})
2222
assert notifications.total_count == 1
2323

2424
notify = notifications.entries |> List.first()
@@ -32,7 +32,7 @@ defmodule GroupherServer.Test.Accounts.Hooks.Notify do
3232
Hooks.Notify.handle(:follow, user, user2)
3333
Hooks.Notify.handle(:undo, :follow, user, user2)
3434

35-
{:ok, notifications} = Delivery.fetch(:notification, user.id, %{page: 1, size: 20})
35+
{:ok, notifications} = Delivery.fetch(:notification, user, %{page: 1, size: 20})
3636
assert notifications.total_count == 0
3737
end
3838
end

test/groupher_server/cms/hooks/notify_blog_test.exs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
2727
{:ok, article} = CMS.upvote_article(:blog, blog.id, user2)
2828
Hooks.Notify.handle(:upvote, article, user2)
2929

30-
{:ok, notifications} =
31-
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
30+
{:ok, notifications} = Delivery.fetch(:notification, blog.author.user, %{page: 1, size: 20})
3231

3332
assert notifications.total_count == 1
3433

@@ -46,8 +45,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
4645

4746
Hooks.Notify.handle(:upvote, comment, user2)
4847

49-
{:ok, notifications} =
50-
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
48+
{:ok, notifications} = Delivery.fetch(:notification, comment.author, %{page: 1, size: 20})
5149

5250
assert notifications.total_count == 1
5351

@@ -69,8 +67,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
6967
{:ok, article} = CMS.undo_upvote_article(:blog, blog.id, user2)
7068
Hooks.Notify.handle(:undo, :upvote, article, user2)
7169

72-
{:ok, notifications} =
73-
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
70+
{:ok, notifications} = Delivery.fetch(:notification, blog.author.user, %{page: 1, size: 20})
7471

7572
assert notifications.total_count == 0
7673
end
@@ -85,8 +82,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
8582

8683
{:ok, comment} = preload_author(comment)
8784

88-
{:ok, notifications} =
89-
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
85+
{:ok, notifications} = Delivery.fetch(:notification, comment.author, %{page: 1, size: 20})
9086

9187
assert notifications.total_count == 0
9288
end
@@ -99,8 +95,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
9995
{:ok, _} = CMS.collect_article(:blog, blog.id, user2)
10096
Hooks.Notify.handle(:collect, blog, user2)
10197

102-
{:ok, notifications} =
103-
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
98+
{:ok, notifications} = Delivery.fetch(:notification, blog.author.user, %{page: 1, size: 20})
10499

105100
assert notifications.total_count == 1
106101

@@ -121,8 +116,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
121116
{:ok, _} = CMS.undo_upvote_article(:blog, blog.id, user2)
122117
Hooks.Notify.handle(:undo, :collect, blog, user2)
123118

124-
{:ok, notifications} =
125-
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
119+
{:ok, notifications} = Delivery.fetch(:notification, blog.author.user, %{page: 1, size: 20})
126120

127121
assert notifications.total_count == 0
128122
end
@@ -135,8 +129,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do
135129
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user2)
136130
Hooks.Notify.handle(:comment, comment, user2)
137131

138-
{:ok, notifications} =
139-
Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20})
132+
{:ok, notifications} = Delivery.fetch(:notification, blog.author.user, %{page: 1, size: 20})
140133

141134
assert notifications.total_count == 1
142135

test/groupher_server/cms/hooks/notify_job_test.exs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
2727
{:ok, article} = CMS.upvote_article(:job, job.id, user2)
2828
Hooks.Notify.handle(:upvote, article, user2)
2929

30-
{:ok, notifications} =
31-
Delivery.fetch(:notification, job.author.user.id, %{page: 1, size: 20})
30+
{:ok, notifications} = Delivery.fetch(:notification, job.author.user, %{page: 1, size: 20})
3231

3332
assert notifications.total_count == 1
3433

@@ -46,8 +45,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
4645

4746
Hooks.Notify.handle(:upvote, comment, user2)
4847

49-
{:ok, notifications} =
50-
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
48+
{:ok, notifications} = Delivery.fetch(:notification, comment.author, %{page: 1, size: 20})
5149

5250
assert notifications.total_count == 1
5351

@@ -69,8 +67,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
6967
{:ok, article} = CMS.undo_upvote_article(:job, job.id, user2)
7068
Hooks.Notify.handle(:undo, :upvote, article, user2)
7169

72-
{:ok, notifications} =
73-
Delivery.fetch(:notification, job.author.user.id, %{page: 1, size: 20})
70+
{:ok, notifications} = Delivery.fetch(:notification, job.author.user, %{page: 1, size: 20})
7471

7572
assert notifications.total_count == 0
7673
end
@@ -85,8 +82,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
8582

8683
{:ok, comment} = preload_author(comment)
8784

88-
{:ok, notifications} =
89-
Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20})
85+
{:ok, notifications} = Delivery.fetch(:notification, comment.author, %{page: 1, size: 20})
9086

9187
assert notifications.total_count == 0
9288
end
@@ -99,8 +95,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
9995
{:ok, _} = CMS.collect_article(:job, job.id, user2)
10096
Hooks.Notify.handle(:collect, job, user2)
10197

102-
{:ok, notifications} =
103-
Delivery.fetch(:notification, job.author.user.id, %{page: 1, size: 20})
98+
{:ok, notifications} = Delivery.fetch(:notification, job.author.user, %{page: 1, size: 20})
10499

105100
assert notifications.total_count == 1
106101

@@ -121,8 +116,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
121116
{:ok, _} = CMS.undo_upvote_article(:job, job.id, user2)
122117
Hooks.Notify.handle(:undo, :collect, job, user2)
123118

124-
{:ok, notifications} =
125-
Delivery.fetch(:notification, job.author.user.id, %{page: 1, size: 20})
119+
{:ok, notifications} = Delivery.fetch(:notification, job.author.user, %{page: 1, size: 20})
126120

127121
assert notifications.total_count == 0
128122
end
@@ -135,8 +129,7 @@ defmodule GroupherServer.Test.CMS.Hooks.NotifyJob do
135129
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user2)
136130
Hooks.Notify.handle(:comment, comment, user2)
137131

138-
{:ok, notifications} =
139-
Delivery.fetch(:notification, job.author.user.id, %{page: 1, size: 20})
132+
{:ok, notifications} = Delivery.fetch(:notification, job.author.user, %{page: 1, size: 20})
140133

141134
assert notifications.total_count == 1
142135

0 commit comments

Comments
 (0)