@Transactional protected static ReviewComment saveReviewComment( Resource target, User sender, Content content, String messageID, Address[] allRecipients) throws MessagingException, IOException, NoSuchAlgorithmException { ReviewComment comment; CommentThread thread = CommentThread.find.byId(Long.valueOf(target.getId())); if (thread == null) { throw new IllegalArgumentException(); } comment = new ReviewComment(); comment.setContents(content.body); comment.author = new UserIdent(sender); comment.thread = thread; comment.save(); Map<String, Attachment> relatedAttachments = saveAttachments(content.attachments, comment.asResource()); if (new ContentType(content.type).match(MimeType.HTML)) { // replace cid with attachments comment.setContents(replaceCidWithAttachments(comment.getContents(), relatedAttachments)); comment.update(); } new OriginalEmail(messageID, comment.asResource()).save(); // Add the event if (thread.isOnPullRequest()) { addEvent( NotificationEvent.forNewComment(sender, thread.pullRequest, comment), allRecipients, sender); } else { try { String commitId; if (thread instanceof CodeCommentThread) { commitId = ((CodeCommentThread) thread).commitId; } else if (thread instanceof NonRangedCodeCommentThread) { commitId = ((NonRangedCodeCommentThread) thread).commitId; } else { throw new IllegalArgumentException(); } addEvent( NotificationEvent.forNewCommitComment(target.getProject(), comment, commitId, sender), allRecipients, sender); } catch (Exception e) { Logger.warn("Failed to send a notification", e); } } return comment; }
private Notification convert(final NotificationEvent event) { final Notification notification = new Notification(); notification.setNodeUrn(new NodeUrn(event.getNodeUrn())); notification.setTimestamp(new DateTime(event.getTimestamp())); notification.setMsg(event.getMessage()); return notification; }
@Override public void write(org.apache.thrift.protocol.TProtocol prot, NotificationEventResponse struct) throws org.apache.thrift.TException { TTupleProtocol oprot = (TTupleProtocol) prot; { oprot.writeI32(struct.events.size()); for (NotificationEvent _iter554 : struct.events) { _iter554.write(oprot); } } }
@Override public void startup() throws Exception { super.startup(); NotificationIndex index = receiver.getNotificationIndex(); // filter down to listeners who can handle requeueing gracefully ArrayList<NotificationListener> gracefulListeners = new ArrayList<NotificationListener>(); Iterator<NotificationListener> iter = this.notificationListeners.keySet().iterator(); while (iter.hasNext()) { NotificationListener listener = iter.next(); // make sure each index only notifies each listener once String key = listener.getName() + '|' + index.getName(); if (AUTOLOADED_INDEXES.contains(key)) { // already loaded this notification index for this listener // another receiver is sharing this notification index } else if (listener instanceof DefaultNotificationListener && ((DefaultNotificationListener) listener).getNotificationIndex() != null) { gracefulListeners.add(listener); AUTOLOADED_INDEXES.add(key); } } if (gracefulListeners.size() == 0) { // don't bother searching if nobody is listening return; } LOGGER.info( "[" + receiver.getName() + "] requeueing notification index '" + index.getName() + "'"); // find all existing notifications Iterator<Notification> allNotifications = index .findNotifications((List<String>) null, (List<String>) null, (List<String>) null) .iterator(); LOGGER.info("Done finding existing notifications"); // queue them for processing in case they were previous missed Date now = new Date(); while (allNotifications.hasNext()) { NotificationEvent event = new NotificationEvent(receiver, allNotifications.next()); if (event.getNotification().getExpirationDate().after(now)) { // still valid this.notifyListeners(event, gracefulListeners); } } LOGGER.info("All notifications queued"); // keep track that we've processed this notification index AUTOLOADED_INDEXES.add(index.getName()); }
/** * Create a comment from the given email. * * @param message * @param target * @throws MessagingException * @throws MailHandlerException * @throws IOException * @throws NoSuchAlgorithmException */ @Transactional public static Comment saveComment(IMAPMessage message, Resource target) throws MessagingException, MailHandlerException, IOException, NoSuchAlgorithmException { User author = IMAPMessageUtil.extractSender(message); if (!AccessControl.isProjectResourceCreatable(author, target.getProject(), target.getType())) { throw new PermissionDenied( cannotCreateMessage(author, target.getProject(), target.getType())); } Content parsedMessage = extractContent(message); Comment comment = makeNewComment(target, author, parsedMessage.body); comment.save(); Map<String, Attachment> relatedAttachments = saveAttachments(parsedMessage.attachments, comment.asResource()); if (new ContentType(parsedMessage.type).match(MimeType.HTML)) { // replace cid with attachments comment.contents = replaceCidWithAttachments(comment.contents, relatedAttachments); comment.update(); } new OriginalEmail(message.getMessageID(), comment.asResource()).save(); // Add the event addEvent(NotificationEvent.forNewComment(comment, author), message.getAllRecipients(), author); return comment; }
@Transactional public static Issue saveIssue( String subject, Project project, User sender, Content parsedMessage, String messageId, Address[] recipients) throws MessagingException, IOException, NoSuchAlgorithmException { Issue issue = new Issue(project, sender, subject, parsedMessage.body); issue.save(); NotificationEvent event = NotificationEvent.forNewIssue(issue, sender); Map<String, Attachment> relatedAttachments = saveAttachments(parsedMessage.attachments, issue.asResource()); if (new ContentType(parsedMessage.type).match(MimeType.HTML)) { // replace cid with attachments issue.body = replaceCidWithAttachments(issue.body, relatedAttachments); issue.update(); } new OriginalEmail(messageId, issue.asResource()).save(); // Add the event addEvent(event, recipients, sender); return issue; }
@Override public void read(org.apache.thrift.protocol.TProtocol prot, NotificationEventResponse struct) throws org.apache.thrift.TException { TTupleProtocol iprot = (TTupleProtocol) prot; { org.apache.thrift.protocol.TList _list555 = new org.apache.thrift.protocol.TList( org.apache.thrift.protocol.TType.STRUCT, iprot.readI32()); struct.events = new ArrayList<NotificationEvent>(_list555.size); NotificationEvent _elem556; for (int _i557 = 0; _i557 < _list555.size; ++_i557) { _elem556 = new NotificationEvent(); _elem556.read(iprot); struct.events.add(_elem556); } } struct.setEventsIsSet(true); }
private static void addEvent(NotificationEvent event, Address[] recipients, User sender) { HashSet<User> emailUsers = new HashSet<>(); emailUsers.add(sender); for (Address addr : recipients) { emailUsers.add(User.findByEmail(((InternetAddress) addr).getAddress())); } event.receivers.removeAll(emailUsers); NotificationEvent.add(event); }
@Override public void onStart(Application app) { isSecretInvalid = equalsDefaultSecret(); insertInitialData(); PullRequest.onStart(); NotificationMail.onStart(); NotificationEvent.onStart(); Attachment.onStart(); }
@Override public void onPostExecute(NotificationEvent _event) { if (_event == null || _event.getTravelTime() == FAILURE) { Log.w(TAG, "Some problem determining whether late or not"); // TODO User notification return; } Time t = new Time(); t.setToNow(); long nowSec = TimeUnit.SECONDS.convert(t.toMillis(false), TimeUnit.MILLISECONDS); boolean isLate = isLate(nowSec, _event.getTravelTime(), _event.getCalendarEvent().getWhen()); if (isLate) { Log.i(TAG, "Event *is* late: " + mCalendarEvent.getDebugString()); buildLateNotification(_event, mCalendarEvent); } else { Log.i(TAG, "Event *is not* late: " + mCalendarEvent.getDebugString()); } }
public void write(org.apache.thrift.protocol.TProtocol oprot, NotificationEventResponse struct) throws org.apache.thrift.TException { struct.validate(); oprot.writeStructBegin(STRUCT_DESC); if (struct.events != null) { oprot.writeFieldBegin(EVENTS_FIELD_DESC); { oprot.writeListBegin( new org.apache.thrift.protocol.TList( org.apache.thrift.protocol.TType.STRUCT, struct.events.size())); for (NotificationEvent _iter553 : struct.events) { _iter553.write(oprot); } oprot.writeListEnd(); } oprot.writeFieldEnd(); } oprot.writeFieldStop(); oprot.writeStructEnd(); }
protected void queueNotification( final NotificationListener listener, final NotificationEvent event) { if (acceptBeforeQueuing && listener instanceof DefaultNotificationListener) { if (!((DefaultNotificationListener) listener) .accept(event.getNotification().getProductId())) { return; } } // determine retry delay long retryDelay = 0L; if (listener instanceof AbstractListener) { retryDelay = ((AbstractListener) listener).getRetryDelay(); } ExecutorService listenerExecutor = notificationListeners.get(listener); ExecutorTask<Void> listenerTask = new ExecutorTask<Void>( listenerExecutor, listener.getMaxTries(), listener.getTimeout(), new NotificationListenerCallable(listener, event), retryTimer, retryDelay); listenerExecutor.submit(listenerTask); // log how many notifications are pending if (listenerExecutor instanceof ThreadPoolExecutor) { BlockingQueue<Runnable> pending = ((ThreadPoolExecutor) listenerExecutor).getQueue(); LOGGER.fine( "[" + event.getNotificationReceiver().getName() + "] listener (" + listener.getName() + ") has " + pending.size() + " queued notifications"); } }
public void read(org.apache.thrift.protocol.TProtocol iprot, NotificationEventResponse struct) throws org.apache.thrift.TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { case 1: // EVENTS if (schemeField.type == org.apache.thrift.protocol.TType.LIST) { { org.apache.thrift.protocol.TList _list550 = iprot.readListBegin(); struct.events = new ArrayList<NotificationEvent>(_list550.size); NotificationEvent _elem551; for (int _i552 = 0; _i552 < _list550.size; ++_i552) { _elem551 = new NotificationEvent(); _elem551.read(iprot); struct.events.add(_elem551); } iprot.readListEnd(); } struct.setEventsIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); struct.validate(); }
@Override public int compare(NotificationEvent notificationEvent1, NotificationEvent notificationEvent2) { if (notificationEvent1.equals(notificationEvent2)) { return 0; } long value = notificationEvent1.getDeliverBy() - notificationEvent2.getDeliverBy(); if (value == 0) { value = notificationEvent1.getTimestamp() - notificationEvent2.getTimestamp(); } if (value == 0) { value = notificationEvent1.hashCode() - notificationEvent2.hashCode(); } if (_ascending) { return (int) value; } else { return (int) -value; } }
protected void buildLateNotification(NotificationEvent _event, CalendarEvent _calendarEvent) { // long lateBy = Math.abs(currentSec - leaveTime); long minutes = TimeUnit.MINUTES.convert(_event.getTravelTime(), TimeUnit.SECONDS); String lateText = "Let's go!\n" + "It takes " + minutes + " minutes to get there!"; showNotification(_calendarEvent, lateText); }