Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 15.1: Add a party invitation method
- Loading branch information
1 parent
f4adbab
commit 51dc559
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {Parties} from '../collections/parties.collection'; | ||
import {Email} from 'meteor/email'; | ||
import {check} from 'meteor/check'; | ||
import {Meteor} from 'meteor/meteor'; | ||
|
||
function getContactEmail(user:Meteor.User):string { | ||
if (user.emails && user.emails.length) | ||
return user.emails[0].address; | ||
|
||
return null; | ||
} | ||
|
||
Meteor.methods({ | ||
invite: function (partyId:string, userId:string) { | ||
check(partyId, String); | ||
check(userId, String); | ||
|
||
let party = Parties.collection.findOne(partyId); | ||
|
||
if (!party) | ||
throw new Meteor.Error('404', 'No such party!'); | ||
|
||
if (party.public) | ||
throw new Meteor.Error('400', 'That party is public. No need to invite people.'); | ||
|
||
if (party.owner !== this.userId) | ||
throw new Meteor.Error('403', 'No permissions!'); | ||
|
||
if (userId !== party.owner && (party.invited || []).indexOf(userId) == -1) { | ||
Parties.collection.update(partyId, {$addToSet: {invited: userId}}); | ||
|
||
let from = getContactEmail(Meteor.users.findOne(this.userId)); | ||
let to = getContactEmail(Meteor.users.findOne(userId)); | ||
|
||
if (Meteor.isServer && to) { | ||
Email.send({ | ||
from: 'noreply@socially.com', | ||
to: to, | ||
replyTo: from || undefined, | ||
subject: 'PARTY: ' + party.name, | ||
text: `Hi, I just invited you to ${party.name} on Socially. | ||
\n\nCome check it out: ${Meteor.absoluteUrl()}\n` | ||
}); | ||
} | ||
} | ||
} | ||
}); |