SPEC: add spec to ensure discobot works in French

PERF: memoize cooked triggers

Follow-up to 3c31884b
This commit is contained in:
Régis Hanol
2020-06-26 12:48:11 +02:00
parent c16ad39f8e
commit d234e0f922
2 changed files with 16 additions and 1 deletions

View File

@ -222,9 +222,15 @@ module DiscourseNarrativeBot
end
end
@@cooked_triggers = {}
def cook(trigger)
@@cooked_triggers[trigger] ||= PrettyText.cook("@#{self.discobot_username} #{trigger}")
end
def match_trigger?(trigger)
# we remove the leading <p> to allow for trigger to be at the end of a paragraph
cooked_trigger = PrettyText.cook("@#{self.discobot_username} #{trigger}")[3..-1]
cooked_trigger = cook(trigger)[3..-1]
regexp = Regexp.new(cooked_trigger, 'i')
match = @post.cooked.match(regexp)