Use database to return friends of a Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
1 /*
2  * Sone - SoneInserter.java - Copyright © 2010–2013 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.core;
19
20 import static java.lang.String.format;
21 import static java.lang.System.currentTimeMillis;
22 import static java.util.logging.Logger.getLogger;
23 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
24
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.StringWriter;
28 import java.nio.charset.Charset;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35
36 import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
37 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
38 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
39 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
40 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
41 import net.pterodactylus.sone.data.Album;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.Reply;
44 import net.pterodactylus.sone.data.Sone;
45 import net.pterodactylus.sone.data.Sone.SoneStatus;
46 import net.pterodactylus.sone.freenet.StringBucket;
47 import net.pterodactylus.sone.main.SonePlugin;
48 import net.pterodactylus.util.io.Closer;
49 import net.pterodactylus.util.service.AbstractService;
50 import net.pterodactylus.util.template.HtmlFilter;
51 import net.pterodactylus.util.template.ReflectionAccessor;
52 import net.pterodactylus.util.template.Template;
53 import net.pterodactylus.util.template.TemplateContext;
54 import net.pterodactylus.util.template.TemplateContextFactory;
55 import net.pterodactylus.util.template.TemplateException;
56 import net.pterodactylus.util.template.TemplateParser;
57 import net.pterodactylus.util.template.XmlFilter;
58
59 import com.google.common.annotations.VisibleForTesting;
60 import com.google.common.base.Optional;
61 import com.google.common.collect.FluentIterable;
62 import com.google.common.collect.Ordering;
63 import com.google.common.eventbus.EventBus;
64 import com.google.common.eventbus.Subscribe;
65
66 import freenet.client.async.ManifestElement;
67 import freenet.keys.FreenetURI;
68
69 /**
70  * A Sone inserter is responsible for inserting a Sone if it has changed.
71  *
72  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
73  */
74 public class SoneInserter extends AbstractService {
75
76         /** The logger. */
77         private static final Logger logger = getLogger("Sone.Inserter");
78
79         /** The insertion delay (in seconds). */
80         private static final AtomicInteger insertionDelay = new AtomicInteger(60);
81
82         /** The template factory used to create the templates. */
83         private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
84
85         static {
86                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
87                 templateContextFactory.addFilter("xml", new XmlFilter());
88                 templateContextFactory.addFilter("html", new HtmlFilter());
89         }
90
91         /** The UTF-8 charset. */
92         private static final Charset utf8Charset = Charset.forName("UTF-8");
93
94         /** The core. */
95         private final Core core;
96
97         /** The event bus. */
98         private final EventBus eventBus;
99
100         /** The Freenet interface. */
101         private final FreenetInterface freenetInterface;
102
103         private final SoneModificationDetector soneModificationDetector;
104         private final long delay;
105         private final String soneId;
106
107         /**
108          * Creates a new Sone inserter.
109          *
110          * @param core
111          *            The core
112          * @param eventBus
113          *            The event bus
114          * @param freenetInterface
115          *            The freenet interface
116          * @param soneId
117          *            The ID of the Sone to insert
118          */
119         public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) {
120                 this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
121                         @Override
122                         public boolean isLocked() {
123                                 final Optional<Sone> sone = core.getSone(soneId);
124                                 if (!sone.isPresent()) {
125                                         return false;
126                                 }
127                                 return core.isLocked(sone.get());
128                         }
129
130                         @Override
131                         public String getFingerprint() {
132                                 final Optional<Sone> sone = core.getSone(soneId);
133                                 if (!sone.isPresent()) {
134                                         return null;
135                                 }
136                                 return sone.get().getFingerprint();
137                         }
138                 }, insertionDelay), 1000);
139         }
140
141         @VisibleForTesting
142         SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
143                 super("Sone Inserter for “" + soneId + "”", false);
144                 this.core = core;
145                 this.eventBus = eventBus;
146                 this.freenetInterface = freenetInterface;
147                 this.soneId = soneId;
148                 this.soneModificationDetector = soneModificationDetector;
149                 this.delay = delay;
150         }
151
152         //
153         // ACCESSORS
154         //
155
156         @VisibleForTesting
157         static AtomicInteger getInsertionDelay() {
158                 return insertionDelay;
159         }
160
161         /**
162          * Changes the insertion delay, i.e. the time the Sone inserter waits after it
163          * has noticed a Sone modification before it starts the insert.
164          *
165          * @param insertionDelay
166          *            The insertion delay (in seconds)
167          */
168         private static void setInsertionDelay(int insertionDelay) {
169                 SoneInserter.insertionDelay.set(insertionDelay);
170         }
171
172         /**
173          * Returns the fingerprint of the last insert.
174          *
175          * @return The fingerprint of the last insert
176          */
177         public String getLastInsertFingerprint() {
178                 return soneModificationDetector.getOriginalFingerprint();
179         }
180
181         /**
182          * Sets the fingerprint of the last insert.
183          *
184          * @param lastInsertFingerprint
185          *            The fingerprint of the last insert
186          */
187         public void setLastInsertFingerprint(String lastInsertFingerprint) {
188                 soneModificationDetector.setFingerprint(lastInsertFingerprint);
189         }
190
191         /**
192          * Returns whether the Sone inserter has detected a modification of the
193          * Sone.
194          *
195          * @return {@code true} if the Sone has been modified, {@code false}
196          *         otherwise
197          */
198         public boolean isModified() {
199                 return soneModificationDetector.isModified();
200         }
201
202         //
203         // SERVICE METHODS
204         //
205
206         /**
207          * {@inheritDoc}
208          */
209         @Override
210         protected void serviceRun() {
211                 while (!shouldStop()) {
212                         try {
213                                 /* check every second. */
214                                 sleep(delay);
215
216                                 if (soneModificationDetector.isEligibleForInsert()) {
217                                         Optional<Sone> soneOptional = core.getSone(soneId);
218                                         if (!soneOptional.isPresent()) {
219                                                 logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
220                                                 return;
221                                         }
222                                         Sone sone = soneOptional.get();
223                                         InsertInformation insertInformation = new InsertInformation(sone);
224                                         logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
225
226                                         boolean success = false;
227                                         try {
228                                                 sone.setStatus(SoneStatus.inserting);
229                                                 long insertTime = currentTimeMillis();
230                                                 eventBus.post(new SoneInsertingEvent(sone));
231                                                 FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
232                                                 eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
233                                                 /* at this point we might already be stopped. */
234                                                 if (shouldStop()) {
235                                                         /* if so, bail out, don’t change anything. */
236                                                         break;
237                                                 }
238                                                 sone.setTime(insertTime);
239                                                 sone.setLatestEdition(finalUri.getEdition());
240                                                 core.touchConfiguration();
241                                                 success = true;
242                                                 logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
243                                         } catch (SoneException se1) {
244                                                 eventBus.post(new SoneInsertAbortedEvent(sone, se1));
245                                                 logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1);
246                                         } finally {
247                                                 sone.setStatus(SoneStatus.idle);
248                                         }
249
250                                         /*
251                                          * reset modification counter if Sone has not been modified
252                                          * while it was inserted.
253                                          */
254                                         if (success) {
255                                                 synchronized (sone) {
256                                                         if (insertInformation.getFingerprint().equals(sone.getFingerprint())) {
257                                                                 logger.log(Level.FINE, String.format("Sone “%s” was not modified further, resetting counter…", sone));
258                                                                 soneModificationDetector.setFingerprint(insertInformation.getFingerprint());
259                                                                 core.touchConfiguration();
260                                                         }
261                                                 }
262                                         }
263                                 }
264                         } catch (Throwable t1) {
265                                 logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
266                         }
267                 }
268         }
269
270         @Subscribe
271         public void insertionDelayChanged(InsertionDelayChangedEvent insertionDelayChangedEvent) {
272                 setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay());
273         }
274
275         /**
276          * Container for information that are required to insert a Sone. This
277          * container merely exists to copy all relevant data without holding a lock
278          * on the {@link Sone} object for too long.
279          *
280          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
281          */
282         @VisibleForTesting
283         class InsertInformation {
284
285                 private final String fingerprint;
286                 private final ManifestCreator manifestCreator;
287
288                 /**
289                  * Creates a new insert information container.
290                  *
291                  * @param sone
292                  *            The sone to insert
293                  */
294                 public InsertInformation(Sone sone) {
295                         this.fingerprint = sone.getFingerprint();
296                         Map<String, Object> soneProperties = new HashMap<String, Object>();
297                         soneProperties.put("id", sone.getId());
298                         soneProperties.put("name", sone.getName());
299                         soneProperties.put("time", currentTimeMillis());
300                         soneProperties.put("requestUri", sone.getRequestUri());
301                         soneProperties.put("profile", sone.getProfile());
302                         soneProperties.put("posts", Ordering.from(Post.TIME_COMPARATOR).sortedCopy(sone.getPosts()));
303                         soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
304                         soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
305                         soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
306                         soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
307                         manifestCreator = new ManifestCreator(core, soneProperties);
308                 }
309
310                 //
311                 // ACCESSORS
312                 //
313
314                 @VisibleForTesting
315                 String getFingerprint() {
316                         return fingerprint;
317                 }
318
319                 //
320                 // ACTIONS
321                 //
322
323                 /**
324                  * Generates all manifest entries required to insert this Sone.
325                  *
326                  * @return The manifest entries for the Sone insert
327                  */
328                 public HashMap<String, Object> generateManifestEntries() {
329                         HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
330
331                         /* first, create an index.html. */
332                         manifestEntries.put("index.html", manifestCreator.createManifestElement(
333                                         "index.html", "text/html; charset=utf-8",
334                                         "/templates/insert/index.html"));
335
336                         /* now, store the sone. */
337                         manifestEntries.put("sone.xml", manifestCreator.createManifestElement(
338                                         "sone.xml", "text/xml; charset=utf-8",
339                                         "/templates/insert/sone.xml"));
340
341                         return manifestEntries;
342                 }
343
344         }
345
346         /**
347          * Creates manifest elements for an insert by rendering a template.
348          *
349          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
350          */
351         @VisibleForTesting
352         static class ManifestCreator {
353
354                 private final Core core;
355                 private final Map<String, Object> soneProperties;
356
357                 ManifestCreator(Core core, Map<String, Object> soneProperties) {
358                         this.core = core;
359                         this.soneProperties = soneProperties;
360                 }
361
362                 public ManifestElement createManifestElement(String name, String contentType, String templateName) {
363                         InputStreamReader templateInputStreamReader = null;
364                         InputStream templateInputStream = null;
365                         Template template;
366                         try {
367                                 templateInputStream = getClass().getResourceAsStream(templateName);
368                                 templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset);
369                                 template = TemplateParser.parse(templateInputStreamReader);
370                         } catch (TemplateException te1) {
371                                 logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), te1);
372                                 return null;
373                         } finally {
374                                 Closer.close(templateInputStreamReader);
375                                 Closer.close(templateInputStream);
376                         }
377
378                         TemplateContext templateContext = templateContextFactory.createTemplateContext();
379                         templateContext.set("core", core);
380                         templateContext.set("currentSone", soneProperties);
381                         templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
382                         templateContext.set("version", SonePlugin.VERSION);
383                         StringWriter writer = new StringWriter();
384                         StringBucket bucket = null;
385                         try {
386                                 template.render(templateContext, writer);
387                                 bucket = new StringBucket(writer.toString(), utf8Charset);
388                                 return new ManifestElement(name, bucket, contentType, bucket.size());
389                         } catch (TemplateException te1) {
390                                 logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1);
391                                 return null;
392                         } finally {
393                                 Closer.close(writer);
394                                 if (bucket != null) {
395                                         bucket.free();
396                                 }
397                         }
398                 }
399
400         }
401
402 }