Skip to content

Commit a013f9c

Browse files
authored
Merge pull request #46 from GetStream/feature/followMany
Feature/follow many
2 parents 52a935e + 60c33d5 commit a013f9c

File tree

10 files changed

+337
-95
lines changed

10 files changed

+337
-95
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.getstream.client.model.beans;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonValue;
6+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
7+
import com.google.common.collect.ImmutableList;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Helper bean used to perform bulk unfollow.
13+
*/
14+
public class UnfollowMany {
15+
16+
@JsonSerialize(contentAs = UnfollowMany.Entry.class)
17+
private final List<Entry> entries;
18+
19+
private UnfollowMany(final List<Entry> entries) {
20+
this.entries = entries;
21+
}
22+
23+
@JsonValue
24+
public List<Entry> getEntries() {
25+
return entries;
26+
}
27+
28+
/**
29+
* Provide an easy way to build an immutable list of unfollow.
30+
*/
31+
public static class Builder {
32+
private ImmutableList.Builder<Entry> followEntries = new ImmutableList.Builder<>();
33+
34+
/**
35+
* Add a new unfollow source/target pair. Keep history is set to false.
36+
* @param source Source feed
37+
* @param target Target feed
38+
* @return This builder.
39+
*/
40+
public Builder add(final String source, final String target) {
41+
this.followEntries.add(new Entry(source, target, false));
42+
return this;
43+
}
44+
45+
/**
46+
* Add a new unfollow source/target pair.
47+
* @param source Source feed
48+
* @param target Target feed
49+
* @param keepHistory Whether the history must be preserved.
50+
* @return This builder.
51+
*/
52+
public Builder add(final String source, final String target, final boolean keepHistory) {
53+
this.followEntries.add(new Entry(source, target, keepHistory));
54+
return this;
55+
}
56+
57+
public Builder addMany(final List<Entry> entries) {
58+
this.followEntries.addAll(entries);
59+
return this;
60+
}
61+
62+
/**
63+
* Build an immutable list of unfollow.
64+
*
65+
* @return A marked activity
66+
*/
67+
public UnfollowMany build() {
68+
return new UnfollowMany(followEntries.build());
69+
}
70+
}
71+
72+
public static class Entry {
73+
74+
private String source;
75+
private String target;
76+
private boolean keepHistory;
77+
78+
@JsonCreator
79+
public Entry(@JsonProperty("source") final String source,
80+
@JsonProperty("target") final String target,
81+
@JsonProperty("keep_history") final boolean keepHistory) {
82+
this.source = source;
83+
this.target = target;
84+
this.keepHistory = keepHistory;
85+
}
86+
87+
public String getSource() {
88+
return source;
89+
}
90+
91+
public void setSource(String source) {
92+
this.source = source;
93+
}
94+
95+
public String getTarget() {
96+
return target;
97+
}
98+
99+
public void setTarget(String target) {
100+
this.target = target;
101+
}
102+
103+
public boolean getKeepHistory() {
104+
return keepHistory;
105+
}
106+
107+
public void setKeepHistory(boolean keepHistory) {
108+
this.keepHistory = keepHistory;
109+
}
110+
}
111+
}

stream-core/src/main/java/io/getstream/client/model/feeds/BaseFeed.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.getstream.client.model.activities.BaseActivity;
55
import io.getstream.client.model.beans.FeedFollow;
66
import io.getstream.client.model.beans.FollowMany;
7+
import io.getstream.client.model.beans.UnfollowMany;
78
import io.getstream.client.model.filters.FeedFilter;
89
import io.getstream.client.repo.StreamRepository;
910
import io.getstream.client.service.AggregatedActivityServiceImpl;
@@ -69,6 +70,11 @@ public void followMany(FollowMany follows) throws IOException, StreamClientExcep
6970
streamRepository.followMany(this, follows, DEFAULT_ACTIVITY_COPY_LIMIT);
7071
}
7172

73+
@Override
74+
public void unfollowMany(UnfollowMany unfollowMany) throws IOException, StreamClientException {
75+
streamRepository.unfollowMany(this, unfollowMany);
76+
}
77+
7278
@Override
7379
public void unfollow(String feedSlug, String userId) throws IOException, StreamClientException {
7480
String feedId = String.format("%s:%s", feedSlug, userId);

stream-core/src/main/java/io/getstream/client/model/feeds/Feed.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.getstream.client.model.activities.BaseActivity;
55
import io.getstream.client.model.beans.FeedFollow;
66
import io.getstream.client.model.beans.FollowMany;
7+
import io.getstream.client.model.beans.UnfollowMany;
78
import io.getstream.client.model.filters.FeedFilter;
89
import io.getstream.client.service.AggregatedActivityServiceImpl;
910
import io.getstream.client.service.FlatActivityServiceImpl;
@@ -60,7 +61,7 @@ public interface Feed {
6061
void follow(String feedSlug, String userId, int activityCopyLimit) throws IOException, StreamClientException;
6162

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

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

85+
/**
86+
* Unfollow many feeds in one shot.
87+
*
88+
* @param unfollowMany A {@link UnfollowMany} object which contains a list of sources and targets.
89+
* Any arbitrary feed can be specified as {@link io.getstream.client.model.beans.UnfollowMany.Entry#setSource(String)},
90+
* regardless the one used to trigger this operation.
91+
* @throws StreamClientException in case of functional or server-side exception
92+
* @throws IOException in case of network/socket exceptions
93+
*/
94+
void unfollowMany(UnfollowMany unfollowMany) throws IOException, StreamClientException;
95+
8496
/**
8597
* Unfollow the given target feed.
8698
*

stream-core/src/main/java/io/getstream/client/repo/StreamRepository.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.getstream.client.model.beans.MarkedActivity;
1010
import io.getstream.client.model.beans.StreamActivitiesResponse;
1111
import io.getstream.client.model.beans.StreamResponse;
12+
import io.getstream.client.model.beans.UnfollowMany;
1213
import io.getstream.client.model.feeds.BaseFeed;
1314
import io.getstream.client.model.filters.FeedFilter;
1415

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

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

73+
/**
74+
* Unfollow many feeds in one shot.
75+
*
76+
* @param feed Feed that wants to follow a target feed.
77+
* @param unfollowManyInput A {@link UnfollowMany} object which contains a list of sources and targets.
78+
* @throws StreamClientException in case of functional or server-side exception
79+
* @throws IOException in case of network/socket exceptions
80+
*/
81+
void unfollowMany(BaseFeed feed, UnfollowMany unfollowManyInput) throws StreamClientException, IOException;
82+
7283
/**
7384
* Unfollow a feed.
7485
*

stream-repo-apache/src/main/java/io/getstream/client/apache/repo/StreamRepositoryImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.getstream.client.model.beans.MarkedActivity;
1818
import io.getstream.client.model.beans.StreamActivitiesResponse;
1919
import io.getstream.client.model.beans.StreamResponse;
20+
import io.getstream.client.model.beans.UnfollowMany;
2021
import io.getstream.client.model.feeds.BaseFeed;
2122
import io.getstream.client.model.filters.FeedFilter;
2223
import io.getstream.client.repo.StreamRepository;
@@ -104,6 +105,16 @@ public void followMany(BaseFeed feed, FollowMany followManyInput, int activityCo
104105
fireAndForget(request);
105106
}
106107

108+
@Override
109+
public void unfollowMany(BaseFeed feed, UnfollowMany unfollowManyInput) throws StreamClientException, IOException {
110+
HttpPost request = new HttpPost(UriBuilder.fromEndpoint(baseEndpoint)
111+
.path("unfollow_many/")
112+
.build());
113+
request.addHeader(HttpSignatureInterceptor.X_API_KEY_HEADER, apiKey);
114+
request.setEntity(new StringEntity(objectMapper.writeValueAsString(unfollowManyInput), APPLICATION_JSON));
115+
fireAndForget(request);
116+
}
117+
107118
@Override
108119
public void unfollow(BaseFeed feed, String targetFeedId, boolean keepHistory) throws StreamClientException, IOException {
109120
HttpDelete request = new HttpDelete(UriBuilder.fromEndpoint(baseEndpoint)

stream-repo-apache/src/test/java/io/getstream/client/apache/IntegrationTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.getstream.client.model.beans.MarkedActivity;
2121
import io.getstream.client.model.beans.StreamActivitiesResponse;
2222
import io.getstream.client.model.beans.StreamResponse;
23+
import io.getstream.client.model.beans.UnfollowMany;
2324
import io.getstream.client.model.feeds.Feed;
2425
import io.getstream.client.model.filters.FeedFilter;
2526
import io.getstream.client.service.AggregatedActivityServiceImpl;
@@ -178,6 +179,40 @@ public void shouldFollowMany() throws IOException, StreamClientException {
178179
streamClient.shutdown();
179180
}
180181

182+
@Test
183+
public void shouldUnfollowMany() throws IOException, StreamClientException {
184+
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,
185+
API_SECRET);
186+
187+
String followerId = this.getTestUserId("shouldunfollowMany");
188+
Feed feed = streamClient.newFeed("user", followerId);
189+
190+
List<FeedFollow> following = feed.getFollowing();
191+
assertThat(following.size(), is(0));
192+
193+
FollowMany followMany = new FollowMany.Builder()
194+
.add("user:" + followerId, "user:1")
195+
.add("user:" + followerId, "user:2")
196+
.add("user:" + followerId, "user:3")
197+
.build();
198+
feed.followMany(followMany);
199+
200+
List<FeedFollow> followingAfter = feed.getFollowing();
201+
assertThat(followingAfter.size(), is(3));
202+
203+
UnfollowMany unfollowMany = new UnfollowMany.Builder()
204+
.add("user:" + followerId, "user:1")
205+
.add("user:" + followerId, "user:2", true)
206+
.add("user:" + followerId, "user:3", false)
207+
.build();
208+
feed.unfollowMany(unfollowMany);
209+
210+
List<FeedFollow> unfollowingAfter = feed.getFollowing();
211+
assertThat(unfollowingAfter.size(), is(0));
212+
213+
streamClient.shutdown();
214+
}
215+
181216
@Test
182217
public void shouldHaveOriginField() throws IOException, StreamClientException, InterruptedException {
183218
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,

0 commit comments

Comments
 (0)