1
0
mirror of git://jb55.com/damus synced 2024-09-29 16:30:44 +00:00

notes: count words in notes during artifact parsing

This commit is contained in:
William Casarin 2023-06-01 11:45:04 -07:00
parent fe077fa5c2
commit 6ca9bda01e
6 changed files with 28 additions and 13 deletions

View File

@ -46,6 +46,7 @@ typedef struct note_block {
} block_t;
typedef struct note_blocks {
int words;
int num_blocks;
struct note_block *blocks;
} blocks_t;

View File

@ -216,6 +216,7 @@ int damus_parse_content(struct note_blocks *blocks, const char *content) {
struct note_block block;
u8 *start, *pre_mention;
blocks->words = 0;
blocks->num_blocks = 0;
make_cursor((u8*)content, (u8*)content + strlen(content), &cur);
@ -224,6 +225,11 @@ int damus_parse_content(struct note_blocks *blocks, const char *content) {
cp = peek_char(&cur, -1);
c = peek_char(&cur, 0);
// new word
if (is_whitespace(cp) && !is_whitespace(c)) {
blocks->words++;
}
pre_mention = cur.p;
if (cp == -1 || is_whitespace(cp) || c == '#') {
if (c == '#' && (parse_mention_index(&cur, &block) || parse_hashtag(&cur, &block))) {

View File

@ -1170,7 +1170,7 @@ func process_local_notification(damus_state: DamusState, event ev: NostrEvent) {
}
if type == .text && damus_state.settings.mention_notification {
let blocks = ev.blocks(damus_state.keypair.privkey)
let blocks = ev.blocks(damus_state.keypair.privkey).blocks
for case .mention(let mention) in blocks where mention.ref.ref_id == damus_state.keypair.pubkey {
let content = NSAttributedString(render_note_content(ev: ev, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey).content.attributed).string

View File

@ -150,7 +150,12 @@ func render_blocks(blocks: [Block]) -> String {
}
}
func parse_mentions(content: String, tags: [[String]]) -> [Block] {
struct Blocks {
let words: Int
let blocks: [Block]
}
func parse_mentions(content: String, tags: [[String]]) -> Blocks {
var out: [Block] = []
var bs = note_blocks()
@ -174,9 +179,10 @@ func parse_mentions(content: String, tags: [[String]]) -> [Block] {
i += 1
}
let words = Int(bs.words)
blocks_free(&bs)
return out
return Blocks(words: words, blocks: out)
}
func strblock_to_string(_ s: str_block_t) -> String? {

View File

@ -83,8 +83,8 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
return calculate_event_id(ev: self) == self.id
}
private var _blocks: [Block]? = nil
func blocks(_ privkey: String?) -> [Block] {
private var _blocks: Blocks? = nil
func blocks(_ privkey: String?) -> Blocks {
if let bs = _blocks {
return bs
}
@ -93,7 +93,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
return blocks
}
func get_blocks(content: String) -> [Block] {
func get_blocks(content: String) -> Blocks {
return parse_mentions(content: content, tags: self.tags)
}
@ -118,7 +118,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
if let rs = _event_refs {
return rs
}
let refs = interpret_event_refs(blocks: self.blocks(privkey), tags: self.tags)
let refs = interpret_event_refs(blocks: self.blocks(privkey).blocks, tags: self.tags)
self._event_refs = refs
return refs
}
@ -232,7 +232,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
func note_language(_ privkey: String?) -> String? {
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer.
let originalBlocks = blocks(privkey)
let originalBlocks = blocks(privkey).blocks
let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ")
// Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate.
@ -942,7 +942,7 @@ func last_etag(tags: [[String]]) -> String? {
}
func first_eref_mention(ev: NostrEvent, privkey: String?) -> Mention? {
let blocks = ev.blocks(privkey).filter { block in
let blocks = ev.blocks(privkey).blocks.filter { block in
guard case .mention(let mention) = block else {
return false
}

View File

@ -188,7 +188,7 @@ struct NoteContentView: View {
.onReceive(handle_notify(.profile_updated)) { notif in
let profile = notif.object as! ProfileUpdate
let blocks = event.blocks(damus_state.keypair.privkey)
for block in blocks {
for block in blocks.blocks {
switch block {
case .mention(let m):
if m.type == .pubkey && m.ref.ref_id == profile.pubkey {
@ -261,6 +261,7 @@ struct NoteArtifacts: Equatable {
}
let content: CompatibleText
let words: Int
let urls: [UrlType]
let invoices: [Invoice]
@ -278,7 +279,7 @@ struct NoteArtifacts: Equatable {
static func just_content(_ content: String) -> NoteArtifacts {
let txt = CompatibleText(attributed: AttributedString(stringLiteral: content))
return NoteArtifacts(content: txt, urls: [], invoices: [])
return NoteArtifacts(content: txt, words: 0, urls: [], invoices: [])
}
}
@ -313,9 +314,10 @@ func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?) -
return render_blocks(blocks: blocks, profiles: profiles)
}
func render_blocks(blocks: [Block], profiles: Profiles) -> NoteArtifacts {
func render_blocks(blocks bs: Blocks, profiles: Profiles) -> NoteArtifacts {
var invoices: [Invoice] = []
var urls: [UrlType] = []
let blocks = bs.blocks
let one_note_ref = blocks
.filter({ $0.is_note_mention })
@ -369,7 +371,7 @@ func render_blocks(blocks: [Block], profiles: Profiles) -> NoteArtifacts {
}
}
return NoteArtifacts(content: txt, urls: urls, invoices: invoices)
return NoteArtifacts(content: txt, words: bs.words, urls: urls, invoices: invoices)
}
enum MediaUrl {