Skip to content

Instantly share code, notes, and snippets.

@austinwang
Created March 13, 2015 00:27
Show Gist options
  • Save austinwang/3bd66f61166534020833 to your computer and use it in GitHub Desktop.
Save austinwang/3bd66f61166534020833 to your computer and use it in GitHub Desktop.
ordering email threads
var histories = [];
this.state.currentObject.activities.forEach(function(activityHistory) {
// TYPE FILTER
if (thisComponent.state.typeFilter && thisComponent.state.typeFilter.indexOf(activityHistory.ActivityType) === -1) {
return;
}
histories.push(activityHistory);
});
// Get all the emails
var emails = [];
var nonEmails = [];
histories.forEach(function(history) {
if ( history.Subject && history.Subject.indexOf('Email:') > -1 ){
emails.push(history);
} else {
nonEmails.push(history);
}
})
var threads = {};
// Go through all the emails
emails.forEach(function(email) {
var cleanSubject = email.Subject.replace('Email: << ','') // replace both email indicators attached by groove
.replace('Email: >> ','')
.replace('Email: ','')
.replace('re: ','') // thread regardless of response
.replace('Re: ','').trim() || 'No Subject';
if (threads.hasOwnProperty(cleanSubject)) {
threads[cleanSubject]['messages'].push(email);
} else {
threads[cleanSubject] = {
isThread: true,
lastMessageDate: new Date(email.ActivityDate),
subject: cleanSubject,
messages: [email]
}
}
});
var threadArray = $.map(threads, function(value, index) {
return value;
});
var condensedHistory = threadArray.concat(nonEmails);
sortedHistory = condensedHistory.sort(function(a,b){
var aDate, bDate;
if (a.isThread) {
aDate = a.lastMessageDate;
} else {
aDate = new Date(a.ActivityDate);
}
if (b.isThread) {
bDate = b.lastMessageDate;
} else {
bDate = new Date(b.ActivityDate);
}
if (aDate > bDate)
return -1;
if (aDate < bDate)
return 1;
return 0;
});
var finalHistory = [];
sortedHistory.forEach(function(history) {
if (history.isThread) {
finalHistory.push(<EmailThread threadData={history} />);
} else {
finalHistory.push(<Activity activityData={history}/>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment