Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only accept the 100 most recent posts from the past year (configurable) #28

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/net/pterodactylus/sone/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ public void loadSone(Sone sone) {
sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
sone.getOptions().setDownloadBackwardsLimitDays(configuration.getIntValue(sonePrefix + "/Options/DownloadBackwardsLimit").getValue(365));
sone.getOptions().setDownloadCountLimit(configuration.getIntValue(sonePrefix + "/Options/DownloadCountLimit").getValue(100));
sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));

Expand Down Expand Up @@ -1430,6 +1432,8 @@ private synchronized void saveSone(Sone sone) {
configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
configuration.getIntValue(sonePrefix + "/Options/DownloadBackwardsLimit").setValue(sone.getOptions().getDownloadBackwardsLimitDays());
configuration.getIntValue(sonePrefix + "/Options/DownloadCountLimit").setValue(sone.getOptions().getDownloadCountLimit());
configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/pterodactylus/sone/data/Sone.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import net.pterodactylus.sone.freenet.wot.Identity;

import freenet.keys.FreenetURI;
Expand Down Expand Up @@ -228,6 +230,9 @@ public enum SoneStatus {
@Nonnull
Sone setPosts(@Nonnull Collection<Post> posts);

@NotNull
List<Post> filterRemotePosts(List<Post> sortedPosts);

/**
* Adds the given post to this Sone. The post will not be added if its {@link
* Post#getSone() Sone} is not this Sone.
Expand Down Expand Up @@ -263,6 +268,9 @@ public enum SoneStatus {
@Nonnull
Sone setReplies(@Nonnull Collection<PostReply> replies);

@NotNull
List<PostReply> filterRemoteReplies(List<PostReply> sortedReplies);

/**
* Adds a reply to this Sone. If the given reply was not made by this Sone,
* nothing is added to this Sone.
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/net/pterodactylus/sone/data/SoneOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface SoneOptions {

boolean isShowNewReplyNotifications();
void setShowNewReplyNotifications(boolean showNewReplyNotifications);
int getDownloadBackwardsLimitDays();
void setDownloadBackwardsLimitDays(int days);
int getDownloadCountLimit();
void setDownloadCountLimit(int count);

LoadExternalContent getShowCustomAvatars();
void setShowCustomAvatars(LoadExternalContent showCustomAvatars);
Expand Down Expand Up @@ -62,9 +66,10 @@ public class DefaultSoneOptions implements SoneOptions {
private boolean showNewSoneNotifications = true;
private boolean showNewPostNotifications = true;
private boolean showNewReplyNotifications = true;
private int downloadBackwardsLimitDays = 365;
private int downloadCountLimitDays = 100;
private LoadExternalContent showCustomAvatars = NEVER;
private LoadExternalContent loadLinkedImages = NEVER;

@Override
public boolean isAutoFollow() {
return autoFollow;
Expand Down Expand Up @@ -115,6 +120,27 @@ public void setShowNewReplyNotifications(boolean showNewReplyNotifications) {
this.showNewReplyNotifications = showNewReplyNotifications;
}

@Override
public int getDownloadBackwardsLimitDays() {
return downloadBackwardsLimitDays;
}

@Override
public void setDownloadBackwardsLimitDays(int downloadBackwardsLimitDays) {
this.downloadBackwardsLimitDays = downloadBackwardsLimitDays;
}

@Override
public int getDownloadCountLimit() {
return downloadCountLimitDays;
}

@Override
public void setDownloadCountLimit(int count) {
this.downloadCountLimitDays = count;

}

@Override
public LoadExternalContent getShowCustomAvatars() {
return showCustomAvatars;
Expand All @@ -135,7 +161,6 @@ public LoadExternalContent getLoadLinkedImages() {
public void setLoadLinkedImages(@Nonnull LoadExternalContent loadLinkedImages) {
this.loadLinkedImages = loadLinkedImages;
}

}

}
24 changes: 24 additions & 0 deletions src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import java.util.List;
import java.util.Set;

import org.jetbrains.annotations.NotNull;

import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;

import static java.util.stream.Collectors.toList;
import static net.pterodactylus.sone.data.PostKt.noOldPost;
import static net.pterodactylus.sone.data.ReplyKt.noOldReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.SoneOptions;
Expand Down Expand Up @@ -130,6 +136,15 @@ public Sone setPosts(Collection<Post> posts) {
return this;
}

@NotNull
@Override
public List<Post> filterRemotePosts(List<Post> sortedPosts) {
return sortedPosts.stream()
.filter(post -> noOldPost().invoke(post, this.getOptions().getDownloadBackwardsLimitDays()))
.limit(this.getOptions().getDownloadCountLimit())
.collect(toList());
}

@Override
public void addPost(Post post) {
}
Expand All @@ -148,6 +163,15 @@ public Sone setReplies(Collection<PostReply> replies) {
return this;
}

@NotNull
@Override
public List<PostReply> filterRemoteReplies(List<PostReply> sortedReplies) {
return sortedReplies.stream()
.filter(reply -> noOldReply().invoke(reply, this.getOptions().getDownloadBackwardsLimitDays()))
.limit(this.getOptions().getDownloadCountLimit())
.collect(toList());
}

@Override
public void addReply(PostReply reply) {
}
Expand Down
53 changes: 50 additions & 3 deletions src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Logger.getLogger;
import static java.util.stream.Collectors.toList;
import static net.pterodactylus.sone.data.PostKt.newestPostFirst;
import static net.pterodactylus.sone.data.PostKt.noOldPost;
import static net.pterodactylus.sone.data.ReplyKt.newestReplyFirst;
import static net.pterodactylus.sone.data.ReplyKt.noOldReply;
import static net.pterodactylus.sone.data.SoneKt.*;

import java.net.MalformedURLException;
Expand All @@ -38,6 +41,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.AlbumKt;
import net.pterodactylus.sone.data.Client;
Expand Down Expand Up @@ -367,7 +372,7 @@ public List<Post> getPosts() {
sortedPosts = new ArrayList<>(posts);
}
sortedPosts.sort(newestPostFirst());
return sortedPosts;
return sortedPosts;
}

/**
Expand All @@ -379,13 +384,38 @@ public List<Post> getPosts() {
*/
@Nonnull
public Sone setPosts(@Nonnull Collection<Post> posts) {
List<Post> sortedPosts;
synchronized (this) {
sortedPosts = new ArrayList<>(posts);
}
sortedPosts.sort(newestPostFirst());
List<Post> limitedPosts = this.local
? sortedPosts
: this.filterRemotePosts(sortedPosts);
synchronized (this) {
this.posts.clear();
this.posts.addAll(posts);
this.posts.addAll(limitedPosts);
}
return this;
}

/**
* Filters posts of downloaded Sones.
*
* @param sortedPosts
* The new (and only) posts of this Sone
*
* @return posts limited by download-count-limit and may time backwards.
* */
@Override
@NotNull
public List<Post> filterRemotePosts(List<Post> sortedPosts) {
return sortedPosts.stream()
.filter(post -> noOldPost().invoke(post, this.getOptions().getDownloadBackwardsLimitDays()))
.limit(this.getOptions().getDownloadCountLimit())
.collect(toList());
}

/**
* Adds the given post to this Sone. The post will not be added if its {@link
* Post#getSone() Sone} is not this Sone.
Expand Down Expand Up @@ -430,11 +460,28 @@ public Set<PostReply> getReplies() {
*/
@Nonnull
public Sone setReplies(@Nonnull Collection<PostReply> replies) {
List<PostReply> sortedReplies;
synchronized (this) {
sortedReplies = new ArrayList<>(replies);
}
sortedReplies.sort(newestReplyFirst());
List<PostReply> limitedReplies = this.local
? sortedReplies
: this.filterRemoteReplies(sortedReplies);
this.replies.clear();
this.replies.addAll(replies);
this.replies.addAll(limitedReplies);
return this;
}

@Override
@NotNull
public List<PostReply> filterRemoteReplies(List<PostReply> sortedReplies) {
return sortedReplies.stream()
.filter(reply -> noOldReply().invoke(reply, this.getOptions().getDownloadBackwardsLimitDays()))
.limit(this.getOptions().getDownloadCountLimit())
.collect(toList());
}

/**
* Adds a reply to this Sone. If the given reply was not made by this Sone,
* nothing is added to this Sone.
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.google.common.eventbus.EventBus
import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent
import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent
import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent
import net.pterodactylus.sone.core.event.DownloadBackwardsLimitChangedEvent
import net.pterodactylus.sone.core.event.DownloadCountLimitChangedEvent
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.pterodactylus.sone.core.event

data class DownloadBackwardsLimitChangedEvent(val insertionDelay: Int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.pterodactylus.sone.core.event

data class DownloadCountLimitChangedEvent(val insertionDelay: Int)
10 changes: 10 additions & 0 deletions src/main/kotlin/net/pterodactylus/sone/data/Post.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.pterodactylus.sone.data

import java.util.Comparator.comparing
import kotlin.time.ExperimentalTime
import kotlin.time.days

/**
* Predicate that returns whether a post is _not_ from the future,
Expand All @@ -9,6 +11,14 @@ import java.util.Comparator.comparing
@get:JvmName("noFuturePost")
val noFuturePost: (Post) -> Boolean = { it.time <= System.currentTimeMillis() }

/**
* Predicate that returns whether a post less than a year old,
* i.e. whether it should be visible now.
*/
@OptIn(ExperimentalTime::class)
@get:JvmName("noOldPost")
val noOldPost: (Post, Int) -> Boolean = { p: Post, days: Int -> p.time > (System.currentTimeMillis() - days.days.inMilliseconds) }

/**
* Comparator that orders posts by their time, newest posts first.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/net/pterodactylus/sone/data/Reply.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package net.pterodactylus.sone.data

import java.util.Comparator.comparing
import kotlin.time.ExperimentalTime
import kotlin.time.days

/**
* Comparator that orders replies by their time, newest replies first.
Expand All @@ -26,6 +28,14 @@ import java.util.Comparator.comparing
val newestReplyFirst: Comparator<Reply<*>> =
comparing(Reply<*>::getTime).reversed()

/**
* Predicate that returns whether a reply less than a year old,
* i.e. whether it should be visible now.
*/
@OptIn(ExperimentalTime::class)
@get:JvmName("noOldReply")
val noOldReply: (Reply<*>, Int) -> Boolean = { r: Reply<*>, days: Int -> r.getTime() > (System.currentTimeMillis() - days.days.inMilliseconds) }

/**
* Predicate that returns whether a reply is _not_ from the future,
* i.e. whether it should be visible now.
Expand Down
16 changes: 15 additions & 1 deletion src/main/kotlin/net/pterodactylus/sone/web/pages/OptionsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class OptionsPage @Inject constructor(webInterface: WebInterface, loaders: Loade
val showNewSoneNotification = "show-notification-new-sones" in soneRequest.parameters
val showNewPostNotification = "show-notification-new-posts" in soneRequest.parameters
val showNewReplyNotification = "show-notification-new-replies" in soneRequest.parameters
val downloadBackwardsLimitDays = soneRequest.parameters["download-backwards-limit"].emptyToNull
val downloadCountLimitDays = soneRequest.parameters["download-count-limit"].emptyToNull

options.isAutoFollow = autoFollow
options.isSoneInsertNotificationEnabled = enableSoneInsertNotification
Expand All @@ -37,7 +39,17 @@ class OptionsPage @Inject constructor(webInterface: WebInterface, loaders: Loade
options.isShowNewReplyNotifications = showNewReplyNotification
loadLinkedImages?.also { if (cantSetOption { options.loadLinkedImages = LoadExternalContent.valueOf(loadLinkedImages) }) fieldsWithErrors += "load-linked-images" }
showCustomAvatars?.also { if (cantSetOption { options.showCustomAvatars = LoadExternalContent.valueOf(showCustomAvatars) }) fieldsWithErrors += "show-custom-avatars" }
}
downloadBackwardsLimitDays?.also { if (cantSetOption { options.downloadBackwardsLimitDays = downloadBackwardsLimitDays.toInt() }) fieldsWithErrors += "download-backwards-limit" }
if (options.downloadBackwardsLimitDays < -1 || downloadBackwardsLimitDays.isNullOrBlank()) {
options.downloadBackwardsLimitDays = 365
fieldsWithErrors += "download-backwards-limit"
}
downloadCountLimitDays?.also { if (cantSetOption { options.downloadCountLimit = downloadCountLimitDays.toInt() }) fieldsWithErrors += "download-count-limit" }
if (options.downloadCountLimit < -1 || downloadCountLimitDays.isNullOrBlank()) {
options.downloadCountLimit = 100
fieldsWithErrors += "download-count-limit"
}
}
val fullAccessRequired = "require-full-access" in soneRequest.parameters
val fcpInterfaceActive = "fcp-interface-active" in soneRequest.parameters
val strictFiltering = "strict-filtering" in soneRequest.parameters
Expand Down Expand Up @@ -72,6 +84,8 @@ class OptionsPage @Inject constructor(webInterface: WebInterface, loaders: Loade
templateContext["show-notification-new-posts"] = options.isShowNewPostNotifications
templateContext["show-notification-new-replies"] = options.isShowNewReplyNotifications
templateContext["enable-sone-insert-notifications"] = options.isSoneInsertNotificationEnabled
templateContext["download-count-limit"] = options.downloadCountLimit
templateContext["download-backwards-limit"] = options.downloadBackwardsLimitDays
templateContext["load-linked-images"] = options.loadLinkedImages.toString()
templateContext["show-custom-avatars"] = options.showCustomAvatars.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import net.pterodactylus.sone.web.page.*
import net.pterodactylus.sone.web.pages.SearchPage.Optionality.*
import net.pterodactylus.util.template.*
import net.pterodactylus.util.text.*
import java.util.concurrent.TimeUnit.*
import javax.inject.*
import kotlin.time.ExperimentalTime
import kotlin.time.minutes
import kotlin.time.toJavaDuration

/**
* This page lets the user search for posts and replies that contain certain
* words.
*/
@OptIn(ExperimentalTime::class)
@TemplatePath("/templates/search.html")
@ToadletPath("search.html")
class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, ticker: Ticker = Ticker.systemTicker()) :
Expand All @@ -27,7 +30,7 @@ class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer:
constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
this(webInterface, loaders, templateRenderer, Ticker.systemTicker())

private val cache: Cache<Iterable<Phrase>, Pagination<Post>> = CacheBuilder.newBuilder().ticker(ticker).expireAfterAccess(5, MINUTES).build()
private val cache: Cache<Iterable<Phrase>, Pagination<Post>> = CacheBuilder.newBuilder().ticker(ticker).expireAfterAccess(5.minutes.toJavaDuration()).build()

override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
val startTime = System.currentTimeMillis()
Expand Down
Loading