Implementing Auto-Watch

Developers perform this task.

Auto-watch is a feature that automatically adds a user as a watcher after performing an action on that item. For example, if a user posts a comment to an article’s conversation, that user can automatically become a watcher on that article.

You implement auto-watch in actions by recasting the passed item as a Record and then calling the sendMessage method. The following snippet is an example taken from the ConversationWidget class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
private static boolean processForm(ToolPageContext page, Recordable content) throws Exception {

/* Statements to instantiate and validate the post associated with the content. */

     post.save();

     Record record = (Record) content;
     record.sendMessage("watch.autoAdd", page.getUser());

/* Return true or false */
}

Referring to the previous snippet—

  • Line 5 commits the post associated with the content to the database.
  • Line 7 instantiates a Record variable based on the passed content.
  • Line 8 establishes a watch on the passed content for the user who posted the conversation.

The first parameter for the sendMessage method is one of the following:

  • watch.autoAdd—Adds the user unless the user previously removed the watch.
  • watch.manualAdd—Adds the user regardless if user previously removed the watch.
  • watch.autoRemove—Removes the user unless the user manually created a watch.
  • watch.manualRemove—Adds a user regardless if user previously created a watch.