I18nMessages resources,
boolean notifySubscribers)
throws MailingException {
WeblogEntry entry = commentObject.getWeblogEntry();
Weblog weblog = entry.getWebsite();
User user = entry.getCreator();
// Only send email if email notificaiton is enabled
boolean notify = WebloggerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
if (!notify || !weblog.getEmailComments().booleanValue()) {
// notifications disabled, just bail
return;
}
log.debug("Comment notification enabled ... preparing email");
// Determine message and addressing options from init parameters
boolean hideCommenterAddrs = WebloggerConfig.getBooleanProperty(
"comment.notification.hideCommenterAddresses");
// use either the weblog configured from address or the site configured from address
String from = weblog.getEmailFromAddress();
if(StringUtils.isEmpty(from)) {
// TODO: this should not be the users email address
from = user.getEmailAddress();
}
// build list of email addresses to send notification to
Set subscribers = new TreeSet();
// If we are to notify subscribers, then...
if (notifySubscribers) {
log.debug("Sending notification email to all subscribers");
// Get all the subscribers to this comment thread
List comments = entry.getComments(true, true);
for (Iterator it = comments.iterator(); it.hasNext();) {
WeblogEntryComment comment = (WeblogEntryComment) it.next();
if (!StringUtils.isEmpty(comment.getEmail())) {
// If user has commented twice,
// count the most recent notify setting
if (comment.getNotify().booleanValue()) {
// only add those with valid email
if (comment.getEmail().matches(EMAIL_ADDR_REGEXP)) {
subscribers.add(comment.getEmail());
}
} else {
// remove user who doesn't want to be notified
subscribers.remove(comment.getEmail());
}
}
}
} else {
log.debug("Sending notification email only to weblog owner");
}
// Form array of commenter addrs
String[] commenterAddrs = (String[])subscribers.toArray(new String[0]);
//------------------------------------------
// --- Form the messages to be sent -
// Build separate owner and commenter (aka subscriber) messages
// Determine with mime type to use for e-mail
StringBuffer msg = new StringBuffer();
StringBuffer ownermsg = new StringBuffer();
boolean escapeHtml = !WebloggerRuntimeConfig.getBooleanProperty("users.comments.htmlenabled");
// first the commenter message
if (!escapeHtml) {
msg.append("<html><body style=\"background: white; ");
msg.append(" color: black; font-size: 12px\">");
}
if (!StringUtils.isEmpty(commentObject.getName())) {
msg.append(commentObject.getName() + " "
+ resources.getString("email.comment.wrote")+": ");
} else {
msg.append(resources.getString("email.comment.anonymous")+": ");
}
msg.append((escapeHtml) ? "\n\n" : "<br /><br />");
msg.append((escapeHtml) ? Utilities.escapeHTML(commentObject.getContent())
: Utilities.transformToHTMLSubset(Utilities.escapeHTML(commentObject.getContent())));
msg.append((escapeHtml) ? "\n\n----\n"
: "<br /><br /><hr /><span style=\"font-size: 11px\">");
msg.append(resources.getString("email.comment.respond") + ": ");
msg.append((escapeHtml) ? "\n" : "<br />");
// Build link back to comment
String commentURL = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogCommentsURL(weblog, null, entry.getAnchor(), true);
if (escapeHtml) {
msg.append(commentURL);
} else {
msg.append("<a href=\""+commentURL+"\">"+commentURL+"</a></span>");
}
// next the owner message
// First, list any messages from the system that were passed in:
if (messages.getMessageCount() > 0) {
ownermsg.append((escapeHtml) ? "" : "<p>");
ownermsg.append(resources.getString("commentServlet.email.thereAreSystemMessages"));
ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
ownermsg.append((escapeHtml) ? "" : "<ul>");
}
for (Iterator it = messages.getMessages(); it.hasNext();) {
RollerMessage rollerMessage = (RollerMessage)it.next();
ownermsg.append((escapeHtml) ? "" : "<li>");
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()), (Object[])rollerMessage.getArgs()) );
ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
}
if (messages.getMessageCount() > 0) {
ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
}
// Next, list any validation error messages that were passed in:
if (messages.getErrorCount() > 0) {
ownermsg.append((escapeHtml) ? "" : "<p>");
ownermsg.append(resources.getString("commentServlet.email.thereAreErrorMessages"));
ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
ownermsg.append((escapeHtml) ? "" : "<ul>");
}
for (Iterator it = messages.getErrors(); it.hasNext();) {
RollerMessage rollerMessage = (RollerMessage)it.next();
ownermsg.append((escapeHtml) ? "" : "<li>");
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()), (Object[])rollerMessage.getArgs()) );
ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
}
if (messages.getErrorCount() > 0) {
ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
}
ownermsg.append(msg);
// add link to weblog edit page so user can login to manage comments
ownermsg.append((escapeHtml) ? "\n\n----\n" :
"<br /><br /><hr /><span style=\"font-size: 11px\">");
ownermsg.append("Link to comment management page:");
ownermsg.append((escapeHtml) ? "\n" : "<br />");
Map<String, String> parameters = new HashMap();
parameters.put("bean.entryId", entry.getId());
String deleteURL = WebloggerFactory.getWeblogger().getUrlStrategy().getActionURL(
"comments", "/roller-ui/authoring", weblog.getHandle(), parameters, true);
if (escapeHtml) {
ownermsg.append(deleteURL);
} else {
ownermsg.append(