X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ftriggers%2FNewTorrentTrigger.java;h=9e3e91ee22e1c563f1989394c01b9cace522d9bb;hb=22e98b8896aa47b397ba89d887bfdbe212850517;hp=2803bb5c653782788b58ab5077075013a7dae086;hpb=6ec36ef950c23c135bf0e112d932c5b7068189b8;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java index 2803bb5..9e3e91e 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java @@ -20,6 +20,7 @@ package net.pterodactylus.rhynodge.triggers; import static com.google.common.base.Preconditions.checkState; import java.util.List; +import java.util.Set; import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.State; @@ -32,6 +33,7 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; import org.apache.commons.lang3.StringEscapeUtils; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; /** * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that @@ -41,30 +43,44 @@ import com.google.common.collect.Lists; */ public class NewTorrentTrigger implements Trigger { + /** All known torrents. */ + private final Set allTorrentFiles = Sets.newHashSet(); + /** The newly detected torrent files. */ - private List torrentFiles = Lists.newArrayList(); + private final List newTorrentFiles = Lists.newArrayList(); // // TRIGGER METHODS // /** - * {@inheritDoc} + * {@inheritDocs} */ @Override - public boolean triggers(State currentState, State previousState) { + public State mergeStates(State previousState, State currentState) { checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName()); checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName()); TorrentState currentTorrentState = (TorrentState) currentState; TorrentState previousTorrentState = (TorrentState) previousState; - torrentFiles.clear(); + + allTorrentFiles.clear(); + newTorrentFiles.clear(); + allTorrentFiles.addAll(previousTorrentState.torrentFiles()); for (TorrentFile torrentFile : currentTorrentState) { - torrentFiles.add(torrentFile); - } - for (TorrentFile torrentFile : previousTorrentState) { - torrentFiles.remove(torrentFile); + if (allTorrentFiles.add(torrentFile)) { + newTorrentFiles.add(torrentFile); + } } - return !torrentFiles.isEmpty(); + + return new TorrentState(allTorrentFiles); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean triggers() { + return !newTorrentFiles.isEmpty(); } /** @@ -72,27 +88,27 @@ public class NewTorrentTrigger implements Trigger { */ @Override public Output output(Reaction reaction) { - DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", torrentFiles.size(), reaction.name())); - output.addText("text/plain", getPlainTextList(torrentFiles)); - output.addText("text/html", getHtmlTextList(torrentFiles)); + DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", newTorrentFiles.size(), reaction.name())); + output.addText("text/plain", getPlainTextList(reaction)); + output.addText("text/html", getHtmlTextList(reaction)); return output; } // - // STATIC METHODS + // PRIVATE METHODS // /** * Generates a plain text list of torrent files. * - * @param torrentFiles - * The torrent files to list + * @param reaction + * The reaction that was triggered * @return The generated plain text */ - private static String getPlainTextList(List torrentFiles) { + private String getPlainTextList(Reaction reaction) { StringBuilder plainText = new StringBuilder(); plainText.append("New Torrents:\n\n"); - for (TorrentFile torrentFile : torrentFiles) { + for (TorrentFile torrentFile : newTorrentFiles) { plainText.append(torrentFile.name()).append('\n'); plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n"); plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n"); @@ -110,29 +126,45 @@ public class NewTorrentTrigger implements Trigger { /** * Generates an HTML list of the given torrent files. * - * @param torrentFiles - * The torrent files to list + * @param reaction + * The reaction that was triggered * @return The generated HTML */ - private static String getHtmlTextList(List torrentFiles) { - StringBuilder htmlText = new StringBuilder(); - htmlText.append("\n"); - htmlText.append("

New Torrents

\n"); - htmlText.append("
    \n"); - for (TorrentFile torrentFile : torrentFiles) { - htmlText.append("
  • ").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("
  • "); - htmlText.append("
    Size: ").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append(" in ").append(torrentFile.fileCount()).append(" file(s)
    "); - htmlText.append("
    ").append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)
    "); - if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) { - htmlText.append(String.format("", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri()))); - } - if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) { - htmlText.append(String.format("", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri()))); + private String getHtmlTextList(Reaction reaction) { + StringBuilder htmlBuilder = new StringBuilder(); + htmlBuilder.append("\n"); + htmlBuilder.append("\n\n"); + htmlBuilder.append("\n"); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append("\n"); + htmlBuilder.append("\n"); + htmlBuilder.append("\n"); + for (TorrentFile torrentFile : allTorrentFiles) { + if (newTorrentFiles.contains(torrentFile)) { + htmlBuilder.append(""); + } else { + htmlBuilder.append(""); } + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append("\n"); } - htmlText.append("\n"); - htmlText.append("\n"); - return htmlText.toString(); + htmlBuilder.append("\n"); + htmlBuilder.append("
    All Known Torrents
    FilenameSizeFile(s)SeedsLeechersMagnetDownload
    ").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("").append(torrentFile.fileCount()).append("").append(torrentFile.seedCount()).append("").append(torrentFile.leechCount()).append("LinkLink
    \n"); + htmlBuilder.append("\n"); + return htmlBuilder.toString(); } }