11/* global Vulcan */
2- import { newMutation , registerSetting , getSetting } from 'meteor/vulcan:core' ;
32import moment from 'moment' ;
3+ import { newMutation , registerSetting , getSetting } from 'meteor/vulcan:core' ;
4+ import Users from 'meteor/vulcan:users' ;
5+ import { Promise } from 'meteor/promise' ;
46import { Posts } from '../../modules/posts/index.js' ;
57import { Comments } from '../../modules/comments/index.js' ;
6- import { Categories } from '../../modules/categories/index.js' ;
7- import Users from 'meteor/vulcan:users' ;
88
99registerSetting ( 'forum.seedOnStart' , true , 'Seed the app with dummy content on startup' ) ;
1010
@@ -15,9 +15,10 @@ if (getSetting('forum.seedOnStart')) {
1515 fieldSchema : {
1616 type : Boolean ,
1717 optional : true ,
18- hidden : true
19- }
20- }
18+ hidden : true ,
19+ } ,
20+ } ;
21+
2122 Users . addField ( dummyFlag ) ;
2223 Posts . addField ( dummyFlag ) ;
2324 Comments . addField ( dummyFlag ) ;
@@ -27,135 +28,130 @@ if (getSetting('forum.seedOnStart')) {
2728 fieldSchema : {
2829 type : String ,
2930 optional : true ,
30- hidden : true // never show this
31- }
31+ hidden : true , // never show this
32+ } ,
3233 } ) ;
3334
34- var toTitleCase = function ( str ) {
35+ const toTitleCase = ( str ) => {
3536 return str . replace ( / \w \S * / g, function ( txt ) { return txt . charAt ( 0 ) . toUpperCase ( ) + txt . substr ( 1 ) . toLowerCase ( ) ; } ) ;
3637 } ;
3738
38- var createPost = function ( slug , postedAt , username , thumbnail ) {
39-
40- const user = Users . findOne ( { username : username } ) ;
41-
42- var post = {
39+ const createPost = async ( slug , postedAt , username , thumbnail ) => {
40+ const currentUser = await Users . rawCollection ( ) . findOne ( { username : username } ) ;
41+ const document = {
4342 postedAt : postedAt ,
4443 body : Assets . getText ( "lib/assets/content/" + slug + ".md" ) ,
4544 title : toTitleCase ( slug . replace ( / _ / g, ' ' ) ) ,
4645 dummySlug : slug ,
4746 isDummy : true ,
48- userId : user . _id
47+ userId : currentUser . _id ,
4948 } ;
5049
51- if ( typeof thumbnail !== "undefined" )
52- post . thumbnailUrl = "/packages/example-forum/lib/assets/images/" + thumbnail ;
50+ if ( typeof thumbnail !== "undefined" ) {
51+ document . thumbnailUrl = "/packages/example-forum/lib/assets/images/" + thumbnail ;
52+ }
5353
54- newMutation ( {
54+ return newMutation ( {
5555 collection : Posts ,
56- document : post ,
57- currentUser : user ,
58- validate : false
56+ document,
57+ currentUser,
58+ validate : false ,
5959 } ) ;
60-
6160 } ;
6261
63- var createComment = function ( slug , username , body , parentBody ) {
64-
65- const user = Users . findOne ( { username : username } ) ;
66-
67- var comment = {
68- postId : Posts . findOne ( { dummySlug : slug } ) . _id ,
62+ const createComment = async ( slug , username , body , parentBody ) => {
63+ const user = await Users . rawCollection ( ) . findOne ( { username : username } ) ;
64+ const comment = {
65+ postId : await Posts . rawCollection ( ) . findOne ( { dummySlug : slug } ) . _id ,
6966 userId : user . _id ,
7067 body : body ,
7168 isDummy : true ,
72- disableNotifications : true
69+ disableNotifications : true ,
7370 } ;
74- var parentComment = Comments . findOne ( { body : parentBody } ) ;
75- if ( parentComment )
71+ const parentComment = await Comments . rawCollection ( ) . findOne ( { body : parentBody } ) ;
72+
73+ if ( parentComment ) {
7674 comment . parentCommentId = parentComment . _id ;
75+ }
7776
78- newMutation ( {
77+ return newMutation ( {
7978 collection : Comments ,
8079 document : comment ,
8180 currentUser : user ,
82- validate : false
81+ validate : false ,
8382 } ) ;
8483 } ;
8584
86- const createUser = function ( username , email ) {
87- const user = {
85+ const createUser = async ( username , email ) => {
86+ const document = {
8887 username,
8988 email,
90- isDummy : true
89+ isDummy : true ,
9190 } ;
92- newMutation ( {
91+
92+ return newMutation ( {
9393 collection : Users ,
94- document : user ,
95- validate : false
94+ document,
95+ validate : false ,
9696 } ) ;
97- }
97+ } ;
9898
99- var createDummyUsers = function ( ) {
99+ const createDummyUsers = async ( ) => {
100100 // eslint-disable-next-line no-console
101101 console . log ( '// inserting dummy users…' ) ;
102- createUser ( 'Bruce' , 'dummyuser1@telescopeapp.org' ) ;
103- createUser ( 'Arnold' , 'dummyuser2@telescopeapp.org' ) ;
104- createUser ( 'Julia' , 'dummyuser3@telescopeapp.org' ) ;
102+ return Promise . all ( [
103+ createUser ( 'Bruce' , 'dummyuser1@telescopeapp.org' ) ,
104+ createUser ( 'Arnold' , 'dummyuser2@telescopeapp.org' ) ,
105+ createUser ( 'Julia' , 'dummyuser3@telescopeapp.org' ) ,
106+ ] ) ;
105107 } ;
106108
107- var createDummyPosts = function ( ) {
109+ const createDummyPosts = async ( ) => {
108110 // eslint-disable-next-line no-console
109111 console . log ( '// inserting dummy posts' ) ;
110112
111- createPost ( "read_this_first" , moment ( ) . toDate ( ) , "Bruce" , "telescope.png" ) ;
112-
113- createPost ( "deploying" , moment ( ) . subtract ( 10 , 'minutes' ) . toDate ( ) , "Arnold" ) ;
114-
115- createPost ( "customizing" , moment ( ) . subtract ( 3 , 'hours' ) . toDate ( ) , "Julia" ) ;
116-
117- createPost ( "getting_help" , moment ( ) . subtract ( 1 , 'days' ) . toDate ( ) , "Bruce" , "stackoverflow.png" ) ;
118-
119- createPost ( "removing_getting_started_posts" , moment ( ) . subtract ( 2 , 'days' ) . toDate ( ) , "Julia" ) ;
120-
113+ return Promise . all ( [
114+ createPost ( "read_this_first" , moment ( ) . toDate ( ) , "Bruce" , "telescope.png" ) ,
115+ createPost ( "deploying" , moment ( ) . subtract ( 10 , 'minutes' ) . toDate ( ) , "Arnold" ) ,
116+ createPost ( "customizing" , moment ( ) . subtract ( 3 , 'hours' ) . toDate ( ) , "Julia" ) ,
117+ createPost ( "getting_help" , moment ( ) . subtract ( 1 , 'days' ) . toDate ( ) , "Bruce" , "stackoverflow.png" ) ,
118+ createPost ( "removing_getting_started_posts" , moment ( ) . subtract ( 2 , 'days' ) . toDate ( ) , "Julia" ) ,
119+ ] ) ;
121120 } ;
122121
123- var createDummyComments = function ( ) {
122+ const createDummyComments = async ( ) => {
124123 // eslint-disable-next-line no-console
125124 console . log ( '// inserting dummy comments…' ) ;
126125
127- createComment ( "read_this_first" , "Bruce" , "What an awesome app!" ) ;
128-
129- createComment ( "deploying" , "Arnold" , "Deploy to da choppah!" ) ;
130- createComment ( "deploying" , "Julia" , "Do you really need to say this all the time?" , "Deploy to da choppah!" ) ;
131-
132- createComment ( "customizing" , "Julia" , "This is really cool!" ) ;
133-
134- createComment ( "removing_getting_started_posts" , "Bruce" , "Yippee ki-yay!" ) ;
135- createComment ( "removing_getting_started_posts" , "Arnold" , "I'll be back." , "Yippee ki-yay!" ) ;
136-
126+ return Promise . all ( [
127+ createComment ( "read_this_first" , "Bruce" , "What an awesome app!" ) ,
128+ createComment ( "deploying" , "Arnold" , "Deploy to da choppah!" ) ,
129+ createComment ( "deploying" , "Julia" , "Do you really need to say this all the time?" , "Deploy to da choppah!" ) ,
130+ createComment ( "customizing" , "Julia" , "This is really cool!" ) ,
131+ createComment ( "removing_getting_started_posts" , "Bruce" , "Yippee ki-yay!" ) ,
132+ createComment ( "removing_getting_started_posts" , "Arnold" , "I'll be back." , "Yippee ki-yay!" ) ,
133+ ] ) ;
137134 } ;
138135
139136 Vulcan . removeGettingStartedContent = ( ) => {
140- Users . remove ( { 'profile.isDummy' : true } ) ;
141- Posts . remove ( { isDummy : true } ) ;
142- Comments . remove ( { isDummy : true } ) ;
143- Categories . remove ( { isDummy : true } ) ;
137+ Users . remove ( { 'profile.isDummy' : true } ) ;
138+ Posts . remove ( { isDummy : true } ) ;
139+ Comments . remove ( { isDummy : true } ) ;
144140 // eslint-disable-next-line no-console
145- console . log ( '// Getting started content removed' ) ;
141+ console . log ( '// Getting started content removed from seed_posts ' ) ;
146142 } ;
147143
148- Meteor . startup ( function ( ) {
144+ // Uses Promise.await to await async functions in a Fiber to make them "Meteor synchronous"
145+ Meteor . startup ( ( ) => {
149146 // insert dummy content only if createDummyContent hasn't happened and there aren't any posts or users in the db
150147 if ( ! Users . find ( ) . count ( ) ) {
151- createDummyUsers ( ) ;
148+ Promise . await ( createDummyUsers ( ) ) ;
152149 }
153150 if ( ! Posts . find ( ) . count ( ) ) {
154- createDummyPosts ( ) ;
151+ Promise . await ( createDummyPosts ( ) ) ;
155152 }
156153 if ( ! Comments . find ( ) . count ( ) ) {
157- createDummyComments ( ) ;
154+ Promise . await ( createDummyComments ( ) ) ;
158155 }
159156 } ) ;
160-
161157}
0 commit comments