2014-10-04 08:48:44 +02:00
|
|
|
docs = new Meteor.Collection 'docs'
|
|
|
|
|
2014-10-06 15:42:11 +02:00
|
|
|
cleanDocuments = ->
|
|
|
|
docs.remove {
|
|
|
|
owner: {$exists: no},
|
|
|
|
dateCreated: $lte: moment().subtract(7, 'days').unix()
|
|
|
|
}, (e,n) ->
|
|
|
|
if e then console.log e
|
|
|
|
console.log n+' anonymous documents \
|
|
|
|
not updated by more than 7 days have been removed'
|
|
|
|
|
|
|
|
Meteor.startup ->
|
|
|
|
cleanDocuments()
|
|
|
|
Meteor.setInterval cleanDocuments, 3600000
|
|
|
|
|
2014-10-04 17:16:27 +02:00
|
|
|
validatedUser = (uid) ->
|
|
|
|
u = Meteor.users.findOne uid
|
2014-10-07 12:36:39 +02:00
|
|
|
return no unless u
|
2014-10-04 17:16:27 +02:00
|
|
|
return yes for mail in u.emails when mail.verified is yes; no
|
|
|
|
|
|
|
|
Meteor.publish 'doc', (id) -> docs.find {_id: id}, limit: 1
|
2014-10-06 12:50:24 +02:00
|
|
|
Meteor.publish 'docs', (userId) ->
|
2014-10-07 10:21:07 +02:00
|
|
|
if userId?
|
|
|
|
if userId is @userId
|
|
|
|
docs.find {owner: userId}, fields: text: 0
|
|
|
|
else docs.find {owner: userId, public: yes}, fields: text: 0
|
2014-10-06 12:50:24 +02:00
|
|
|
else docs.find {}, fields: text: 0
|
2014-10-06 17:14:26 +02:00
|
|
|
Meteor.publish 'user', (id) ->
|
2014-10-07 12:36:39 +02:00
|
|
|
if @userId is id
|
|
|
|
Meteor.users.find id, fields: {username: 1, emails: 1}
|
|
|
|
else Meteor.users.find id, fields: {username: 1}
|
2014-10-04 09:39:07 +02:00
|
|
|
|
|
|
|
docs.allow
|
2014-10-04 17:16:27 +02:00
|
|
|
insert: (uid,doc) ->
|
2014-10-07 12:36:39 +02:00
|
|
|
return no if uid and !validatedUser(uid)
|
2014-10-06 08:42:31 +02:00
|
|
|
if doc.text and doc.title
|
|
|
|
doc.dateCreated = moment().unix()
|
2014-10-06 15:42:11 +02:00
|
|
|
doc.showTitle ?= yes
|
2014-10-06 08:42:31 +02:00
|
|
|
if doc.owner and !uid then return no
|
|
|
|
if uid then doc.owner = uid
|
|
|
|
return yes
|
|
|
|
return no
|
|
|
|
docs.allow
|
2014-10-04 17:16:27 +02:00
|
|
|
# Owners can update and remove their documents
|
2014-10-06 15:42:11 +02:00
|
|
|
update: (uid,doc) ->
|
2014-10-07 12:36:39 +02:00
|
|
|
return no if uid and !validatedUser(uid)
|
2014-10-06 15:42:11 +02:00
|
|
|
return no unless uid is doc.owner
|
|
|
|
docs.update doc._id, $set: lastModified: moment().unix()
|
|
|
|
return yes
|
2014-10-04 17:16:27 +02:00
|
|
|
remove: (uid,doc) -> doc.owner is uid
|
2014-10-07 12:36:39 +02:00
|
|
|
fetch: ['owner','emails']
|
2014-10-04 17:16:27 +02:00
|
|
|
|
2014-10-06 17:14:26 +02:00
|
|
|
Meteor.methods
|
|
|
|
'deleteMe': ->
|
|
|
|
if @userId
|
|
|
|
Meteor.users.remove @userId
|
|
|
|
docs.remove owner: @userId
|
2014-10-07 12:36:39 +02:00
|
|
|
'amIValid': -> validatedUser @userId
|
2014-10-06 17:14:26 +02:00
|
|
|
'sendVerificationEmail': ->
|
2014-10-07 12:36:39 +02:00
|
|
|
if @userId and not validatedUser @userId
|
|
|
|
Accounts.sendVerificationEmail @userId
|
|
|
|
return 'email sent'
|
|
|
|
else return 'could not send email'
|