Skip to content

Commit

Permalink
Add option control whether to keep history for unfollow many operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Klyga committed Jun 7, 2019
1 parent 388c70d commit f2d064b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group 'io.getstream.client'
version = '3.1.7'
version = '3.1.8'

dependencies {
sourceCompatibility = 1.8
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/io/getstream/client/BatchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import io.getstream.core.StreamBatch;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.Token;
import io.getstream.core.models.Activity;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.FollowRelation;
import io.getstream.core.models.ForeignIDTimePair;
import io.getstream.core.models.*;
import io.getstream.core.KeepHistory;
import io.getstream.core.utils.DefaultOptions;
import java8.util.J8Arrays;
import java8.util.concurrent.CompletableFuture;

import java.util.List;
Expand Down Expand Up @@ -48,7 +47,23 @@ public CompletableFuture<Void> followMany(Iterable<FollowRelation> follows) thro

public CompletableFuture<Void> unfollowMany(FollowRelation... follows) throws StreamException {
final Token token = buildFollowToken(secret, TokenAction.WRITE);
return batch.unfollowMany(token, follows);
final UnfollowOperation[] ops = J8Arrays.stream(follows)
.map(follow -> new UnfollowOperation(follow, io.getstream.core.KeepHistory.YES))
.toArray(UnfollowOperation[]::new);
return batch.unfollowMany(token, ops);
}

public CompletableFuture<Void> unfollowMany(KeepHistory keepHistory, FollowRelation... follows) throws StreamException {
final Token token = buildFollowToken(secret, TokenAction.WRITE);
final UnfollowOperation[] ops = J8Arrays.stream(follows)
.map(follow -> new UnfollowOperation(follow, keepHistory))
.toArray(UnfollowOperation[]::new);
return batch.unfollowMany(token, ops);
}

public CompletableFuture<Void> unfollowMany(UnfollowOperation... unfollows) throws StreamException {
final Token token = buildFollowToken(secret, TokenAction.WRITE);
return batch.unfollowMany(token, unfollows);
}

public CompletableFuture<List<Activity>> getActivitiesByID(Iterable<String> activityIDs) throws StreamException {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/io/getstream/core/StreamBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Token;
import io.getstream.core.models.Activity;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.FollowRelation;
import io.getstream.core.models.ForeignIDTimePair;
import io.getstream.core.models.*;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.RequestOption;
import java8.util.J8Arrays;
Expand Down Expand Up @@ -89,12 +86,12 @@ public CompletableFuture<Void> followMany(Token token, int activityCopyLimit, Fo
}
}

public CompletableFuture<Void> unfollowMany(Token token, FollowRelation... follows) throws StreamException {
checkNotNull(follows, "No feeds to unfollow");
checkArgument(follows.length > 0, "No feeds to unfollow");
public CompletableFuture<Void> unfollowMany(Token token, UnfollowOperation... unfollows) throws StreamException {
checkNotNull(unfollows, "No feeds to unfollow");
checkArgument(unfollows.length > 0, "No feeds to unfollow");

try {
final byte[] payload = toJSON(follows);
final byte[] payload = toJSON(unfollows);
final URL url = buildUnfollowManyURL(baseURL);
return httpClient.execute(buildPost(url, key, token, payload))
.thenApply(response -> {
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/io/getstream/core/models/UnfollowOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.getstream.core.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import io.getstream.core.KeepHistory;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

@JsonIgnoreProperties(ignoreUnknown = true)
public final class UnfollowOperation {
private final String source;
private final String target;
private final KeepHistory keepHistory;

@JsonCreator
public UnfollowOperation(@JsonProperty("feed_id") String source, @JsonProperty("target_id") String target, @JsonProperty("keep_history") KeepHistory keepHistory) {
checkNotNull(source, "UnfollowOperation 'source' field required");
checkNotNull(target, "UnfollowOperation 'target' field required");
checkNotNull(keepHistory, "UnfollowOperation 'keep history' field required");

this.source = source;
this.target = target;
this.keepHistory = keepHistory;
}

public UnfollowOperation(FollowRelation follow, KeepHistory keepHistory) {
checkNotNull(follow, "UnfollowOperation 'follow' field required");
checkNotNull(keepHistory, "UnfollowOperation 'keep history' field required");

this.source = follow.getSource();
this.target = follow.getTarget();
this.keepHistory = keepHistory;
}

public String getSource() {
return this.source;
}

public String getTarget() {
return this.target;
}

public KeepHistory getKeepHistory() {
return this.keepHistory;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UnfollowOperation that = (UnfollowOperation) o;
return Objects.equals(source, that.source) &&
Objects.equals(target, that.target) &&
Objects.equals(keepHistory, that.keepHistory);
}

@Override
public int hashCode() {
return Objects.hash(source, target, keepHistory);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("source", this.source)
.add("target", this.target)
.add("keep_history", this.keepHistory)
.toString();
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/getstream/client/BatchClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.getstream.client;

import com.google.common.collect.ImmutableMap;
import io.getstream.core.KeepHistory;
import io.getstream.core.models.*;
import org.junit.Test;

Expand Down Expand Up @@ -45,6 +46,16 @@ public void unfollowMany() throws Exception {
new FollowRelation("flat:1", "flat:2"),
new FollowRelation("aggregated:1", "flat:1")
}).join();

client.unfollowMany(KeepHistory.NO, new FollowRelation[]{
new FollowRelation("flat:1", "flat:2"),
new FollowRelation("aggregated:1", "flat:1")
}).join();

client.unfollowMany(new UnfollowOperation[]{
new UnfollowOperation("flat:1", "flat:2", KeepHistory.NO),
new UnfollowOperation("aggregated:1", "flat:1", KeepHistory.YES)
}).join();
}

@Test
Expand Down

0 comments on commit f2d064b

Please sign in to comment.