v0.7.0: bubble for Android 0.7.0 — react to messages, reactions in other languages, and edits
Android, ported from the desktop's reactions.ts / edits.ts (same readings:
checked side by side on the same 32 cases):
- Long-press a message: the quick reaction row (yours lit, tap again to take
it back), + for every emoji (EmojiPicker, the desktop's emoji list as
res/raw/emoji_list.txt, minus any the font can't draw), and Copy text.
- Settings → Reactions → Quick reactions: pick a spot, then its emoji; Reset.
- Tapbacks in other languages fold like the desktop's; "Edited to “…”" edits
its message, which says "Edited · time"; tap it for the earlier texts.
- Chat list and notifications preview an edit as "✏️ new text".
- push_sandbox.py has a Spanish tapback and an edit to try it on.
Desktop: version 0.7.0; gen-emoji also writes the Android list.
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
package me.teafry.voipmessages;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* "Edited to “…”" texts (an iPhone editing a message it sent us) applied to
|
||||
* the message they're about -- a port of the desktop's src/shared/edits.ts:
|
||||
* the same person's message from the last 20 minutes that reads most like
|
||||
* the new text (or their only one there), or a near-copy within a day.
|
||||
*/
|
||||
final class Edits {
|
||||
private Edits() {}
|
||||
|
||||
static final class Version {
|
||||
final String body;
|
||||
final long ts;
|
||||
|
||||
Version(String body, long ts) {
|
||||
this.body = body;
|
||||
this.ts = ts;
|
||||
}
|
||||
}
|
||||
|
||||
private static final Pattern EDIT = Pattern.compile(
|
||||
"^(edited to|editado a|editó a|se editó a|editado para|editou para|a modifié en|modifié en|a été modifié en|bearbeitet zu|bearbeitet"
|
||||
+ "|geändert in|geändert zu|modificato in|ha modificato in|bewerkt naar|gewijzigd in):?\\s*[“\"”«„‚]([\\s\\S]*)[”\"“»‘’]$",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
||||
|
||||
private static final long WINDOW = 20 * 60_000L, LATE_WINDOW = 24 * 60 * 60_000L;
|
||||
private static final double LIKENESS = 0.4, LATE_LIKENESS = 0.6;
|
||||
private static final int LOOK_BACK = 40;
|
||||
|
||||
/** The new text of an "Edited to “…”" message, or null. */
|
||||
static String parse(String body) {
|
||||
if (body == null) return null;
|
||||
Matcher m = EDIT.matcher(body.trim());
|
||||
if (!m.matches()) return null;
|
||||
String text = m.group(2).replaceAll("^[\\s\\u00a0\\u202f]+|[\\s\\u00a0\\u202f]+$", "");
|
||||
return text.isEmpty() ? null : text;
|
||||
}
|
||||
|
||||
static String preview(String body) {
|
||||
String t = parse(body);
|
||||
return t == null ? null : "✏️ " + Reactions.norm(t);
|
||||
}
|
||||
|
||||
/** 1 = the same, 0 = nothing in common (one minus edit distance over the longer length). */
|
||||
static double likeness(String a, String b) {
|
||||
int[] x = Reactions.norm(a).toLowerCase().codePoints().limit(600).toArray();
|
||||
int[] y = Reactions.norm(b).toLowerCase().codePoints().limit(600).toArray();
|
||||
if (x.length == 0 && y.length == 0) return 1;
|
||||
int[] prev = new int[y.length + 1];
|
||||
for (int j = 0; j <= y.length; j++) prev[j] = j;
|
||||
for (int i = 1; i <= x.length; i++) {
|
||||
int[] cur = new int[y.length + 1];
|
||||
cur[0] = i;
|
||||
for (int j = 1; j <= y.length; j++)
|
||||
cur[j] = Math.min(Math.min(prev[j] + 1, cur[j - 1] + 1), prev[j - 1] + (x[i - 1] == y[j - 1] ? 0 : 1));
|
||||
prev = cur;
|
||||
}
|
||||
return 1 - prev[y.length] / (double) Math.max(x.length, y.length);
|
||||
}
|
||||
|
||||
private static String who(Store.Msg m) {
|
||||
return m.in() ? (m.sender != null ? m.sender : m.contact) : "me";
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the "Edited to" texts it could place from `msgs` and gives their
|
||||
* messages the new text; returns each edited message's texts, oldest first.
|
||||
*/
|
||||
static Map<Long, List<Version>> fold(List<Store.Msg> msgs) {
|
||||
Set<Long> hidden = new HashSet<>();
|
||||
Map<Long, String> current = new HashMap<>();
|
||||
Map<Long, List<Version>> versions = new HashMap<>();
|
||||
for (int i = 0; i < msgs.size(); i++) {
|
||||
Store.Msg m = msgs.get(i);
|
||||
if ("failed".equals(m.status)) continue;
|
||||
String text = parse(m.body);
|
||||
if (text == null) continue;
|
||||
String from = who(m);
|
||||
Store.Msg near = null, late = null;
|
||||
double nearScore = -1, lateScore = -1;
|
||||
int nearCount = 0;
|
||||
for (int j = i - 1; j >= 0 && j >= i - LOOK_BACK; j--) {
|
||||
Store.Msg c = msgs.get(j);
|
||||
if (m.ts - c.ts > LATE_WINDOW) break;
|
||||
if (hidden.contains(c.id) || !Objects.equals(who(c), from) || parse(c.body) != null || Reactions.isTapback(c.body)) continue;
|
||||
String body = current.containsKey(c.id) ? current.get(c.id) : c.body;
|
||||
if (body.trim().isEmpty()) continue;
|
||||
double score = likeness(body, text);
|
||||
if (m.ts - c.ts <= WINDOW) {
|
||||
nearCount++;
|
||||
if (score > nearScore) {
|
||||
nearScore = score;
|
||||
near = c;
|
||||
}
|
||||
} else if (score > lateScore) {
|
||||
lateScore = score;
|
||||
late = c;
|
||||
}
|
||||
}
|
||||
Store.Msg hit = near != null && (nearScore >= LIKENESS || nearCount == 1) ? near : late != null && lateScore >= LATE_LIKENESS ? late : null;
|
||||
if (hit == null) continue;
|
||||
hidden.add(m.id);
|
||||
List<Version> list = versions.get(hit.id);
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
list.add(new Version(hit.body, hit.ts));
|
||||
versions.put(hit.id, list);
|
||||
}
|
||||
list.add(new Version(text, m.ts));
|
||||
current.put(hit.id, text);
|
||||
}
|
||||
if (hidden.isEmpty()) return versions;
|
||||
List<Store.Msg> shown = new ArrayList<>();
|
||||
for (Store.Msg m : msgs) {
|
||||
if (hidden.contains(m.id)) continue;
|
||||
String now = current.get(m.id);
|
||||
if (now != null) m.body = now; // a fresh copy from the database each load
|
||||
shown.add(m);
|
||||
}
|
||||
msgs.clear();
|
||||
msgs.addAll(shown);
|
||||
return versions;
|
||||
}
|
||||
|
||||
/** Edited messages' texts before the current one, for reactions that quote them. */
|
||||
static Map<Long, List<String>> earlier(Map<Long, List<Version>> versions) {
|
||||
Map<Long, List<String>> out = new HashMap<>();
|
||||
for (Map.Entry<Long, List<Version>> e : versions.entrySet()) {
|
||||
List<String> old = new ArrayList<>();
|
||||
List<Version> v = e.getValue();
|
||||
for (int i = 0; i < v.size() - 1; i++) old.add(v.get(i).body);
|
||||
out.put(e.getKey(), old);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user