@@ -7,21 +7,21 @@ def initialize(author:)
77 @author = author
88 end
99
10- def create! ( topic :, message : nil , body :)
10+ def create! ( topic :, message : nil , body :, tags : [ ] , mention_names : [ ] )
1111 raise Error , "You must be signed in to add a note" unless author
1212 body_text = body . to_s . strip
1313 raise Error , "Note cannot be blank" if body_text . blank?
1414
1515 ActiveRecord ::Base . transaction do
1616 note = Note . new ( topic :, message :, author :, last_editor : author , body : body_text )
1717 note . save!
18- mentionables , tags = rebuild_mentions_and_tags! ( note , body_text )
18+ mentionables , tags = rebuild_mentions_and_tags! ( note , tags : , mention_names : )
1919 fan_out! ( note :, mentionables :, mark_author_read : true )
2020 note
2121 end
2222 end
2323
24- def update! ( note :, body :)
24+ def update! ( note :, body :, tags : [ ] , mention_names : [ ] )
2525 raise Error , "You do not have permission to edit this note" unless note . author_id == author . id
2626 body_text = body . to_s . strip
2727 raise Error , "Note cannot be blank" if body_text . blank?
@@ -30,7 +30,7 @@ def update!(note:, body:)
3030 note . note_edits . create! ( editor : author , body : note . body ) if note . body != body_text
3131 note . update! ( body : body_text , last_editor : author )
3232
33- mentionables , _tags = rebuild_mentions_and_tags! ( note , body_text )
33+ mentionables , _tags = rebuild_mentions_and_tags! ( note , tags : , mention_names : )
3434 fan_out! ( note :, mentionables :, mark_author_read : false )
3535 note
3636 end
@@ -40,38 +40,37 @@ def update!(note:, body:)
4040
4141 attr_reader :author
4242
43- def rebuild_mentions_and_tags! ( note , body_text )
44- mention_names = extract_mentions ( body_text )
45- mentionables = resolve_mentions ( mention_names )
46- tags = extract_tags ( body_text )
43+ def rebuild_mentions_and_tags! ( note , tags : , mention_names : )
44+ normalized_mentions = normalize_mentions ( mention_names )
45+ mentionables = resolve_mentions ( normalized_mentions )
46+ normalized_tags = normalize_tags ( tags )
4747
4848 note . note_mentions . delete_all
4949 mentionables . each do |mentionable |
5050 note . note_mentions . create! ( mentionable :)
5151 end
5252
5353 note . note_tags . delete_all
54- tags . each do |tag |
54+ normalized_tags . each do |tag |
5555 note . note_tags . create! ( tag :)
5656 end
5757
58- [ mentionables , tags ]
58+ [ mentionables , normalized_tags ]
5959 end
6060
61- def extract_mentions ( text )
62- text . to_s . scan ( /(?:^|[^@ \w ])@([A-Za-z0-9_.-]+)/ )
63- . flatten
64- . map { |m | NameReservation . normalize ( m ) }
65- . reject ( &:blank? )
66- . uniq
61+ def normalize_mentions ( mention_names )
62+ Array ( mention_names )
63+ . map { | m | m . to_s . delete_prefix ( "@" ) }
64+ . map { |m | NameReservation . normalize ( m ) }
65+ . reject ( &:blank? )
66+ . uniq
6767 end
6868
69- def extract_tags ( text )
70- text . to_s . scan ( /(?:^|[^#\w ])#([A-Za-z0-9_.-]+)/ )
71- . flatten
72- . map { |t | t . to_s . strip . downcase }
73- . select { |tag | tag . match? ( NoteTag ::TAG_FORMAT ) }
74- . uniq
69+ def normalize_tags ( tags )
70+ Array ( tags )
71+ . map { |t | t . to_s . delete_prefix ( "#" ) . strip . downcase }
72+ . select { |tag | tag . match? ( NoteTag ::TAG_FORMAT ) }
73+ . uniq
7574 end
7675
7776 def resolve_mentions ( names )
0 commit comments