setComment: function(id: string, comment: string) { check(comment, String); check(id, String); comment = comment.replace(/[^\w\s]/gi, ''); let character = Characters.findOne({ uid: this.userId }); if (!character) { throw new Meteor.Error("error", "The server does not know about your character."); } let fit = _.findWhere(character.fits, { fid: id, }); if (!fit) { throw new Meteor.Error("error", "Can't find that fit in your fits."); } return Characters.update({ _id: character._id, "fits.fid": id }, { $set: { "fits.$.comment": comment } }); },
addMessageTask: function (taskId: string, message: string) { check(taskId, String); check(message, String); if (!message.length || message.length < 3) { throw new Meteor.Error('400', 'message-to-short'); } if (message.length > 1024) { throw new Meteor.Error('400', 'message-to-long'); } // verif user & task let user = Meteor.user(); if (!user) { throw new Meteor.Error('403', 'not-authorized'); } let task = Tasks.find({ _id: taskId, $or: [ { owner: this.userId }, { 'targets.userId': this.userId } ] }); if (!task) { throw new Meteor.Error('403', 'not-authorized'); } Tasks.update(taskId, { $push: { conversation: { message: message, createdAt: new Date(), owner: user._id, ownerEmail: user.emails[0].address } } }); },
createNewConversation:function(title:string, sender:string, recipient:string, message:string){ check(title, String); check(sender, String); check(recipient, String); check(message, String); var dateCreated = moment().format('MMMM Do YYYY, h:mm:ss a'); var first = Users.find({"_id":sender}).fetch()[0].profile.firstname; var last = Users.find({"_id":sender}).fetch()[0].profile.lastname; var senderName = first + " " + last; ConversationStreams.insert({ title:title, created: dateCreated, subscribers:[ {'user':sender, 'unread':0, 'subscribed':1}, {'user':recipient, 'unread':1, 'subscribed':1} ], messages:[ new Message(dateCreated, senderName, message) ] }, (err, insertionID)=>{ if(err){ // message insertion failed console.log(err); }else{ // Insert new conversation reference in the users subscriptions updateUserSubscription(sender, insertionID, 'sender'); updateUserSubscription(recipient, insertionID, 'recipient'); // console.log('maybe we got it right!'); } }); },
invite: function (trackId:string, userId:string) { check(trackId, String); check(userId, String); let track = Tracks.collection.findOne(trackId); if (!track) throw new Meteor.Error('404', 'No such track!'); if (track.public) throw new Meteor.Error('400', 'That track is public. No need to invite people.'); if (track.owner !== this.userId) throw new Meteor.Error('403', 'No permissions!'); if (userId !== track.owner && (track.invited || []).indexOf(userId) == -1) { Tracks.collection.update(trackId, {$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: '*****@*****.**', to: to, replyTo: from || undefined, subject: 'TRACK: ' + track.name, text: `Hi, I just invited you to ${track.name} on Beetrut. \n\nCome check it out: ${Meteor.absoluteUrl()}\n` }); } } },
invite: function (partyId:string, userId:string) { check(partyId, String); check(userId, String); let party = Parties.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.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: '*****@*****.**', 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` }); } } },
foo: function (arg1: string, arg2: number[]) { check(arg1, String); check(arg2, [Number]); var you_want_to_throw_an_error = true; if (you_want_to_throw_an_error) throw new Meteor.Error("404", "Can't find my pants"); return "some return value"; },
addChat: function (roomId: string, message: { text: string, timestamp: Date, tags: string }) { check(roomId, String); check(message, { text: String, timestamp: Date, // Optional, but if present must be an array of strings. tags: Match.Optional('Test String') }); // ... do something with the message ... }
constructor( hCurObserver: Meteor.LiveQueryHandle, hAutoNotify?: Tracker.Computation) { check(hAutoNotify, Match.Optional(Tracker.Computation)); check(hCurObserver, Match.Where(function(observer) { return !!observer.stop; })); this._hAutoNotify = hAutoNotify; this._hCurObserver = hCurObserver; }
reply: function(partyId: string, rsvp: string) { check(partyId, String); check(rsvp, String); if(!this.userId) { throw new Meteor.Error('403', 'You must be logged in to reply'); } if(['yes', 'no', 'maybe'].indexOf(rsvp) === -1) { throw new Meteor.Error('400', 'Invalid RSVP'); } let party = Parties.findOne(partyId); if(!party) { throw new Meteor.Error('404', 'No such party!'); } if(party.owner === this.userId) { throw new Meteor.Error('500', 'You are the owner!'); } if(!party.public && (!party.invited || party.invited.indexOf(this.userId) === -1)) { throw new Meteor.Error('403', 'No such party!'); // It's private but let's not tell user that } let rsvpIndex = party.rsvps ? party.rsvps.findIndex((rsvp) => rsvp.userId === this.userId) : -1; if(rsvpIndex !== -1) { // update existing rsvp entry if(Meteor.isServer) { // update the appropriate rsvp entry with $ Parties.update( {_id: partyId, 'rsvps.userId': this.userId}, {$set: {'rsvps.$.response': rsvp}} ); } else { // minimongo doesn't yet support $ in modifier. as a temporary // workaround, make a modifier that uses an index. this is // safe on the client since there's only one thread. let modifier = {$set: {} }; modifier.$set['rsvps.' + rsvpIndex + '.response'] = rsvp; Parties.update(partyId, modifier); } } else { // add new rsvp entry Parties.update(partyId, { $push: {rsvps: {userId: this.userId, response: rsvp}} }); } }
sendAnswer: function (ticket: Ticket) { check(ticket._id, String); check(ticket.nbTokensToGive, Number); check(ticket.answerComment, String); Tickets.update(ticket._id, { $set: { nbTokensToGive: ticket.nbTokensToGive, answerComment: ticket.answerComment } }); //TODO : send an email }