2018-04-24 18:09:54 +00:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
import android.content.Context;
|
|
|
|
|
2019-06-05 19:47:14 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2018-04-24 18:09:54 +00:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2020-12-04 23:31:58 +00:00
|
|
|
import org.signal.core.util.logging.Log;
|
2018-04-24 18:09:54 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2019-08-07 18:22:51 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
2018-04-24 18:09:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the information required to find the {@link MessageRecord} pointed to by a quote.
|
|
|
|
*/
|
|
|
|
public class QuoteId {
|
|
|
|
|
2021-03-29 22:37:22 +00:00
|
|
|
private static final String TAG = Log.tag(QuoteId.class);
|
2018-04-24 18:09:54 +00:00
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
private static final String ID = "id";
|
|
|
|
private static final String AUTHOR_DEPRECATED = "author";
|
|
|
|
private static final String AUTHOR = "author_id";
|
2018-04-24 18:09:54 +00:00
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
private final long id;
|
|
|
|
private final RecipientId author;
|
2018-04-24 18:09:54 +00:00
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
public QuoteId(long id, @NonNull RecipientId author) {
|
2018-04-24 18:09:54 +00:00
|
|
|
this.id = id;
|
|
|
|
this.author = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
public @NonNull RecipientId getAuthor() {
|
2018-04-24 18:09:54 +00:00
|
|
|
return author;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @NonNull String serialize() {
|
|
|
|
try {
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
|
object.put(ID, id);
|
|
|
|
object.put(AUTHOR, author.serialize());
|
|
|
|
return object.toString();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, "Failed to serialize to json", e);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
public static @Nullable QuoteId deserialize(@NonNull Context context, @NonNull String serialized) {
|
2018-04-24 18:09:54 +00:00
|
|
|
try {
|
2019-08-07 18:22:51 +00:00
|
|
|
JSONObject json = new JSONObject(serialized);
|
|
|
|
RecipientId id = json.has(AUTHOR) ? RecipientId.from(json.getString(AUTHOR))
|
|
|
|
: Recipient.external(context, json.getString(AUTHOR_DEPRECATED)).getId();
|
|
|
|
|
|
|
|
return new QuoteId(json.getLong(ID), id);
|
2018-04-24 18:09:54 +00:00
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, "Failed to deserialize from json", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|