Repository navigation
Expand file tree
/
Copy pathTapbackActions.mjs
More file actions
123 lines (123 loc) · 3.9 KB
/
Copy pathTapbackActions.mjs
File metadata and controls
123 lines (123 loc) · 3.9 KB
1
2
3
4
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// tapback-actions.ts
var TAPBACKS = [
{ kind: "love", emoji: "❤️" },
{ kind: "like", emoji: "\uD83D\uDC4D" },
{ kind: "dislike", emoji: "\uD83D\uDC4E" },
{ kind: "laugh", emoji: "\uD83D\uDE02" },
{ kind: "emphasize", emoji: "‼️" },
{ kind: "question", emoji: "❓" }
];
function tapbackForKey(text) {
const i = ["1", "2", "3", "4", "5", "6"].indexOf(String(text || ""));
return i >= 0 ? TAPBACKS[i].kind : "";
}
function initialTapbackCursor(current, keyboard) {
const i = TAPBACKS.findIndex((t) => t.kind === current);
return i >= 0 ? i : keyboard ? 0 : -1;
}
function moveTapbackCursor(cursor, dir, n = TAPBACKS.length) {
if (n <= 0)
return -1;
if (cursor < 0 || cursor >= n)
return dir > 0 ? 0 : n - 1;
return (cursor + (dir > 0 ? 1 : n - 1)) % n;
}
function tapbacksOn(conf) {
let v = "";
for (const raw of String(conf || "").split(`
`)) {
const line = raw.replace(/#.*/, "").replace(/\s+/g, "");
const eq = line.indexOf("=");
if (eq < 0 || line.slice(0, eq) !== "tapbacks")
continue;
v = line.slice(eq + 1).replace(/^"/, "").replace(/"$/, "").replace(/^'/, "").replace(/'$/, "");
}
return ["on", "yes", "true", "1"].includes(v.toLowerCase());
}
var LINK = /:\/\/|\bwww\./i;
function tapbackKind(emoji) {
const bare = (s) => String(s || "").replace(/️/g, "");
const hit = TAPBACKS.find((t) => bare(t.emoji) === bare(emoji));
return hit ? hit.kind : "";
}
function myTapback(message) {
for (const t of message?.tapbacks ?? []) {
if (t.from_me) {
const kind = tapbackKind(t.emoji);
if (kind)
return kind;
}
}
return "";
}
function canTapback(message, group) {
if (!message || group)
return false;
if (!String(message.guid || ""))
return false;
if (message.retracted || message.audio || message.pending || message.failed || message.scheduled)
return false;
if ((message.attachments ?? []).length > 0 || message.link)
return false;
const text = String(message.text || "").trim();
return text !== "" && !LINK.test(text);
}
function tapbackArgs(guid, kind, current) {
return ["--guid", guid, kind, ...current === kind ? ["--remove"] : [], "--yes"];
}
var SETTLE_TRIES = 8;
function pendingTapback(chat, guid, kind, current) {
return { chat, guid, kind, remove: current === kind, done: false, tries: 0 };
}
function tapbackSettled(message, p) {
return !!p && myTapback(message) === (p.remove ? "" : p.kind);
}
function shownTapbacks(message, p) {
const list = message?.tapbacks ?? [];
if (!message || !p || String(message.guid || "") !== p.guid || tapbackSettled(message, p))
return list;
const hit = TAPBACKS.find((t) => t.kind === p.kind);
if (!hit)
return list;
const others = list.filter((t) => !(t.from_me && tapbackKind(t.emoji)));
return [...others, { emoji: hit.emoji, from_me: true, pending: true }];
}
function pendingAfterLoad(p, chat, bubbles, startedAfterDone) {
if (!p || p.chat !== chat)
return p;
const target = bubbles.find((b) => String(b.guid || "") === p.guid) ?? null;
if (target && tapbackSettled(target, p))
return null;
if (!startedAfterDone)
return p;
if (!target || p.tries + 1 >= SETTLE_TRIES)
return null;
return Object.assign({}, p, { tries: p.tries + 1 });
}
var TAPBACK_NOT_STARTED = "tapback: imsg-react did not start; re-run blip-setup";
function tapbackFailure(code, stderr) {
if (code === 69 || code === 255)
return "tapback: Mac unreachable";
const line = String(stderr || "").split(`
`).map((l) => l.trim()).filter((l) => l !== "").pop() || "";
const why = line.replace(/^imsg-react:\s*/, "").slice(0, 200);
return "tapback: " + (why || "imsg-react exit " + code);
}
export {
SETTLE_TRIES,
TAPBACKS,
TAPBACK_NOT_STARTED,
canTapback,
initialTapbackCursor,
moveTapbackCursor,
myTapback,
pendingAfterLoad,
pendingTapback,
shownTapbacks,
tapbackArgs,
tapbackFailure,
tapbackForKey,
tapbackKind,
tapbackSettled,
tapbacksOn
};