2014-05-27 15:33:50 +02:00
|
|
|
# Homework - Server Side
|
2014-05-31 18:09:56 +02:00
|
|
|
notes = share.notes
|
2014-05-28 18:45:41 +02:00
|
|
|
console.log "Started Homework server!"
|
2014-05-30 08:27:49 +02:00
|
|
|
if process.env.MAIL_URL
|
|
|
|
console.log "Sending emails using "+process.env.MAIL_URL
|
|
|
|
else
|
|
|
|
console.log "Not Sending Emails, please set the MAIL_URL environment variable"
|
2014-05-28 18:45:41 +02:00
|
|
|
|
2014-05-27 15:33:50 +02:00
|
|
|
notes = new Meteor.Collection "notes"
|
2014-05-28 18:45:41 +02:00
|
|
|
getUser = (id) -> Meteor.users.findOne { _id: id }
|
2014-06-03 14:34:06 +02:00
|
|
|
isUsers = (u,doc) -> u and doc.userId is u
|
2014-05-27 16:02:37 +02:00
|
|
|
|
2014-05-28 10:19:06 +02:00
|
|
|
# Returns true if the user has verified at least one email address
|
|
|
|
userValidated = (user) ->
|
2014-06-01 11:47:27 +02:00
|
|
|
return no unless user?
|
2014-05-28 10:19:06 +02:00
|
|
|
return yes for mail in user.emails when mail.verified is yes; no
|
|
|
|
|
2014-05-30 08:27:49 +02:00
|
|
|
# Publish user's notes to each user.
|
2014-05-27 15:33:50 +02:00
|
|
|
Meteor.publish "my-notes", ->
|
2014-05-28 18:45:41 +02:00
|
|
|
if userValidated getUser(@userId)
|
2014-06-01 11:47:27 +02:00
|
|
|
notes.find userId: @userId, archived: no
|
|
|
|
Meteor.publish "archive", ->
|
|
|
|
if userValidated getUser(@userId)
|
|
|
|
notes.find userId: @userId, archived: yes
|
2014-05-27 15:33:50 +02:00
|
|
|
|
2014-06-03 14:34:06 +02:00
|
|
|
# Database Permissions
|
|
|
|
# Allow all users to insert, update and remove their notes.
|
|
|
|
notes.allow insert: isUsers, update: isUsers, remove: isUsers
|
|
|
|
|
2014-05-28 10:19:06 +02:00
|
|
|
# Methods that the clients can invoke
|
|
|
|
Meteor.methods
|
2014-05-28 18:45:41 +02:00
|
|
|
resendConfirmEmail: ->
|
|
|
|
u = getUser(@userId)
|
|
|
|
if not u
|
2014-05-30 10:33:32 +02:00
|
|
|
console.log "Validating nonexisting user!"; return no
|
2014-05-28 18:45:41 +02:00
|
|
|
if userValidated(u) is no
|
|
|
|
Accounts.sendVerificationEmail @userId
|
|
|
|
console.log "Sent verification email to "+u.emails[0].address
|
|
|
|
return yes
|
|
|
|
else
|
|
|
|
console.log "User "+u.emails[0].address+" already validated."
|
|
|
|
return no
|
|
|
|
deleteMe: ->
|
|
|
|
if @userId
|
|
|
|
Meteor.users.remove @userId
|
|
|
|
# Automagically log out the user by invalidating every token he has
|
|
|
|
Meteor.users.update {_id: @userId},
|
|
|
|
{$set : { "resume.loginTokens" : [] } }, { multi: yes }
|
2014-05-30 10:33:32 +02:00
|
|
|
return yes
|
|
|
|
else no
|