Skip to content

Commit

Permalink
enable clearing all topic tags
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Dec 15, 2022
1 parent e33321b commit 4e09dbd
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/co/tinode/tindroid/AccPersonalFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void showEditTags() {
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
String[] tags1 = UiUtils.parseTags(tagsEditor.getText().toString());
// noinspection unchecked
me.setMeta(new MsgSetMeta(tags1))
me.setMeta(new MsgSetMeta.Builder().with(tags1).build())
.thenCatch(new UiUtils.ToastFailureListener(activity));
})
.setNegativeButton(android.R.string.cancel, null)
Expand Down Expand Up @@ -291,7 +291,7 @@ private void showAddCredential() {
if (parsed != null) {
final MeTopic me = Cache.getTinode().getMeTopic();
// noinspection unchecked
me.setMeta(new MsgSetMeta(parsed))
me.setMeta(new MsgSetMeta.Builder().with(parsed).build())
.thenCatch(new UiUtils.ToastFailureListener(activity));

// Dismiss once everything is OK.
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/co/tinode/tindroid/FindFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ private String doSearch(String query) {
return query;
}

mFndTopic.setMeta(new MsgSetMeta<>(
new MetaSetDesc<>(query == null ? Tinode.NULL_VALUE : query, null)));
mFndTopic.setMeta(new MsgSetMeta.Builder<String,String>().with(
new MetaSetDesc<>(query == null ? Tinode.NULL_VALUE : query, null)).build());
if (query != null) {
toggleProgressIndicator(true);
mFndTopic.getMeta(MsgGetMeta.sub()).thenFinally(new PromisedReply.FinalListener() {
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/co/tinode/tindroid/MessagesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import co.tinode.tinodesdk.model.Drafty;
import co.tinode.tinodesdk.model.MetaSetSub;
import co.tinode.tinodesdk.model.MsgSetMeta;
import co.tinode.tinodesdk.model.PrivateType;
import co.tinode.tinodesdk.model.ServerMessage;
import co.tinode.tinodesdk.model.Subscription;

Expand Down Expand Up @@ -392,7 +393,8 @@ public void afterTextChanged(Editable editable) {
// Enable peer.
Acs am = new Acs(mTopic.getAccessMode());
am.update(new AccessChange(null, "+RW"));
mTopic.setMeta(new MsgSetMeta<>(new MetaSetSub(mTopic.getName(), am.getGiven())))
mTopic.setMeta(new MsgSetMeta.Builder<VxCard, PrivateType>()
.with(new MetaSetSub(mTopic.getName(), am.getGiven())).build())
.thenCatch(new UiUtils.ToastFailureListener(activity));
});

Expand Down Expand Up @@ -1086,15 +1088,16 @@ private void showChatInvitationDialog() {
int id = view1.getId();
if (id == R.id.buttonAccept) {
final String mode = mTopic.getAccessMode().getGiven();
response = mTopic.setMeta(new MsgSetMeta<>(new MetaSetSub(mode)));
response = mTopic.setMeta(new MsgSetMeta.Builder<VxCard,PrivateType>()
.with(new MetaSetSub(mode)).build());
if (mTopic.isP2PType()) {
// For P2P topics change 'given' permission of the peer too.
// In p2p topics the other user has the same name as the topic.
response = response.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
return mTopic.setMeta(
new MsgSetMeta<>(new MetaSetSub(mTopic.getName(), mode)));
return mTopic.setMeta(new MsgSetMeta.Builder<VxCard,PrivateType>()
.with(new MetaSetSub(mTopic.getName(), mode)).build());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void showEditTags() {
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
String[] tags1 = UiUtils.parseTags(tagsEditor.getText().toString());
// noinspection unchecked
mTopic.setMeta(new MsgSetMeta(tags1))
mTopic.setMeta(new MsgSetMeta.Builder().with(tags1).build())
.thenApply(new PromisedReply.SuccessListener() {
@Override
public PromisedReply onSuccess(Object result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ public void onPerformSync(final Account account, final Bundle extras, String aut
if (lastSyncMarker == null) {
// Send contacts list to the server only if it has changed since last update, i.e. a full
// update is performed.
tinode.setMeta(Tinode.TOPIC_FND,
new MsgSetMeta(new MetaSetDesc(null, contacts))).getResult();
tinode.setMeta(Tinode.TOPIC_FND, new MsgSetMeta.Builder()
.with(new MetaSetDesc(null, contacts)).build()).getResult();
}

final MsgGetMeta meta = new MsgGetMeta(new MetaGetSub(lastSyncMarker, null));
Expand Down
4 changes: 2 additions & 2 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/ComTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public PromisedReply<ServerMessage> subscribe() {
if (mDesc.pub != null) {
desc.attachments = mDesc.pub.getPhotoRefs();
}
return subscribe(new MsgSetMeta<>(desc, null, mTags, null), null);
return subscribe(new MsgSetMeta.Builder<DP, PrivateType>().with(desc).with(mTags).build(), null);
}
return super.subscribe();
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public Subscription<DP, PrivateType> getPeer() {
public PromisedReply<ServerMessage> updateArchived(final boolean arch) {
PrivateType priv = new PrivateType();
priv.setArchived(arch);
return setMeta(new MsgSetMeta<>(new MetaSetDesc<>(null, priv)));
return setMeta(new MsgSetMeta.Builder<DP, PrivateType>().with(new MetaSetDesc<>(null, priv)).build());
}

public static class ComListener<DP> implements Listener<DP,PrivateType,DP,PrivateType> {
Expand Down
3 changes: 2 additions & 1 deletion tinodesdk/src/main/java/co/tinode/tinodesdk/MeTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
}

public PromisedReply<ServerMessage> confirmCred(final String meth, final String resp) {
return setMeta(new MsgSetMeta<>(new Credential(meth, null, resp, null)));
return setMeta(new MsgSetMeta.Builder<DP,PrivateType>()
.with(new Credential(meth, null, resp, null)).build());
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,6 +59,7 @@
import co.tinode.tinodesdk.model.MsgClientNote;
import co.tinode.tinodesdk.model.MsgClientPub;
import co.tinode.tinodesdk.model.MsgClientSet;
import co.tinode.tinodesdk.model.MsgClientSetSerializer;
import co.tinode.tinodesdk.model.MsgClientSub;
import co.tinode.tinodesdk.model.MsgGetMeta;
import co.tinode.tinodesdk.model.MsgRange;
Expand All @@ -67,6 +69,7 @@
import co.tinode.tinodesdk.model.MsgServerMeta;
import co.tinode.tinodesdk.model.MsgServerPres;
import co.tinode.tinodesdk.model.MsgSetMeta;
import co.tinode.tinodesdk.model.MsgSetMetaSerializer;
import co.tinode.tinodesdk.model.Pair;
import co.tinode.tinodesdk.model.PrivateType;
import co.tinode.tinodesdk.model.ServerMessage;
Expand Down Expand Up @@ -142,6 +145,12 @@ public class Tinode {
sDateFormat = new RFC3339Format();
sJsonMapper.setDateFormat(sDateFormat);

// Add custom serializer for MsgSetMeta.
SimpleModule module = new SimpleModule();
module.addSerializer(new MsgSetMetaSerializer());
module.addSerializer(new MsgClientSetSerializer());
sJsonMapper.registerModule(module);

sTypeFactory = sJsonMapper.getTypeFactory();
}

Expand Down
4 changes: 2 additions & 2 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
* @throws NotConnectedException if there is no connection to the server
*/
protected PromisedReply<ServerMessage> setDescription(final MetaSetDesc<DP, DR> desc) {
return setMeta(new MsgSetMeta<>(desc));
return setMeta(new MsgSetMeta.Builder<DP, DR>().with(desc).build());
}

/**
Expand Down Expand Up @@ -1301,7 +1301,7 @@ public PromisedReply<ServerMessage> updateDefAcs(String auth, String anon) {
* @throws NotConnectedException if there is no connection to the server
*/
protected PromisedReply<ServerMessage> setSubscription(final MetaSetSub sub) {
return setMeta(new MsgSetMeta<>(sub));
return setMeta(new MsgSetMeta.Builder<DP, DR>().with(sub).build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.tinode.tinodesdk.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package co.tinode.tinodesdk.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Arrays;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT;

/**
* Metadata update packet
* Metadata update packet: description, subscription, tags, credentials.
*
* topic metadata, new topic &amp; new subscriptions only
* Desc *MsgSetDesc `json:"desc,omitempty"`
Expand All @@ -17,6 +19,10 @@
*/
@JsonInclude(NON_DEFAULT)
public class MsgClientSet<Pu,Pr> implements Serializable {
// Keep track of NULL assignments to fields.
@JsonIgnore
final boolean[] nulls = new boolean[]{false, false, false, false};

public String id;
public String topic;

Expand All @@ -29,6 +35,12 @@ public MsgClientSet() {}

public MsgClientSet(String id, String topic, MsgSetMeta<Pu,Pr> meta) {
this(id, topic, meta.desc, meta.sub, meta.tags, meta.cred);
System.arraycopy(meta.nulls, 0, nulls, 0, meta.nulls.length);
}

protected MsgClientSet(String id, String topic) {
this.id = id;
this.topic = topic;
}

protected MsgClientSet(String id, String topic, MetaSetDesc<Pu, Pr> desc,
Expand All @@ -40,4 +52,36 @@ protected MsgClientSet(String id, String topic, MetaSetDesc<Pu, Pr> desc,
this.tags = tags;
this.cred = cred;
}

public static class Builder<Pu,Pr> {
private final MsgClientSet<Pu,Pr> msm;

public Builder(String id, String topic) {
msm = new MsgClientSet<>(id, topic);
}

public void with(MetaSetDesc<Pu,Pr> desc) {
msm.desc = desc;
msm.nulls[MsgSetMeta.NULL_DESC] = desc == null;
}

public void with(MetaSetSub sub) {
msm.sub = sub;
msm.nulls[MsgSetMeta.NULL_SUB] = sub == null;
}

public void with(String[] tags) {
msm.tags = tags;
msm.nulls[MsgSetMeta.NULL_TAGS] = tags == null || tags.length == 0;
}

public void with(Credential cred) {
msm.cred = cred;
msm.nulls[MsgSetMeta.NULL_CRED] = cred == null;
}

public MsgClientSet<Pu,Pr> build() {
return msm;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package co.tinode.tinodesdk.model;

import android.util.Log;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

import co.tinode.tinodesdk.Tinode;

/**
* Custom serializer for MsgSetMeta to serialize assigned NULL fields as Tinode.NULL_VALUE string.
*/
public class MsgClientSetSerializer extends StdSerializer<MsgClientSet<?,?>> {
public MsgClientSetSerializer() {
super(MsgClientSet.class, false);
}

@Override
public void serialize(MsgClientSet<?,?> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
if (value.id != null && value.id.length() > 0) {
gen.writeStringField("id", value.id);
}
if (value.topic != null && value.topic.length() > 0) {
gen.writeStringField("topic", value.topic);
}

if (value.desc != null) {
gen.writeObjectField("desc", value.desc);
} else if (value.nulls[MsgSetMeta.NULL_DESC]) {
gen.writeStringField("desc", Tinode.NULL_VALUE);
}

if (value.sub != null) {
gen.writeObjectField("sub", value.sub);
} else if (value.nulls[MsgSetMeta.NULL_SUB]) {
gen.writeStringField("sub", Tinode.NULL_VALUE);
}

if (value.tags != null && value.tags.length != 0) {
gen.writeFieldName("tags");
gen.writeArray(value.tags, 0, value.tags.length);
} else if (value.nulls[MsgSetMeta.NULL_TAGS]) {
gen.writeFieldName("tags");
gen.writeArray(new String[]{Tinode.NULL_VALUE}, 0, 1);
}

if (value.cred != null) {
gen.writeObjectField("cred", value.cred);
} else if (value.nulls[MsgSetMeta.NULL_CRED]) {
gen.writeStringField("cred", Tinode.NULL_VALUE);
}
gen.writeEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.tinode.tinodesdk.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

Expand Down
Loading

0 comments on commit 4e09dbd

Please sign in to comment.