Mercurial Hosting > nabble
diff src/nabble/view/web/template/NodeNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 18cf4872fd7f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/web/template/NodeNamespace.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,1109 @@ +package nabble.view.web.template; + +import fschmidt.util.java.Filter; +import fschmidt.util.mail.MailAddress; +import nabble.model.MailingList; +import nabble.model.ModelException; +import nabble.model.Node; +import nabble.model.NodeIterator; +import nabble.model.Person; +import nabble.model.Subscription; +import nabble.model.User; +import nabble.naml.compiler.Command; +import nabble.naml.compiler.CommandSpec; +import nabble.naml.compiler.IPrintWriter; +import nabble.naml.compiler.Interpreter; +import nabble.naml.compiler.Namespace; +import nabble.naml.compiler.ScopedInterpreter; +import nabble.naml.namespaces.CommandDoc; +import nabble.naml.namespaces.ListSequence; +import nabble.naml.namespaces.TemplateException; +import nabble.view.lib.Jtp; +import nabble.view.lib.Permissions; +import nabble.view.web.forum.Permalink; +import nabble.view.web.forum.Thumbnail; +import nabble.view.web.mailing_list.MailingListNamespace; + +import javax.servlet.ServletException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + + +@Namespace ( + name = "node", + global = false +) +public final class NodeNamespace { + private Node nodeR; + public final ServletNamespaceUtils servletNsUtils = new ServletNamespaceUtils(); + + public NodeNamespace(Node node) { + if( node == null ) + throw new NullPointerException("node is null"); + this.nodeR = node; + } + + public void refreshNode() { + nodeR = nodeR.getGoodCopy(); + } + + public Node node() { + return nodeR; + } + + private long nodeId() { + return node().getId(); + } + + public static final CommandSpec this_node = CommandSpec.DO; + + @Command public void this_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + out.print( interp.getArg(this,"do") ); + } + + // should this be _owner_user? not sure -fschmidt + public static final CommandSpec owner = CommandSpec.DO; + + @Command public void owner(IPrintWriter out,ScopedInterpreter<UserNamespace> interp) { + UserNamespace visitorModel = new UserNamespace(node().getOwner()); + out.print( interp.getArg(visitorModel,"do") ); + } + + public static final CommandSpec last_node = CommandSpec.DO; + + @Command public void last_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) + { + NodeNamespace ns = new NodeNamespace(node().getLastNode()); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + + public static final CommandSpec topic_node = CommandSpec.DO; + + @Command public void topic_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) + { + NodeNamespace ns = new NodeNamespace(node().getTopic()); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + + public static final CommandSpec topic_count = new CommandSpec.Builder() + .optionalParameters("filter") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void topic_count(IPrintWriter out,Interpreter interp) + throws ServletException + { + out.print( getTopicCount(interp) ); + } + + public Filter<Node> filter(Interpreter interp) + throws ServletException + { + if( Jtp.isCached(servletNsUtils.request(interp),servletNsUtils.response(interp)) ) { + return Permissions.canBeViewedByParentViewersFilter; + } else { + return Permissions.canBeViewedByPersonFilter(servletNsUtils.visitor(interp)); + } + } + + private int getTopicCount(Interpreter interp) + throws ServletException + { + String topicFilter = interp.getArgString("filter"); + return node().getTopicCount(topicFilter,filter(interp)); + } + + @Command public void child_count(IPrintWriter out,Interpreter interp) { + out.print( node().getChildCount() ); + } + + @Command public void subject_impl(IPrintWriter out,Interpreter interp) { + out.print( interp.encode(node().getSubject()) ); + } + + @Command public void raw_subject(IPrintWriter out,Interpreter interp) { + out.print( node().getSubject() ); + } + + @Command public void url_encoded_subject(IPrintWriter out,Interpreter interp) { + out.print( Jtp.subjectEncode(node().getSubjectHtml()) ); + } + + public static final CommandSpec message = CommandSpec.DO; + + @Command public void message(IPrintWriter out,ScopedInterpreter<MessageNamespace> interp) { + out.print( interp.getArg( new MessageNamespace(node().getMessage()), "do" ) ); + } + + @Command public void post_path(IPrintWriter out,Interpreter interp) { + Node node = node(); + if( node.getKind() != Node.Kind.POST ) + throw new RuntimeException("must be post"); + Node topic = node.getTopic(); + if( topic.equals(node) ) + node = null; + out.print( interp.encode( Permalink.path(topic,node) ) ); + } + + public static final CommandSpec when_created = CommandSpec.DO; + + @Command public void when_created(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) { + out.print( interp.getArg( new DateNamespace(node().getWhenCreated()), "do" ) ); + } + + @Command public void was_updated(IPrintWriter out,Interpreter interp) { + out.print( node().getWhenUpdated() != null ); + } + + public static final CommandSpec when_updated = CommandSpec.DO; + + @Command public void when_updated(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) { + out.print( interp.getArg( new DateNamespace(node().getWhenUpdated()), "do" ) ); + } + + public static final CommandSpec has_topics = new CommandSpec.Builder() + .optionalParameters("filter") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void has_topics(IPrintWriter out,Interpreter interp) + throws ServletException + { + out.print( getTopicCount(interp) > 0 ); + } + + @Command public void has_children(IPrintWriter out,Interpreter interp) { + boolean hasChildren = node().getChildCount() > 0; + out.print( hasChildren ); + } + + + private boolean checkedSubapps = false; + private boolean hasSubapps; + + @Command public void has_subapps(IPrintWriter out,Interpreter interp) { + if( !checkedSubapps ) { + hasSubapps = node().hasChildApps(); + checkedSubapps = true; + } + out.print(hasSubapps); + } + + private boolean checkedPrivateSubapps = false; + private boolean hasPrivateSubapps; + + private boolean hasPrivateSubapps(Node node) { + NodeIterator<? extends Node> childAppIterator = node.getChildApps(); + try { + for (Node n : childAppIterator) { + if (Permissions.isPrivate(n)) + return true; + else { + boolean hasPrivateChildren = hasPrivateSubapps(n); + if (hasPrivateChildren) + return true; + } + } + } finally { + childAppIterator.close(); + } + return false; + } + + @Command public void has_private_subapps(IPrintWriter out,Interpreter interp) { + if( !checkedPrivateSubapps ) { + hasPrivateSubapps = hasPrivateSubapps(node()); + checkedPrivateSubapps = true; + } + out.print(hasPrivateSubapps); + } + + private boolean checkedPinnedSubapps = false; + private boolean hasPinnedSubapps; + + @Command public void has_pinned_subapps(IPrintWriter out,Interpreter interp) { + if( !checkedPinnedSubapps ) { + hasPinnedSubapps = node().hasPinnedApps(); + checkedPinnedSubapps = true; + } + out.print(hasPinnedSubapps); + } + + private boolean checkedPinnedTopics = false; + private boolean hasPinnedTopics; + + @Command public void has_pinned_topics(IPrintWriter out,Interpreter interp) { + if( !checkedPinnedTopics ) { + hasPinnedTopics = node().hasPinnedTopics(); + checkedPinnedTopics = true; + } + out.print(hasPinnedTopics); + } + + @Command public void has_child_topics(IPrintWriter out,Interpreter interp) { + out.print( node().hasChildTopics() ); + } + + @Command public void id(IPrintWriter out,Interpreter interp) { + out.print( nodeId() ); + } + + @Command public void post_count(IPrintWriter out,Interpreter interp) { + out.print( node().getDescendantPostCount() ); + } + + @Command public void is_app(IPrintWriter out,Interpreter interp) { + out.print( node().getKind() == Node.Kind.APP ); + } + + @Command public void is_post(IPrintWriter out,Interpreter interp) { + out.print( node().getKind() == Node.Kind.POST ); + } + + @Command public void is_topic(IPrintWriter out,Interpreter interp) { + Node node = node(); + out.print( node.getKind() == Node.Kind.POST && (node.getParent() == null || node.getParent().getKind() == Node.Kind.APP)); + } + + @Command public void is_private(IPrintWriter out,Interpreter interp) { + out.print(Permissions.isPrivate(node())); + } + + @Command public void is_pending(IPrintWriter out,Interpreter interp) { + Node node = node(); + Node.MailToList mail = node.getMailToList(); + out.print(mail != null && node.getOwner() instanceof User && mail.isPending()); + } + + @Command public void descendant_count(IPrintWriter out,Interpreter interp) { + out.print( node().getDescendantCount() ); + } + + @Command public void replies(IPrintWriter out,Interpreter interp) { + out.print( node().getDescendantCount()-1 ); + } + + @Command public void has_replies(IPrintWriter out,Interpreter interp) { + out.print( node().getDescendantCount() > 1 ); + } + + public static final CommandSpec first_reply = CommandSpec.DO; + + @Command public void first_reply(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + Node node = node(); + List<Node> children = node.getChildren().get(0,1); + if( children.isEmpty() ) + throw new RuntimeException("node="+node+" replies="+(node.getDescendantCount()-1)); + NodeNamespace ns = new NodeNamespace(children.get(0)); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + + public static final CommandSpec type = new CommandSpec.Builder() + .optionalParameters("equals") + .build() + ; + + @Command public void type(IPrintWriter out,Interpreter interp) { + String equals = interp.getArgString("equals"); + if (equals == null) + out.print( node().getType() ); + else { + out.print( equals.trim().equals(node().getType()) ); + } + } + + @Command public void is_in_app(IPrintWriter out,Interpreter interp) { + out.print( node().getApp() != null ); + } + + public static final CommandSpec get_app_node = CommandSpec.DO; + + @Command public void get_app_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + NodeNamespace ns = new NodeNamespace(node().getApp()); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + + public static final CommandSpec parent_node = CommandSpec.DO; + + @Command public void parent_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + out.print( interp.getArg(new NodeNamespace(node().getParent()),"do") ); + } + + @Command public void is_root(IPrintWriter out,Interpreter interp) { + out.print( node().isRoot() ); + } + + // loops + + @Command public void change_language_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/app/Languages.jtp" ) ); + } + + @Command public void extras_and_addons_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/app/Addons.jtp" ) ); + } + + @Command public void change_domain_name_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/forum/ChangeDomainName.jtp?site=" + node().getSite().getId() ) ); + } + + @Command public void manage_pinned_topics_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/catalog/ChangePinOrder.jtp?forum=" + nodeId() + "&what=threads" ) ); + } + + @Command public void manage_sub_apps_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/catalog/ChangePinOrder.jtp?forum=" + nodeId() + "&what=forums" ) ); + } + + @Command public void parent_options_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/catalog/ChangeParent.jtp?forum=" + nodeId() ) ); + } + + @Command public void subscription_instructions_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/mailing_list/SubscribeToMailingList.jtp?node=" + nodeId() ) ); + } + + @Command public void embed_post_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/embed/EmbedOptions.jtp?node=" + nodeId() ) ); + } + + @Command public void reply_to_author_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/user/SendEmail.jtp?type=pm&post=" + nodeId() ) ); + } + + @Command public void unsubscription_instructions_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( "/mailing_list/UnsubscribeFromMailingList.jtp?node=" + nodeId() ) ); + } + + @Command public void embedding_options_path(IPrintWriter out,Interpreter interp) { + out.print( interp.encode("/embed/EmbedOptions.jtp?node=" + nodeId()) ); + } + + public static final CommandSpec monthly_archives = new CommandSpec.Builder() + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .build() + ; + + @Command public void monthly_archives(IPrintWriter out,ScopedInterpreter<MonthlyArchivesNamespace> interp) { + Node node = node(); + if (node.getKind() == Node.Kind.APP) { + MonthlyArchivesNamespace archiveNs = new MonthlyArchivesNamespace(node); + out.print(interp.getArg(archiveNs,"do")); + } + } + + + + @Command public void is_mail_to_list(IPrintWriter out,Interpreter interp) { + out.print( node().getMailToList() != null ); + } + + + public static final CommandSpec get_this_mailing_list_archive = CommandSpec.DO; + + @Command public void get_this_mailing_list_archive(IPrintWriter out,ScopedInterpreter<MailingListNamespace> interp) { + out.print(interp.getArg(new MailingListNamespace(node()),"do")); + } + + public static final CommandSpec get_associated_mailing_list_archive = CommandSpec.DO; + + @Command public void get_associated_mailing_list_archive(IPrintWriter out,ScopedInterpreter<MailingListNamespace> interp) { + MailingList mailingList = node().getAssociatedMailingList(); + out.print(interp.getArg(new MailingListNamespace(mailingList),"do")); + } + + @Command public void is_a_mailing_list_archive(IPrintWriter out,Interpreter interp) { + out.print(node().getMailingList() != null); + } + + @Command public void is_associated_with_mailing_list_archive(IPrintWriter out,Interpreter interp) { + out.print(node().getAssociatedMailingList() != null); + } + + @Command public void has_sub_archive(IPrintWriter out,Interpreter interp) { + boolean hasSubArchive = false; + List<Node> childApps = node().getChildApps(null).get(0, 100); + for (Node n : childApps) { + if (n.getAssociatedMailingList() != null) { + hasSubArchive = true; + break; + } + } + out.print(hasSubArchive); + } + + + @Command public void default_meta_description(IPrintWriter out,Interpreter interp) { + out.print( Jtp.metaDescription(node()) ); + } + + @Command public void default_meta_keywords(IPrintWriter out,Interpreter interp) { + out.print( Jtp.metaKeywords(node()) ); + } + + + + @Command public void pinned_filter(IPrintWriter out,Interpreter interp) { + out.print( "pin is not null" ); + } + + @Command public void no_pinned_subapps_filter(IPrintWriter out,Interpreter interp) { + out.print( "(pin is null or is_app = 'f' or is_app is null)" ); + } + + public static final CommandSpec date_filter = new CommandSpec.Builder() + .parameters("date") + .build() + ; + + @CommandDoc( + value= "Creates a filter for a specific month and year combination. ", + params = {"date=Month and year in the format YYYYMM."}, + seeAlso = {"date_range_filter"} + ) + @Command public void date_filter(IPrintWriter out,Interpreter interp) { + String date = interp.getArgString("date"); + String currentYear = date.substring(0, 4); + int currentMonth = Integer.valueOf(date.substring(4)); + out.print( "date_part('year', when_created) = " + currentYear + " and date_part('month', when_created) = " + currentMonth ); + } + + public static final CommandSpec date_range_filter = new CommandSpec.Builder() + .parameters("from_date","to_date") + .build() + ; + + private static final Pattern YYYYMMDD = Pattern.compile("\\d{4}\\d{2}\\d{2}"); + + @Command public void date_range_filter(IPrintWriter out,Interpreter interp) + throws ModelException.InvalidDate + { + String from = interp.getArgString("from_date"); + String to = interp.getArgString("to_date"); + if (!YYYYMMDD.matcher(to).find()) + throw new ModelException.InvalidDate(to); + if (!YYYYMMDD.matcher(from).find()) + throw new ModelException.InvalidDate(from); + + // SQL format is YYYY-MM-DD + String fromCnd = from.substring(0, 4) + '-' + from.substring(4, 6) + '-' + from.substring(6); + String toCnd = to.substring(0, 4) + '-' + to.substring(4, 6) + '-' + to.substring(6); + out.print( "when_created >= DATE '" + fromCnd + "' and when_created <= DATE '" + toCnd + "'" ); + } + + public static final CommandSpec exclude_parent_filter = new CommandSpec.Builder() + .parameters("parent_id") + .build() + ; + + @Command public void exclude_parent_filter(IPrintWriter out,Interpreter interp) { + long parentId = interp.getArgAsLong("parent_id"); + out.print( "parent_id <> " + parentId ); + } + + @Command public void children_filter(IPrintWriter out,Interpreter interp) { + out.print( "parent_id = " + nodeId() ); + } + + @Command public void post_filter(IPrintWriter out,Interpreter interp) { + out.print( "is_app is null or not is_app" ); + } + + public static final CommandSpec subapps_list = new CommandSpec.Builder() + .optionalParameters("filter") + .scopedParameters("do") + .dotParameter("do") + .build() + ; + + @Command public void subapps_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) { + NodeList.subapps(out,interp,node(),interp.getArgString("filter")); + } + + public static final CommandSpec descendant_apps_list = new CommandSpec.Builder() + .scopedParameters("do") + .dotParameter("do") + .build() + ; + + @Command public void descendant_apps_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) { + NodeList.descendantApps(out,interp,node()); + } + + public static final CommandSpec ancestors_list = new CommandSpec.Builder() + .optionalParameters("order") + .scopedParameters("do") + .dotParameter("do") + .build() + ; + + @Command public void ancestors_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) { + NodeList.ancestors(out,interp,node(),interp.getArgString("order")); + } + + public static final CommandSpec children_list_standard = CommandSpec.DO() + .parameters("length") + .optionalParameters("start", "filter") + .build() + ; + + @Command public void children_list_standard(IPrintWriter out,ScopedInterpreter<NodeList> interp) { + Node node = node(); + int start = getLoopStart(interp); + int length = getLoopLength(interp); + String filter = interp.getArgString("filter"); + NodeIterator<? extends Node> nodeIter = node.getChildren(filter); + NodeList.children(out,interp,node,nodeIter,start,length); + } + + public static final CommandSpec topics_list_standard = CommandSpec.DO() + .parameters("length") + .optionalParameters("start","sort","filter") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void topics_list_standard(IPrintWriter out,ScopedInterpreter<NodeList> interp) + throws ServletException + { + Node node = node(); + int start = getLoopStart(interp); + int length = getLoopLength(interp); + String filter = interp.getArgString("filter"); + String sortBy = interp.getArgString("sort"); + NodeIterator<? extends Node> nodeIter; + if ("pinned-and-last-node-date".equals(sortBy)) { + nodeIter = node.getTopicsByPinnedAndLastNodeDate(filter,filter(interp)); + } else if ("pinned-and-root-node-date".equals(sortBy)) { + nodeIter = node.getTopicsByPinnedAndRootNodeDate(filter,filter(interp)); + } else if ("popularity".equals(sortBy)) { + nodeIter = node.getTopicsByPopularity(filter,filter(interp)); + } else if ("last-node-date".equals(sortBy)) { + nodeIter = node.getTopicsByLastNodeDate(filter,filter(interp)); + } else if ("topic-subject".equals(sortBy)) { + nodeIter = node.getTopicsBySubject(filter,filter(interp)); + } else { + throw new RuntimeException("'sort' attribute not set"); + } + try { + NodeList.topics(out,interp,node,nodeIter,start,length); + } finally { + nodeIter.close(); + } + } + + public static final CommandSpec post_list = CommandSpec.DO() + .parameters("length","sort") + .optionalParameters("start") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void post_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) + throws ServletException + { + NodeList.posts(out,interp,node(),getLoopStart(interp),getLoopLength(interp),interp.getArgString("sort"),filter(interp)); + } + + public static int getLoopStart(Interpreter interp) { + return interp.getArgAsInt("start",0); + } + + public static int getLoopLength(Interpreter interp) { + try { + return Integer.valueOf( interp.getArgString("length").trim() ); + } catch(NumberFormatException e) { + throw new RuntimeException("Invalid loop length",e); + } + } + + public static final CommandSpec can_be_viewed_by_visitor = new CommandSpec.Builder() + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void can_be_viewed_by_visitor(IPrintWriter out,Interpreter interp) + throws ServletException + { + if( Jtp.isCached(servletNsUtils.request(interp),servletNsUtils.response(interp)) ) { + out.print( Permissions.canBeViewedByParentViewers(node()) ); + } else { + out.print( Permissions.canBeViewedByPerson(node(),servletNsUtils.visitor(interp)) ); + } + } + + public static final CommandSpec groups_have_permission = new CommandSpec.Builder() + .parameters("groups","permission") + .build() + ; + + @Command public void groups_have_permission(IPrintWriter out,Interpreter interp) + throws ServletException + { + String groups = interp.getArgString("groups"); + String perm = interp.getArgString("permission"); + for( String group : groups.split(",") ) { + if( Permissions.hasPermission(node(),group.trim(),perm) ) { + out.print( true ); + return; + } + } + out.print( false ); + } + + public static final CommandSpec node_has_permission = new CommandSpec.Builder() + .dotParameter("permission") + .build() + ; + + @Command public void node_has_permission(IPrintWriter out,Interpreter interp) { + String perm = interp.getArgString("permission"); + out.print( Permissions.nodeHasPermission(node(),perm) ); + } + + public static final CommandSpec has_permission = new CommandSpec.Builder() + .parameters("group","permission") + .build() + ; + + @Command public void has_permission(IPrintWriter out,Interpreter interp) { + String group = interp.getArgString("group"); + String perm = interp.getArgString("permission"); + out.print( Permissions.hasPermission(node(),group,perm) ); + } + + public static final CommandSpec node_with_permission = new CommandSpec.Builder() + .parameters("permission") + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .optionalParameters("do") + .build() + ; + + @Command public void node_with_permission(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + String perm = interp.getArgString("permission"); + Node node = Permissions.getPermissionNode(node(),perm); + out.print( interp.getArg(new NodeNamespace(node),"do") ); + } + + public static final CommandSpec users_with_permission = new CommandSpec.Builder() + .parameters("permission") + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .build() + ; + + @Command public void users_with_permission(IPrintWriter out,ScopedInterpreter<UserNamespace.UserList> interp) { + String perm = interp.getArgString("permission"); + List<User> users = Permissions.getUsersWithPermission(node(),perm); + UserNamespace.UserList usersNs = new UserNamespace.UserList(users); + out.print( interp.getArg(usersNs,"do") ); + } + + public static final CommandSpec has_groups_with_permission = new CommandSpec.Builder() + .dotParameter("permission") + .build() + ; + + @Command public void has_groups_with_permission(IPrintWriter out,Interpreter interp) { + String perm = interp.getArgString("permission"); + out.print( Permissions.hasGroupsWithPermission(node(),perm) ); + } + + public static final CommandSpec groups_with_permission = CommandSpec.DO() + .parameters("permission") + .build() + ; + + @Command public void groups_with_permission(IPrintWriter out,ScopedInterpreter<NabbleNamespace.GroupList> interp) + throws ServletException + { + String perm = interp.getArgString("permission"); + List<String> groups = Permissions.getGroupsWithPermission(node(),perm); + Object block = interp.getArg(new NabbleNamespace.GroupList(groups),"do"); + out.print(block); + } + + + public static final CommandSpec visitor_subscription = new CommandSpec.Builder() + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void visitor_subscription(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp) + throws ServletException + { + User user = servletNsUtils.visitorUser(interp); + SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(node(), user); + out.print( interp.getArg(subscriptionModel,"do") ); + } + + public static final CommandSpec subscription_for = CommandSpec.DO() + .parameters("email") + .build() + ; + + @Command public void subscription_for(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp) + throws ModelException.EmailFormat + { + Node node = node(); + String email = interp.getArgString("email"); + if (!new MailAddress(email).isValid()) + throw new ModelException.EmailFormat(email); + User user = node.getSite().getOrCreateUser(email); + SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(node, user); + out.print( interp.getArg(subscriptionModel,"do") ); + } + + public static final CommandSpec get_subscription_by_code = new CommandSpec.Builder() + .parameters("code") + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .requiredInStack(ServletNamespace.class) + .build() + ; + + @Command public void get_subscription_by_code(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp) + throws TemplateException + { + String code = interp.getArgString("code"); + SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(code, servletNsUtils.request(interp)); + out.print( interp.getArg(subscriptionModel,"do") ); + } + + public static final CommandSpec visitor_is_subscribed = ServletNamespaceUtils.requiresServletNamespace; + + @Command public void visitor_is_subscribed(IPrintWriter out,Interpreter interp) + throws ServletException + { + User user = servletNsUtils.visitorUser(interp); + out.print( user != null && user.isSubscribed(node()) ); + } + + public static final CommandSpec unsubscribe_visitor = new CommandSpec.Builder() + .requiredInStack(ServletNamespace.class) + .outputtedParameters() + .build() + ; + + @Command public void unsubscribe_visitor(IPrintWriter out,Interpreter interp) + throws ServletException + { + User user = servletNsUtils.visitorUser(interp); + if( user != null ) { + Subscription s = user.getSubscription(node()); + if( s != null ) + s.delete(); + } + } + + public static final CommandSpec subscribe_visitor = new CommandSpec.Builder() + .requiredInStack(ServletNamespace.class) + .outputtedParameters() + .build() + ; + + @Command public void subscribe_visitor(IPrintWriter out,Interpreter interp) + throws ServletException + { + Node node = node(); + User user = servletNsUtils.visitorUser(interp); + Subscription s = user.getSubscription(node); + if( s == null ) { + user.subscribe(node,Subscription.To.DESCENDANTS,Subscription.Type.INSTANT); + } + } + + public static final CommandSpec user_address = new CommandSpec.Builder() + .parameters("email") + .build() + ; + + @Command public void user_address(IPrintWriter out,Interpreter interp) { + Node node = node(); + String email = interp.getArgString("email"); + User user = node.getSite().getOrCreateUser(email); + out.print( user.getDecoratedAddress(node) ); + } + + + @Command public void default_rows_per_page(IPrintWriter out,Interpreter interp) { + out.print( Jtp.getDefaultRowsPerPage(node().getType()) ); + } + + public static final CommandSpec delete_message_or_node = CommandSpec.NO_OUTPUT; + + @Command public void delete_message_or_node(IPrintWriter out,Interpreter interp) { + node().deleteMessageOrNode(); + } + + public static final CommandSpec delete_recursively = CommandSpec.NO_OUTPUT; + + @Command public void delete_recursively(IPrintWriter out,Interpreter interp) { + node().deleteRecursively(); + } + + + public static final CommandSpec as_node_page = CommandSpec.DO; + + @Command public void as_node_page(IPrintWriter out,ScopedInterpreter<NodePageNamespace> interp) { + out.print( interp.getArg(new NodePageNamespace(this),"do") ); + } + + + @Command public void has_thumbnail(IPrintWriter out,Interpreter interp) { + out.print( Thumbnail.getThumbnailFile(node()) != null ); + } + + @Command public void thumbnail_url(IPrintWriter out,Interpreter interp) { + out.print( interp.encode( Thumbnail.getThumbnailFile(node()) ) ); + } + + @Command public void is_pinned(IPrintWriter out,Interpreter interp) { + out.print( node().isPinned() ); + } + + public static final CommandSpec pin = CommandSpec.NO_OUTPUT; + + @Command public void pin(IPrintWriter out,Interpreter interp) { + Node node = node(); + Jtp.addPinnedChild(node.getParent(), node); + } + + public static final CommandSpec unpin = CommandSpec.NO_OUTPUT; + + @Command public void unpin(IPrintWriter out,Interpreter interp) { + Node node = node(); + Jtp.unpinChild(node.getParent(), node); + } + + public static final CommandSpec equals = new CommandSpec.Builder() + .dotParameter("node") + .build() + ; + + @Command public void equals(IPrintWriter out,Interpreter interp) { + NodeNamespace ns = interp.getArgAsNamespace(NodeNamespace.class,"node"); + out.print( ns != null && ns.node() != null && ns.node().equals(node()) ); + } + + public static final CommandSpec NAME = new CommandSpec.Builder() + .dotParameter("name") + .build() + ; + + public static final CommandSpec has_property = NAME; + + @Command public void has_property(IPrintWriter out,Interpreter interp) { + String name = interp.getArgString("name"); + out.print(node().getProperty(name) != null); + } + + public static final CommandSpec get_property = NAME; + + @Command public void get_property(IPrintWriter out,Interpreter interp) { + String name = interp.getArgString("name"); + out.print(node().getProperty(name)); + } + + public static final CommandSpec delete_property = CommandSpec.NO_OUTPUT() + .parameters("name") + .build() + ; + + @Command public void delete_property(IPrintWriter out,Interpreter interp) { + Node node = node(); + String name = interp.getArgString("name"); + node.setProperty(name, null); + node.update(); + } + + public static final CommandSpec set_property = CommandSpec.NO_OUTPUT() + .parameters("name", "value") + .build() + ; + + @Command public void set_property(IPrintWriter out,Interpreter interp) { + Node node = node(); + String name = interp.getArgString("name"); + String value = interp.getArgString("value"); + node.setProperty(name, value); + node.update(); + } + + @Command public void subscription_count(IPrintWriter out,Interpreter interp) { + out.print( node().getSubscriptionCount() ); + } + + public static final CommandSpec subscriptions = new CommandSpec.Builder() + .parameters("length") + .optionalParameters("start") + .scopedParameters("do") + .dotParameter("do") + .outputtedParameters("do") + .build() + ; + + @Command public void subscriptions(IPrintWriter out,ScopedInterpreter<SubscriptionList> interp) { + int start = interp.getArgAsInt("start",0); + int length = interp.getArgAsInt("length"); + Collection<Subscription> subscriptions = node().getSubscriptions(start,length); + List<SubscriptionNamespace> subscriptionNs = new ArrayList<SubscriptionNamespace>(subscriptions.size()); + for (Subscription s : subscriptions) { + subscriptionNs.add(new SubscriptionNamespace(s)); + } + out.print( interp.getArg(new SubscriptionList(subscriptionNs),"do") ); + } + + @Namespace ( + name = "subscriptions", + global = true + ) + public static final class SubscriptionList extends ListSequence<SubscriptionNamespace> { + + SubscriptionList(List<SubscriptionNamespace> subscriptions) { + super(subscriptions); + } + + public static final CommandSpec subscription = CommandSpec.DO; + + @Command public void subscription(IPrintWriter out,ScopedInterpreter<SubscriptionNamespace> interp) { + out.print(interp.getArg(get(),"do")); + } + } + + + public static final CommandSpec get_private_node = CommandSpec.DO; + + @Command public void get_private_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { + out.print( interp.getArg(new NodeNamespace(Permissions.getPrivateNode(node())),"do") ); + } + + private String getRedirectionUrl() { + String embeddingUrl = null; + for( + Node n = node(); + embeddingUrl == null && n != null; + n = n.getParent() + ) { + embeddingUrl = n.getEmbeddingUrl(); + } + return embeddingUrl; + } + + @Command public void has_embedding_redirection_url(IPrintWriter out,Interpreter interp) { + out.print( getRedirectionUrl() != null ); + } + + @Command public void embedding_redirection_url(IPrintWriter out,Interpreter interp) { + out.print( getRedirectionUrl() ); + } + + + @Command public void default_reply_subject(IPrintWriter out,Interpreter interp) { + Node node = node(); + String subject = null; + if( node.getKind() == Node.Kind.POST ) { + subject = node.getSubject(); + if( !subject.startsWith("Re: ") && !subject.startsWith("RE: ") ) + subject = "Re: " + subject; + } + out.print(subject); + } + + public static final CommandSpec descendant_nodes_by_user = CommandSpec.DO; + + @Command public void descendant_nodes_by_user(IPrintWriter out,ScopedInterpreter<NodesGroupedByUser> interp) { + Map<User,List<Node>> map = new HashMap<User,List<Node>>(); + for( Node n : node().getDescendants() ) { + Person u = n.getOwner(); + if( !(u instanceof User) ) + continue; + User owner = (User)u; + List<Node> nodes = map.get(owner); + if( nodes == null ) { + nodes = new ArrayList<Node>(); + map.put(owner,nodes); + } + nodes.add(n); + } + out.print(interp.getArg(new NodesGroupedByUser(map),"do")); + } + + @Namespace ( + name = "nodes_grouped_by_user", + global = true + ) + public static final class NodesGroupedByUser extends UserNamespace.UserList { + + Map<User, List<Node>> userNodes; + + NodesGroupedByUser(Map<User, List<Node>> userNodes) { + super(Arrays.asList(userNodes.keySet().toArray(new User[userNodes.size()]))); + this.userNodes = userNodes; + } + + public static final CommandSpec nodes_list = CommandSpec.DO; + + @Command public void nodes_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) { + List<Node> nodes = userNodes.get(get()); + out.print(interp.getArg(new NodeList(nodes, null, false),"do")); + } + } + + + public static final CommandSpec get_instant_emails = CommandSpec.DO; + + @Command public void get_instant_emails(IPrintWriter out,ScopedInterpreter<InstantMailNamespace> interp) { + Node node = node(); + Map<User,Subscription> map = node.getSubscribersToNotify(); + if( !map.isEmpty() ) + out.print( interp.getArg(new InstantMailNamespace(node, map),"do") ); + } + + @Command public void has_prev_topic(IPrintWriter out, Interpreter interp) { + out.print(node().hasPreviousTopic()); + } + + @Command public void has_next_topic(IPrintWriter out, Interpreter interp) { + out.print(node().hasNextTopic()); + } + + public static final CommandSpec prev_topic = CommandSpec.DO; + + @Command public void prev_topic(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) + { + NodeNamespace ns = new NodeNamespace(node().getPreviousTopic()); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + + public static final CommandSpec next_topic = CommandSpec.DO; + + @Command public void next_topic(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) + { + NodeNamespace ns = new NodeNamespace(node().getNextTopic()); + Object obj = interp.getArg(ns,"do"); + out.print(obj); + } + +}