@@ -6,6 +6,10 @@ import Post from 'app/models/post';
6
6
import Reaction from 'app/models/reaction' ;
7
7
import User from 'app/models/user' ;
8
8
9
+ const createMessageTemplate = resourceType => ( name , reactionType ) => (
10
+ `${ name } reacted to your ${ resourceType } with ${ reactionType } `
11
+ ) ;
12
+
9
13
/* TODO: Add support for polymorphic relationship to a 'trackable'.
10
14
* https://github.com/postlight/lux/issues/75
11
15
*/
@@ -18,70 +22,70 @@ class Action extends Model {
18
22
19
23
async notifyOwner ( ) {
20
24
const { trackableId, trackableType } = this ;
21
- let params ;
22
25
23
26
if ( trackableType === 'Comment' ) {
24
- const {
25
- postId,
26
- userId
27
- } = await Comment . find ( trackableId , {
28
- select : [ 'postId' , 'userId' ]
29
- } ) ;
27
+ const trackable = await Comment
28
+ . first ( )
29
+ . select ( 'postId' , 'userId' )
30
+ . where ( { id : trackableId } ) ;
30
31
31
- const [
32
- { name : userName } ,
33
- { userId : recipientId }
34
- ] = await Promise . all ( [
35
- User . find ( userId , { select : [ 'name' ] } ) ,
36
- Post . find ( postId , { select : [ 'userId' ] } )
37
- ] ) ;
32
+ if ( trackable ) {
33
+ const [ user , post ] = await Promise . all ( [
34
+ User
35
+ . first ( )
36
+ . select ( 'name' )
37
+ . where ( { id : trackable . userId } ) ,
38
+ Post
39
+ . first ( )
40
+ . select ( 'userId' )
41
+ . where ( { id : trackable . postId } )
42
+ ] ) ;
38
43
39
- params = {
40
- recipientId,
41
- message : `${ userName } commented on your post!`
42
- } ;
44
+ if ( user && post ) {
45
+ await Notification . create ( {
46
+ message : `${ user . name } commented on your post!` ,
47
+ recipientId : post . userId
48
+ } ) ;
49
+ }
50
+ }
43
51
} else if ( trackableType === 'Reaction' ) {
44
52
let reactableId ;
45
- let reactableModel = Post ;
46
-
47
- const {
48
- commentId,
49
- postId,
50
- type,
51
- userId
52
- } = await Reaction . find ( trackableId , {
53
- select : [
54
- 'commentId' ,
55
- 'postId' ,
56
- 'type' ,
57
- 'userId'
58
- ]
59
- } ) ;
53
+ let createMessage ;
54
+ let ReactableModel ;
60
55
61
- if ( ! postId ) {
62
- reactableId = commentId ;
63
- reactableModel = Comment ;
64
- } else {
65
- reactableId = postId ;
66
- }
56
+ const reaction = await Reaction
57
+ . first ( )
58
+ . where ( { id : trackableId } ) ;
67
59
68
- const [
69
- { name : userName } ,
70
- { userId : recipientId }
71
- ] = await Promise . all ( [
72
- User . find ( userId , { select : [ 'name' ] } ) ,
73
- reactableModel . find ( reactableId , { select : [ 'userId' ] } )
74
- ] ) ;
60
+ if ( reaction ) {
61
+ if ( ! reaction . postId ) {
62
+ ReactableModel = Comment ;
63
+ createMessage = createMessageTemplate ( 'comment' ) ;
64
+ reactableId = reaction . commentId ;
65
+ } else {
66
+ ReactableModel = Post ;
67
+ createMessage = createMessageTemplate ( 'post' ) ;
68
+ reactableId = reaction . postId ;
69
+ }
75
70
76
- params = {
77
- recipientId,
78
- message : `${ userName } reacted with ${ type } to your ` +
79
- `${ reactableModel . name . toLowerCase ( ) } !`
80
- } ;
81
- }
71
+ const [ user , reactable ] = await Promise . all ( [
72
+ User
73
+ . first ( )
74
+ . select ( 'name' )
75
+ . where ( { id : reaction . userId } ) ,
76
+ ReactableModel
77
+ . first ( )
78
+ . select ( 'userId' )
79
+ . where ( { id : reactableId } )
80
+ ] ) ;
82
81
83
- if ( params ) {
84
- return await Notification . create ( params ) ;
82
+ if ( user && reactable ) {
83
+ await Notification . create ( {
84
+ message : createMessage ( user . name , reaction . type ) ,
85
+ recipientId : reactable . userId
86
+ } ) ;
87
+ }
88
+ }
85
89
}
86
90
}
87
91
}
0 commit comments