🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ReplyGroupFilter.java
1 /*
2  * Sone - ReplyGroupFilter.java - Copyright Â© 2010–2020 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.util.template.Filter;
31 import net.pterodactylus.util.template.TemplateContext;
32
33 /**
34  * {@link Filter} implementation that groups replies by the post the are in
35  * reply to, returning a map with the post as key and the list of replies as
36  * values.
37  */
38 public class ReplyGroupFilter implements Filter {
39
40         /**
41          * {@inheritDoc}
42          */
43         @Override
44         public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
45                 @SuppressWarnings("unchecked")
46                 List<PostReply> allReplies = (List<PostReply>) data;
47                 Map<Post, Set<Sone>> postSones = new HashMap<>();
48                 Map<Post, Set<PostReply>> postReplies = new HashMap<>();
49                 for (PostReply reply : allReplies) {
50                         /*
51                          * All replies from a new-reply notification have posts,
52                          * ListNotificationFilters takes care of that.
53                          */
54                         Post post = reply.getPost().get();
55                         Set<Sone> sones = postSones.get(post);
56                         if (sones == null) {
57                                 sones = new HashSet<>();
58                                 postSones.put(post, sones);
59                         }
60                         sones.add(reply.getSone());
61                         Set<PostReply> replies = postReplies.get(post);
62                         if (replies == null) {
63                                 replies = new HashSet<>();
64                                 postReplies.put(post, replies);
65                         }
66                         replies.add(reply);
67                 }
68                 Map<Post, Map<String, Set<?>>> result = new HashMap<>();
69                 for (Entry<Post, Set<Sone>> postEntry : postSones.entrySet()) {
70                         Map<String, Set<?>> postResult = new HashMap<>();
71                         postResult.put("sones", postEntry.getValue());
72                         postResult.put("replies", postReplies.get(postEntry.getKey()));
73                         result.put(postEntry.getKey(), postResult);
74                 }
75                 return result;
76         }
77
78 }