@@ -48,13 +48,22 @@ exports.all = function*() {
4848 }
4949
5050 //yield CourseGroup.populate(invite.group, {path: 'participants'});
51- yield CourseGroup . populate ( invite . group , 'participants course' ) ;
52- yield User . populate ( invite . group , 'participants.user' ) ;
51+ yield CourseGroup . populate ( invite . group , 'course' ) ;
52+
53+ var userByEmail = yield User . findOne ( {
54+ email : invite . email
55+ } ) . exec ( ) ;
56+
57+ var participantByEmail = yield CourseParticipant . findOne ( {
58+ isActive : true ,
59+ group : invite . group . _id ,
60+ user : userByEmail . _id
61+ } ) . exec ( ) ;
62+
5363
54- var participantsByEmail = _ . indexBy ( _ . pluck ( invite . group . participants , 'user' ) , 'email' ) ;
5564 // invite was NOT accepted, but this guy is a participant (added manually?),
5665 // so show the same as accepted
57- if ( participantsByEmail [ invite . email ] ) {
66+ if ( participantByEmail ) {
5867 this . addFlashMessage ( "success" , "Вы уже участник курса. Ниже, рядом с курсом, вы найдёте инструкцию." ) ;
5968 this . redirect ( this . user . getProfileUrl ( ) + '/courses' ) ;
6069 return ;
@@ -109,7 +118,8 @@ function* askParticipantDetails(invite) {
109118 var participantData = _ . pick ( this . request . body ,
110119 'photoId firstName surname country city aboutLink occupation purpose wishes' . split ( ' ' )
111120 ) ;
112- participantData . user = this . user ;
121+ participantData . user = this . user . _id ;
122+ participantData . group = invite . group . _id ;
113123
114124 if ( participantData . photoId ) {
115125 var photo = yield ImgurImage . findOne ( { imgurId : this . request . body . photoId } ) . exec ( ) ;
@@ -122,6 +132,7 @@ function* askParticipantDetails(invite) {
122132 participantData . photo = this . user . photo ;
123133 }
124134
135+
125136 var participant = new CourseParticipant ( participantData ) ;
126137
127138 try {
@@ -161,9 +172,7 @@ function* askParticipantDetails(invite) {
161172
162173}
163174
164- function * acceptParticipant ( invite , participant ) {
165-
166- invite . group . participants . push ( participant . _id ) ;
175+ function * acceptParticipant ( invite ) {
167176
168177 this . user . profileTabsEnabled . addToSet ( 'courses' ) ;
169178 yield this . user . persist ( ) ;
@@ -174,16 +183,21 @@ function* acceptParticipant(invite, participant) {
174183
175184 yield invite . group . persist ( ) ;
176185
186+ yield CourseGroup . populate ( invite . group , 'course' ) ;
187+
188+
189+ var participants = yield CourseParticipant . find ( {
190+ group : invite . group . _id ,
191+ isActive : true
192+ } ) . populate ( 'user' ) . exec ( ) ;
177193
178- yield CourseGroup . populate ( invite . group , 'participants course' ) ;
179- yield User . populate ( invite . group , 'participants.user' ) ;
180194
181195 if ( process . env . NODE_ENV != 'development' ) {
182- yield * grantXmppChatMemberships ( invite . group ) ;
196+ yield * grantXmppChatMemberships ( invite . group , participants ) ;
183197 }
184198
185199 if ( invite . group . course . videoKeyTag ) {
186- yield * grantVideoKeys ( invite . group ) ;
200+ yield * grantVideoKeys ( invite . group , participants ) ;
187201 }
188202
189203
@@ -262,7 +276,7 @@ function* loginByInvite(invite) {
262276}
263277
264278
265- function * grantXmppChatMemberships ( group ) {
279+ function * grantXmppChatMemberships ( group , participants ) {
266280 log . debug ( "Grant xmpp chat membership" ) ;
267281 // grant membership in chat
268282 var client = new xmppClient ( {
@@ -277,14 +291,13 @@ function* grantXmppChatMemberships(group) {
277291 membersOnly : 1
278292 } ) ;
279293
280-
281294 var jobs = [ ] ;
282- for ( var i = 0 ; i < group . participants . length ; i ++ ) {
283- var participant = group . participants [ i ] ;
295+ for ( var i = 0 ; i < participants . length ; i ++ ) {
296+ var participant = participants [ i ] ;
284297
285298 log . debug ( "grant " + roomJid + " to" , participant . user . profileName , participant . firstName , participant . surname ) ;
286299
287- jobs . push ( client . grantMember ( roomJid , participant . user . profileName , participant . firstName + ' ' + participant . surname ) ) ;
300+ jobs . push ( client . grantMember ( roomJid , participant . user . profileName , participant . fullName ) ) ;
288301 }
289302
290303 // grant all in parallel
@@ -293,25 +306,25 @@ function* grantXmppChatMemberships(group) {
293306 client . disconnect ( ) ;
294307}
295308
296- function * grantVideoKeys ( group ) {
309+ function * grantVideoKeys ( group , participants ) {
297310
298- var participants = group . participants . filter ( function ( participant ) {
311+ var participantsWithoutKeys = participants . filter ( function ( participant ) {
299312 return ! participant . videoKey ;
300313 } ) ;
301314
302315 var videoKeys = yield VideoKey . find ( {
303316 tag : group . course . videoKeyTag ,
304317 used : false
305- } ) . limit ( participants . length ) . exec ( ) ;
318+ } ) . limit ( participantsWithoutKeys . length ) . exec ( ) ;
306319
307320 log . debug ( "Keys selected" , videoKeys && videoKeys . toArray ( ) ) ;
308321
309- if ( ! videoKeys || videoKeys . length != participants . length ) {
310- throw new Error ( "Недостаточно серийных номеров " + participants . length ) ;
322+ if ( ! videoKeys || videoKeys . length != participantsWithoutKeys . length ) {
323+ throw new Error ( "Недостаточно серийных номеров " + participantsWithoutKeys . length ) ;
311324 }
312325
313- for ( var i = 0 ; i < participants . length ; i ++ ) {
314- var participant = participants [ i ] ;
326+ for ( var i = 0 ; i < participantsWithoutKeys . length ; i ++ ) {
327+ var participant = participantsWithoutKeys [ i ] ;
315328 participant . videoKey = videoKeys [ i ] . key ;
316329 videoKeys [ i ] . used = true ;
317330 }
0 commit comments