X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ftriggers%2FNewTorrentTrigger.java;h=0ff193c7c5f080dde9fc1876474f25d25f92cbd9;hb=5d962b76adef88663cfa4acc093836c71fe9dd82;hp=7b28fee851d9a16ba0487f1452b2e50fc8aedf39;hpb=6d8a3475d8e56cea63d53d456baef434318223a7;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 7b28fee..0ff193c 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java @@ -17,24 +17,15 @@ package net.pterodactylus.rhynodge.triggers; -import static com.google.common.base.Preconditions.checkState; - -import java.util.List; +import java.util.HashSet; import java.util.Set; -import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.Trigger; -import net.pterodactylus.rhynodge.output.DefaultOutput; -import net.pterodactylus.rhynodge.output.Output; import net.pterodactylus.rhynodge.states.TorrentState; import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; -import org.apache.commons.lang3.StringEscapeUtils; - -import com.google.common.collect.Lists; -import com.google.common.collect.Ordering; -import com.google.common.collect.Sets; +import static com.google.common.base.Preconditions.checkState; /** * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that @@ -44,11 +35,7 @@ import com.google.common.collect.Sets; */ public class NewTorrentTrigger implements Trigger { - /** All known torrents. */ - private final Set allTorrentFiles = Sets.newHashSet(); - - /** The newly detected torrent files. */ - private final List newTorrentFiles = Lists.newArrayList(); + private boolean triggered = false; // // TRIGGER METHODS @@ -61,19 +48,17 @@ public class NewTorrentTrigger implements Trigger { 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; - allTorrentFiles.clear(); - newTorrentFiles.clear(); - allTorrentFiles.addAll(previousTorrentState.torrentFiles()); - for (TorrentFile torrentFile : currentTorrentState) { + Set allTorrentFiles = new HashSet<>(((TorrentState) previousState).torrentFiles()); + Set newTorrentFiles = new HashSet<>(); + for (TorrentFile torrentFile : (TorrentState) currentState) { if (allTorrentFiles.add(torrentFile)) { newTorrentFiles.add(torrentFile); + triggered = true; } } - return new TorrentState(allTorrentFiles); + return new TorrentState(allTorrentFiles, newTorrentFiles); } /** @@ -81,114 +66,7 @@ public class NewTorrentTrigger implements Trigger { */ @Override public boolean triggers() { - return !newTorrentFiles.isEmpty(); - } - - /** - * {@inheritDoc} - */ - @Override - public Output output(Reaction reaction) { - 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; - } - - // - // PRIVATE METHODS - // - - /** - * Generates a plain text list of torrent files. - * - * @param reaction - * The reaction that was triggered - * @return The generated plain text - */ - private String getPlainTextList(Reaction reaction) { - StringBuilder plainText = new StringBuilder(); - plainText.append("New Torrents:\n\n"); - 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"); - if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) { - plainText.append('\t').append(torrentFile.magnetUri()).append('\n'); - } - if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) { - plainText.append('\t').append(torrentFile.downloadUri()).append('\n'); - } - plainText.append('\n'); - } - return plainText.toString(); - } - - /** - * Generates an HTML list of the given torrent files. - * - * @param reaction - * The reaction that was triggered - * @return The generated HTML - */ - 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 : sortNewFirst().sortedCopy(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"); - } - 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(); - } - - /** - * Returns an ordering that sorts torrent files by whether they are new - * (according to {@link #newTorrentFiles}) or not. New files will be sorted - * first. - * - * @return An ordering for “new files first” - */ - private Ordering sortNewFirst() { - return new Ordering() { - - @Override - public int compare(TorrentFile leftTorrentFile, TorrentFile rightTorrentFile) { - if (newTorrentFiles.contains(leftTorrentFile) && !newTorrentFiles.contains(rightTorrentFile)) { - return -1; - } - if (!newTorrentFiles.contains(leftTorrentFile) && newTorrentFiles.contains(rightTorrentFile)) { - return 1; - } - return 0; - } - }; + return triggered; } }