comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.view.web.template;
2
3 import fschmidt.util.java.Filter;
4 import fschmidt.util.mail.MailAddress;
5 import nabble.model.MailingList;
6 import nabble.model.ModelException;
7 import nabble.model.Node;
8 import nabble.model.NodeIterator;
9 import nabble.model.Person;
10 import nabble.model.Subscription;
11 import nabble.model.User;
12 import nabble.naml.compiler.Command;
13 import nabble.naml.compiler.CommandSpec;
14 import nabble.naml.compiler.IPrintWriter;
15 import nabble.naml.compiler.Interpreter;
16 import nabble.naml.compiler.Namespace;
17 import nabble.naml.compiler.ScopedInterpreter;
18 import nabble.naml.namespaces.CommandDoc;
19 import nabble.naml.namespaces.ListSequence;
20 import nabble.naml.namespaces.TemplateException;
21 import nabble.view.lib.Jtp;
22 import nabble.view.lib.Permissions;
23 import nabble.view.web.forum.Permalink;
24 import nabble.view.web.forum.Thumbnail;
25 import nabble.view.web.mailing_list.MailingListNamespace;
26
27 import javax.servlet.ServletException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.regex.Pattern;
35
36
37 @Namespace (
38 name = "node",
39 global = false
40 )
41 public final class NodeNamespace {
42 private Node nodeR;
43 public final ServletNamespaceUtils servletNsUtils = new ServletNamespaceUtils();
44
45 public NodeNamespace(Node node) {
46 if( node == null )
47 throw new NullPointerException("node is null");
48 this.nodeR = node;
49 }
50
51 public void refreshNode() {
52 nodeR = nodeR.getGoodCopy();
53 }
54
55 public Node node() {
56 return nodeR;
57 }
58
59 private long nodeId() {
60 return node().getId();
61 }
62
63 public static final CommandSpec this_node = CommandSpec.DO;
64
65 @Command public void this_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
66 out.print( interp.getArg(this,"do") );
67 }
68
69 // should this be _owner_user? not sure -fschmidt
70 public static final CommandSpec owner = CommandSpec.DO;
71
72 @Command public void owner(IPrintWriter out,ScopedInterpreter<UserNamespace> interp) {
73 UserNamespace visitorModel = new UserNamespace(node().getOwner());
74 out.print( interp.getArg(visitorModel,"do") );
75 }
76
77 public static final CommandSpec last_node = CommandSpec.DO;
78
79 @Command public void last_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp)
80 {
81 NodeNamespace ns = new NodeNamespace(node().getLastNode());
82 Object obj = interp.getArg(ns,"do");
83 out.print(obj);
84 }
85
86 public static final CommandSpec topic_node = CommandSpec.DO;
87
88 @Command public void topic_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp)
89 {
90 NodeNamespace ns = new NodeNamespace(node().getTopic());
91 Object obj = interp.getArg(ns,"do");
92 out.print(obj);
93 }
94
95 public static final CommandSpec topic_count = new CommandSpec.Builder()
96 .optionalParameters("filter")
97 .requiredInStack(ServletNamespace.class)
98 .build()
99 ;
100
101 @Command public void topic_count(IPrintWriter out,Interpreter interp)
102 throws ServletException
103 {
104 out.print( getTopicCount(interp) );
105 }
106
107 public Filter<Node> filter(Interpreter interp)
108 throws ServletException
109 {
110 if( Jtp.isCached(servletNsUtils.request(interp),servletNsUtils.response(interp)) ) {
111 return Permissions.canBeViewedByParentViewersFilter;
112 } else {
113 return Permissions.canBeViewedByPersonFilter(servletNsUtils.visitor(interp));
114 }
115 }
116
117 private int getTopicCount(Interpreter interp)
118 throws ServletException
119 {
120 String topicFilter = interp.getArgString("filter");
121 return node().getTopicCount(topicFilter,filter(interp));
122 }
123
124 @Command public void child_count(IPrintWriter out,Interpreter interp) {
125 out.print( node().getChildCount() );
126 }
127
128 @Command public void subject_impl(IPrintWriter out,Interpreter interp) {
129 out.print( interp.encode(node().getSubject()) );
130 }
131
132 @Command public void raw_subject(IPrintWriter out,Interpreter interp) {
133 out.print( node().getSubject() );
134 }
135
136 @Command public void url_encoded_subject(IPrintWriter out,Interpreter interp) {
137 out.print( Jtp.subjectEncode(node().getSubjectHtml()) );
138 }
139
140 public static final CommandSpec message = CommandSpec.DO;
141
142 @Command public void message(IPrintWriter out,ScopedInterpreter<MessageNamespace> interp) {
143 out.print( interp.getArg( new MessageNamespace(node().getMessage()), "do" ) );
144 }
145
146 @Command public void post_path(IPrintWriter out,Interpreter interp) {
147 Node node = node();
148 if( node.getKind() != Node.Kind.POST )
149 throw new RuntimeException("must be post");
150 Node topic = node.getTopic();
151 if( topic.equals(node) )
152 node = null;
153 out.print( interp.encode( Permalink.path(topic,node) ) );
154 }
155
156 public static final CommandSpec when_created = CommandSpec.DO;
157
158 @Command public void when_created(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) {
159 out.print( interp.getArg( new DateNamespace(node().getWhenCreated()), "do" ) );
160 }
161
162 @Command public void was_updated(IPrintWriter out,Interpreter interp) {
163 out.print( node().getWhenUpdated() != null );
164 }
165
166 public static final CommandSpec when_updated = CommandSpec.DO;
167
168 @Command public void when_updated(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) {
169 out.print( interp.getArg( new DateNamespace(node().getWhenUpdated()), "do" ) );
170 }
171
172 public static final CommandSpec has_topics = new CommandSpec.Builder()
173 .optionalParameters("filter")
174 .requiredInStack(ServletNamespace.class)
175 .build()
176 ;
177
178 @Command public void has_topics(IPrintWriter out,Interpreter interp)
179 throws ServletException
180 {
181 out.print( getTopicCount(interp) > 0 );
182 }
183
184 @Command public void has_children(IPrintWriter out,Interpreter interp) {
185 boolean hasChildren = node().getChildCount() > 0;
186 out.print( hasChildren );
187 }
188
189
190 private boolean checkedSubapps = false;
191 private boolean hasSubapps;
192
193 @Command public void has_subapps(IPrintWriter out,Interpreter interp) {
194 if( !checkedSubapps ) {
195 hasSubapps = node().hasChildApps();
196 checkedSubapps = true;
197 }
198 out.print(hasSubapps);
199 }
200
201 private boolean checkedPrivateSubapps = false;
202 private boolean hasPrivateSubapps;
203
204 private boolean hasPrivateSubapps(Node node) {
205 NodeIterator<? extends Node> childAppIterator = node.getChildApps();
206 try {
207 for (Node n : childAppIterator) {
208 if (Permissions.isPrivate(n))
209 return true;
210 else {
211 boolean hasPrivateChildren = hasPrivateSubapps(n);
212 if (hasPrivateChildren)
213 return true;
214 }
215 }
216 } finally {
217 childAppIterator.close();
218 }
219 return false;
220 }
221
222 @Command public void has_private_subapps(IPrintWriter out,Interpreter interp) {
223 if( !checkedPrivateSubapps ) {
224 hasPrivateSubapps = hasPrivateSubapps(node());
225 checkedPrivateSubapps = true;
226 }
227 out.print(hasPrivateSubapps);
228 }
229
230 private boolean checkedPinnedSubapps = false;
231 private boolean hasPinnedSubapps;
232
233 @Command public void has_pinned_subapps(IPrintWriter out,Interpreter interp) {
234 if( !checkedPinnedSubapps ) {
235 hasPinnedSubapps = node().hasPinnedApps();
236 checkedPinnedSubapps = true;
237 }
238 out.print(hasPinnedSubapps);
239 }
240
241 private boolean checkedPinnedTopics = false;
242 private boolean hasPinnedTopics;
243
244 @Command public void has_pinned_topics(IPrintWriter out,Interpreter interp) {
245 if( !checkedPinnedTopics ) {
246 hasPinnedTopics = node().hasPinnedTopics();
247 checkedPinnedTopics = true;
248 }
249 out.print(hasPinnedTopics);
250 }
251
252 @Command public void has_child_topics(IPrintWriter out,Interpreter interp) {
253 out.print( node().hasChildTopics() );
254 }
255
256 @Command public void id(IPrintWriter out,Interpreter interp) {
257 out.print( nodeId() );
258 }
259
260 @Command public void post_count(IPrintWriter out,Interpreter interp) {
261 out.print( node().getDescendantPostCount() );
262 }
263
264 @Command public void is_app(IPrintWriter out,Interpreter interp) {
265 out.print( node().getKind() == Node.Kind.APP );
266 }
267
268 @Command public void is_post(IPrintWriter out,Interpreter interp) {
269 out.print( node().getKind() == Node.Kind.POST );
270 }
271
272 @Command public void is_topic(IPrintWriter out,Interpreter interp) {
273 Node node = node();
274 out.print( node.getKind() == Node.Kind.POST && (node.getParent() == null || node.getParent().getKind() == Node.Kind.APP));
275 }
276
277 @Command public void is_private(IPrintWriter out,Interpreter interp) {
278 out.print(Permissions.isPrivate(node()));
279 }
280
281 @Command public void is_pending(IPrintWriter out,Interpreter interp) {
282 Node node = node();
283 Node.MailToList mail = node.getMailToList();
284 out.print(mail != null && node.getOwner() instanceof User && mail.isPending());
285 }
286
287 @Command public void descendant_count(IPrintWriter out,Interpreter interp) {
288 out.print( node().getDescendantCount() );
289 }
290
291 @Command public void replies(IPrintWriter out,Interpreter interp) {
292 out.print( node().getDescendantCount()-1 );
293 }
294
295 @Command public void has_replies(IPrintWriter out,Interpreter interp) {
296 out.print( node().getDescendantCount() > 1 );
297 }
298
299 public static final CommandSpec first_reply = CommandSpec.DO;
300
301 @Command public void first_reply(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
302 Node node = node();
303 List<Node> children = node.getChildren().get(0,1);
304 if( children.isEmpty() )
305 throw new RuntimeException("node="+node+" replies="+(node.getDescendantCount()-1));
306 NodeNamespace ns = new NodeNamespace(children.get(0));
307 Object obj = interp.getArg(ns,"do");
308 out.print(obj);
309 }
310
311 public static final CommandSpec type = new CommandSpec.Builder()
312 .optionalParameters("equals")
313 .build()
314 ;
315
316 @Command public void type(IPrintWriter out,Interpreter interp) {
317 String equals = interp.getArgString("equals");
318 if (equals == null)
319 out.print( node().getType() );
320 else {
321 out.print( equals.trim().equals(node().getType()) );
322 }
323 }
324
325 @Command public void is_in_app(IPrintWriter out,Interpreter interp) {
326 out.print( node().getApp() != null );
327 }
328
329 public static final CommandSpec get_app_node = CommandSpec.DO;
330
331 @Command public void get_app_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
332 NodeNamespace ns = new NodeNamespace(node().getApp());
333 Object obj = interp.getArg(ns,"do");
334 out.print(obj);
335 }
336
337 public static final CommandSpec parent_node = CommandSpec.DO;
338
339 @Command public void parent_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
340 out.print( interp.getArg(new NodeNamespace(node().getParent()),"do") );
341 }
342
343 @Command public void is_root(IPrintWriter out,Interpreter interp) {
344 out.print( node().isRoot() );
345 }
346
347 // loops
348
349 @Command public void change_language_path(IPrintWriter out,Interpreter interp) {
350 out.print( interp.encode( "/app/Languages.jtp" ) );
351 }
352
353 @Command public void extras_and_addons_path(IPrintWriter out,Interpreter interp) {
354 out.print( interp.encode( "/app/Addons.jtp" ) );
355 }
356
357 @Command public void change_domain_name_path(IPrintWriter out,Interpreter interp) {
358 out.print( interp.encode( "/forum/ChangeDomainName.jtp?site=" + node().getSite().getId() ) );
359 }
360
361 @Command public void manage_pinned_topics_path(IPrintWriter out,Interpreter interp) {
362 out.print( interp.encode( "/catalog/ChangePinOrder.jtp?forum=" + nodeId() + "&what=threads" ) );
363 }
364
365 @Command public void manage_sub_apps_path(IPrintWriter out,Interpreter interp) {
366 out.print( interp.encode( "/catalog/ChangePinOrder.jtp?forum=" + nodeId() + "&what=forums" ) );
367 }
368
369 @Command public void parent_options_path(IPrintWriter out,Interpreter interp) {
370 out.print( interp.encode( "/catalog/ChangeParent.jtp?forum=" + nodeId() ) );
371 }
372
373 @Command public void subscription_instructions_path(IPrintWriter out,Interpreter interp) {
374 out.print( interp.encode( "/mailing_list/SubscribeToMailingList.jtp?node=" + nodeId() ) );
375 }
376
377 @Command public void embed_post_path(IPrintWriter out,Interpreter interp) {
378 out.print( interp.encode( "/embed/EmbedOptions.jtp?node=" + nodeId() ) );
379 }
380
381 @Command public void reply_to_author_path(IPrintWriter out,Interpreter interp) {
382 out.print( interp.encode( "/user/SendEmail.jtp?type=pm&post=" + nodeId() ) );
383 }
384
385 @Command public void unsubscription_instructions_path(IPrintWriter out,Interpreter interp) {
386 out.print( interp.encode( "/mailing_list/UnsubscribeFromMailingList.jtp?node=" + nodeId() ) );
387 }
388
389 @Command public void embedding_options_path(IPrintWriter out,Interpreter interp) {
390 out.print( interp.encode("/embed/EmbedOptions.jtp?node=" + nodeId()) );
391 }
392
393 public static final CommandSpec monthly_archives = new CommandSpec.Builder()
394 .scopedParameters("do")
395 .dotParameter("do")
396 .outputtedParameters("do")
397 .build()
398 ;
399
400 @Command public void monthly_archives(IPrintWriter out,ScopedInterpreter<MonthlyArchivesNamespace> interp) {
401 Node node = node();
402 if (node.getKind() == Node.Kind.APP) {
403 MonthlyArchivesNamespace archiveNs = new MonthlyArchivesNamespace(node);
404 out.print(interp.getArg(archiveNs,"do"));
405 }
406 }
407
408
409
410 @Command public void is_mail_to_list(IPrintWriter out,Interpreter interp) {
411 out.print( node().getMailToList() != null );
412 }
413
414
415 public static final CommandSpec get_this_mailing_list_archive = CommandSpec.DO;
416
417 @Command public void get_this_mailing_list_archive(IPrintWriter out,ScopedInterpreter<MailingListNamespace> interp) {
418 out.print(interp.getArg(new MailingListNamespace(node()),"do"));
419 }
420
421 public static final CommandSpec get_associated_mailing_list_archive = CommandSpec.DO;
422
423 @Command public void get_associated_mailing_list_archive(IPrintWriter out,ScopedInterpreter<MailingListNamespace> interp) {
424 MailingList mailingList = node().getAssociatedMailingList();
425 out.print(interp.getArg(new MailingListNamespace(mailingList),"do"));
426 }
427
428 @Command public void is_a_mailing_list_archive(IPrintWriter out,Interpreter interp) {
429 out.print(node().getMailingList() != null);
430 }
431
432 @Command public void is_associated_with_mailing_list_archive(IPrintWriter out,Interpreter interp) {
433 out.print(node().getAssociatedMailingList() != null);
434 }
435
436 @Command public void has_sub_archive(IPrintWriter out,Interpreter interp) {
437 boolean hasSubArchive = false;
438 List<Node> childApps = node().getChildApps(null).get(0, 100);
439 for (Node n : childApps) {
440 if (n.getAssociatedMailingList() != null) {
441 hasSubArchive = true;
442 break;
443 }
444 }
445 out.print(hasSubArchive);
446 }
447
448
449 @Command public void default_meta_description(IPrintWriter out,Interpreter interp) {
450 out.print( Jtp.metaDescription(node()) );
451 }
452
453 @Command public void default_meta_keywords(IPrintWriter out,Interpreter interp) {
454 out.print( Jtp.metaKeywords(node()) );
455 }
456
457
458
459 @Command public void pinned_filter(IPrintWriter out,Interpreter interp) {
460 out.print( "pin is not null" );
461 }
462
463 @Command public void no_pinned_subapps_filter(IPrintWriter out,Interpreter interp) {
464 out.print( "(pin is null or is_app = 'f' or is_app is null)" );
465 }
466
467 public static final CommandSpec date_filter = new CommandSpec.Builder()
468 .parameters("date")
469 .build()
470 ;
471
472 @CommandDoc(
473 value= "Creates a filter for a specific month and year combination. ",
474 params = {"date=Month and year in the format YYYYMM."},
475 seeAlso = {"date_range_filter"}
476 )
477 @Command public void date_filter(IPrintWriter out,Interpreter interp) {
478 String date = interp.getArgString("date");
479 String currentYear = date.substring(0, 4);
480 int currentMonth = Integer.valueOf(date.substring(4));
481 out.print( "date_part('year', when_created) = " + currentYear + " and date_part('month', when_created) = " + currentMonth );
482 }
483
484 public static final CommandSpec date_range_filter = new CommandSpec.Builder()
485 .parameters("from_date","to_date")
486 .build()
487 ;
488
489 private static final Pattern YYYYMMDD = Pattern.compile("\\d{4}\\d{2}\\d{2}");
490
491 @Command public void date_range_filter(IPrintWriter out,Interpreter interp)
492 throws ModelException.InvalidDate
493 {
494 String from = interp.getArgString("from_date");
495 String to = interp.getArgString("to_date");
496 if (!YYYYMMDD.matcher(to).find())
497 throw new ModelException.InvalidDate(to);
498 if (!YYYYMMDD.matcher(from).find())
499 throw new ModelException.InvalidDate(from);
500
501 // SQL format is YYYY-MM-DD
502 String fromCnd = from.substring(0, 4) + '-' + from.substring(4, 6) + '-' + from.substring(6);
503 String toCnd = to.substring(0, 4) + '-' + to.substring(4, 6) + '-' + to.substring(6);
504 out.print( "when_created >= DATE '" + fromCnd + "' and when_created <= DATE '" + toCnd + "'" );
505 }
506
507 public static final CommandSpec exclude_parent_filter = new CommandSpec.Builder()
508 .parameters("parent_id")
509 .build()
510 ;
511
512 @Command public void exclude_parent_filter(IPrintWriter out,Interpreter interp) {
513 long parentId = interp.getArgAsLong("parent_id");
514 out.print( "parent_id <> " + parentId );
515 }
516
517 @Command public void children_filter(IPrintWriter out,Interpreter interp) {
518 out.print( "parent_id = " + nodeId() );
519 }
520
521 @Command public void post_filter(IPrintWriter out,Interpreter interp) {
522 out.print( "is_app is null or not is_app" );
523 }
524
525 public static final CommandSpec subapps_list = new CommandSpec.Builder()
526 .optionalParameters("filter")
527 .scopedParameters("do")
528 .dotParameter("do")
529 .build()
530 ;
531
532 @Command public void subapps_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) {
533 NodeList.subapps(out,interp,node(),interp.getArgString("filter"));
534 }
535
536 public static final CommandSpec descendant_apps_list = new CommandSpec.Builder()
537 .scopedParameters("do")
538 .dotParameter("do")
539 .build()
540 ;
541
542 @Command public void descendant_apps_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) {
543 NodeList.descendantApps(out,interp,node());
544 }
545
546 public static final CommandSpec ancestors_list = new CommandSpec.Builder()
547 .optionalParameters("order")
548 .scopedParameters("do")
549 .dotParameter("do")
550 .build()
551 ;
552
553 @Command public void ancestors_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) {
554 NodeList.ancestors(out,interp,node(),interp.getArgString("order"));
555 }
556
557 public static final CommandSpec children_list_standard = CommandSpec.DO()
558 .parameters("length")
559 .optionalParameters("start", "filter")
560 .build()
561 ;
562
563 @Command public void children_list_standard(IPrintWriter out,ScopedInterpreter<NodeList> interp) {
564 Node node = node();
565 int start = getLoopStart(interp);
566 int length = getLoopLength(interp);
567 String filter = interp.getArgString("filter");
568 NodeIterator<? extends Node> nodeIter = node.getChildren(filter);
569 NodeList.children(out,interp,node,nodeIter,start,length);
570 }
571
572 public static final CommandSpec topics_list_standard = CommandSpec.DO()
573 .parameters("length")
574 .optionalParameters("start","sort","filter")
575 .requiredInStack(ServletNamespace.class)
576 .build()
577 ;
578
579 @Command public void topics_list_standard(IPrintWriter out,ScopedInterpreter<NodeList> interp)
580 throws ServletException
581 {
582 Node node = node();
583 int start = getLoopStart(interp);
584 int length = getLoopLength(interp);
585 String filter = interp.getArgString("filter");
586 String sortBy = interp.getArgString("sort");
587 NodeIterator<? extends Node> nodeIter;
588 if ("pinned-and-last-node-date".equals(sortBy)) {
589 nodeIter = node.getTopicsByPinnedAndLastNodeDate(filter,filter(interp));
590 } else if ("pinned-and-root-node-date".equals(sortBy)) {
591 nodeIter = node.getTopicsByPinnedAndRootNodeDate(filter,filter(interp));
592 } else if ("popularity".equals(sortBy)) {
593 nodeIter = node.getTopicsByPopularity(filter,filter(interp));
594 } else if ("last-node-date".equals(sortBy)) {
595 nodeIter = node.getTopicsByLastNodeDate(filter,filter(interp));
596 } else if ("topic-subject".equals(sortBy)) {
597 nodeIter = node.getTopicsBySubject(filter,filter(interp));
598 } else {
599 throw new RuntimeException("'sort' attribute not set");
600 }
601 try {
602 NodeList.topics(out,interp,node,nodeIter,start,length);
603 } finally {
604 nodeIter.close();
605 }
606 }
607
608 public static final CommandSpec post_list = CommandSpec.DO()
609 .parameters("length","sort")
610 .optionalParameters("start")
611 .requiredInStack(ServletNamespace.class)
612 .build()
613 ;
614
615 @Command public void post_list(IPrintWriter out,ScopedInterpreter<NodeList> interp)
616 throws ServletException
617 {
618 NodeList.posts(out,interp,node(),getLoopStart(interp),getLoopLength(interp),interp.getArgString("sort"),filter(interp));
619 }
620
621 public static int getLoopStart(Interpreter interp) {
622 return interp.getArgAsInt("start",0);
623 }
624
625 public static int getLoopLength(Interpreter interp) {
626 try {
627 return Integer.valueOf( interp.getArgString("length").trim() );
628 } catch(NumberFormatException e) {
629 throw new RuntimeException("Invalid loop length",e);
630 }
631 }
632
633 public static final CommandSpec can_be_viewed_by_visitor = new CommandSpec.Builder()
634 .requiredInStack(ServletNamespace.class)
635 .build()
636 ;
637
638 @Command public void can_be_viewed_by_visitor(IPrintWriter out,Interpreter interp)
639 throws ServletException
640 {
641 if( Jtp.isCached(servletNsUtils.request(interp),servletNsUtils.response(interp)) ) {
642 out.print( Permissions.canBeViewedByParentViewers(node()) );
643 } else {
644 out.print( Permissions.canBeViewedByPerson(node(),servletNsUtils.visitor(interp)) );
645 }
646 }
647
648 public static final CommandSpec groups_have_permission = new CommandSpec.Builder()
649 .parameters("groups","permission")
650 .build()
651 ;
652
653 @Command public void groups_have_permission(IPrintWriter out,Interpreter interp)
654 throws ServletException
655 {
656 String groups = interp.getArgString("groups");
657 String perm = interp.getArgString("permission");
658 for( String group : groups.split(",") ) {
659 if( Permissions.hasPermission(node(),group.trim(),perm) ) {
660 out.print( true );
661 return;
662 }
663 }
664 out.print( false );
665 }
666
667 public static final CommandSpec node_has_permission = new CommandSpec.Builder()
668 .dotParameter("permission")
669 .build()
670 ;
671
672 @Command public void node_has_permission(IPrintWriter out,Interpreter interp) {
673 String perm = interp.getArgString("permission");
674 out.print( Permissions.nodeHasPermission(node(),perm) );
675 }
676
677 public static final CommandSpec has_permission = new CommandSpec.Builder()
678 .parameters("group","permission")
679 .build()
680 ;
681
682 @Command public void has_permission(IPrintWriter out,Interpreter interp) {
683 String group = interp.getArgString("group");
684 String perm = interp.getArgString("permission");
685 out.print( Permissions.hasPermission(node(),group,perm) );
686 }
687
688 public static final CommandSpec node_with_permission = new CommandSpec.Builder()
689 .parameters("permission")
690 .scopedParameters("do")
691 .dotParameter("do")
692 .outputtedParameters("do")
693 .optionalParameters("do")
694 .build()
695 ;
696
697 @Command public void node_with_permission(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
698 String perm = interp.getArgString("permission");
699 Node node = Permissions.getPermissionNode(node(),perm);
700 out.print( interp.getArg(new NodeNamespace(node),"do") );
701 }
702
703 public static final CommandSpec users_with_permission = new CommandSpec.Builder()
704 .parameters("permission")
705 .scopedParameters("do")
706 .dotParameter("do")
707 .outputtedParameters("do")
708 .build()
709 ;
710
711 @Command public void users_with_permission(IPrintWriter out,ScopedInterpreter<UserNamespace.UserList> interp) {
712 String perm = interp.getArgString("permission");
713 List<User> users = Permissions.getUsersWithPermission(node(),perm);
714 UserNamespace.UserList usersNs = new UserNamespace.UserList(users);
715 out.print( interp.getArg(usersNs,"do") );
716 }
717
718 public static final CommandSpec has_groups_with_permission = new CommandSpec.Builder()
719 .dotParameter("permission")
720 .build()
721 ;
722
723 @Command public void has_groups_with_permission(IPrintWriter out,Interpreter interp) {
724 String perm = interp.getArgString("permission");
725 out.print( Permissions.hasGroupsWithPermission(node(),perm) );
726 }
727
728 public static final CommandSpec groups_with_permission = CommandSpec.DO()
729 .parameters("permission")
730 .build()
731 ;
732
733 @Command public void groups_with_permission(IPrintWriter out,ScopedInterpreter<NabbleNamespace.GroupList> interp)
734 throws ServletException
735 {
736 String perm = interp.getArgString("permission");
737 List<String> groups = Permissions.getGroupsWithPermission(node(),perm);
738 Object block = interp.getArg(new NabbleNamespace.GroupList(groups),"do");
739 out.print(block);
740 }
741
742
743 public static final CommandSpec visitor_subscription = new CommandSpec.Builder()
744 .scopedParameters("do")
745 .dotParameter("do")
746 .outputtedParameters("do")
747 .requiredInStack(ServletNamespace.class)
748 .build()
749 ;
750
751 @Command public void visitor_subscription(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp)
752 throws ServletException
753 {
754 User user = servletNsUtils.visitorUser(interp);
755 SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(node(), user);
756 out.print( interp.getArg(subscriptionModel,"do") );
757 }
758
759 public static final CommandSpec subscription_for = CommandSpec.DO()
760 .parameters("email")
761 .build()
762 ;
763
764 @Command public void subscription_for(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp)
765 throws ModelException.EmailFormat
766 {
767 Node node = node();
768 String email = interp.getArgString("email");
769 if (!new MailAddress(email).isValid())
770 throw new ModelException.EmailFormat(email);
771 User user = node.getSite().getOrCreateUser(email);
772 SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(node, user);
773 out.print( interp.getArg(subscriptionModel,"do") );
774 }
775
776 public static final CommandSpec get_subscription_by_code = new CommandSpec.Builder()
777 .parameters("code")
778 .scopedParameters("do")
779 .dotParameter("do")
780 .outputtedParameters("do")
781 .requiredInStack(ServletNamespace.class)
782 .build()
783 ;
784
785 @Command public void get_subscription_by_code(IPrintWriter out, ScopedInterpreter<SubscriptionNamespace> interp)
786 throws TemplateException
787 {
788 String code = interp.getArgString("code");
789 SubscriptionNamespace subscriptionModel = new SubscriptionNamespace(code, servletNsUtils.request(interp));
790 out.print( interp.getArg(subscriptionModel,"do") );
791 }
792
793 public static final CommandSpec visitor_is_subscribed = ServletNamespaceUtils.requiresServletNamespace;
794
795 @Command public void visitor_is_subscribed(IPrintWriter out,Interpreter interp)
796 throws ServletException
797 {
798 User user = servletNsUtils.visitorUser(interp);
799 out.print( user != null && user.isSubscribed(node()) );
800 }
801
802 public static final CommandSpec unsubscribe_visitor = new CommandSpec.Builder()
803 .requiredInStack(ServletNamespace.class)
804 .outputtedParameters()
805 .build()
806 ;
807
808 @Command public void unsubscribe_visitor(IPrintWriter out,Interpreter interp)
809 throws ServletException
810 {
811 User user = servletNsUtils.visitorUser(interp);
812 if( user != null ) {
813 Subscription s = user.getSubscription(node());
814 if( s != null )
815 s.delete();
816 }
817 }
818
819 public static final CommandSpec subscribe_visitor = new CommandSpec.Builder()
820 .requiredInStack(ServletNamespace.class)
821 .outputtedParameters()
822 .build()
823 ;
824
825 @Command public void subscribe_visitor(IPrintWriter out,Interpreter interp)
826 throws ServletException
827 {
828 Node node = node();
829 User user = servletNsUtils.visitorUser(interp);
830 Subscription s = user.getSubscription(node);
831 if( s == null ) {
832 user.subscribe(node,Subscription.To.DESCENDANTS,Subscription.Type.INSTANT);
833 }
834 }
835
836 public static final CommandSpec user_address = new CommandSpec.Builder()
837 .parameters("email")
838 .build()
839 ;
840
841 @Command public void user_address(IPrintWriter out,Interpreter interp) {
842 Node node = node();
843 String email = interp.getArgString("email");
844 User user = node.getSite().getOrCreateUser(email);
845 out.print( user.getDecoratedAddress(node) );
846 }
847
848
849 @Command public void default_rows_per_page(IPrintWriter out,Interpreter interp) {
850 out.print( Jtp.getDefaultRowsPerPage(node().getType()) );
851 }
852
853 public static final CommandSpec delete_message_or_node = CommandSpec.NO_OUTPUT;
854
855 @Command public void delete_message_or_node(IPrintWriter out,Interpreter interp) {
856 node().deleteMessageOrNode();
857 }
858
859 public static final CommandSpec delete_recursively = CommandSpec.NO_OUTPUT;
860
861 @Command public void delete_recursively(IPrintWriter out,Interpreter interp) {
862 node().deleteRecursively();
863 }
864
865
866 public static final CommandSpec as_node_page = CommandSpec.DO;
867
868 @Command public void as_node_page(IPrintWriter out,ScopedInterpreter<NodePageNamespace> interp) {
869 out.print( interp.getArg(new NodePageNamespace(this),"do") );
870 }
871
872
873 @Command public void has_thumbnail(IPrintWriter out,Interpreter interp) {
874 out.print( Thumbnail.getThumbnailFile(node()) != null );
875 }
876
877 @Command public void thumbnail_url(IPrintWriter out,Interpreter interp) {
878 out.print( interp.encode( Thumbnail.getThumbnailFile(node()) ) );
879 }
880
881 @Command public void is_pinned(IPrintWriter out,Interpreter interp) {
882 out.print( node().isPinned() );
883 }
884
885 public static final CommandSpec pin = CommandSpec.NO_OUTPUT;
886
887 @Command public void pin(IPrintWriter out,Interpreter interp) {
888 Node node = node();
889 Jtp.addPinnedChild(node.getParent(), node);
890 }
891
892 public static final CommandSpec unpin = CommandSpec.NO_OUTPUT;
893
894 @Command public void unpin(IPrintWriter out,Interpreter interp) {
895 Node node = node();
896 Jtp.unpinChild(node.getParent(), node);
897 }
898
899 public static final CommandSpec equals = new CommandSpec.Builder()
900 .dotParameter("node")
901 .build()
902 ;
903
904 @Command public void equals(IPrintWriter out,Interpreter interp) {
905 NodeNamespace ns = interp.getArgAsNamespace(NodeNamespace.class,"node");
906 out.print( ns != null && ns.node() != null && ns.node().equals(node()) );
907 }
908
909 public static final CommandSpec NAME = new CommandSpec.Builder()
910 .dotParameter("name")
911 .build()
912 ;
913
914 public static final CommandSpec has_property = NAME;
915
916 @Command public void has_property(IPrintWriter out,Interpreter interp) {
917 String name = interp.getArgString("name");
918 out.print(node().getProperty(name) != null);
919 }
920
921 public static final CommandSpec get_property = NAME;
922
923 @Command public void get_property(IPrintWriter out,Interpreter interp) {
924 String name = interp.getArgString("name");
925 out.print(node().getProperty(name));
926 }
927
928 public static final CommandSpec delete_property = CommandSpec.NO_OUTPUT()
929 .parameters("name")
930 .build()
931 ;
932
933 @Command public void delete_property(IPrintWriter out,Interpreter interp) {
934 Node node = node();
935 String name = interp.getArgString("name");
936 node.setProperty(name, null);
937 node.update();
938 }
939
940 public static final CommandSpec set_property = CommandSpec.NO_OUTPUT()
941 .parameters("name", "value")
942 .build()
943 ;
944
945 @Command public void set_property(IPrintWriter out,Interpreter interp) {
946 Node node = node();
947 String name = interp.getArgString("name");
948 String value = interp.getArgString("value");
949 node.setProperty(name, value);
950 node.update();
951 }
952
953 @Command public void subscription_count(IPrintWriter out,Interpreter interp) {
954 out.print( node().getSubscriptionCount() );
955 }
956
957 public static final CommandSpec subscriptions = new CommandSpec.Builder()
958 .parameters("length")
959 .optionalParameters("start")
960 .scopedParameters("do")
961 .dotParameter("do")
962 .outputtedParameters("do")
963 .build()
964 ;
965
966 @Command public void subscriptions(IPrintWriter out,ScopedInterpreter<SubscriptionList> interp) {
967 int start = interp.getArgAsInt("start",0);
968 int length = interp.getArgAsInt("length");
969 Collection<Subscription> subscriptions = node().getSubscriptions(start,length);
970 List<SubscriptionNamespace> subscriptionNs = new ArrayList<SubscriptionNamespace>(subscriptions.size());
971 for (Subscription s : subscriptions) {
972 subscriptionNs.add(new SubscriptionNamespace(s));
973 }
974 out.print( interp.getArg(new SubscriptionList(subscriptionNs),"do") );
975 }
976
977 @Namespace (
978 name = "subscriptions",
979 global = true
980 )
981 public static final class SubscriptionList extends ListSequence<SubscriptionNamespace> {
982
983 SubscriptionList(List<SubscriptionNamespace> subscriptions) {
984 super(subscriptions);
985 }
986
987 public static final CommandSpec subscription = CommandSpec.DO;
988
989 @Command public void subscription(IPrintWriter out,ScopedInterpreter<SubscriptionNamespace> interp) {
990 out.print(interp.getArg(get(),"do"));
991 }
992 }
993
994
995 public static final CommandSpec get_private_node = CommandSpec.DO;
996
997 @Command public void get_private_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
998 out.print( interp.getArg(new NodeNamespace(Permissions.getPrivateNode(node())),"do") );
999 }
1000
1001 private String getRedirectionUrl() {
1002 String embeddingUrl = null;
1003 for(
1004 Node n = node();
1005 embeddingUrl == null && n != null;
1006 n = n.getParent()
1007 ) {
1008 embeddingUrl = n.getEmbeddingUrl();
1009 }
1010 return embeddingUrl;
1011 }
1012
1013 @Command public void has_embedding_redirection_url(IPrintWriter out,Interpreter interp) {
1014 out.print( getRedirectionUrl() != null );
1015 }
1016
1017 @Command public void embedding_redirection_url(IPrintWriter out,Interpreter interp) {
1018 out.print( getRedirectionUrl() );
1019 }
1020
1021
1022 @Command public void default_reply_subject(IPrintWriter out,Interpreter interp) {
1023 Node node = node();
1024 String subject = null;
1025 if( node.getKind() == Node.Kind.POST ) {
1026 subject = node.getSubject();
1027 if( !subject.startsWith("Re: ") && !subject.startsWith("RE: ") )
1028 subject = "Re: " + subject;
1029 }
1030 out.print(subject);
1031 }
1032
1033 public static final CommandSpec descendant_nodes_by_user = CommandSpec.DO;
1034
1035 @Command public void descendant_nodes_by_user(IPrintWriter out,ScopedInterpreter<NodesGroupedByUser> interp) {
1036 Map<User,List<Node>> map = new HashMap<User,List<Node>>();
1037 for( Node n : node().getDescendants() ) {
1038 Person u = n.getOwner();
1039 if( !(u instanceof User) )
1040 continue;
1041 User owner = (User)u;
1042 List<Node> nodes = map.get(owner);
1043 if( nodes == null ) {
1044 nodes = new ArrayList<Node>();
1045 map.put(owner,nodes);
1046 }
1047 nodes.add(n);
1048 }
1049 out.print(interp.getArg(new NodesGroupedByUser(map),"do"));
1050 }
1051
1052 @Namespace (
1053 name = "nodes_grouped_by_user",
1054 global = true
1055 )
1056 public static final class NodesGroupedByUser extends UserNamespace.UserList {
1057
1058 Map<User, List<Node>> userNodes;
1059
1060 NodesGroupedByUser(Map<User, List<Node>> userNodes) {
1061 super(Arrays.asList(userNodes.keySet().toArray(new User[userNodes.size()])));
1062 this.userNodes = userNodes;
1063 }
1064
1065 public static final CommandSpec nodes_list = CommandSpec.DO;
1066
1067 @Command public void nodes_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) {
1068 List<Node> nodes = userNodes.get(get());
1069 out.print(interp.getArg(new NodeList(nodes, null, false),"do"));
1070 }
1071 }
1072
1073
1074 public static final CommandSpec get_instant_emails = CommandSpec.DO;
1075
1076 @Command public void get_instant_emails(IPrintWriter out,ScopedInterpreter<InstantMailNamespace> interp) {
1077 Node node = node();
1078 Map<User,Subscription> map = node.getSubscribersToNotify();
1079 if( !map.isEmpty() )
1080 out.print( interp.getArg(new InstantMailNamespace(node, map),"do") );
1081 }
1082
1083 @Command public void has_prev_topic(IPrintWriter out, Interpreter interp) {
1084 out.print(node().hasPreviousTopic());
1085 }
1086
1087 @Command public void has_next_topic(IPrintWriter out, Interpreter interp) {
1088 out.print(node().hasNextTopic());
1089 }
1090
1091 public static final CommandSpec prev_topic = CommandSpec.DO;
1092
1093 @Command public void prev_topic(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp)
1094 {
1095 NodeNamespace ns = new NodeNamespace(node().getPreviousTopic());
1096 Object obj = interp.getArg(ns,"do");
1097 out.print(obj);
1098 }
1099
1100 public static final CommandSpec next_topic = CommandSpec.DO;
1101
1102 @Command public void next_topic(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp)
1103 {
1104 NodeNamespace ns = new NodeNamespace(node().getNextTopic());
1105 Object obj = interp.getArg(ns,"do");
1106 out.print(obj);
1107 }
1108
1109 }