Session.java 1.84 KB
Newer Older
1 2 3
package chat.rocket.android.model.internal;

import chat.rocket.android.helper.LogcatIfError;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
4
import chat.rocket.android.helper.TextUtils;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import org.json.JSONObject;

/**
 * Login session info.
 */
public class Session extends RealmObject {
  public static final int DEFAULT_ID = 0;
  @PrimaryKey private int sessionId; //only 0 is used!
  private String token;
  private boolean tokenVerified;
  private String error;

  public int getSessionId() {
    return sessionId;
  }

  public void setSessionId(int sessionId) {
    this.sessionId = sessionId;
  }

  public String getToken() {
    return token;
  }

  public void setToken(String token) {
    this.token = token;
  }

  public boolean isTokenVerified() {
    return tokenVerified;
  }

  public void setTokenVerified(boolean tokenVerified) {
    this.tokenVerified = tokenVerified;
  }

  public String getError() {
    return error;
  }

  public void setError(String error) {
    this.error = error;
  }

  /**
   * Log the server connection is lost due to soem exception.
   */
  @DebugLog public static void logError(RealmHelper realmHelper, Exception exception) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    String errString = exception.getMessage();
    if (!TextUtils.isEmpty(errString) && errString.contains("[403]")) {
      realmHelper.executeTransaction(realm -> {
        realm.delete(Session.class);
        return null;
      }).continueWith(new LogcatIfError());
    } else {
      realmHelper.executeTransaction(
          realm -> realm.createOrUpdateObjectFromJson(Session.class, new JSONObject()
              .put("sessionId", Session.DEFAULT_ID)
              .put("tokenVerified", false)
              .put("error", errString)))
          .continueWith(new LogcatIfError());
    }
71 72
  }
}