Skip to content

Commit

Permalink
Merge pull request #134 from GetStream/soft-delete
Browse files Browse the repository at this point in the history
[PBE-1134] Added soft delete reactions support
  • Loading branch information
xernobyl committed Oct 23, 2023
2 parents 346f408 + f0c847a commit c58917e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

## 📝 About Stream

You can sign up for a Stream account at our [Get Started](https://getstream.io/chat/get_started/) page.
You can sign up for a Stream account at our [Get Started](https://getstream.io/activity-feeds/docs/java/?language=java) page.

You can use this library to access feeds API endpoints server-side.

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/getstream/client/ReactionsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ public CompletableFuture<Void> update(Reaction reaction, FeedID... targetFeeds)

public CompletableFuture<Void> delete(String id) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.DELETE);
return reactions.delete(token, id);
return reactions.delete(token, id, false);
}

public CompletableFuture<Void> delete(String id, Boolean soft) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.DELETE);
return reactions.delete(token, id, soft);
}

public CompletableFuture<Void> softDelete(String id) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.DELETE);
return reactions.delete(token, id, true);
}

public CompletableFuture<Void> restore(String id) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.WRITE);
return reactions.restore(token, id);
}
}
14 changes: 13 additions & 1 deletion src/main/java/io/getstream/cloud/CloudReactionsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,19 @@ public CompletableFuture<Void> update(Reaction reaction, FeedID... targetFeeds)
}

public CompletableFuture<Void> delete(String id) throws StreamException {
return reactions.delete(token, id);
return reactions.delete(token, id, false);
}

public CompletableFuture<Void> delete(String id, Boolean soft) throws StreamException {
return reactions.delete(token, id, soft);
}

public CompletableFuture<Void> softDelete(String id) throws StreamException {
return reactions.delete(token, id, true);
}

public CompletableFuture<Void> restore(String id) throws StreamException {
return reactions.restore(token, id);
}

public class Params {
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/io/getstream/core/StreamReactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.ImmutableMap;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Request;
import io.getstream.core.http.Token;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Paginated;
Expand Down Expand Up @@ -283,14 +284,40 @@ public CompletableFuture<Void> update(Token token, Reaction reaction, FeedID...
}
}

public CompletableFuture<Void> delete(Token token, String id) throws StreamException {
public CompletableFuture<Void> delete(Token token, String id, Boolean soft) throws StreamException {
checkNotNull(id, "Reaction id can't be null");
checkArgument(!id.isEmpty(), "Reaction id can't be empty");

try {
final URL url = buildReactionsURL(baseURL, id + '/');

final Request deleteRequest = soft ? buildDelete(url, key, token, new CustomQueryParameter("soft", "true"))
: buildDelete(url, key, token);

return httpClient
.execute(deleteRequest)
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}

public CompletableFuture<Void> restore(Token token, String id) throws StreamException {
checkNotNull(id, "Reaction id can't be null");
checkArgument(!id.isEmpty(), "Reaction id can't be empty");

try {
final URL url = buildReactionsURL(baseURL, id + "/restore/");
byte[] payload = new byte[0];
return httpClient
.execute(buildDelete(url, key, token))
.execute(buildPut(url, key, token, payload))
.thenApply(
response -> {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/getstream/core/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ public Request build() throws MalformedURLException, URISyntaxException {
return new Request(this);
}
}
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/getstream/client/ReactionsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,15 @@ public void delete() throws Exception {
Reaction reply = client.reactions().add("user-id", data, new FeedID("flat", "1")).join();
client.reactions().delete(reply.getId()).join();
}

@Test
public void softDelete() throws Exception {
Client client = Client.builder(apiKey, secret).build();

Reaction data =
Reaction.builder().activityID("ed2837a6-0a3b-4679-adc1-778a1704852d").kind("like").build();
Reaction reply = client.reactions().add("user-id", data, new FeedID("flat", "1")).join();
client.reactions().softDelete(reply.getId()).join();
client.reactions().restore(reply.getId()).join();
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/getstream/cloud/CloudReactionsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ public void delete() throws Exception {
Reaction reply = client.reactions().add("user-id", data, new FeedID("flat", "1")).join();
client.reactions().delete(reply.getId()).join();
}

@Test
public void softDelete() throws Exception {
Client client = Client.builder(apiKey, secret).build();

Reaction data =
Reaction.builder().activityID("ed2837a6-0a3b-4679-adc1-778a1704852d").kind("like").build();
Reaction reply = client.reactions().add("user-id", data, new FeedID("flat", "1")).join();
client.reactions().softDelete(reply.getId()).join();
client.reactions().restore(reply.getId()).join();
}
}

0 comments on commit c58917e

Please sign in to comment.