示例#1
0
 cloudStorageUtils.putBuffer(data, s3Path, headers, true, false, function(err, res) {
   if ( err ) {
     winston.handleError(err);
   } else {
     winston.doInfo('putBuffer successful');
   }
 });
示例#2
0
        utils.streamToBuffer( parsedMailAttachment.stream, false, function(err, attachmentBuffer ) {
          if ( err ) {
            isDone = true;
            callback( err );

          } else {
            parsedMailAttachment.myId = crypto.randomBytes (32).toString ('hex');

            if ( ( ! parsedMailAttachment.myId ) || ( attachmentBuffers[parsedMailAttachment.myId] ) ) {
              isDone = true;
              callback( winston.makeError('invalid or duplicate id for attachment', {randomBytes: parsedMailAttachment.myId}) );

            } else {
              attachmentBuffers[parsedMailAttachment.myId] = attachmentBuffer;
              if ( parsedMailComplete && ( Object.keys(attachmentBuffers).length == parsedMailComplete.attachments.length ) ) {
                winston.doInfo('streamed attachments finished after mailparser');
                isDone = true;
                if ( attachmentHandler.addAttachmentBuffersAndValidate( parsedMailComplete, attachmentBuffers ) ) {
                  mailReader.runMailProcessor( message, parsedMailComplete, mailId, userId, mailProcessorCallback );
                } else {
                  callback( winston.makeError('invalid attachments', {mailId: mailId}) );
                }
              }
            }
          }
        });
示例#3
0
  mailParser.on('end', function( parsedMail ) {
    if ( isDone ) {
      winston.doWarn('mailparser ended after already done');

    } else {
      parsedMailComplete = parsedMail;
      if ( ! mailReaderConstants.STREAM_ATTACHMENTS ) {
        isDone = true;
        mailReader.runMailProcessor( message, parsedMailComplete, mailId, userId, mailProcessorCallback );

      } else {
        if ( ( ! parsedMailComplete.attachments )
          || ( ! ( parsedMailComplete.attachments.length > 0 ) )
          || ( Object.keys(attachmentBuffers).length === parsedMailComplete.attachments.length ) ) {

            isDone = true;
            if ( attachmentHandler.addAttachmentBuffersAndValidate( parsedMailComplete, attachmentBuffers ) ) {
              mailReader.runMailProcessor( message, parsedMailComplete, mailId, userId, mailProcessorCallback );
            } else {
              callback( winston.makeError('invalid attachments', {mailId: mailId}) );
            }
        } else {
          winston.doWarn('mailparser ended but attachments are still outstanding', {mailId: mailId, attachmentBuffersLength: attachmentBuffers.length, parsedMailCompleteAttachmentsLength: parsedMailComplete.attachments.length});
        }
      }
    }
  });
示例#4
0
exports.setMailReaderState = function( mailId, state, userId, callback ) {

  winston.doInfo('mailReader: setMailReaderState...', {mailId : mailId, state : state});

  if ( ! mailId ) { callback( winston.makeMissingParamError('mailId') ); return; }
  if ( ! state ) { callback( winston.makeMissingParamError('state') ); return; }

  var filter = {
      _id: mailId
  };

  var updateSet = {$set: {
    mailReaderState: state
  }, $inc : {
    tries : 1
  }};

  MailModel.update( filter, updateSet, function (err, num) {
    if ( err ) {
      callback( winston.makeMongoError(err) );

    } else if ( num === 0 ) {
      callback( winston.makeError('no updated mail', {mailId: mailId, state: state, userId: userId}) );

    } else {
      callback();
    }
  });
}
 attachmentInfo.save (function (err) {
   count+=1;
   winston.doInfo('saved attachmentInfo', {count: count});
   if (err) {
     winston.doMongoError (err);
   }
 });
示例#6
0
	UserModel.findOne({email:userEmail}, function(mongoErr, foundUser) {
		if ( mongoErr ) {
			callback( winston.makeMongoError(mongoErr) );

		} else if ( ! foundUser ) {
			callback( winston.makeMissingParamError('user') );

		} else {
			var userId = foundUser._id;
			var extraDays = new ExtraDaysModel({
					userId: userId
				, numExtraDays: numExtraDays
				, grantorLastName: grantorLastName
			});

			extraDays.save( function(mongoErr) {
				if ( mongoErr ) {
					callback( winston.makeMongoError(mongoErr) );

				} else {
					upgradeUtils.updateUserAccountStatusByUserId( userId, false, callback );
				}
			});
		}
	});
示例#7
0
    fs.readFile( emailFile, function(err, res) {
      if ( err ) {
        forEachSeriesCallback( winston.makeError('error reading file') );
        
      } else if ( ! res ) {
        forEachSeriesCallback( winston.makeMissingParamError('res') );

      } else {

        var mailParser = new MailParser();

        mailParser.on('end', function( parsedMail ) {
          threadLink.handleParsedMail( parsedMail, function(err) {
            if ( err ) {
              winston.handleError(err);
            }
            mailParserDoneCallback();
          });
        });

        mailParser.write(res);
        mailParser.end();
        mailParserDoneCallback = forEachSeriesCallback;
      }
    });
示例#8
0
exports.grantUserPromotionAction = function( userEmail, promotionAction, callback ) {
  if ( ! userEmail ) { callback( winston.makeMissingParamError('userEmail') ); return; }
  if ( ! promotionAction ) { callback( winston.makeMissingParamError('promotionAction') ); return; }

  if ( ( promotionAction != serverCommonConstants.PROMOTION_TYPE_CHROMESTORE_REVIEW ) && ( promotionAction != serverCommonConstants.PROMOTION_TYPE_FACEBOOK_LIKE ) ) {
    callback( winston.makeError('invalid promotionAction', {promotionAction: promotionAction}) );
    return;
  }

  var updateData = {$set: {}};
  if ( promotionAction == serverCommonConstants.PROMOTION_TYPE_CHROMESTORE_REVIEW ) {
    updateData['$set']['clickedChromeStoreReview'] = true;
  } else if ( promotionAction == serverCommonConstants.PROMOTION_TYPE_FACEBOOK_LIKE ) {
    updateData['$set']['clickedFacebookLike'] = true;
  }

  UserModel.findOneAndUpdate({email: userEmail}, updateData, function(mongoErr, foundUser) {
    if ( mongoErr ) {
      callback( winston.makeMongoError( mongoErr ) );

    } else {
      var forceResumeDownloadJob = false;
      upgradeUtils.updateUserAccountStatus( foundUser, forceResumeDownloadJob, callback );
    }
  });
}
 function (err) {
   if (err) {
     winston.handleError (err);
   } else {
     winston.doInfo('all done for all users');
   }
 });
    .exec (function (err, foundLinks) {
      if (err) {
        cb (winston.makeMongoError (err));
      } else if (foundLinks && foundLinks.length) {
        var len = foundLinks.length;
        var totalCallbacks = 0;
        winston.doInfo ('About to create link reindexing jobs for user ', {userId : userId, numLinks : len});

        foundLinks.forEach (function (link) {
          
          if (createReindexingJobs.jobAlreadyQueued (link)) {
            totalCallbacks++;
          }
          else {
            indexingHandler.createIndexingJobForDocument ( link, true, true, function (err) {
              totalCallbacks++;
              if (err) {
                winston.doError ('could not push job to queue for link', {err :err, linkId : link._id});
              } else if (len == totalCallbacks) {
                cb ();
              }
            });
          }
        });

      } else {
        cb ();
      }
    });
    .exec (function (err, foundAttachments) {
      if (err) {
        cb (winston.makeMongoError (err));
      } else if (foundAttachments && foundAttachments.length) {
        var len = foundAttachments.length;
        var totalCallbacks = 0;
        winston.doInfo ('About to create attachment reindexing jobs for user ', {userId : userId, numAttachments : len});

        foundAttachments.forEach (function (attachment) {

          if (createReindexingJobs.jobAlreadyQueued (attachment)) {
            winston.doInfo('job already queued');
            totalCallbacks++;
          }
          else {

            indexingHandler.createIndexingJobForDocument ( attachment, false, true, function (err) {
              totalCallbacks++;
              if (err) {
                winston.doError ('could not push job to queue for attachment', {err :err, attachmentId : attachment._id});
              } else if (len == totalCallbacks) {
                cb ();
              }
            });
          }
        });
      } else {
        cb ();
      }
    });
示例#12
0
exports.buildAttachment = function( message, parsedMailAttachment, mail ) {

  if ( ! parsedMailAttachment ) { winston.doMissingParamError('parsedMailAttachment'); return null; }
  if ( ! mail ) { winston.doMissingParamError('mail'); return null; }
  if ( ! mail.userId ) { winston.doMissingParamError('mail.userId'); return null; }

  var attachment = new AttachmentModel({
      userId: mail.userId
    , mailId: mail._id
    , filename: parsedMailAttachment.fileName
    , contentType: parsedMailAttachment.contentType
    , isImage: attachmentUtils.isAttachmentImage( parsedMailAttachment )
    , docType : attachmentUtils.getDocType (parsedMailAttachment.contentType)
    , fileSize: parsedMailAttachment.length
    , sentDate: mail.sentDate
    , sender: mailUtils.copySender( mail.sender )
    , recipients: mail.recipients
    , mailCleanSubject: mail.cleanSubject
    , gmThreadId: mail.gmThreadId
    , gmMsgId: mail.gmMsgId
    , gmMsgHex : mailUtils.getHexValue (mail.gmMsgId)
    , isPromoted: attachmentHandler.isPromotable(parsedMailAttachment )
    , hash: attachmentHandler.getMailAttachmentHash(parsedMailAttachment )
    //, image: image //If it's an image itself, we'll set a signedURL on this in the route.
  });

  return attachment;  
}
示例#13
0
exports.handleAttachment = function( message, parsedMailAttachment, parsedMail, mail, callback ) {

  if ( ! parsedMailAttachment ) { callback( winston.makeMissingParamError('parsedMailAttachment') ); return; }
  if ( ! parsedMail ) { callback( winston.makeMissingParamError('parsedMail') ); return; }
  if ( ! mail ) { callback( winston.makeMissingParamError('mail') ); return; }

  //winston.doInfo('attachmentHandler: handleAttachment...', {mailId: mail._id});

  // set a default filename if it doesn't exist
  if (! parsedMailAttachment.fileName ) {
    parsedMailAttachment.fileName = "untitled";
  }

  //Filter invalid attachments...
  if ( ! parsedMailAttachment.length || !parsedMailAttachment.contentType) {
    var warnData = {mailId: mail._id, contentType: parsedMailAttachment.contentType};
    winston.doWarn('attachmentHandler: handleAttachment: missing filename or contentType', warnData);
    callback();

  } else {

    attachmentHandler.lookForDuplicateOnThread ( parsedMailAttachment, mail, function (err, duplicateAttachment) {
      if (err) {
        callback (err);
      } else if (duplicateAttachment) {
        attachmentHandler.handleDuplicateOnThread (message, parsedMailAttachment, mail, duplicateAttachment, callback);
      } else {
        attachmentHandler.buildCheckSaveUploadAttachment(message, parsedMailAttachment, parsedMail, mail, callback );
      }
    });

  }
}
 function (err) {
   if (err) {
     winston.handleError (err);
   }
   winston.doInfo('all done');
   process.exit (1);
 }]);
示例#15
0
exports.lookForDuplicateOnThread = function (parsedMailAttachment, mail, callback) {
  if ( ! parsedMailAttachment ) { callback( winston.makeMissingParamError('parsedMailAttachment') ); return; }
  if ( ! mail ) { callback( winston.makeMissingParamError('mail') ); return; }

  var hash = attachmentHandler.getMailAttachmentHash(parsedMailAttachment );
  var fileSize = parsedMailAttachment.length;

  var duplicateSearchCriteria = {
      hash: hash
    , fileSize: fileSize
    , userId: mail.userId
    , gmThreadId: mail.gmThreadId
  }

  AttachmentModel.findOne( duplicateSearchCriteria, function(err, foundAttachment) {
    if ( err ) {
      callback( winston.makeMongoError(err) );

    } else if ( ! foundAttachment ) {
      //No duplicate on this thread, move along...
      callback();
    
    } else {
      callback (null, foundAttachment);

    }
  });
}
示例#16
0
  async.waterfall (operations, function (err) {

    if (err && !err.warning) {
      winston.doWarn ('Could not finish updating', err);
      callback (err);
    }
    else {
      winston.doInfo('closing mailbox ' + user.email);

      // close the mailbox
      imapConnect.closeMailbox (myConnection, function (err) {
        if (err) {
          winston.doError ('Could not close mailbox', {err : err});
        }
        else {
          imapConnect.logout (myConnection, function (err) {
            if (err) {
              winston.doError ('Could not logout', {err : err});
            }
          });
        }
      });

      callback ();
    }

  })
              AttachmentModel.findOne ({hash : attachmentInfo.hash, fileSize : attachmentInfo.fileSize}, function (err, att) {
                if (err) {
                  asyncCb (winston.makeMongoError (err));
                } else if (!att) {
                  winston.doWarn ('AttachmentInfo without attachment', {hash : attachmentInfo.hash, fileSize : attachmentInfo.fileSize});
                  asyncCb ();
                } else {
                  MailModel.findById (att.mailId, function (err, foundMail) {
                    if (err) {
                      asyncCb (winston.makeMongoError (err));

                    } else if (!foundMail) {
                      winston.doWarn ('Could not find mail with id', {_id : att.mailId})
                      asyncCb ();

                    } else {

                      foundMail.mailReaderState = 'softFail'
                      foundMail.save (function (err) {
                        if (err) {
                          asyncCb (winston.makeMongoError (err));
                          return;     
                        } else {
                          winston.doInfo ('updated mail state to softFail', {mailId : att.mailId});
                          asyncCb ();
                        }
                      });
                    }
                  });
                }
              });
示例#18
0
cloudStorageUtils.downloadAndSaveImage( url, true, function (err, path) {
  if (err) {
    winston.doError('test failed', {err: err});
    return;
  }
  winston.doInfo('path', {path: path});
});
示例#19
0
exports.updateMail = function( parsedMail, mail, callback ) {

  //winston.doInfo('mailReader: updateMail...');

  if ( ! parsedMail ) { callback( winston.makeMissingParamError('parsedMail') ); return; }
  if ( ! mail ) { callback( winston.makeMissingParamError('mail') ); return; }

  var sentDate = mailUtils.getSentDate( parsedMail );
  if ( mail.gmDate ) {
    sentDate = mail.gmDate;
  }

  var filter = {
      _id: mail._id
  };
  
  var updateSet = { $set: {
      messageId: mailUtils.getMessageId( parsedMail )
    , subject: mailUtils.getDirtySubject( parsedMail.subject )
    , cleanSubject: mailUtils.getCleanSubject( parsedMail.subject )
    , numAttachments: mailUtils.getNumAttachments( parsedMail )
    , sentDate: sentDate
    , mailReaderState: 'started'
  }};

  MailModel.findOneAndUpdate( filter, updateSet, function(err, updatedMail) {
    if ( err ) {
      callback( winston.makeMongoError(err) );
    } else {
      updatedMail.bodyText = mailUtils.getBodyText( parsedMail );
      updatedMail.bodyHTML = mailUtils.getBodyHTML( parsedMail );
      callback(null, updatedMail);
    }
  });
}
示例#20
0
exports.getLinks = function(req, res) {

  if ( ( ! req ) || ( ! req.user ) || ( ! req.user._id ) ) {
    winston.doWarn('routeLinks: getLinks: missing userId');
    res.send(400, 'missing userId');
    return;
  }

  var user = req.user;
  var userId = user._id;
  var before = req.query.before;
  var after = req.query.after;
  var limit = req.query.limit;
  var isFavorite = req.query.isFavorite;

  if (!isFavorite) {
    isFavorite = false;
  }

  if (!limit) {
    limit = 50
  }

  var query = LinkModel.find({userId:userId, 'isPromoted':true, 'isFollowed':true, 'isDeleted' : false, 'isFavorite' : isFavorite })

  if ( before && ( before != Infinity ) && ( before != 'Infinity' ) ) {
    query.where ('sentDate').lt(before);
  }

  if ( after && ( after != -Infinity ) && ( after != '-Infinity' ) ) {
    query.where('sentDate').gt(after);
  }

  if ( ! user.isPremium ) {
    var daysLimit = user.daysLimit;
    if ( ! daysLimit ) {
      winston.doError('user is not premium, but has no daysLimit', {userId: userId});

    } else {
      var currentTime = Date.now();
      var cutoffDate = new Date(currentTime - daysLimit*constants.ONE_DAY_IN_MS);
      if ( ( ! after ) || ( cutoffDate > new Date(after) ) ) {
        query.where('sentDate').gt(cutoffDate);
      }
    }
  }
      
  query.sort ('-sentDate')
    .limit (limit)
    .select(constants.DEFAULT_FIELDS_LINK)
    .exec(function(err, foundLinks) {
      if ( err ) {
        winston.doMongoError(err, null, res);
      } else {
        linkHelpers.addSignedURLs( foundLinks )
        res.send( foundLinks );
      }
    }
  );
}
示例#21
0
    .exec (function (err, linkInfos) {
      if (err) {
        winston.doMongoError (err);
      }
      else {
        var numLinkInfos = linkInfos.length;
        winston.doInfo ('requeing linkinfos ', {links : numLinkInfos});

        async.forEach (linkInfos, function (linkInfo, asyncCb) {
          // find random user that has the link
          LinkModel.findOne ({comparableURLHash : linkInfo.comparableURLHash})
            .select ('userId')
            .exec (function (err, link) {
              if (err) {
                asyncCb (winston.makeMongoError (err));
              } else if (!link) {
                asyncCb ();
              } else {
                followLinkUtils.createFollowLinkJob (linkInfo, link.userId);
                asyncCb();
              }
            });
        }, function (err) {
          if (err) {
            callback (err);
          } else {
            if (numLinkInfos == limit) {
              callback (null, numLinkInfos + skip);
            } else {
              callback ();
            }
          }
        });
      }
    });
示例#22
0
    schemaModel.findById( modelId, function(err, foundModel) {
      if ( err ) {
        callback( winston.makeMongoError( err ) );

      } else if ( ! foundModel ) {
        callback( winston.makeError('no model with id', {modelId: modelId, isLink: isLink}) );

      } else {
        foundModel["index"] = undefined;
        foundModel.save( function(mongoErr) {
          if ( mongoErr ) {
            callback( winston.makeMongoError( mongoErr ) );

          } else {
            schemaModel.findById( modelId, function(err, foundModel) {
              if ( err ) {
                callback( winston.makeMongoError( err ) );

              } else if ( ! foundModel ) {
                callback( winston.makeError('no model with id', {modelId: modelId, isLink: isLink}) );

              } if ( foundModel.index && foundModel.index.length ) {
                callback( winston.makeError('model still has index') );

              } else {
                var job = indexingHandler.getIndexingJobForDocument( foundModel, isLink );
                job = JSON.parse(JSON.stringify(job));

                if ( ! job ) {
                  callback( winston.makeError('no job') );

                } else {
                  indexingHandler.doIndexingJob( job, function(err) {
                    if ( err ) {
                      callback( err );

                    } else {
                      schemaModel.findById( modelId, function(err, foundModel) {
                        if ( err ) {
                          callback( winston.makeMongoError( err ) );

                        } else if ( ! foundModel ) {
                          callback( winston.makeError('no model with id', {modelId: modelId, isLink: isLink}) );

                        } else if ( !foundModel.index ) {
                          callback( winston.makeError('model has NO index') );

                        } else {
                          callback();
                        }
                      });
                    }
                  });
                }
              }
            });
          }
        });
      }
    });
示例#23
0
exports.inheritAttachmentFromAttachmentInfo = function(attachment, attachmentInfo ) {

  if ( ! attachment ) { winston.doMissingParamError('attachment'); return; }
  if ( ! attachmentInfo ) { winston.doMissingParamError('attachmentInfo'); return; }

  attachment.attachmentThumbExists = attachmentInfo.attachmentThumbExists;
}
示例#24
0
 }, function(err) {
   if ( err ) {
     winston.handleError(err);
   } else {
     mongoose.disconnect();
     winston.doInfo('cleanup done!');
   }
 });
示例#25
0
  run( function(err) {
    if ( err ) {
      winston.handleError( err );

    } else {
      winston.doInfo('success');
    }
    mongoose.disconnect();
  })
 foundMail.save (function (err) {
   if (err) {
     asyncCb (winston.makeMongoError (err));
     return;     
   } else {
     winston.doInfo ('updated mail state to softFail', {mailId : att.mailId});
     asyncCb ();
   }
 });
 var processBatchCallback  = function (err, newIndex) {
   if (err) {
     winston.doMongoError (err);
   } else if (newIndex) {
     processBatch (newIndex, processBatchCallback);
   } else {
     winston.doInfo('all done');
   }
 }
示例#28
0
 function doBatchCallback (err, skip) {
   if (err) {
     winston.doError('error', {err: err});
   } 
   else if (skip) {
     retryFollows.doBatch (skip, doBatchCallback);
   } else {
     winston.doInfo('all done');
   }
 }
示例#29
0
exports.getAttachmentInfo = function(attachment, parsedMailAttachment, callback ) {

  if ( ! attachment ) { callback( winston.makeMissingParamError('attachment') ); return; }
  if ( ! parsedMailAttachment ) { callback( winston.makeMissingParamError('parsedMailAttachment') ); return; }

  //winston.doInfo('attachmentHandler: getAttachmentInfo...', {attachmentHash: attachment.hash});

  var filter = {
      hash: attachment.hash
    , fileSize: attachment.fileSize
  }

  var updateSet = { 
    $set: {
        hash: attachment.hash
      , fileSize: attachment.fileSize
      , isImage: attachment.isImage
      , docType: attachment.docType
    }
  };
  
  if (attachment.contentType) {
    updateSet['$set']['contentType'] = attachment.contentType.trim();
  }

  var options = {
      upsert:true
    , new: false
  }

  AttachmentInfoModel.findOneAndUpdate(filter, updateSet, options, function(err, previousAttachmentInfo) {
    if ( err ) {
      callback( winston.makeMongoError( err ) );

    } else {
      //Lookup the thing we just saved.
      //This seems wasteful, but I'd really like to have the clean attachmentInfo.
      AttachmentInfoModel.findOne(filter, function(err, attachmentInfo) {
        if ( err ) {
          callback(err);

        } else if ( ! attachmentInfo ) {
          callback( winston.makeError('failed to find attachmentInfo we just upserted', filter) );

        } else if ( ( ! previousAttachmentInfo._id ) || ( ! attachmentInfo.isUploaded ) ) {
          //we either just created this attachmentInfo, or we haven't uploaded it before
          attachmentHandler.uploadAttachmentInfoToCloud(attachmentInfo, parsedMailAttachment, callback );
        } else {
          callback( null, attachmentInfo );
        }
      });
    }
  });
}
示例#30
0
    imapConnect.openMailbox (myConnection, user.email, function (err, mailbox) {

      if (err) {
        winston.addExtra( err, {userId : user._id, userEmail : user.email} );
        winston.handleError(err);
        return;
      }

      winston.doInfo('Connection opened for user: ' + user.email);
      mailUpdateHelper.updateMailbox (user, myConnection, mailbox, 0, pollQueueCallback);
    })