Skip to content

Commit

Permalink
add topic arhiving to the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Feb 7, 2019
1 parent 40aeb2c commit a08ab5d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
Expand Up @@ -137,7 +137,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
* @param topic topic handle for action
*/
private void showNotification(String title, String body, Bitmap avatar, String topic) {
Log.d(TAG, "Notification title=" + title + ", body=" + body + "topic=" + topic);
// Log.d(TAG, "Notification title=" + title + ", body=" + body + ", topic=" + topic);

Intent intent;
if (TextUtils.isEmpty(topic)) {
Expand Down
28 changes: 27 additions & 1 deletion tinodesdk/src/main/java/co/tinode/tinodesdk/ComTopic.java
@@ -1,11 +1,15 @@
package co.tinode.tinodesdk;

import co.tinode.tinodesdk.model.Description;
import co.tinode.tinodesdk.model.MetaSetDesc;
import co.tinode.tinodesdk.model.MsgServerMeta;
import co.tinode.tinodesdk.model.MsgSetMeta;
import co.tinode.tinodesdk.model.PrivateType;
import co.tinode.tinodesdk.model.ServerMessage;
import co.tinode.tinodesdk.model.Subscription;

/* Communication topic: a P2P or Group.
/**
* Communication topic: a P2P or Group.
*/
public class ComTopic<DP> extends Topic<DP,PrivateType,DP,PrivateType> {

Expand Down Expand Up @@ -39,6 +43,28 @@ public String getComment() {
return p != null ? p.getComment() : null;
}

/**
* Checks if the topic is archived. Not all topics support archiving.
* @return true if the topic is archived, false otherwise.
*/
@Override
public boolean isArchived() {
PrivateType p = super.getPriv();
return p != null ? p.isArchived() : false;
}

/**
* Archive topic by issuing {@link Topic#setMeta} with priv set to {arch: true/false}.
*
* @throws NotSubscribedException if the client is not subscribed to the topic
* @throws NotConnectedException if there is no connection to the server
*/
public PromisedReply<ServerMessage> archive(final boolean arch) {
PrivateType priv = new PrivateType();
priv.setArchived(arch);
return setMeta(new MsgSetMeta<>(new MetaSetDesc<DP,PrivateType>(null, priv)));
}

public static class ComListener<DP> extends Listener<DP,PrivateType,DP,PrivateType> {
/** {meta} message received */
public void onMeta(MsgServerMeta<DP,PrivateType,DP,PrivateType> meta) {}
Expand Down
10 changes: 8 additions & 2 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Expand Up @@ -68,12 +68,18 @@ public class Tinode {
public static final String TOPIC_NEW = "new";
public static final String TOPIC_ME = "me";
public static final String TOPIC_FND = "fnd";

public static final String TOPIC_GRP_PREFIX = "grp";
public static final String TOPIC_USR_PREFIX = "usr";

protected static final String NOTE_KP = "kp";
protected static final String NOTE_READ = "read";
protected static final String NOTE_RECV = "recv";

private static final String TAG = "Tinode";

public static final String NULL_VALUE = "\u2421";

// Delay in milliseconds between sending two key press notifications on the
// same topic.
private static final long NOTE_KP_DELAY = 3000L;
Expand Down Expand Up @@ -200,10 +206,10 @@ public static ObjectMapper getJsonMapper() {
return sJsonMapper;
}

// FIXME(gene): this is broken. Figure out how to handle nulls
// Compares object to a string which signifies "null" to the server.
public static boolean isNull(Object obj) {
// Del control character
return (obj instanceof String) && obj.equals("\u2421");
return (obj instanceof String) && obj.equals(NULL_VALUE);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Topic.java
Expand Up @@ -460,6 +460,15 @@ public void setPriv(DR priv) {
mDesc.priv = priv;
}

/**
* Checks if the topic is archived. Not all topics support archiving.
* @return true if the topic is archived, false otherwise.
*/
public boolean isArchived() {
return false;
}


public Storage.Range getCachedMessageRange() {
return mStore == null ? null : mStore.getCachedMessagesRange(this);
}
Expand Down
30 changes: 27 additions & 3 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/model/PrivateType.java
Expand Up @@ -4,6 +4,8 @@

import java.util.HashMap;

import co.tinode.tinodesdk.Tinode;

/**
* Common type of the `private` field of {meta}: holds structured
* data, such as comment and archival status.
Expand All @@ -15,11 +17,33 @@ public PrivateType() {

@JsonIgnore
public String getComment() {
return (String) get("comment");
String comment = (String) get("comment");
if (Tinode.isNull(comment)) {
return null;
}
return comment;
}

@JsonIgnore
public void setComment(String comment) {
put("comment", comment != null && comment.length() > 0 ? comment : Tinode.NULL_VALUE);
}

@JsonIgnore
public String setComment(String comment) {
return (String) put("comment", comment);
public Boolean isArchived() {
Object arch = get("arch");
if (arch == null) {
return null;
}
if (Tinode.isNull(arch)) {
return false;
}
return (Boolean) arch;
}

@JsonIgnore
public void setArchived(boolean arch) {
put("arch", arch ? true : Tinode.NULL_VALUE);
}

}

0 comments on commit a08ab5d

Please sign in to comment.