|
6 | 6 | import io.getstream.client.FlatFeed; |
7 | 7 | import io.getstream.client.NotificationFeed; |
8 | 8 | import io.getstream.core.KeepHistory; |
| 9 | +import io.getstream.core.LookupKind; |
9 | 10 | import io.getstream.core.Region; |
10 | 11 | import io.getstream.core.models.*; |
11 | 12 | import io.getstream.core.options.ActivityMarker; |
| 13 | +import io.getstream.core.options.EnrichmentFlags; |
12 | 14 | import io.getstream.core.options.Filter; |
13 | 15 | import io.getstream.core.options.Pagination; |
14 | 16 |
|
|
17 | 19 | import java.util.List; |
18 | 20 | import java.util.Map; |
19 | 21 |
|
| 22 | +import static io.getstream.core.utils.Enrichment.createCollectionReference; |
| 23 | +import static io.getstream.core.utils.Enrichment.createUserReference; |
| 24 | + |
20 | 25 | //TODO: move this to an appropriate place |
21 | 26 | class Example { |
22 | 27 | private static final String apiKey = "gp6e8sxxzud6"; |
@@ -167,7 +172,7 @@ public static void main(String[] args) throws Exception { |
167 | 172 | /* -------------------------------------------------------- */ |
168 | 173 |
|
169 | 174 | // 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(); |
171 | 176 | // Get activities from 5 to 10 (Pagination-based - Slower) |
172 | 177 | response = userFeed.getActivities(new Pagination().offset(0).limit(5)).join(); |
173 | 178 | // Get activities sorted by rank (Ranked Feeds Enabled): |
@@ -362,14 +367,153 @@ public static void main(String[] args) throws Exception { |
362 | 367 |
|
363 | 368 | /* -------------------------------------------------------- */ |
364 | 369 |
|
| 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 | + |
365 | 509 | // 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() |
367 | 511 | .set("name", "John Doe") |
368 | 512 | .set("occupation", "Software Engineer") |
369 | 513 | .set("gender", "male")); |
370 | 514 |
|
371 | 515 | // 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() |
373 | 517 | .set("name", "John Doe") |
374 | 518 | .set("occupation", "Software Engineer") |
375 | 519 | .set("gender", "male")); |
|
0 commit comments