Skip to content

Commit 667ae59

Browse files
author
Max Klyga
committed
Add enrichment flags to all feed classes. Ensure either filter or pagination option is used but never both. Rename batch collection methods to match other clients. Add more examples
1 parent 2dadc64 commit 667ae59

File tree

14 files changed

+507
-233
lines changed

14 files changed

+507
-233
lines changed

src/main/java/example/Example.java

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import io.getstream.client.FlatFeed;
77
import io.getstream.client.NotificationFeed;
88
import io.getstream.core.KeepHistory;
9+
import io.getstream.core.LookupKind;
910
import io.getstream.core.Region;
1011
import io.getstream.core.models.*;
1112
import io.getstream.core.options.ActivityMarker;
13+
import io.getstream.core.options.EnrichmentFlags;
1214
import io.getstream.core.options.Filter;
1315
import io.getstream.core.options.Pagination;
1416

@@ -17,6 +19,9 @@
1719
import java.util.List;
1820
import java.util.Map;
1921

22+
import static io.getstream.core.utils.Enrichment.createCollectionReference;
23+
import static io.getstream.core.utils.Enrichment.createUserReference;
24+
2025
//TODO: move this to an appropriate place
2126
class Example {
2227
private static final String apiKey = "gp6e8sxxzud6";
@@ -167,7 +172,7 @@ public static void main(String[] args) throws Exception {
167172
/* -------------------------------------------------------- */
168173

169174
// Get 5 activities with id less than the given UUID (Faster - Recommended!)
170-
response = userFeed.getActivities(new Pagination().limit(5), new Filter().idLessThan("e561de8f-00f1-11e4-b400-0cc47a024be0")).join();
175+
response = userFeed.getActivities(new Filter().idLessThan("e561de8f-00f1-11e4-b400-0cc47a024be0").limit(5)).join();
171176
// Get activities from 5 to 10 (Pagination-based - Slower)
172177
response = userFeed.getActivities(new Pagination().offset(0).limit(5)).join();
173178
// Get activities sorted by rank (Ranked Feeds Enabled):
@@ -362,14 +367,153 @@ public static void main(String[] args) throws Exception {
362367

363368
/* -------------------------------------------------------- */
364369

370+
// read bob's timeline and include most recent reactions to all activities and their total count
371+
client.flatFeed("timeline", "bob")
372+
.getEnrichedActivities(new EnrichmentFlags()
373+
.withRecentReactions()
374+
.withReactionCounts());
375+
376+
// read bob's timeline and include most recent reactions to all activities and her own reactions
377+
client.flatFeed("timeline", "bob")
378+
.getEnrichedActivities(new EnrichmentFlags()
379+
.withOwnReactions()
380+
.withRecentReactions()
381+
.withReactionCounts());
382+
383+
/* -------------------------------------------------------- */
384+
385+
// retrieve all kind of reactions for an activity
386+
List<Reaction> reactions = client.reactions().filter(LookupKind.ACTIVITY, "ed2837a6-0a3b-4679-adc1-778a1704852d").join();
387+
388+
// retrieve first 10 likes for an activity
389+
reactions = client.reactions()
390+
.filter(LookupKind.ACTIVITY,
391+
"ed2837a6-0a3b-4679-adc1-778a1704852d",
392+
new Filter().limit(10),
393+
"like").join();
394+
395+
// retrieve the next 10 likes using the id_lt param
396+
reactions = client.reactions()
397+
.filter(LookupKind.ACTIVITY,
398+
"ed2837a6-0a3b-4679-adc1-778a1704852d",
399+
new Filter().idLessThan("e561de8f-00f1-11e4-b400-0cc47a024be0"),
400+
"like").join();
401+
402+
/* -------------------------------------------------------- */
403+
404+
// adds a like to the previously created comment
405+
Reaction reaction = client.reactions().addChild("john-doe", comment.getId(), Reaction.builder().kind("like").build()).join();
406+
407+
/* -------------------------------------------------------- */
408+
409+
client.reactions().update(Reaction.builder()
410+
.id(reaction.getId())
411+
.extraField("text", "love it!")
412+
.build());
413+
414+
/* -------------------------------------------------------- */
415+
416+
client.reactions().delete(reaction.getId());
417+
418+
/* -------------------------------------------------------- */
419+
420+
client.collections().add("food", new CollectionData("cheese-burger")
421+
.set("name", "Cheese Burger")
422+
.set("rating", "4 stars"));
423+
424+
// if you don't have an id on your side, just use null as the ID and Stream will generate a unique ID
425+
client.collections().add("food", new CollectionData()
426+
.set("name", "Cheese Burger")
427+
.set("rating", "4 stars"));
428+
429+
/* -------------------------------------------------------- */
430+
431+
CollectionData collection = client.collections().get("food", "cheese-burger").join();
432+
433+
/* -------------------------------------------------------- */
434+
435+
client.collections().delete("food", "cheese-burger");
436+
437+
/* -------------------------------------------------------- */
438+
439+
client.collections().update("food", new CollectionData("cheese-burger")
440+
.set("name", "Cheese Burger")
441+
.set("rating", "1 star"));
442+
443+
/* -------------------------------------------------------- */
444+
445+
client.collections().upsert("visitor",
446+
new CollectionData("123")
447+
.set("name", "John")
448+
.set("favorite_color", "blue"),
449+
new CollectionData("124")
450+
.set("name", "Jane")
451+
.set("favorite_color", "purple")
452+
.set("interests", Lists.newArrayList("fashion", "jazz")));
453+
454+
/* -------------------------------------------------------- */
455+
456+
// select the entries with ID 123 and 124 from items collection
457+
List<CollectionData> objects = client.collections().select("items", "123", "124").join();
458+
459+
/* -------------------------------------------------------- */
460+
461+
// delete the entries with ID 123 and 124 from visitor collection
462+
client.collections().deleteMany("visitor", "123", "124");
463+
464+
/* -------------------------------------------------------- */
465+
466+
// first we add our object to the food collection
467+
CollectionData cheeseBurger = client.collections().add("food", new CollectionData("123")
468+
.set("name", "Cheese Burger")
469+
.set("ingredients", Lists.newArrayList("cheese", "burger", "bread", "lettuce", "tomato"))).join();
470+
471+
// the object returned by .add can be embedded directly inside of an activity
472+
userFeed.addActivity(Activity.builder()
473+
.actor(createUserReference("john-doe"))
474+
.verb("grill")
475+
.object(createCollectionReference(cheeseBurger.getCollection(), cheeseBurger.getID()))
476+
.build());
477+
478+
// if we now read the feed, the activity we just added will include the entire full object
479+
userFeed.getEnrichedActivities();
480+
481+
// we can then update the object and Stream will propagate the change to all activities
482+
client.collections().update(cheeseBurger.getCollection(), cheeseBurger
483+
.set("name", "Amazing Cheese Burger")
484+
.set("ingredients", Lists.newArrayList("cheese", "burger", "bread", "lettuce", "tomato"))).join();
485+
486+
/* -------------------------------------------------------- */
487+
488+
// First create a collection entry with upsert api
489+
client.collections().upsert("food", new CollectionData().set("name", "Cheese Burger"));
490+
491+
// Then create a user
492+
client.user("john-doe").create(new Data()
493+
.set("name", "John Doe")
494+
.set("occupation", "Software Engineer")
495+
.set("gender", "male"));
496+
497+
// Since we know their IDs we can create references to both without reading from APIs
498+
String cheeseBurgerRef = createCollectionReference("food", "cheese-burger");
499+
String johnDoeRef = createUserReference("john-doe");
500+
501+
client.flatFeed("user", "john").addActivity(Activity.builder()
502+
.actor(johnDoeRef)
503+
.verb("eat")
504+
.object(cheeseBurgerRef)
505+
.build());
506+
507+
/* -------------------------------------------------------- */
508+
365509
// create a new user, if the user already exist an error is returned
366-
client.user("john-doe").create(new Data("")
510+
client.user("john-doe").create(new Data()
367511
.set("name", "John Doe")
368512
.set("occupation", "Software Engineer")
369513
.set("gender", "male"));
370514

371515
// get or create a new user, if the user already exist the user is returned
372-
client.user("john-doe").getOrCreate(new Data("")
516+
client.user("john-doe").getOrCreate(new Data()
373517
.set("name", "John Doe")
374518
.set("occupation", "Software Engineer")
375519
.set("gender", "male"));

0 commit comments

Comments
 (0)