Skip to content

Commit

Permalink
Closes #173
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Mar 21, 2020
1 parent 7ea9495 commit 7699245
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ImageLoader.ImageContainer getImage(
String formattedName;
Logger.d(TAG, "Loading image " + getImageUrlForLogs(postImage) + " from the disk");

if (postImage.spoiler) {
if (postImage.spoiler()) {
String extension = StringUtils.extractFileNameExtension(postImage.spoilerThumbnailUrl.toString());

formattedName = ThreadSaveManager.formatSpoilerImageName(extension);
Expand All @@ -83,7 +83,7 @@ public ImageLoader.ImageContainer getImage(
}
}

return getFromDisk(loadable, formattedName, postImage.spoiler, imageListener, width, height, () -> {
return getFromDisk(loadable, formattedName, postImage.spoiler(), imageListener, width, height, () -> {
Logger.d(TAG, "Falling back to imageLoaderV1 load the image " + getImageUrlForLogs(postImage));

return imageLoader.get(postImage.getThumbnailUrl().toString(), imageListener, width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static com.github.adamantcheese.chan.core.manager.FilterType.COUNTRY_CODE;
import static com.github.adamantcheese.chan.core.manager.FilterType.FILENAME;
import static com.github.adamantcheese.chan.core.manager.FilterType.ID;
import static com.github.adamantcheese.chan.core.manager.FilterType.IMAGE;
import static com.github.adamantcheese.chan.core.manager.FilterType.NAME;
import static com.github.adamantcheese.chan.core.manager.FilterType.SUBJECT;
import static com.github.adamantcheese.chan.core.manager.FilterType.TRIPCODE;
Expand Down Expand Up @@ -184,7 +185,11 @@ public void saveBoardsToFilter(List<Board> appliedBoards, boolean all, Filter fi
}
}

// This method must be a duplicate of the one below
/**
* @param filter the filter to use
* @param post the post content to test against
* @return true if the filter matches and should be applied to the content, false if not
*/
@AnyThread
public boolean matches(Filter filter, Post.Builder post) {
if (!post.moderatorCapcode.equals("") || post.sticky) return false;
Expand All @@ -196,6 +201,19 @@ public boolean matches(Filter filter, Post.Builder post) {
if (typeMatches(filter, COMMENT) && matches(filter, post.comment.toString(), false)) return true;
if (typeMatches(filter, ID) && matches(filter, post.posterId, false)) return true;
if (typeMatches(filter, SUBJECT) && matches(filter, post.subject, false)) return true;
for (PostImage image : post.images) {
if (typeMatches(filter, IMAGE) && matches(filter, image.fileHash, false)) {
//for filtering image hashes, we don't want to apply the post-level filter (thus return false)
//this takes care of it at an image level, either flagging it to be hidden, which applies a
//custom spoiler image, or removes the image from the post entirely since this is a Post.Builder instance
if (filter.action == FilterAction.HIDE.id) {
image.hidden = true;
} else if (filter.action == FilterAction.REMOVE.id) {
post.images.remove(image);
}
return false;
}
}

//figure out if the post has a country code, if so check the filter
String countryCode = "";
Expand Down Expand Up @@ -223,6 +241,14 @@ public boolean matches(Filter filter, Post.Builder post) {
return false;
}

/**
* This method is a rough duplicate of the one with Post.Builder
* However is only used for post-processing, like filter watching
*
* @param filter the filter to use
* @param post the post content to test against
* @return true if the filter matches and should be applied to the content, false if not
*/
@AnyThread
public boolean matches(Filter filter, Post post) {
if (!post.capcode.equals("") || post.isSticky()) return false;
Expand All @@ -234,6 +260,10 @@ public boolean matches(Filter filter, Post post) {
if (typeMatches(filter, COMMENT) && matches(filter, post.comment.toString(), false)) return true;
if (typeMatches(filter, ID) && matches(filter, post.id, false)) return true;
if (typeMatches(filter, SUBJECT) && matches(filter, post.subject, false)) return true;
for (PostImage image : post.images) {
//for this case, we don't do any actions, so just return if it actually does match
if (typeMatches(filter, IMAGE) && matches(filter, image.fileHash, false)) return true;
}

//figure out if the post has a country code, if so check the filter
String countryCode = "";
Expand All @@ -249,16 +279,13 @@ public boolean matches(Filter filter, Post post) {
return true;
}

if (post.images != null) {
StringBuilder files = new StringBuilder();
for (PostImage image : post.images) {
files.append(image.filename).append(" ");
}
String fnames = files.toString();
return !fnames.isEmpty() && typeMatches(filter, FILENAME) && matches(filter, fnames, false);
//images is not null, must be at least an empty collection here
StringBuilder files = new StringBuilder();
for (PostImage image : post.images) {
files.append(image.filename).append(" ");
}

return false;
String fnames = files.toString();
return !fnames.isEmpty() && typeMatches(filter, FILENAME) && matches(filter, fnames, false);
}

@AnyThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public enum FilterType {
ID(0x8),
SUBJECT(0x10),
FILENAME(0x20),
COUNTRY_CODE(0x40);
COUNTRY_CODE(0x40),
IMAGE(0x80);

public final int flag;

Expand Down Expand Up @@ -64,6 +65,8 @@ public static String filterTypeName(FilterType type) {
return getString(R.string.filter_filename);
case COUNTRY_CODE:
return getString(R.string.filter_country_code);
case IMAGE:
return getString(R.string.filter_image_hash);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static SerializablePostImage toSerializablePostImage(PostImage postImage)
postImage.spoilerThumbnailUrl.toString(),
postImage.imageWidth,
postImage.imageHeight,
postImage.spoiler,
postImage.spoiler(),
postImage.size,
postImage.fileHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.adamantcheese.chan.BuildConfig;
import com.github.adamantcheese.chan.core.settings.ChanSettings;
import com.github.adamantcheese.chan.utils.StringUtils;

Expand All @@ -40,6 +41,8 @@ public enum Type {
SWF
}

public boolean hidden;

public final String serverFilename;
public final HttpUrl thumbnailUrl;
public final HttpUrl spoilerThumbnailUrl;
Expand All @@ -49,7 +52,7 @@ public enum Type {
public final String extension;
public final int imageWidth;
public final int imageHeight;
public final boolean spoiler;
private final boolean spoiler;
public final boolean isInlined;
public final long size;
@Nullable
Expand Down Expand Up @@ -104,13 +107,21 @@ public boolean equalUrl(PostImage other) {
}

public HttpUrl getThumbnailUrl() {
if (!spoiler) {
if (!spoiler()) {
return thumbnailUrl;
} else {
return spoilerThumbnailUrl;
if (!hidden) {
return spoilerThumbnailUrl;
} else {
return HttpUrl.get(BuildConfig.RESOURCES_ENDPOINT + "hide_thumb.png");
}
}
}

public boolean spoiler() {
return spoiler || hidden;
}

public static final class Builder {
private String serverFilename;
private HttpUrl thumbnailUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void onViewMeasured() {
// Pager is measured, but still invisible
callback.startPreviewInTransition(loadable, images.get(selectedPosition));
PostImage postImage = images.get(selectedPosition);
callback.setTitle(postImage, selectedPosition, images.size(), postImage.spoiler);
callback.setTitle(postImage, selectedPosition, images.size(), postImage.spoiler());
}

public boolean isTransitioning() {
Expand Down Expand Up @@ -295,7 +295,7 @@ private void onPageSwipedTo(int position) {
private void onLowResInCenter() {
PostImage postImage = images.get(selectedPosition);

if (imageAutoLoad(loadable, postImage) && (!postImage.spoiler || ChanSettings.revealimageSpoilers.get())) {
if (imageAutoLoad(loadable, postImage) && (!postImage.spoiler() || ChanSettings.revealimageSpoilers.get())) {
if (postImage.type == STATIC) {
callback.setImageMode(postImage, BIGIMAGE, true);
} else if (postImage.type == GIF) {
Expand Down Expand Up @@ -471,7 +471,7 @@ public void onTap() {
// Don't mistake a swipe when the pager is disabled as a tap
if (viewPagerVisible) {
PostImage postImage = images.get(selectedPosition);
if (imageAutoLoad(loadable, postImage) && !postImage.spoiler) {
if (imageAutoLoad(loadable, postImage) && !postImage.spoiler()) {
if (postImage.type == MOVIE && callback.getImageMode(postImage) != VIDEO) {
callback.setImageMode(postImage, VIDEO, true);
} else {
Expand Down Expand Up @@ -629,7 +629,7 @@ private void setTitle(PostImage postImage, int position) {
callback.setTitle(postImage,
position,
images.size(),
postImage.spoiler && callback.getImageMode(postImage) == LOWRES
postImage.spoiler() && callback.getImageMode(postImage) == LOWRES
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ public class ThreadPresenter
private static final int POST_OPTION_HIDE = 12;
private static final int POST_OPTION_OPEN_BROWSER = 13;
private static final int POST_OPTION_FILTER_TRIPCODE = 14;
private static final int POST_OPTION_EXTRA = 15;
private static final int POST_OPTION_REMOVE = 16;
private static final int POST_OPTION_MOCK_REPLY = 17;
private static final int POST_OPTION_FILTER_IMAGE_HASH = 15;
private static final int POST_OPTION_EXTRA = 16;
private static final int POST_OPTION_REMOVE = 17;
private static final int POST_OPTION_MOCK_REPLY = 18;

private final WatchManager watchManager;
private final DatabaseManager databaseManager;
Expand Down Expand Up @@ -895,6 +896,10 @@ public Object onPopulatePostOptions(Post post, List<FloatingMenuItem> menu, List
menu.add(new FloatingMenuItem(POST_OPTION_HIGHLIGHT_TRIPCODE, R.string.post_highlight_tripcode));
menu.add(new FloatingMenuItem(POST_OPTION_FILTER_TRIPCODE, R.string.post_filter_tripcode));
}

if(loadable.site.siteFeature(Site.SiteFeature.IMAGE_FILE_HASH)) {
menu.add(new FloatingMenuItem(POST_OPTION_FILTER_IMAGE_HASH, R.string.post_filter_image_hash));
}
}

if (loadable.site.siteFeature(Site.SiteFeature.POST_DELETE) && databaseManager.getDatabaseSavedReplyManager()
Expand Down Expand Up @@ -965,6 +970,9 @@ public void onPostOptionClicked(Post post, Object id, boolean inPopup) {
case POST_OPTION_FILTER_TRIPCODE:
threadPresenterCallback.filterPostTripcode(post.tripcode);
break;
case POST_OPTION_FILTER_IMAGE_HASH:
threadPresenterCallback.filterPostImageHash(post);
break;
case POST_OPTION_DELETE:
requestDeletePost(post);
break;
Expand Down Expand Up @@ -1017,7 +1025,7 @@ public void onPostOptionClicked(Post post, Object id, boolean inPopup) {
if (chanLoader.getThread().getLoadable().mode == Loadable.Mode.CATALOG) {
threadPresenterCallback.hideThread(post, post.no, hide);
} else {
boolean isEmpty = false;
boolean isEmpty;

synchronized (post.repliesFrom) {
isEmpty = post.repliesFrom.isEmpty();
Expand Down Expand Up @@ -1242,7 +1250,7 @@ private void showPostInfo(Post post) {
.append(getReadableFileSize(image.size));
}

if (image.spoiler && image.size != -1) { //all linked files are spoilered, don't say that
if (image.spoiler() && image.size != -1) { //all linked files are spoilered, don't say that
text.append("\nSpoilered");
}

Expand Down Expand Up @@ -1437,6 +1445,8 @@ public interface ThreadPresenterCallback {

void filterPostTripcode(String tripcode);

void filterPostImageHash(Post post);

void selectPost(int post);

void showSearch(boolean show);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ private void bindPost(Theme theme, Post post) {
boolean postFileName = ChanSettings.postFilename.get();
if (postFileName) {
//that special character forces it to be left-to-right, as textDirection didn't want to be obeyed
String filename = '\u200E' + (image.spoiler
? getString(R.string.image_spoiler_filename)
: image.filename + "." + image.extension);
String filename = '\u200E' + (image.spoiler() ? (image.hidden
? getString(R.string.image_hidden_filename)
: getString(R.string.image_spoiler_filename)) : image.filename + "." + image.extension);
SpannableString fileInfo = new SpannableString("\n" + filename);
fileInfo.setSpan(new ForegroundColorSpanHashed(theme.detailsColor), 0, fileInfo.length(), 0);
fileInfo.setSpan(new AbsoluteSizeSpanHashed(detailsSizePx), 0, fileInfo.length(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ public void onClick(View v) {
//generate tasks before prompting
List<ImageSaveTask> tasks = new ArrayList<>(items.size());
for (AlbumDownloadItem item : items) {
if (item.postImage.isInlined) {
if (item.postImage.isInlined || item.postImage.hidden) {
// Do not download inlined files via the Album downloads (because they often
// fail with SSL exceptions) and we can't really trust those files.
// Also don't download filter hidden items
continue;
}

Expand Down Expand Up @@ -268,9 +269,10 @@ public void setPostImages(Loadable loadable, List<PostImage> postImages) {
this.loadable = loadable;
for (int i = 0, postImagesSize = postImages.size(); i < postImagesSize; i++) {
PostImage postImage = postImages.get(i);
if (postImage.isInlined) {
if (postImage.isInlined || postImage.hidden) {
// Do not allow downloading inlined files via the Album downloads (because they often
// fail with SSL exceptions) and we can't really trust those files.
// Also don't allow filter hidden items
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public void onSearchEntered(String entered) {
}

@Override
public void openFilterForTripcode(String tripcode) {
public void openFilterForType(FilterType type, String filterText) {
FiltersController filtersController = new FiltersController(context);
if (doubleNavigationController != null) {
doubleNavigationController.pushController(filtersController);
Expand All @@ -258,8 +258,8 @@ public void openFilterForTripcode(String tripcode) {
}

Filter filter = new Filter();
filter.type = FilterType.TRIPCODE.flag;
filter.pattern = '/' + tripcode + '/';
filter.type = type.flag;
filter.pattern = '/' + filterText + '/';

filtersController.showFilterDialog(filter);
}
Expand Down
Loading

0 comments on commit 7699245

Please sign in to comment.