package io.ashdavies.eternity.chat;

import com.google.firebase.appindexing.FirebaseAppIndex;
import com.google.firebase.appindexing.Indexable;
import com.google.firebase.appindexing.builders.Indexables;
import com.google.firebase.appindexing.builders.PersonBuilder;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import javax.inject.Inject;

class MessageIndexer implements Consumer<Message> {

  private static final String MESSAGE_URL = "https://eternity.ashdavies.io/message";

  private final FirebaseAppIndex index;
  private final FirebaseUser user;

  @Inject
  MessageIndexer() {
    this(FirebaseAppIndex.getInstance(), FirebaseAuth.getInstance().getCurrentUser());
  }

  private MessageIndexer(FirebaseAppIndex index, FirebaseUser user) {
    this.index = index;
    this.user = user;
  }

  @Override
  public void accept(@NonNull Message message) throws Exception {
    index.update(getIndexable(message));
  }

  private Indexable getIndexable(Message message) {
    return Indexables.messageBuilder()
        .setName(message.text())
        .setUrl(String.format("%s/%s", MESSAGE_URL, message.uuid()))
        .setRecipient(getRecipient(message))
        .setSender(getSender(message))
        .build();
  }

  private PersonBuilder getRecipient(Message message) {
    return Indexables.personBuilder()
        .setName(message.author().name())
        .setUrl(String.format("%s/%s/recipient", MESSAGE_URL, message.uuid()));
  }

  private PersonBuilder getSender(Message message) {
    return Indexables.personBuilder()
        .setIsSelf(user.getUid().equals(message.author().uuid()))
        .setName(message.author().name())
        .setUrl(String.format("%s/%s/sender", MESSAGE_URL, message.uuid()));
  }
}

@@ -69,32 +69,139 @@
 ndexing.
-FirebaseAppIndex
+Action;%0Aimport com.google.firebase.appindexing.FirebaseAppIndex;%0Aimport com.google.firebase.appindexing.FirebaseUserActions
 ;%0Aimport
@@ -424,45 +424,45 @@
 ort 
-com.google.firebase.auth.FirebaseUser
+io.ashdavies.commons.util.StringUtils
 ;%0Aim
@@ -714,433 +714,237 @@
 %0A%0A  
-private final FirebaseAppIndex index;%0A  private final FirebaseUser user;%0A%0A  @Inject%0A  MessageIndexer() %7B%0A    this(FirebaseAppIndex.getInstance(), FirebaseAuth.getInstance().getCurrentUser());%0A  %7D%0A%0A  private MessageIndexer(FirebaseAppIndex index, FirebaseUser user) %7B%0A    this.index = index;%0A    this.user = user;%0A  %7D%0A%0A  @Override%0A  public void accept(@NonNull Message message) throws Exception %7B%0A    index.update(getIndexable
+@Inject%0A  MessageIndexer() %7B%0A  %7D%0A%0A  @Override%0A  public void accept(@NonNull Message message) throws Exception %7B%0A    FirebaseAppIndex.getInstance().update(getIndexable(message));%0A    FirebaseUserActions.getInstance().end(getAction
 (mes
@@ -1100,44 +1100,15 @@
 Url(
-String.format(%22%25s/%25s%22, MESSAGE_URL, 
+getUrl(
 mess
@@ -1219,32 +1219,409 @@
  .build();%0A  %7D%0A%0A
+  private String getUrl(String... queries) %7B%0A    return MESSAGE_URL.concat(StringUtils.join(queries, %22/%22));%0A  %7D%0A%0A  private Action getAction(Message message) %7B%0A    return new Action.Builder(Action.Builder.VIEW_ACTION)%0A        .setObject(message.author().name(), getUrl(message.uuid()))%0A        .setMetadata(new Action.Metadata.Builder().setUpload(false))%0A        .build();%0A  %7D%0A%0A
   private Person
@@ -1760,54 +1760,15 @@
 Url(
-String.format(%22%25s/%25s/recipient%22, MESSAGE_URL, 
+getUrl(
 mess
@@ -1769,32 +1769,45 @@
 l(message.uuid()
+, %22recipient%22
 ));%0A  %7D%0A%0A  priva
@@ -1844,32 +1844,122 @@
 sage message) %7B%0A
+    String uid = FirebaseAuth.getInstance()%0A        .getCurrentUser()%0A        .getUid();%0A%0A
     return Index
@@ -2004,20 +2004,10 @@
 lf(u
-ser.getUid()
+id
 .equ
@@ -2098,51 +2098,15 @@
 Url(
-String.format(%22%25s/%25s/sender%22, MESSAGE_URL, 
+getUrl(
 mess
@@ -2115,16 +2115,26 @@
 e.uuid()
+, %22sender%22
 ));%0A  %7D%0A

Include app indexing actions