Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.getstream.client.model.beans;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* Helper bean used to perform bulk unfollow.
*/
public class UnfollowMany {

@JsonSerialize(contentAs = UnfollowMany.Entry.class)
private final List<Entry> entries;

private UnfollowMany(final List<Entry> entries) {
this.entries = entries;
}

@JsonValue
public List<Entry> getEntries() {
return entries;
}

/**
* Provide an easy way to build an immutable list of unfollow.
*/
public static class Builder {
private ImmutableList.Builder<Entry> followEntries = new ImmutableList.Builder<>();

/**
* Add a new unfollow source/target pair. Keep history is set to false.
* @param source Source feed
* @param target Target feed
* @return This builder.
*/
public Builder add(final String source, final String target) {
this.followEntries.add(new Entry(source, target, false));
return this;
}

/**
* Add a new unfollow source/target pair.
* @param source Source feed
* @param target Target feed
* @param keepHistory Whether the history must be preserved.
* @return This builder.
*/
public Builder add(final String source, final String target, final boolean keepHistory) {
this.followEntries.add(new Entry(source, target, keepHistory));
return this;
}

public Builder addMany(final List<Entry> entries) {
this.followEntries.addAll(entries);
return this;
}

/**
* Build an immutable list of unfollow.
*
* @return A marked activity
*/
public UnfollowMany build() {
return new UnfollowMany(followEntries.build());
}
}

public static class Entry {

private String source;
private String target;
private boolean keepHistory;

@JsonCreator
public Entry(@JsonProperty("source") final String source,
@JsonProperty("target") final String target,
@JsonProperty("keep_history") final boolean keepHistory) {
this.source = source;
this.target = target;
this.keepHistory = keepHistory;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}

public boolean getKeepHistory() {
return keepHistory;
}

public void setKeepHistory(boolean keepHistory) {
this.keepHistory = keepHistory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.getstream.client.model.activities.BaseActivity;
import io.getstream.client.model.beans.FeedFollow;
import io.getstream.client.model.beans.FollowMany;
import io.getstream.client.model.beans.UnfollowMany;
import io.getstream.client.model.filters.FeedFilter;
import io.getstream.client.repo.StreamRepository;
import io.getstream.client.service.AggregatedActivityServiceImpl;
Expand Down Expand Up @@ -69,6 +70,11 @@ public void followMany(FollowMany follows) throws IOException, StreamClientExcep
streamRepository.followMany(this, follows, DEFAULT_ACTIVITY_COPY_LIMIT);
}

@Override
public void unfollowMany(UnfollowMany unfollowMany) throws IOException, StreamClientException {
streamRepository.unfollowMany(this, unfollowMany);
}

@Override
public void unfollow(String feedSlug, String userId) throws IOException, StreamClientException {
String feedId = String.format("%s:%s", feedSlug, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.getstream.client.model.activities.BaseActivity;
import io.getstream.client.model.beans.FeedFollow;
import io.getstream.client.model.beans.FollowMany;
import io.getstream.client.model.beans.UnfollowMany;
import io.getstream.client.model.filters.FeedFilter;
import io.getstream.client.service.AggregatedActivityServiceImpl;
import io.getstream.client.service.FlatActivityServiceImpl;
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface Feed {
void follow(String feedSlug, String userId, int activityCopyLimit) throws IOException, StreamClientException;

/**
* Follow many feed in one shot.
* Follow many feeds in one shot.
*
* @param follows A {@link FollowMany} object which contains a list of sources and targets
* @param activityCopyLimit the maximum number of activities from a
Expand All @@ -71,7 +72,7 @@ public interface Feed {
void followMany(FollowMany follows, int activityCopyLimit) throws IOException, StreamClientException;

/**
* Follow many feed in one shot.
* Follow many feeds in one shot.
* Default activity copy limit is set to 300. Maximum 300 activities from a given source feed
* will be copied to the target feed.
*
Expand All @@ -81,6 +82,17 @@ public interface Feed {
*/
void followMany(FollowMany follows) throws IOException, StreamClientException;

/**
* Unfollow many feeds in one shot.
*
* @param unfollowMany A {@link UnfollowMany} object which contains a list of sources and targets.
* Any arbitrary feed can be specified as {@link io.getstream.client.model.beans.UnfollowMany.Entry#setSource(String)},
* regardless the one used to trigger this operation.
* @throws StreamClientException in case of functional or server-side exception
* @throws IOException in case of network/socket exceptions
*/
void unfollowMany(UnfollowMany unfollowMany) throws IOException, StreamClientException;

/**
* Unfollow the given target feed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.getstream.client.model.beans.MarkedActivity;
import io.getstream.client.model.beans.StreamActivitiesResponse;
import io.getstream.client.model.beans.StreamResponse;
import io.getstream.client.model.beans.UnfollowMany;
import io.getstream.client.model.feeds.BaseFeed;
import io.getstream.client.model.filters.FeedFilter;

Expand Down Expand Up @@ -59,7 +60,7 @@ public interface StreamRepository {
void follow(BaseFeed feed, String targetFeedId, int activityCopyLimit) throws StreamClientException, IOException;

/**
* Follow many feed in one shot.
* Follow many feeds in one shot.
*
* @param feed Feed that wants to follow a target feed.
* @param followManyInput A {@link FollowMany} object which contains a list of sources and targets
Expand All @@ -69,6 +70,16 @@ public interface StreamRepository {
*/
void followMany(BaseFeed feed, FollowMany followManyInput, int activityCopyLimit) throws StreamClientException, IOException;

/**
* Unfollow many feeds in one shot.
*
* @param feed Feed that wants to follow a target feed.
* @param unfollowManyInput A {@link UnfollowMany} object which contains a list of sources and targets.
* @throws StreamClientException in case of functional or server-side exception
* @throws IOException in case of network/socket exceptions
*/
void unfollowMany(BaseFeed feed, UnfollowMany unfollowManyInput) throws StreamClientException, IOException;

/**
* Unfollow a feed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.getstream.client.model.beans.MarkedActivity;
import io.getstream.client.model.beans.StreamActivitiesResponse;
import io.getstream.client.model.beans.StreamResponse;
import io.getstream.client.model.beans.UnfollowMany;
import io.getstream.client.model.feeds.BaseFeed;
import io.getstream.client.model.filters.FeedFilter;
import io.getstream.client.repo.StreamRepository;
Expand Down Expand Up @@ -104,6 +105,16 @@ public void followMany(BaseFeed feed, FollowMany followManyInput, int activityCo
fireAndForget(request);
}

@Override
public void unfollowMany(BaseFeed feed, UnfollowMany unfollowManyInput) throws StreamClientException, IOException {
HttpPost request = new HttpPost(UriBuilder.fromEndpoint(baseEndpoint)
.path("unfollow_many/")
.build());
request.addHeader(HttpSignatureInterceptor.X_API_KEY_HEADER, apiKey);
request.setEntity(new StringEntity(objectMapper.writeValueAsString(unfollowManyInput), APPLICATION_JSON));
fireAndForget(request);
}

@Override
public void unfollow(BaseFeed feed, String targetFeedId, boolean keepHistory) throws StreamClientException, IOException {
HttpDelete request = new HttpDelete(UriBuilder.fromEndpoint(baseEndpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.getstream.client.model.beans.MarkedActivity;
import io.getstream.client.model.beans.StreamActivitiesResponse;
import io.getstream.client.model.beans.StreamResponse;
import io.getstream.client.model.beans.UnfollowMany;
import io.getstream.client.model.feeds.Feed;
import io.getstream.client.model.filters.FeedFilter;
import io.getstream.client.service.AggregatedActivityServiceImpl;
Expand Down Expand Up @@ -178,6 +179,40 @@ public void shouldFollowMany() throws IOException, StreamClientException {
streamClient.shutdown();
}

@Test
public void shouldUnfollowMany() throws IOException, StreamClientException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,
API_SECRET);

String followerId = this.getTestUserId("shouldunfollowMany");
Feed feed = streamClient.newFeed("user", followerId);

List<FeedFollow> following = feed.getFollowing();
assertThat(following.size(), is(0));

FollowMany followMany = new FollowMany.Builder()
.add("user:" + followerId, "user:1")
.add("user:" + followerId, "user:2")
.add("user:" + followerId, "user:3")
.build();
feed.followMany(followMany);

List<FeedFollow> followingAfter = feed.getFollowing();
assertThat(followingAfter.size(), is(3));

UnfollowMany unfollowMany = new UnfollowMany.Builder()
.add("user:" + followerId, "user:1")
.add("user:" + followerId, "user:2", true)
.add("user:" + followerId, "user:3", false)
.build();
feed.unfollowMany(unfollowMany);

List<FeedFollow> unfollowingAfter = feed.getFollowing();
assertThat(unfollowingAfter.size(), is(0));

streamClient.shutdown();
}

@Test
public void shouldHaveOriginField() throws IOException, StreamClientException, InterruptedException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,
Expand Down
Loading