Skip to content

Commit

Permalink
Show previous commenters
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Jan 29, 2025
1 parent db96adb commit 560726a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,22 @@ export function getComments(identity) {
);
}

export function getThreadParticipants(submissionId, lastCommentTimestamp) {
const participants = db
.prepare(
`
SELECT DISTINCT identity, timestamp
FROM comments
WHERE submission_id = ?
AND timestamp < ?
ORDER BY timestamp ASC
`,
)
.all(submissionId, lastCommentTimestamp);

return participants;
}

export function getLastComment(submissionId) {
const lastComment = db
.prepare(
Expand All @@ -589,11 +605,18 @@ export function getLastComment(submissionId) {
const { id, submission_id, ...rest } = lastComment;
const [, index] = id.split("0x");

// Get previous participants in the thread
const previousParticipants = getThreadParticipants(
submission_id,
lastComment.timestamp,
);

return {
...rest,
submissionId: submission_id,
index,
type: "comment",
previousParticipants,
};
}

Expand Down
33 changes: 33 additions & 0 deletions src/views/components/row.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,39 @@ const row = (
style="width: 100%; display: flex; pointer-events: none;"
>
<div style="width:90%;">
${story.lastComment.previousParticipants &&
story.lastComment.previousParticipants.length > 0 &&
html`
<div
style="opacity: 0.7; margin-bottom: 8px; display: flex; align-items: center;"
>
<div
style="margin-left: ${story.lastComment
.previousParticipants.length === 1
? "29px"
: "44px"};
display: inline-flex; position:relative;"
>
${story.lastComment.previousParticipants.map(
(participant, index) => html`
<img
loading="lazy"
src="${DOMPurify.sanitize(
participant.safeAvatar,
)}"
alt="previous participant"
style="z-index: ${index}; width: ${size}px; height: ${size}px; border: 1px solid #828282; border-radius: 2px; margin-left: -${2 *
size +
5}px;"
/>
`,
)}
</div>
<span style="font-size: 9pt; color: #666;">
Previous in thread
</span>
</div>
`}
<div
style="display: flex; align-items: center; gap: 5px; margin-bottom: 3px;"
>
Expand Down
20 changes: 20 additions & 0 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,26 @@ export async function index(
const lastComment = getLastComment(`kiwi:0x${story.index}`);
if (lastComment && lastComment.identity) {
lastComment.identity = await ens.resolve(lastComment.identity);
const uniqueIdentities = new Set(
lastComment.previousParticipants
.map((p) => p.identity)
.filter((identity) => identity !== lastComment.identity),
);

const resolvedParticipants = await Promise.allSettled(
[...uniqueIdentities].map((identity) => ens.resolve(identity)),
);

lastComment.previousParticipants = resolvedParticipants
.filter(
(result) =>
result.status === "fulfilled" && result.value.safeAvatar,
)
.map((result) => ({
identity: result.value.identity,
safeAvatar: result.value.safeAvatar,
displayName: result.value.displayName,
}));
}

const isOriginal = Object.keys(writers).some(
Expand Down
19 changes: 19 additions & 0 deletions src/views/new.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ export async function recompute() {
const lastComment = getLastComment(`kiwi:0x${story.index}`);
if (lastComment && lastComment.identity) {
lastComment.identity = await ens.resolve(lastComment.identity);
const uniqueIdentities = new Set(
lastComment.previousParticipants
.map((p) => p.identity)
.filter((identity) => identity !== lastComment.identity),
);

const resolvedParticipants = await Promise.allSettled(
[...uniqueIdentities].map((identity) => ens.resolve(identity)),
);

lastComment.previousParticipants = resolvedParticipants
.filter(
(result) => result.status === "fulfilled" && result.value.safeAvatar,
)
.map((result) => ({
identity: result.value.identity,
safeAvatar: result.value.safeAvatar,
displayName: result.value.displayName,
}));
}

const ensData = await ens.resolve(story.identity);
Expand Down

0 comments on commit 560726a

Please sign in to comment.