Mercurial Hosting > nabble
annotate src/nabble/view/web/template/UserNamespace.java @ 44:cc5b7d515580
remove post by email
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 09:43:51 -0600 |
parents | 18cf4872fd7f |
children | 72765b66e2c3 |
rev | line source |
---|---|
0 | 1 package nabble.view.web.template; |
2 | |
3 import fschmidt.util.java.Filter; | |
4 import fschmidt.util.java.HtmlUtils; | |
5 import nabble.model.DailyNumber; | |
6 import nabble.model.FilteredNodeIterator; | |
7 import nabble.model.ModelException; | |
8 import nabble.model.ModelHome; | |
9 import nabble.model.Node; | |
10 import nabble.model.NodeIterator; | |
11 import nabble.model.NodeSearcher; | |
12 import nabble.model.Person; | |
13 import nabble.model.Site; | |
14 import nabble.model.Subscription; | |
15 import nabble.model.User; | |
16 import nabble.naml.compiler.Command; | |
17 import nabble.naml.compiler.CommandSpec; | |
18 import nabble.naml.compiler.IPrintWriter; | |
19 import nabble.naml.compiler.Interpreter; | |
20 import nabble.naml.compiler.Namespace; | |
21 import nabble.naml.compiler.ScopedInterpreter; | |
22 import nabble.naml.namespaces.ListSequence; | |
23 import nabble.view.lib.Jtp; | |
24 import nabble.view.lib.Permissions; | |
25 import nabble.view.lib.Shared; | |
26 | |
27 import javax.servlet.ServletException; | |
28 import java.io.IOException; | |
29 import java.util.ArrayList; | |
30 import java.util.Collections; | |
31 import java.util.List; | |
32 import java.util.Map; | |
33 | |
34 | |
35 @Namespace ( | |
36 name = "user", | |
37 global = false | |
38 ) | |
39 public final class UserNamespace { | |
40 private final Person personR; | |
41 private User userR; | |
42 | |
43 public UserNamespace(Person person) { | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
44 /* |
0 | 45 if( person == null ) |
46 throw new NullPointerException("person is null"); | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
47 */ |
0 | 48 this.personR = person; |
49 this.userR = person instanceof User ? (User)person : null; | |
50 } | |
51 | |
52 public void refreshUser() { | |
53 if( isUser() ) | |
54 userR = userR.getGoodCopy(); | |
55 } | |
56 | |
57 public boolean isUser() { | |
58 return userR != null; | |
59 } | |
60 | |
61 public User user() { | |
62 return userR; | |
63 } | |
64 | |
65 public Person person() { | |
66 return isUser() ? user() : personR; | |
67 } | |
68 | |
69 private Site site() { | |
70 return person().getSite(); | |
71 } | |
72 | |
73 public static final CommandSpec this_user = CommandSpec.DO; | |
74 | |
75 @Command public void this_user(IPrintWriter out,ScopedInterpreter<UserNamespace> interp) { | |
76 out.print( interp.getArg(this,"do") ); | |
77 } | |
78 | |
79 @Command public void id(IPrintWriter out,Interpreter interp) { | |
80 out.print( interp.encode( person().getIdString() ) ); | |
81 } | |
82 | |
83 @Command public void search_id(IPrintWriter out,Interpreter interp) { | |
84 out.print( person().getSearchId() ); | |
85 } | |
86 | |
87 public static final CommandSpec name = new CommandSpec.Builder() | |
88 .optionalParameters("truncate") | |
89 .build() | |
90 ; | |
91 | |
92 @Command public void name(IPrintWriter out,Interpreter interp) | |
93 throws IOException | |
94 { | |
95 String truncate = interp.getArgString("truncate"); | |
96 String name = person().getName(); | |
97 if( name==null ) { | |
98 out.print((String)null); | |
99 return; | |
100 } | |
101 if (truncate != null) { | |
102 try { | |
103 name = Jtp.truncate(name,Integer.valueOf(truncate),"..."); | |
104 } catch (NumberFormatException e) { | |
105 throw new RuntimeException("Invalid \"truncate\" attribute: " + truncate); | |
106 } | |
107 } | |
108 out.print( interp.encode(name) ); | |
109 } | |
110 | |
111 public static final CommandSpec avatar_url = new CommandSpec.Builder() | |
112 .optionalParameters("size") | |
113 .build() | |
114 ; | |
115 | |
116 @Command public void avatar_url(IPrintWriter out,Interpreter interp) | |
117 throws IOException | |
118 { | |
119 String size = interp.getArgString("size"); | |
120 out.print( interp.encode(Shared.getAvatarImageURL(person(), "small".equals(size))) ); | |
121 } | |
122 | |
123 public static final CommandSpec default_avatar_url = new CommandSpec.Builder() | |
124 .optionalParameters("size") | |
125 .build() | |
126 ; | |
127 | |
128 @Command public void default_avatar_url(IPrintWriter out,Interpreter interp) | |
129 throws IOException | |
130 { | |
131 String size = interp.getArgString("size"); | |
132 out.print( interp.encode(Shared.getDefaultAvatarImageURL("small".equals(size))) ); | |
133 } | |
134 | |
135 @Command public void is_registered(IPrintWriter out,Interpreter interp) { | |
136 out.print( isUser() && user().isRegistered() ); | |
137 } | |
138 | |
139 @Command public void has_too_many_posts(IPrintWriter out,Interpreter interp) { | |
140 out.print( isUser() && user().hasTooManyPosts() ); | |
141 } | |
142 | |
143 public static final CommandSpec registration_date = CommandSpec.DO; | |
144 | |
145 @Command public void registration_date(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) { | |
146 out.print( interp.getArg( new DateNamespace(user().getRegistered()), "do" ) ); | |
147 } | |
148 | |
149 @Command public void is_deactivated(IPrintWriter out,Interpreter interp) { | |
150 out.print( isUser() && user().isDeactivated() ); | |
151 } | |
152 | |
153 @Command public void is_anonymous(IPrintWriter out,Interpreter interp) { | |
154 out.print( !isUser() ); | |
155 } | |
156 | |
157 @Command public void is_authenticated(IPrintWriter out,Interpreter interp) { | |
158 out.print( isUser() ); | |
159 } | |
160 | |
161 @Command public void user_email(IPrintWriter out,Interpreter interp) { | |
162 out.print( user().getEmail() ); | |
163 } | |
164 | |
165 @Command public void resetcode(IPrintWriter out,Interpreter interp) { | |
166 out.print( user().getResetcode() ); | |
167 } | |
168 | |
169 @Command public void send_email_url(IPrintWriter out,Interpreter interp) { | |
170 out.print( interp.encode( "/user/SendEmail.jtp?type=user&user=" + user().getId() ) ); | |
171 } | |
172 | |
173 | |
174 public static final CommandSpec is_in_group = new CommandSpec.Builder() | |
175 .dotParameter("group") | |
176 .build() | |
177 ; | |
178 | |
179 @Command public void is_in_group(IPrintWriter out,Interpreter interp) { | |
180 String group = interp.getArgString("group"); | |
181 out.print( isUser() && Permissions.isInGroup(user(),group) ); | |
182 } | |
183 | |
184 public static final CommandSpec has_permission = new CommandSpec.Builder() | |
185 .parameters("node","permission_node","permission") | |
186 .build() | |
187 ; | |
188 | |
189 @Command public void has_permission(IPrintWriter out,Interpreter interp) { | |
190 NodeNamespace nodeNs = interp.getArgAsNamespace(NodeNamespace.class,"node"); | |
191 NodeNamespace permissionNodeNs = interp.getArgAsNamespace(NodeNamespace.class,"permission_node"); | |
192 String perm = interp.getArgString("permission"); | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
193 out.print( Permissions.hasPermission(permissionNodeNs.node(),nodeNs.node(),user(),perm) ); |
0 | 194 } |
195 | |
196 public static final CommandSpec has_site_permission = new CommandSpec.Builder() | |
197 .parameters("permission") | |
198 .build() | |
199 ; | |
200 | |
201 @Command public void has_site_permission(IPrintWriter out,Interpreter interp) { | |
202 String perm = interp.getArgString("permission"); | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
203 out.print( Permissions.hasSitePermission(NabbleNamespace.current().site(),user(),perm) ); |
0 | 204 } |
205 | |
206 public static final CommandSpec owns = new CommandSpec.Builder() | |
207 .dotParameter("node") | |
208 .build() | |
209 ; | |
210 | |
211 @Command public void owns(IPrintWriter out,Interpreter interp) { | |
212 NodeNamespace nodeNs = interp.getArgAsNamespace(NodeNamespace.class,"node"); | |
213 out.print( nodeNs.node().getOwner().equals(person()) ); | |
214 } | |
215 | |
216 public static final CommandSpec as_user_page = CommandSpec.DO; | |
217 | |
218 @Command public void as_user_page(IPrintWriter out,ScopedInterpreter<UserPageNamespace> interp) { | |
219 out.print( interp.getArg(new UserPageNamespace(this),"do") ); | |
220 } | |
221 | |
222 | |
223 public static final CommandSpec groups = CommandSpec.DO; | |
224 | |
225 @Command public void groups(IPrintWriter out,ScopedInterpreter<NabbleNamespace.GroupList> interp) { | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
226 List<String> groups = Permissions.getPersonGroups(user()); |
0 | 227 Object block = interp.getArg(new NabbleNamespace.GroupList(groups),"do"); |
228 out.print(block); | |
229 } | |
230 | |
231 public static final CommandSpec nodes_list = CommandSpec.DO() | |
232 .parameters("length") | |
233 .optionalParameters("start","sort","filter") | |
234 .requiredInStack(ServletNamespace.class) | |
235 .build() | |
236 ; | |
237 | |
238 @Command public void nodes_list(IPrintWriter out,ScopedInterpreter<NodeList> interp) | |
239 throws ServletException | |
240 { | |
241 ServletNamespace servletNamespace = interp.getFromStack(ServletNamespace.class); | |
242 int start = NodeNamespace.getLoopStart(interp); | |
243 int length = NodeNamespace.getLoopLength(interp); | |
244 String filter = interp.getArgString("filter"); | |
245 NodeIterator<? extends Node> nodeIter = person().getNodesByDateDesc(filter); | |
246 try { | |
247 Filter<Node> viewFilter = Permissions.canBeViewedByPersonFilter(servletNamespace.getVisitorUser()); | |
248 nodeIter = new FilteredNodeIterator(nodeIter,viewFilter); | |
249 NodeList.userNodes( out, interp, nodeIter, start, length ); | |
250 } finally { | |
251 nodeIter.close(); | |
252 } | |
253 } | |
254 | |
255 @Command public void post_filter(IPrintWriter out,Interpreter interp) { | |
256 out.print( "(is_app is null or not is_app)" ); | |
257 } | |
258 | |
259 @Command public void app_filter(IPrintWriter out,Interpreter interp) { | |
260 out.print( "is_app" ); | |
261 } | |
262 | |
263 @Command public void topic_filter(IPrintWriter out,Interpreter interp) { | |
264 out.print( "(is_app is null or not is_app) and exists(select 1 from node p where node.parent_id=p.node_id and is_app)" ); | |
265 } | |
266 | |
267 @Command public void reply_filter(IPrintWriter out,Interpreter interp) { | |
268 out.print( "(is_app is null or not is_app) and exists(select 1 from node p where node.parent_id=p.node_id and (is_app is null or not is_app))" ); | |
269 } | |
270 | |
271 public static final CommandSpec equals = new CommandSpec.Builder() | |
272 .dotParameter("user") | |
273 .build() | |
274 ; | |
275 | |
276 @Command public void equals(IPrintWriter out,Interpreter interp) { | |
277 UserNamespace userNs = interp.getArgAsNamespace(UserNamespace.class,"user"); | |
278 out.print( userNs.person().equals(person()) ); | |
279 } | |
280 | |
281 | |
282 @Namespace ( | |
283 name = "user_list", | |
284 global = true | |
285 ) | |
286 public static class UserList extends ListSequence<User> { | |
287 | |
288 public UserList(List<User> nodeUsers) { | |
289 super(nodeUsers); | |
290 } | |
291 | |
292 public static final CommandSpec current_user = CommandSpec.DO; | |
293 | |
294 @Command public void current_user(IPrintWriter out,ScopedInterpreter<UserNamespace> interp) { | |
295 out.print( interp.getArg(new UserNamespace(get()),"do") ); | |
296 } | |
297 | |
298 @Command public void next_user(IPrintWriter out,Interpreter interp) { | |
299 next_element(out,interp); | |
300 } | |
301 | |
302 public static final CommandSpec sort_by_node_count_desc = CommandSpec.NO_OUTPUT; | |
303 | |
304 @Command public void sort_by_node_count_desc(IPrintWriter out,Interpreter interp) { | |
305 Collections.sort(elements,User.nodeCountComparatorDesc); | |
306 } | |
307 | |
308 public static final CommandSpec sort_by_name = CommandSpec.NO_OUTPUT; | |
309 | |
310 @Command public void sort_by_name(IPrintWriter out,Interpreter interp) { | |
311 Collections.sort(elements,User.BY_NAME_COMPARATOR); | |
312 } | |
313 | |
314 public static final CommandSpec sub_list = CommandSpec.DO() | |
315 .parameters("length") | |
316 .optionalParameters("start") | |
317 .build() | |
318 ; | |
319 | |
320 @Command public void sub_list(IPrintWriter out,ScopedInterpreter<UserList> interp) { | |
321 int start = interp.getArgAsInt("start",0); | |
322 int length = interp.getArgAsInt("length"); | |
323 UserList ns ; | |
324 if( start==0 && length >= elements.size() ) { | |
325 ns = this; | |
326 } else { | |
327 ns = new UserList(elements.subList(start,Math.min(elements.size(),start+length))); | |
328 } | |
329 out.print( interp.getArg(ns,"do") ); | |
330 } | |
331 } | |
332 | |
333 public static final CommandSpec signature_impl = CommandSpec.DO; | |
334 | |
335 @Command public void signature_impl(IPrintWriter out,ScopedInterpreter<MessageNamespace> interp) { | |
336 out.print( interp.getArg( new MessageNamespace(person().getSignature()), "do" ) ); | |
337 } | |
338 | |
339 @Command public void has_signature(IPrintWriter out,Interpreter interp) { | |
340 out.print( person().getSignature() != null ); | |
341 } | |
342 | |
343 public static final CommandSpec node_count = new CommandSpec.Builder() | |
344 .optionalParameters("filter") | |
345 .build() | |
346 ; | |
347 | |
348 @Command public void node_count(IPrintWriter out,Interpreter interp) { | |
349 out.print( person().getNodeCount(interp.getArgString("filter")) ); | |
350 } | |
351 | |
352 @Command public void is_banned(IPrintWriter out, Interpreter interp) { | |
353 out.print( isUser() && Permissions.isBanned(user()) ); | |
354 } | |
355 | |
356 public static final CommandSpec ban = CommandSpec.NO_OUTPUT; | |
357 | |
358 @Command public void ban(IPrintWriter out, Interpreter interp) { | |
359 Permissions.ban(user()); | |
360 user().setNoArchive(true); | |
361 site().update(); // uncache | |
362 } | |
363 | |
364 public static final CommandSpec unban = CommandSpec.NO_OUTPUT; | |
365 | |
366 @Command public void unban(IPrintWriter out, Interpreter interp) { | |
367 Permissions.unban(user()); | |
368 user().setNoArchive(false); | |
369 site().update(); // uncache | |
370 } | |
371 | |
372 public static final CommandSpec delete_user_nodes = CommandSpec.NO_OUTPUT; | |
373 | |
374 @Command public void delete_user_nodes(IPrintWriter out, Interpreter interp) { | |
375 person().deleteNodes(); | |
376 } | |
377 | |
378 | |
379 @Command public void is_sysadmin(IPrintWriter out, Interpreter interp) { | |
380 out.print( isUser() && Permissions.isSysAdmin(user()) ); | |
381 } | |
382 | |
383 public static final CommandSpec reset_password_path = new CommandSpec.Builder() | |
384 .parameters("email","q") | |
385 .build() | |
386 ; | |
387 | |
388 @Command public void reset_password_path(IPrintWriter out,Interpreter interp) { | |
389 String email = interp.getArgString("email"); | |
390 String q = interp.getArgString("q"); | |
391 out.print( interp.encode( "/user/ResetPassword.jtp" ) ); | |
392 if (email!=null && q!=null) | |
393 out.print( interp.encode("?email=" + HtmlUtils.urlEncode(email) + "&q=" + HtmlUtils.urlEncode(q)) ); | |
394 } | |
395 | |
396 @Command public void change_email_path(IPrintWriter out,Interpreter interp) { | |
397 out.print( interp.encode( "/user/ChangeEmail.jtp?user=" + user().getId() ) ); | |
398 } | |
399 | |
400 @Command public void change_avatar_path(IPrintWriter out,Interpreter interp) { | |
401 out.print( interp.encode( "/user/ChangeAvatar.jtp" ) ); | |
402 } | |
403 | |
404 @Command public void advanced_settings_path(IPrintWriter out,Interpreter interp) { | |
405 out.print( interp.encode( "/user/Advanced.jtp" ) ); | |
406 } | |
407 | |
408 @Command public void remove_account_path(IPrintWriter out,Interpreter interp) { | |
409 out.print( interp.encode( "/user/RemoveAccount.jtp" ) ); | |
410 } | |
411 | |
412 @Command public void pending_posts_path(IPrintWriter out,Interpreter interp) { | |
413 out.print( interp.encode( "/user/UserPendingNodes.jtp" ) ); | |
414 } | |
415 | |
416 private Map<Long,Long> lastVisitedNodeIds = null; | |
417 | |
418 private Long lastVisitedNodeId(Interpreter interp,long nodeId) { | |
419 if( !isUser() ) | |
420 return null; | |
421 if( lastVisitedNodeIds == null ) { | |
422 RequestNamespace.ParameterValueList params = interp.getFromStack(RequestNamespace.ParameterValueList.class); | |
423 List<Long> ids = new ArrayList<Long>(); | |
424 for( String value : params.values() ) { | |
425 ids.add(Long.parseLong(value)); | |
426 } | |
427 lastVisitedNodeIds = user().lastVisitedNodeIds(ids); | |
428 } | |
429 return lastVisitedNodeIds.get(nodeId); | |
430 } | |
431 | |
432 public static final CommandSpec has_visited_node = CommandSpec.DO() | |
433 .parameters("node_id") | |
434 .requiredInStack(RequestNamespace.ParameterValueList.class) | |
435 .build() | |
436 ; | |
437 | |
438 @Command public void has_visited_node(IPrintWriter out, Interpreter interp) { | |
439 Long id = lastVisitedNodeId(interp, interp.getArgAsLong("node_id")); | |
440 Node node = id == null? null : site().getNode(id); | |
441 if (id != null && node == null) | |
442 lastVisitedNodeIds.remove(id); | |
443 out.print( node != null); | |
444 } | |
445 | |
446 public static final CommandSpec last_visited_node = CommandSpec.DO() | |
447 .parameters("node_id") | |
448 .requiredInStack(RequestNamespace.ParameterValueList.class) | |
449 .build() | |
450 ; | |
451 | |
452 @Command public void last_visited_node(IPrintWriter out, ScopedInterpreter<NodeNamespace> interp) { | |
453 Node lastVisitedNode = null; | |
454 long nodeId = interp.getArgAsLong("node_id"); | |
455 Long id = lastVisitedNodeId(interp,nodeId); | |
456 if (id != null) | |
457 lastVisitedNode = site().getNode(id); | |
458 out.print( interp.getArg( new NodeNamespace(lastVisitedNode), "do" ) ); | |
459 } | |
460 | |
461 public static final CommandSpec mark_visited = CommandSpec.NO_OUTPUT() | |
462 .parameters("last_node_id") | |
463 .dotParameter("node") | |
464 .build() | |
465 ; | |
466 | |
467 @Command public void mark_visited(IPrintWriter out, Interpreter interp) { | |
468 if (isUser()) { | |
469 NodeNamespace nodeNs = interp.getArgAsNamespace(NodeNamespace.class,"node"); | |
470 long lastNodeId = interp.getArgAsLong("last_node_id"); | |
471 user().markVisited(nodeNs.node(), lastNodeId); | |
472 } | |
473 } | |
474 | |
475 public static final CommandSpec register = new CommandSpec.Builder() | |
476 .requiredInStack(ServletNamespace.class) | |
477 .build() | |
478 ; | |
479 | |
480 @Command public void register(IPrintWriter out, Interpreter interp) | |
481 throws ModelException, IOException | |
482 { | |
483 user().register(); | |
484 refreshUser(); | |
485 ServletNamespace servletNamespace = interp.getFromStack(ServletNamespace.class); | |
486 Jtp.doLogin(servletNamespace.request, servletNamespace.response, user(), false); | |
487 DailyNumber.registrations.inc(); | |
488 } | |
489 | |
490 public static final CommandSpec NAME = new CommandSpec.Builder() | |
491 .dotParameter("name") | |
492 .build() | |
493 ; | |
494 | |
495 public static final CommandSpec has_property = NAME; | |
496 | |
497 @Command public void has_property(IPrintWriter out,Interpreter interp) { | |
498 String name = interp.getArgString("name"); | |
499 out.print(user().getProperty(name) != null); | |
500 } | |
501 | |
502 public static final CommandSpec get_property = NAME; | |
503 | |
504 @Command public void get_property(IPrintWriter out,Interpreter interp) { | |
505 String name = interp.getArgString("name"); | |
506 out.print(user().getProperty(name)); | |
507 } | |
508 | |
509 public static final CommandSpec delete_property = CommandSpec.NO_OUTPUT() | |
510 .parameters("name") | |
511 .build() | |
512 ; | |
513 | |
514 @Command public void delete_property(IPrintWriter out,Interpreter interp) { | |
515 User user = user(); | |
516 String name = interp.getArgString("name"); | |
517 user.setProperty(name, null); | |
518 user.getSite().update(); | |
519 } | |
520 | |
521 public static final CommandSpec set_property = CommandSpec.NO_OUTPUT() | |
522 .parameters("name", "value") | |
523 .build() | |
524 ; | |
525 | |
526 @Command public void set_property(IPrintWriter out,Interpreter interp) { | |
527 User user = user(); | |
528 String name = interp.getArgString("name"); | |
529 String value = interp.getArgString("value"); | |
530 user.setProperty(name, value); | |
531 user.getSite().update(); | |
532 } | |
533 | |
534 public static final CommandSpec has_subscription_to_descentants_of = CommandSpec.NO_OUTPUT() | |
535 .dotParameter("node") | |
536 .build() | |
537 ; | |
538 | |
539 @Command public void has_subscription_to_descentants_of(IPrintWriter out,Interpreter interp) { | |
540 if (userR == null) { | |
541 out.print(false); | |
542 return; | |
543 } | |
544 NodeNamespace nodeNs = interp.getArgAsNamespace(NodeNamespace.class,"node"); | |
545 for (Node n : nodeNs.node().getAncestors() ) { | |
546 if (userR.isSubscribed(n)) { | |
547 Subscription subscription = userR.getSubscription(n); | |
548 if (subscription.getTo() == Subscription.To.DESCENDANTS) { | |
549 out.print(true); | |
550 return; | |
551 } | |
552 } | |
553 } | |
554 out.print(false); | |
555 } | |
556 | |
557 } |