Last active
September 5, 2024 14:53
-
-
Save Memory-of-Snow/5089f4d7f29b7af45d812bb5cb790cfe to your computer and use it in GitHub Desktop.
演習コードの改造案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* テキスト投稿イベント(リプライ)を組み立てる | |
* @param {string} content 投稿内容 | |
* @param {import("nostr-tools").Event} targetEvent リプライ対象のイベント | |
*/ | |
const composeReplyPost = (content, targetEvent) => { | |
/* Q-1: これまで学んだことを思い出しながら、 | |
リプライを表現するイベントを組み立てよう */ | |
//A-1.ここから | |
//タグの配列の中から、長さが4つで、最初が"e"、4番目に"root"が含まれる配列を探す | |
const targetEventRootTag = targetEvent.tags.filter(subArray => subArray.length === 4 && subArray[0]==="e" && subArray[3] === "root" ); | |
let tagsElement; | |
if(targetEventRootTag.length == 0){ | |
//rootのないReplyの場合、rootタグはreplyされたpostのid、replyタグはなし | |
tagsElement=[ | |
["e", targetEvent.id, "","root"], | |
["p", targetEvent.pubkey, ""] | |
]; | |
} else{ | |
//rootのあるReplyの場合、rootタグはコピー、replayタグに対象のid | |
tagsElement = [ | |
targetEventRootTag[0], | |
["e", targetEvent.id, "","reply"], | |
["p", targetEvent.pubkey, ""] | |
]; | |
} | |
const ev = { | |
kind: 1, | |
content: content, | |
tags: tagsElement, | |
created_at: currUnixtime(), | |
}; | |
// イベントID(ハッシュ値)計算・署名 | |
return finishEvent(ev, BOT_PRIVATE_KEY_HEX); | |
//A-1.ここまで | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment