@@ -3,30 +3,89 @@ defmodule GroupherServer.Accounts.Delegate.Publish do
3
3
user followers / following related
4
4
"""
5
5
import Ecto.Query , warn: false
6
- import Helper.Utils , only: [ done: 1 ]
6
+ import Helper.Utils , only: [ done: 1 , ensure: 2 ]
7
7
# import Helper.ErrorCode
8
8
import ShortMaps
9
9
10
+ import GroupherServer.CMS.Helper.Matcher
10
11
import GroupherServer.CMS.Helper.MatcherOld
11
12
13
+ alias GroupherServer.Accounts . { Embeds , User }
14
+ alias GroupherServer.CMS.ArticleComment
12
15
alias Helper . { ORM , QueryBuilder }
13
- # alias GroupherServer.{Accounts, Repo}
14
16
15
- alias GroupherServer.Accounts.User
16
- # alias GroupherServer.CMS
17
+ @ default_meta Embeds.UserMeta . default_meta ( )
17
18
18
19
@ doc """
19
20
get paged published contets of a user
20
21
"""
21
- def published_contents ( % User { id: user_id } , thread , % { page: page , size: size } = filter ) do
22
- with { :ok , user } <- ORM . find ( User , user_id ) ,
23
- { :ok , content } <- match_action ( thread , :self ) do
24
- content . target
25
- |> join ( :inner , [ content ] , author in assoc ( content , :author ) )
26
- |> where ( [ content , author ] , author . user_id == ^ user . id )
27
- |> select ( [ content , author ] , content )
22
+ def paged_published_articles ( % User { id: user_id } , thread , filter ) do
23
+ with { :ok , info } <- match ( thread ) ,
24
+ { :ok , user } <- ORM . find ( User , user_id ) do
25
+ do_paged_published_articles ( info . model , user , filter )
26
+ end
27
+ end
28
+
29
+ @ doc """
30
+ update published articles count in user meta
31
+ """
32
+ def update_published_states ( user_id , thread ) do
33
+ filter = % { page: 1 , size: 1 }
34
+
35
+ with { :ok , info } <- match ( thread ) ,
36
+ { :ok , user } <- ORM . find ( User , user_id ) ,
37
+ { :ok , paged_published_articles } <- do_paged_published_articles ( info . model , user , filter ) do
38
+ articles_count = paged_published_articles . total_count
39
+
40
+ meta =
41
+ ensure ( user . meta , @ default_meta ) |> Map . put ( :"published_#{ thread } s_count" , articles_count )
42
+
43
+ ORM . update_meta ( user , meta )
44
+ end
45
+ end
46
+
47
+ defp do_paged_published_articles ( queryable , % User { } = user , % { page: page , size: size } = filter ) do
48
+ queryable
49
+ |> join ( :inner , [ article ] , author in assoc ( article , :author ) )
50
+ |> where ( [ article , author ] , author . user_id == ^ user . id )
51
+ |> select ( [ article , author ] , article )
52
+ |> QueryBuilder . filter_pack ( filter )
53
+ |> ORM . paginater ( ~m( page size) a )
54
+ |> done ( )
55
+ end
56
+
57
+ def paged_published_article_comments ( % User { id: user_id } , % { page: page , size: size } = filter ) do
58
+ with { :ok , user } <- ORM . find ( User , user_id ) do
59
+ ArticleComment
60
+ |> join ( :inner , [ comment ] , author in assoc ( comment , :author ) )
61
+ |> where ( [ comment , author ] , author . id == ^ user . id )
62
+ |> QueryBuilder . filter_pack ( filter )
63
+ |> ORM . paginater ( ~m( page size) a )
64
+ |> ORM . extract_and_assign_article ( )
65
+ |> done ( )
66
+ end
67
+ end
68
+
69
+ def paged_published_article_comments (
70
+ % User { id: user_id } ,
71
+ thread ,
72
+ % { page: page , size: size } = filter
73
+ ) do
74
+ with { :ok , user } <- ORM . find ( User , user_id ) do
75
+ thread = thread |> to_string |> String . upcase ( )
76
+ thread_atom = thread |> String . downcase ( ) |> String . to_atom ( )
77
+
78
+ # article_preload = Keyword.new([{thread_atom, :author}])
79
+ # query = from(comment in ArticleComment, preload: ^article_preload)
80
+ query = from ( comment in ArticleComment , preload: ^ thread_atom )
81
+
82
+ query
83
+ |> join ( :inner , [ comment ] , author in assoc ( comment , :author ) )
84
+ |> where ( [ comment , author ] , author . id == ^ user . id )
85
+ |> where ( [ comment , author ] , comment . thread == ^ thread )
28
86
|> QueryBuilder . filter_pack ( filter )
29
87
|> ORM . paginater ( ~m( page size) a )
88
+ |> ORM . extract_and_assign_article ( )
30
89
|> done ( )
31
90
end
32
91
end