@@ -50,7 +50,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
50
50
requireFetch : false ,
51
51
hasTimestamps : true ,
52
52
53
- _localId : null , // Used to store the localId of the post coming from the client and passed back to the client, for optimistic updates
53
+ _localId : null , // Used to store the localId of the post coming from the client and passed back to the client, for optimistic updates
54
54
55
55
// Instance Methods
56
56
@@ -152,10 +152,12 @@ module.exports = bookshelf.Model.extend(Object.assign({
152
152
} ,
153
153
154
154
comments : function ( ) {
155
- return this . hasMany ( Comment , 'post_id' ) . query ( { where : {
156
- 'comments.active' : true ,
157
- 'comments.comment_id' : null
158
- } } )
155
+ return this . hasMany ( Comment , 'post_id' ) . query ( {
156
+ where : {
157
+ 'comments.active' : true ,
158
+ 'comments.comment_id' : null
159
+ }
160
+ } )
159
161
} ,
160
162
161
163
linkPreview : function ( ) {
@@ -256,13 +258,13 @@ module.exports = bookshelf.Model.extend(Object.assign({
256
258
257
259
getCommentersTotal : function ( currentUserId ) {
258
260
return countTotal ( User . query ( commentersQuery ( null , this , currentUserId ) ) . query ( ) , 'users' )
259
- . then ( result => {
260
- if ( isEmpty ( result ) ) {
261
- return 0
262
- } else {
263
- return result [ 0 ] . total
264
- }
265
- } )
261
+ . then ( result => {
262
+ if ( isEmpty ( result ) ) {
263
+ return 0
264
+ } else {
265
+ return result [ 0 ] . total
266
+ }
267
+ } )
266
268
} ,
267
269
268
270
// Emulate the graphql request for a post in the feed so the feed can be
@@ -272,8 +274,8 @@ module.exports = bookshelf.Model.extend(Object.assign({
272
274
getNewPostSocketPayload : function ( ) {
273
275
const { media, groups, linkPreview, tags, user, proposalOptions } = this . relations
274
276
275
- const creator = refineOne ( user , [ 'id' , 'name' , 'avatar_url' ] )
276
- const topics = refineMany ( tags , [ 'id' , 'name' ] )
277
+ const creator = refineOne ( user , [ 'id' , 'name' , 'avatar_url' ] )
278
+ const topics = refineMany ( tags , [ 'id' , 'name' ] )
277
279
278
280
// TODO: Sanitization -- sanitize details here if not passing through `text` getter
279
281
return Object . assign ( { } ,
@@ -301,14 +303,14 @@ module.exports = bookshelf.Model.extend(Object.assign({
301
303
{ name : 'title' , num_people_reacts : 'peopleReactedTotal' , num_votes : 'votesTotal' }
302
304
) ,
303
305
{
304
- attachments : refineMany ( media , [ 'id' , 'type' , 'url' ] ) ,
306
+ attachments : refineMany ( media , [ 'id' , 'type' , 'url' ] ) ,
305
307
// Shouldn't have commenters immediately after creation
306
308
commenters : [ ] ,
307
309
commentsTotal : 0 ,
308
310
creator,
309
311
details : this . details ( ) ,
310
- groups : refineMany ( groups , [ 'id' , 'name' , 'slug' ] ) ,
311
- linkPreview : refineOne ( linkPreview , [ 'id' , 'image_url' , 'title' , 'description' , 'url' ] ) ,
312
+ groups : refineMany ( groups , [ 'id' , 'name' , 'slug' ] ) ,
313
+ linkPreview : refineOne ( linkPreview , [ 'id' , 'image_url' , 'title' , 'description' , 'url' ] ) ,
312
314
proposalOptions,
313
315
proposalVotes : [ ] ,
314
316
topics
@@ -369,11 +371,11 @@ module.exports = bookshelf.Model.extend(Object.assign({
369
371
370
372
unreadCountForUser : function ( userId ) {
371
373
return this . lastReadAtForUser ( userId )
372
- . then ( date => {
373
- if ( date > this . get ( 'updated_at' ) ) return 0
374
- return Aggregate . count ( this . comments ( ) . query ( q =>
375
- q . where ( 'created_at' , '>' , date ) ) )
376
- } )
374
+ . then ( date => {
375
+ if ( date > this . get ( 'updated_at' ) ) return 0
376
+ return Aggregate . count ( this . comments ( ) . query ( q =>
377
+ q . where ( 'created_at' , '>' , date ) ) )
378
+ } )
377
379
} ,
378
380
379
381
// ****** Setters ******//
@@ -391,11 +393,11 @@ module.exports = bookshelf.Model.extend(Object.assign({
391
393
const newUserIds = difference ( userIds , existingUserIds )
392
394
const updatedFollowers = await this . updateFollowers ( existingUserIds , updatedAttribs , { transacting } )
393
395
const newFollowers = [ ]
394
- for ( let id of newUserIds ) {
396
+ for ( const id of newUserIds ) {
395
397
const follower = await this . postUsers ( ) . create (
396
398
Object . assign ( { } , updatedAttribs , {
397
399
user_id : id ,
398
- created_at : new Date ( ) ,
400
+ created_at : new Date ( )
399
401
} ) , { transacting } )
400
402
newFollowers . push ( follower )
401
403
}
@@ -498,11 +500,11 @@ module.exports = bookshelf.Model.extend(Object.assign({
498
500
} ,
499
501
500
502
pushTypingToSockets : function ( userId , userName , isTyping , socketToExclude ) {
501
- pushToSockets ( postRoom ( this . id ) , 'userTyping' , { userId, userName, isTyping} , socketToExclude )
503
+ pushToSockets ( postRoom ( this . id ) , 'userTyping' , { userId, userName, isTyping } , socketToExclude )
502
504
} ,
503
505
504
506
copy : function ( attrs ) {
505
- var that = this . clone ( )
507
+ const that = this . clone ( )
506
508
_ . merge ( that . attributes , Post . newPostAttrs ( ) , attrs )
507
509
delete that . id
508
510
delete that . attributes . id
@@ -517,7 +519,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
517
519
let activitiesToCreate = [ ]
518
520
519
521
const mentions = RichText . getUserMentions ( this . details ( ) )
520
- let mentioned = mentions . map ( userId => ( {
522
+ const mentioned = mentions . map ( userId => ( {
521
523
reader_id : userId ,
522
524
post_id : this . id ,
523
525
actor_id : this . get ( 'user_id' ) ,
@@ -534,20 +536,20 @@ module.exports = bookshelf.Model.extend(Object.assign({
534
536
qb . whereIn ( 'tag_id' , tags . map ( 'id' ) )
535
537
qb . whereIn ( 'tag_follows.group_id' , groups . map ( 'id' ) )
536
538
} )
537
- . fetchAll ( { withRelated : [ 'tag' ] , transacting : trx } )
539
+ . fetchAll ( { withRelated : [ 'tag' ] , transacting : trx } )
538
540
539
541
const tagFollowers = tagFollows . map ( tagFollow => ( {
540
542
reader_id : tagFollow . get ( 'user_id' ) ,
541
543
post_id : this . id ,
542
- actor_id : this . get ( 'user_id' ) ,
543
- group_id : tagFollow . get ( 'group_id' ) ,
544
- reason : `chat: ${ tagFollow . relations . tag . get ( 'name' ) } `
545
- } ) )
544
+ actor_id : this . get ( 'user_id' ) ,
545
+ group_id : tagFollow . get ( 'group_id' ) ,
546
+ reason : `chat: ${ tagFollow . relations . tag . get ( 'name' ) } `
547
+ } ) )
546
548
547
549
activitiesToCreate = activitiesToCreate . concat ( tagFollowers )
548
550
} else {
549
551
// Non-chat posts are sent to all members of the groups the post is in
550
- let members = await Promise . all ( groups . map ( async group => {
552
+ const members = await Promise . all ( groups . map ( async group => {
551
553
const userIds = await group . members ( ) . fetch ( ) . then ( u => u . pluck ( 'id' ) )
552
554
const newPosts = userIds . map ( userId => ( {
553
555
reader_id : userId ,
@@ -579,7 +581,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
579
581
const eventInvitations = await EventInvitation . query ( qb => {
580
582
qb . where ( 'event_id' , this . id )
581
583
} )
582
- . fetchAll ( { transacting : trx } )
584
+ . fetchAll ( { transacting : trx } )
583
585
584
586
const invitees = eventInvitations . map ( eventInvitation => ( {
585
587
reader_id : eventInvitation . get ( 'user_id' ) ,
@@ -683,7 +685,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
683
685
} ,
684
686
685
687
updateProposalOutcome : function ( proposalOutcome ) {
686
- return Post . where ( { id : this . id } ) . query ( ) . update ( { proposal_outcome : proposalOutcome } )
688
+ return Post . where ( { id : this . id } ) . query ( ) . update ( { proposal_outcome : proposalOutcome } )
687
689
} ,
688
690
689
691
removeFromGroup : function ( idOrSlug ) {
@@ -738,7 +740,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
738
740
} ,
739
741
740
742
countForUser : function ( user , type ) {
741
- const attrs = { user_id : user . id , 'posts.active' : true }
743
+ const attrs = { user_id : user . id , 'posts.active' : true }
742
744
if ( type ) attrs . type = type
743
745
return this . query ( ) . count ( ) . where ( attrs ) . then ( rows => rows [ 0 ] . count )
744
746
} ,
@@ -749,13 +751,13 @@ module.exports = bookshelf.Model.extend(Object.assign({
749
751
q . join ( 'tags' , 'tags.id' , 'posts_tags.tag_id' )
750
752
q . whereIn ( 'tags.name' , [ 'request' , 'offer' , 'resource' ] )
751
753
q . groupBy ( 'tags.name' )
752
- q . where ( { 'posts.user_id' : user . id , 'posts.active' : true } )
754
+ q . where ( { 'posts.user_id' : user . id , 'posts.active' : true } )
753
755
q . select ( 'tags.name' )
754
756
} ) . query ( ) . count ( )
755
- . then ( rows => rows . reduce ( ( m , n ) => {
756
- m [ n . name ] = n . count
757
- return m
758
- } , { } ) )
757
+ . then ( rows => rows . reduce ( ( m , n ) => {
758
+ m [ n . name ] = n . count
759
+ return m
760
+ } , { } ) )
759
761
} ,
760
762
761
763
havingExactFollowers ( userIds ) {
@@ -764,7 +766,7 @@ module.exports = bookshelf.Model.extend(Object.assign({
764
766
q . join ( 'posts_users' , 'posts.id' , 'posts_users.post_id' )
765
767
q . where ( 'posts_users.active' , true )
766
768
q . groupBy ( 'posts.id' )
767
- q . having ( bookshelf . knex . raw ( ` array_agg(posts_users.user_id order by posts_users.user_id) = ?` , [ userIds ] ) )
769
+ q . having ( bookshelf . knex . raw ( ' array_agg(posts_users.user_id order by posts_users.user_id) = ?' , [ userIds ] ) )
768
770
} )
769
771
} ,
770
772
@@ -821,16 +823,16 @@ module.exports = bookshelf.Model.extend(Object.assign({
821
823
} )
822
824
} ,
823
825
824
- upcomingPostReminders : function ( collection , digestType ) {
826
+ upcomingPostReminders : async function ( group , digestType ) {
825
827
const startTime = DateTime . now ( ) . setZone ( defaultTimezone ) . toISO ( )
826
828
// If daily digest show posts that have reminders in the next 2 days
827
829
// If weekly digest show posts that have reminders in the next 7 days
828
830
const endTime = digestType === 'daily'
829
831
? DateTime . now ( ) . setZone ( defaultTimezone ) . plus ( { days : 2 } ) . endOf ( 'day' ) . toISO ( )
830
832
: DateTime . now ( ) . setZone ( defaultTimezone ) . plus ( { days : 7 } ) . endOf ( 'day' ) . toISO ( )
831
833
832
- const startingSoon = collection . query ( function ( qb ) {
833
- qb . whereRaw ( 'posts.start_time between ? and ?' , [ startTime , endTime ] )
834
+ const startingSoon = await group . posts ( ) . query ( function ( qb ) {
835
+ qb . whereRaw ( '( posts.start_time between ? and ?) ' , [ startTime , endTime ] )
834
836
qb . whereIn ( 'posts.type' , [ 'event' , 'offer' , 'project' , 'proposal' , 'resource' , 'request' ] )
835
837
qb . where ( 'posts.fulfilled_at' , null )
836
838
qb . where ( 'posts.active' , true )
@@ -839,9 +841,9 @@ module.exports = bookshelf.Model.extend(Object.assign({
839
841
. fetch ( { withRelated : [ 'user' ] } )
840
842
. then ( get ( 'models' ) )
841
843
842
- const endingSoon = collection . query ( function ( qb ) {
843
- qb . whereRaw ( 'posts.end_time between ? and ?' , [ startTime , endTime ] )
844
- qb . whereRaw ( 'start_time < ?' , [ startTime ] ) // Only show posts as ending soon that have already started
844
+ const endingSoon = await group . posts ( ) . query ( function ( qb ) {
845
+ qb . whereRaw ( '( posts.end_time between ? and ?) ' , [ startTime , endTime ] )
846
+ qb . whereRaw ( '(posts. start_time < ?) ' , startTime ) // Explicitly cast to timestamp with time zone
845
847
qb . whereIn ( 'posts.type' , [ 'event' , 'offer' , 'project' , 'proposal' , 'resource' , 'request' ] )
846
848
qb . where ( 'posts.fulfilled_at' , null )
847
849
qb . where ( 'posts.active' , true )
@@ -850,10 +852,10 @@ module.exports = bookshelf.Model.extend(Object.assign({
850
852
. fetch ( { withRelated : [ 'user' ] } )
851
853
. then ( get ( 'models' ) )
852
854
853
- return Promise . all ( [ startingSoon , endingSoon ] ) . then ( ( [ startingSoon , endingSoon ] ) => ( {
855
+ return {
854
856
startingSoon,
855
857
endingSoon
856
- } ) )
858
+ }
857
859
} ,
858
860
859
861
newPostAttrs : ( ) => ( {
@@ -870,17 +872,17 @@ module.exports = bookshelf.Model.extend(Object.assign({
870
872
} ,
871
873
872
874
async updateFromNewComment ( { postId, commentId } ) {
873
- const where = { post_id : postId , 'comments.active' : true }
875
+ const where = { post_id : postId , 'comments.active' : true }
874
876
const now = new Date ( )
875
877
876
878
return Promise . all ( [
877
879
Comment . query ( ) . where ( where ) . orderBy ( 'created_at' , 'desc' ) . limit ( 2 )
878
- . pluck ( 'id' ) . then ( ids => Promise . all ( [
879
- Comment . query ( ) . whereIn ( 'id' , ids ) . update ( 'recent' , true ) ,
880
- Comment . query ( ) . whereNotIn ( 'id' , ids )
881
- . where ( { recent : true , post_id : postId } )
882
- . update ( 'recent' , false )
883
- ] ) ) ,
880
+ . pluck ( 'id' ) . then ( ids => Promise . all ( [
881
+ Comment . query ( ) . whereIn ( 'id' , ids ) . update ( 'recent' , true ) ,
882
+ Comment . query ( ) . whereNotIn ( 'id' , ids )
883
+ . where ( { recent : true , post_id : postId } )
884
+ . update ( 'recent' , false )
885
+ ] ) ) ,
884
886
885
887
// update num_comments and updated_at (only update the latter when
886
888
// creating a comment, not deleting one)
@@ -892,16 +894,16 @@ module.exports = bookshelf.Model.extend(Object.assign({
892
894
893
895
// when creating a comment, mark post as read for the commenter
894
896
commentId && Comment . where ( 'id' , commentId ) . query ( ) . pluck ( 'user_id' )
895
- . then ( ( [ userId ] ) => Post . find ( postId )
896
- . then ( post => post . markAsRead ( userId ) ) )
897
+ . then ( ( [ userId ] ) => Post . find ( postId )
898
+ . then ( post => post . markAsRead ( userId ) ) )
897
899
] )
898
900
} ,
899
901
900
902
deactivate : postId =>
901
903
bookshelf . transaction ( trx =>
902
904
Promise . join (
903
905
Activity . removeForPost ( postId , trx ) ,
904
- Post . where ( 'id' , postId ) . query ( ) . update ( { active : false } ) . transacting ( trx )
906
+ Post . where ( 'id' , postId ) . query ( ) . update ( { active : false } ) . transacting ( trx )
905
907
) ) ,
906
908
907
909
createActivities : ( opts ) =>
0 commit comments