@estiva-app/conversation 0.40.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +2 -1
- package/dist/builders.d.ts +2 -1
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +2 -1
- package/dist/builders.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/places.d.ts +61 -3
- package/dist/places.d.ts.map +1 -1
- package/dist/places.js +107 -9
- package/dist/places.js.map +1 -1
- package/dist/reactions.d.ts +80 -1
- package/dist/reactions.d.ts.map +1 -1
- package/dist/reactions.js +146 -1
- package/dist/reactions.js.map +1 -1
- package/dist/readState.d.ts +79 -12
- package/dist/readState.d.ts.map +1 -1
- package/dist/readState.js +192 -53
- package/dist/readState.js.map +1 -1
- package/dist/readStateQueue.d.ts.map +1 -1
- package/dist/readStateQueue.js +8 -4
- package/dist/readStateQueue.js.map +1 -1
- package/package.json +3 -3
- package/src/builders.ts +2 -1
- package/src/index.ts +15 -1
- package/src/places.ts +167 -20
- package/src/reactions.ts +144 -2
- package/src/readState.ts +194 -49
- package/src/readStateQueue.ts +7 -3
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* apps: it imports no app's data layer, opens no socket, and takes every
|
|
13
13
|
* environment touch — the relay read, the clock, storage — as a parameter.
|
|
14
14
|
*/
|
|
15
|
-
export const CONVERSATION_VERSION = '0.
|
|
15
|
+
export const CONVERSATION_VERSION = '0.41.0'
|
|
16
16
|
|
|
17
17
|
export {
|
|
18
18
|
KIND_MESSAGE,
|
|
@@ -59,9 +59,15 @@ export {
|
|
|
59
59
|
reactionEventsOf,
|
|
60
60
|
foldReactions,
|
|
61
61
|
groupReactions,
|
|
62
|
+
REACTIONS_TO_ME_FROM,
|
|
63
|
+
reactionsToMeFilter,
|
|
64
|
+
reactionTakeBackFilter,
|
|
65
|
+
keepReactionToMe,
|
|
66
|
+
reactionsToMe,
|
|
62
67
|
type ReactionHorizon,
|
|
63
68
|
type ReactionEvent,
|
|
64
69
|
type Reaction,
|
|
70
|
+
type ReactionToMe,
|
|
65
71
|
} from './reactions.js'
|
|
66
72
|
|
|
67
73
|
export { decorationsOf, type Decoration, type Decorations, type QueryFn } from './decorations.js'
|
|
@@ -166,6 +172,10 @@ export {
|
|
|
166
172
|
whatLitListedFile,
|
|
167
173
|
whatLitStream,
|
|
168
174
|
mergeLitLines,
|
|
175
|
+
reactionStreamOf,
|
|
176
|
+
newReactionsIn,
|
|
177
|
+
reactionsSeenWrites,
|
|
178
|
+
type ReactionJudge,
|
|
169
179
|
type LitLine,
|
|
170
180
|
type LitLineOptions,
|
|
171
181
|
type UnreadReason,
|
|
@@ -218,6 +228,10 @@ export {
|
|
|
218
228
|
messageContext,
|
|
219
229
|
toContextSeconds,
|
|
220
230
|
isPublishableContextId,
|
|
231
|
+
isCarriedContextId,
|
|
232
|
+
REACTIONS_FLOOR_CONTEXT,
|
|
233
|
+
reactionsContext,
|
|
234
|
+
reactionsSeenOf,
|
|
221
235
|
loadSlotIdentity,
|
|
222
236
|
rotateSlotId,
|
|
223
237
|
readStateDTag,
|
package/src/places.ts
CHANGED
|
@@ -25,11 +25,23 @@
|
|
|
25
25
|
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-team-has-members-and-followers-and-a-place-is-followed-at-a-level.md
|
|
26
26
|
*/
|
|
27
27
|
import { fileIdOf, KIND, type SignedEvent, type UnsignedEvent } from '@estiva-app/protocol'
|
|
28
|
-
import { FILE_ADDRESS, FOLDER_ID, KIND_APP_DATA, KIND_CHANGE } from './kinds.js'
|
|
28
|
+
import { FILE_ADDRESS, FOLDER_ID, KIND_APP_DATA, KIND_CHANGE, KIND_COMMENT, KIND_MESSAGE } from './kinds.js'
|
|
29
29
|
import { APPDATA_TAG, FOLLOW_FIELD_PREFIX, buildMembershipChange, memberOfField } from './membership.js'
|
|
30
30
|
import { byOrder } from './order.js'
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
31
|
+
import { REACTIONS_TO_ME_FROM, type ReactionToMe } from './reactions.js'
|
|
32
|
+
import {
|
|
33
|
+
REPLY_FLOOR_CONTEXT,
|
|
34
|
+
belowReplyFloor,
|
|
35
|
+
channelContext,
|
|
36
|
+
effectiveReadAt,
|
|
37
|
+
fileContext,
|
|
38
|
+
fileIdContext,
|
|
39
|
+
reactionsContext,
|
|
40
|
+
reactionsSeenOf,
|
|
41
|
+
streamMarkerOf,
|
|
42
|
+
threadContext,
|
|
43
|
+
} from './readState.js'
|
|
44
|
+
import { anchorIdsOf, anchorsOf, peopleNamedInBody } from './strength.js'
|
|
33
45
|
import { groupThreads } from './threads.js'
|
|
34
46
|
import { lightsIn, seenIn, type StreamJudge } from './unread.js'
|
|
35
47
|
|
|
@@ -693,12 +705,12 @@ function lightsOnListedFile(message: Message, root: string | undefined, judge: P
|
|
|
693
705
|
|
|
694
706
|
/**
|
|
695
707
|
* One line of a stream's list (§11.8 *What lit a stream, new and seen*): a message, with why it lit
|
|
696
|
-
* ({@link UnreadReason}), or a reaction to `P`'s message (ACT-7
|
|
697
|
-
*
|
|
708
|
+
* ({@link UnreadReason}), or a reaction to `P`'s message (ACT-7), new until its conversation's `reactions:` key is
|
|
709
|
+
* written past it. `seen` is false for what is unread now: the New part of the list.
|
|
698
710
|
*/
|
|
699
711
|
export type LitLine<E> =
|
|
700
712
|
| { type: 'message'; message: E; reason: UnreadReason; seen: boolean }
|
|
701
|
-
| { type: 'reaction'; reaction:
|
|
713
|
+
| { type: 'reaction'; reaction: ReactionToMe; seen: boolean }
|
|
702
714
|
|
|
703
715
|
/** Options for a stream's list. */
|
|
704
716
|
export interface LitLineOptions extends UnreadReasonOptions {
|
|
@@ -707,6 +719,12 @@ export interface LitLineOptions extends UnreadReasonOptions {
|
|
|
707
719
|
* it. Each part is newest first, and the new part comes before the seen one. Unbounded when left out.
|
|
708
720
|
*/
|
|
709
721
|
limit?: number
|
|
722
|
+
/**
|
|
723
|
+
* The reactions to `P`'s messages in this stream ({@link reactionsToMe}, mapped to it by {@link reactionStreamOf}):
|
|
724
|
+
* each is a line, new by {@link newReactionsIn}, else seen. A reaction to `P` is always about `P`, so a seen one
|
|
725
|
+
* stays in the list.
|
|
726
|
+
*/
|
|
727
|
+
reactions?: readonly ReactionToMe[]
|
|
710
728
|
}
|
|
711
729
|
|
|
712
730
|
/** What stays in the list once seen: what was about `P` (Miky, 2026-11-11). A plain message read is in its conversation. */
|
|
@@ -717,6 +735,7 @@ function linesOf<E extends Message>(
|
|
|
717
735
|
me: string,
|
|
718
736
|
options: LitLineOptions,
|
|
719
737
|
judge: (message: E, root: string | undefined, inSince: number | undefined) => { lights: boolean; unread: boolean },
|
|
738
|
+
reactions: { merged: Readonly<Record<string, number>>; judge: ReactionJudge },
|
|
720
739
|
): LitLine<E>[] {
|
|
721
740
|
const out: LitLine<E>[] = []
|
|
722
741
|
eachMessage(messages, me, (message, root, inSince) => {
|
|
@@ -726,10 +745,12 @@ function linesOf<E extends Message>(
|
|
|
726
745
|
if (unread || KEPT_ONCE_SEEN.has(reason)) out.push({ type: 'message', message, reason, seen: !unread })
|
|
727
746
|
return false
|
|
728
747
|
})
|
|
729
|
-
return mergeLitLines([out], options)
|
|
748
|
+
return mergeLitLines([out, reactionLines<E>(options.reactions ?? [], reactions.merged, reactions.judge)], options)
|
|
730
749
|
}
|
|
731
750
|
|
|
732
|
-
|
|
751
|
+
/** What a line is ordered and told apart by: its message, or its reaction as an event of that id and time. */
|
|
752
|
+
const lineEvent = <E extends Message>(line: LitLine<E>): Pick<Message, 'id' | 'created_at' | 'tags'> =>
|
|
753
|
+
line.type === 'message' ? line.message : { id: line.reaction.id, created_at: line.reaction.at, tags: [] }
|
|
733
754
|
|
|
734
755
|
/**
|
|
735
756
|
* Several streams' lists as one (§11.8 *What lit a stream, new and seen*): a place's list is its own stream's and each
|
|
@@ -754,10 +775,16 @@ export function mergeLitLines<E extends Message>(lists: readonly (readonly LitLi
|
|
|
754
775
|
* reply in their conversation. The unseen lines are exactly {@link unreadOnPlace}'s; judged by today's following.
|
|
755
776
|
*/
|
|
756
777
|
export function whatLitPlace<E extends Message>(messages: readonly E[], merged: Readonly<Record<string, number>>, judge: PlaceJudge, options: LitLineOptions = {}): LitLine<E>[] {
|
|
757
|
-
return linesOf(
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
778
|
+
return linesOf(
|
|
779
|
+
messages,
|
|
780
|
+
judge.me,
|
|
781
|
+
options,
|
|
782
|
+
(message, root, inSince) => {
|
|
783
|
+
const lights = lightsOnPlace(message, root, judge, inSince)
|
|
784
|
+
return { lights, unread: lights && newerThanItsMarker(message, root, merged, judge, startedOn(message, judge.following)) }
|
|
785
|
+
},
|
|
786
|
+
{ merged, judge: { contexts: [judge.stream], following: judge.following, muted: judge.muted } },
|
|
787
|
+
)
|
|
761
788
|
}
|
|
762
789
|
|
|
763
790
|
/** A listed file's lines on the place that lists it, as {@link whatLitPlace}: the unseen ones are {@link unreadOnListedFile}'s. */
|
|
@@ -768,10 +795,16 @@ export function whatLitListedFile<E extends Message>(
|
|
|
768
795
|
options: LitLineOptions = {},
|
|
769
796
|
): LitLine<E>[] {
|
|
770
797
|
if (!judge.following.in) return []
|
|
771
|
-
return linesOf(
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
798
|
+
return linesOf(
|
|
799
|
+
messages,
|
|
800
|
+
judge.me,
|
|
801
|
+
options,
|
|
802
|
+
(message, root, inSince) => {
|
|
803
|
+
const lights = lightsOnListedFile(message, root, judge, inSince)
|
|
804
|
+
return { lights, unread: lights && newerThanItsMarker(message, root, merged, judge, true) }
|
|
805
|
+
},
|
|
806
|
+
{ merged, judge: { contexts: [judge.stream], following: judge.following, muted: judge.muted } },
|
|
807
|
+
)
|
|
775
808
|
}
|
|
776
809
|
|
|
777
810
|
/**
|
|
@@ -779,10 +812,124 @@ export function whatLitListedFile<E extends Message>(
|
|
|
779
812
|
* unseen ones are {@link unreadIn}'s.
|
|
780
813
|
*/
|
|
781
814
|
export function whatLitStream<E extends Message>(messages: readonly E[], merged: Readonly<Record<string, number>>, judge: StreamJudge, options: LitLineOptions = {}): LitLine<E>[] {
|
|
782
|
-
return linesOf(
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
815
|
+
return linesOf(
|
|
816
|
+
messages,
|
|
817
|
+
judge.me,
|
|
818
|
+
options,
|
|
819
|
+
(message, root) => {
|
|
820
|
+
const lights = lightsIn(message, judge)
|
|
821
|
+
return { lights, unread: lights && !seenIn(message, root, merged, judge) }
|
|
822
|
+
},
|
|
823
|
+
// Not a member: no reaction lights, as no message but a mention does.
|
|
824
|
+
{ merged, judge: { contexts: [judge.stream], following: null, muted: judge.muted || !judge.member, since: judge.since } },
|
|
825
|
+
)
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// ── A reaction to your message — §11.8, ACT-7 ───────────────────────────────
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* The §11.1 contexts of the stream a reacted message is in, where its reactions are seen: a reaction on a reply counts
|
|
832
|
+
* in its root's stream. A chat message (`kind:9`, root or reply) is in its channel's general stream, the bare uuid of
|
|
833
|
+
* its `h`. A comment (`kind:1111`, root or reply) is in its file's stream: the address its thread is about and
|
|
834
|
+
* `file:<d>`, both where the comment or the address gives them. Empty for anything else, and for a comment §6.4
|
|
835
|
+
* ignores.
|
|
836
|
+
*
|
|
837
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
838
|
+
*/
|
|
839
|
+
export function reactionStreamOf(target: Pick<SignedEvent, 'kind' | 'tags'>): string[] {
|
|
840
|
+
if (target.kind === KIND_MESSAGE) {
|
|
841
|
+
const channel = channelContext(tag(target, 'h'))
|
|
842
|
+
return channel === undefined ? [] : [channel]
|
|
843
|
+
}
|
|
844
|
+
if (target.kind !== KIND_COMMENT) return []
|
|
845
|
+
const out = new Set<string>()
|
|
846
|
+
for (const address of anchorsOf(target)) {
|
|
847
|
+
const context = fileContext(address)
|
|
848
|
+
if (context === undefined) continue
|
|
849
|
+
out.add(context)
|
|
850
|
+
const byId = fileIdContext(fileIdOf(address))
|
|
851
|
+
if (byId !== undefined) out.add(byId)
|
|
852
|
+
}
|
|
853
|
+
for (const id of anchorIdsOf(target)) {
|
|
854
|
+
const byId = fileIdContext(id)
|
|
855
|
+
if (byId !== undefined) out.add(byId)
|
|
856
|
+
}
|
|
857
|
+
return [...out]
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/** Who is judging the reactions in one conversation stream, and on what terms. */
|
|
861
|
+
export interface ReactionJudge {
|
|
862
|
+
/** The stream's §11.1 contexts ({@link reactionStreamOf}): a file named both ways passes both. */
|
|
863
|
+
contexts: readonly string[]
|
|
864
|
+
/** {@link followingOf} for `P` on the place the stream is on or listed by; `null` for a DM or a stream `P` is a member of. */
|
|
865
|
+
following: Following | null
|
|
866
|
+
/** Whether `P` muted the place (or the stream): a reaction is not a mention, so nothing lights. */
|
|
867
|
+
muted?: boolean
|
|
868
|
+
/** For a DM or a member stream, unix seconds `P` came in; with a `following`, the later of this and its `since` counts. */
|
|
869
|
+
since?: number
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/** When a reaction in the stream starts to count for `P`: the later of `P`'s start and {@link REACTIONS_TO_ME_FROM}; undefined when nothing does. */
|
|
873
|
+
function reactionsStartOf(judge: ReactionJudge): number | undefined {
|
|
874
|
+
if (judge.muted) return undefined
|
|
875
|
+
if (judge.following !== null && !judge.following.in) return undefined
|
|
876
|
+
return Math.max(judge.following?.since ?? 0, judge.since ?? 0, REACTIONS_TO_ME_FROM)
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function reactionLines<E>(reactions: readonly ReactionToMe[], merged: Readonly<Record<string, number>>, judge: ReactionJudge): LitLine<E>[] {
|
|
880
|
+
const start = reactionsStartOf(judge)
|
|
881
|
+
if (start === undefined) return []
|
|
882
|
+
const seenTo = reactionsSeenOf(merged, judge.contexts)
|
|
883
|
+
return reactions
|
|
884
|
+
.filter((reaction) => reaction.at >= start)
|
|
885
|
+
.map((reaction): LitLine<E> => ({ type: 'reaction', reaction, seen: reaction.at <= seenTo }))
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* The reactions to `P`'s messages in one conversation stream that are new (§11.8, ACT-7): `reactions` are already
|
|
890
|
+
* mapped to the stream ({@link reactionStreamOf}); each is new when it is at or after `P`'s start there (following's
|
|
891
|
+
* `since`, or a DM's or a member stream's) and {@link REACTIONS_TO_ME_FROM}, and later than the stream's `reactions:`
|
|
892
|
+
* key and the reactions floor ({@link reactionsSeenOf}). Not in the place, or a muted one: none. Newest first, as given.
|
|
893
|
+
*
|
|
894
|
+
* Nothing but this conversation's `reactions:` key and the reactions floor clears one: opening the conversation reads
|
|
895
|
+
* its messages, and only writing its {@link reactionsSeenWrites} sees its reactions.
|
|
896
|
+
*
|
|
897
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
898
|
+
*/
|
|
899
|
+
export function newReactionsIn(reactions: readonly ReactionToMe[], merged: Readonly<Record<string, number>>, judge: ReactionJudge): ReactionToMe[] {
|
|
900
|
+
return reactionLines<never>(reactions, merged, judge).flatMap((line) => (line.type === 'reaction' && !line.seen ? [line.reaction] : []))
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* What showing reactions to `P` writes (ACT-7): for each conversation, the `reactions:` key at the newest reaction
|
|
905
|
+
* shown — its `created_at`, never the clock, so a reaction that arrives meanwhile stays new — and never later than
|
|
906
|
+
* `nowMs`, so a reaction dated ahead of the clock cannot mark later ones seen. Written under the stream's write
|
|
907
|
+
* context: `file:<d>` for a file named by its id, else its address; the bare uuid for a chat. `contexts` is the
|
|
908
|
+
* reaction's stream ({@link reactionStreamOf}). Each is ready for the read-state queue: `markRead([context], atMs)`.
|
|
909
|
+
* Resting on a row whose card listed them, and opening a conversation that draws them, both write it.
|
|
910
|
+
*
|
|
911
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
912
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/reactions-seen-is-kept-under-reactions-and-opening-it-sees-it.md
|
|
913
|
+
*/
|
|
914
|
+
export function reactionsSeenWrites(
|
|
915
|
+
lines: readonly { reaction: ReactionToMe; contexts: readonly string[] }[],
|
|
916
|
+
nowMs: number = Date.now(),
|
|
917
|
+
): { context: string; atMs: number }[] {
|
|
918
|
+
const newest = new Map<string, number>()
|
|
919
|
+
for (const { reaction, contexts } of lines) {
|
|
920
|
+
const write = writeContextOf(contexts)
|
|
921
|
+
if (write === undefined) continue
|
|
922
|
+
const key = reactionsContext(write)
|
|
923
|
+
const atMs = Math.min(reaction.at * 1000, nowMs)
|
|
924
|
+
if ((newest.get(key) ?? -1) < atMs) newest.set(key, atMs)
|
|
925
|
+
}
|
|
926
|
+
return [...newest].map(([context, atMs]) => ({ context, atMs }))
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/** Where a stream's reactions are written: its `file:<d>` (named, or from an address whose `d` is an id), else its address or channel. */
|
|
930
|
+
function writeContextOf(contexts: readonly string[]): string | undefined {
|
|
931
|
+
const byId = contexts.find((c) => c.startsWith('file:')) ?? contexts.map((c) => fileIdContext(FILE_ADDRESS.test(c) ? fileIdOf(c) : undefined)).find((c) => c !== undefined)
|
|
932
|
+
return byId ?? contexts[0]
|
|
786
933
|
}
|
|
787
934
|
|
|
788
935
|
/**
|
package/src/reactions.ts
CHANGED
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* - **Order** is presentation. This keeps first-reaction order; an app MAY
|
|
20
20
|
* sort by count, and two apps that differ there disagree about nothing.
|
|
21
21
|
*/
|
|
22
|
-
import type
|
|
23
|
-
import { isEventId, KIND_REACTION } from './kinds.js'
|
|
22
|
+
import { effectiveAuthor, type SignedEvent } from '@estiva-app/protocol'
|
|
23
|
+
import { isConversationKind, isEventId, KIND_DELETION, KIND_REACTION } from './kinds.js'
|
|
24
24
|
import { byOrder } from './order.js'
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -149,3 +149,145 @@ export function groupReactions(reactions: readonly ReactionEvent[], viewer?: str
|
|
|
149
149
|
}
|
|
150
150
|
return out
|
|
151
151
|
}
|
|
152
|
+
|
|
153
|
+
// ── The reactions to a person — §6.6 *Finding the reactions to a person* ────
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* When the reactions to `P`'s messages start counting, unix seconds:
|
|
157
|
+
* 2026-11-13T17:00:00Z, just after every reaction began naming whose message
|
|
158
|
+
* it reacts to (ACT-6). One suite constant, set once: an earlier reaction
|
|
159
|
+
* carries no name, so it is never found, and every reader must agree on where
|
|
160
|
+
* orange starts.
|
|
161
|
+
*
|
|
162
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-names-whose-message-it-reacts-to.md
|
|
163
|
+
*/
|
|
164
|
+
export const REACTIONS_TO_ME_FROM = 1794589200
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The read that answers "the reactions to `me`'s messages" (§6.6), stored and
|
|
168
|
+
* kept open: `{kinds:[7], "#p":[me], since}`, never earlier than
|
|
169
|
+
* {@link REACTIONS_TO_ME_FROM}.
|
|
170
|
+
*/
|
|
171
|
+
export function reactionsToMeFilter(me: string, since?: number): { kinds: number[]; '#p': string[]; since: number } {
|
|
172
|
+
return { kinds: [KIND_REACTION], '#p': [me], since: Math.max(since ?? REACTIONS_TO_ME_FROM, REACTIONS_TO_ME_FROM) }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The deletions that take a reaction to `me` back, or delete the message it
|
|
177
|
+
* reacted to later: `{kinds:[5], "#e":[…]}` over the reactions drawn **and**
|
|
178
|
+
* their targets, kept open. What it returns goes in {@link reactionsToMe}'s
|
|
179
|
+
* `deletions`. `null` when there is nothing to watch: skip the request, since
|
|
180
|
+
* an empty `#e` asks a relay for nothing or for everything.
|
|
181
|
+
*/
|
|
182
|
+
export function reactionTakeBackFilter(ids: readonly string[], since?: number): { kinds: number[]; '#e': string[]; since?: number } | null {
|
|
183
|
+
if (ids.length === 0) return null
|
|
184
|
+
return { kinds: [KIND_DELETION], '#e': [...new Set(ids)], ...(since !== undefined ? { since } : {}) }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** How far ahead of the reader's clock a reaction may be dated and still count, in seconds. */
|
|
188
|
+
const CLOCK_SKEW_S = 60
|
|
189
|
+
|
|
190
|
+
/** One reaction to `me`'s message, kept by §6.6. */
|
|
191
|
+
export interface ReactionToMe {
|
|
192
|
+
/** The reaction's event id. */
|
|
193
|
+
id: string
|
|
194
|
+
/** Who reacted: the reaction's effective author. */
|
|
195
|
+
by: string
|
|
196
|
+
/** Its `created_at`, unix seconds. */
|
|
197
|
+
at: number
|
|
198
|
+
/** {@link reactionEmojiOf}. */
|
|
199
|
+
emoji: string
|
|
200
|
+
/** The id of the message it reacts to. */
|
|
201
|
+
target: string
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
type Tagged = Pick<SignedEvent, 'pubkey' | 'kind' | 'tags' | 'created_at'>
|
|
205
|
+
|
|
206
|
+
const lastP = (event: Pick<SignedEvent, 'tags'>): string | undefined => {
|
|
207
|
+
for (let i = event.tags.length - 1; i >= 0; i--) if (event.tags[i][0] === 'p') return event.tags[i][1]
|
|
208
|
+
return undefined
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether a reader keeps a reaction as one to `me`'s message (§6.6, exactly):
|
|
213
|
+
*
|
|
214
|
+
* - its **last** `p` is `me`;
|
|
215
|
+
* - its target (the last 64-hex `e`) is held, is a message or a comment
|
|
216
|
+
* (`kind:9` or `1111`), and `me` is its effective author, worked out with the
|
|
217
|
+
* relay's key — so a reaction that names `me` falsely, or beside someone
|
|
218
|
+
* else, keeps nothing, and without the key a message the relay signed on
|
|
219
|
+
* `me`'s behalf is the relay's;
|
|
220
|
+
* - `me` is not the reaction's effective author;
|
|
221
|
+
* - it is from {@link REACTIONS_TO_ME_FROM} on, and not more than 60 s ahead of
|
|
222
|
+
* `nowMs`: one dated in the future would stay orange past every "seen"
|
|
223
|
+
* written at the time it was shown.
|
|
224
|
+
*
|
|
225
|
+
* `target` is the event the reader fetched by id; undefined when it is deleted
|
|
226
|
+
* or unreadable, which keeps nothing.
|
|
227
|
+
*/
|
|
228
|
+
export function keepReactionToMe(
|
|
229
|
+
reaction: Tagged & Pick<SignedEvent, 'id'>,
|
|
230
|
+
target: (Tagged & Pick<SignedEvent, 'id'>) | undefined,
|
|
231
|
+
me: string,
|
|
232
|
+
relayKey?: string | null,
|
|
233
|
+
nowMs: number = Date.now(),
|
|
234
|
+
): boolean {
|
|
235
|
+
if (reaction.kind !== KIND_REACTION || reaction.created_at < REACTIONS_TO_ME_FROM) return false
|
|
236
|
+
if (reaction.created_at > Math.floor(nowMs / 1000) + CLOCK_SKEW_S) return false
|
|
237
|
+
if (lastP(reaction) !== me) return false
|
|
238
|
+
if (!target || reactionTargetOf(reaction) !== target.id.toLowerCase()) return false
|
|
239
|
+
if (!isConversationKind(target.kind)) return false
|
|
240
|
+
if (effectiveAuthor(target, relayKey) !== me) return false
|
|
241
|
+
return effectiveAuthor(reaction, relayKey) !== me
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The reactions to `me`'s messages, kept by {@link keepReactionToMe}, newest
|
|
246
|
+
* first, one per `(by, target, emoji)` (the earliest, as §6.6 counts).
|
|
247
|
+
* `deletions` are the `kind:5` events read with {@link reactionTakeBackFilter}:
|
|
248
|
+
* one signed by a reaction's author takes that reaction back, and one signed by
|
|
249
|
+
* a target's effective author deletes that message, so none of its reactions
|
|
250
|
+
* lights. A `kind:5` by anybody else removes nothing. `targetsById` holds the
|
|
251
|
+
* targets the reader has, by lowercase id.
|
|
252
|
+
*/
|
|
253
|
+
export function reactionsToMe(
|
|
254
|
+
reactions: readonly SignedEvent[],
|
|
255
|
+
targetsById: ReadonlyMap<string, Tagged & Pick<SignedEvent, 'id'>>,
|
|
256
|
+
me: string,
|
|
257
|
+
opts: { relayKey?: string | null; deletions?: readonly SignedEvent[]; nowMs?: number } = {},
|
|
258
|
+
): ReactionToMe[] {
|
|
259
|
+
const { relayKey, nowMs = Date.now() } = opts
|
|
260
|
+
// Event id → who deleted it, by effective author, so each removal is checked against the author of what it names.
|
|
261
|
+
const deletedBy = new Map<string, Set<string>>()
|
|
262
|
+
for (const deletion of opts.deletions ?? []) {
|
|
263
|
+
if (deletion.kind !== KIND_DELETION) continue
|
|
264
|
+
const signer = effectiveAuthor(deletion, relayKey)
|
|
265
|
+
for (const t of deletion.tags) {
|
|
266
|
+
if (t[0] !== 'e' || !isEventId(t[1])) continue
|
|
267
|
+
const id = t[1].toLowerCase()
|
|
268
|
+
const by = deletedBy.get(id) ?? new Set<string>()
|
|
269
|
+
by.add(signer)
|
|
270
|
+
deletedBy.set(id, by)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const deleted = (event: Tagged & Pick<SignedEvent, 'id'>) => deletedBy.get(event.id.toLowerCase())?.has(effectiveAuthor(event, relayKey)) ?? false
|
|
274
|
+
const out: ReactionToMe[] = []
|
|
275
|
+
const counted = new Set<string>()
|
|
276
|
+
const seen = new Set<string>()
|
|
277
|
+
for (const reaction of [...reactions].sort(byOrder)) {
|
|
278
|
+
if (seen.has(reaction.id)) continue
|
|
279
|
+
seen.add(reaction.id)
|
|
280
|
+
if (deleted(reaction)) continue
|
|
281
|
+
const target = reactionTargetOf(reaction)
|
|
282
|
+
const held = target === undefined ? undefined : targetsById.get(target)
|
|
283
|
+
if (target === undefined || (held !== undefined && deleted(held))) continue
|
|
284
|
+
if (!keepReactionToMe(reaction, held, me, relayKey, nowMs)) continue
|
|
285
|
+
const by = effectiveAuthor(reaction, relayKey)
|
|
286
|
+
const emoji = reactionEmojiOf(reaction)
|
|
287
|
+
const key = JSON.stringify([by, target, emoji])
|
|
288
|
+
if (counted.has(key)) continue
|
|
289
|
+
counted.add(key)
|
|
290
|
+
out.push({ id: reaction.id, by, at: reaction.created_at, emoji, target })
|
|
291
|
+
}
|
|
292
|
+
return out.reverse()
|
|
293
|
+
}
|