-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user-mentions: skeleton to send push_notifications on mentions
- Loading branch information
1 parent
9e85c98
commit 15800a6
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Mentionable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_create :has_mentions!, if: :has_mentions? | ||
end | ||
|
||
MentionsStruct = Struct.new(:instance) do | ||
def initialize(instance: nil) | ||
@instance = instance | ||
end | ||
|
||
def no_html | ||
document = Nokogiri::HTML(@instance.content) | ||
document.css('img, a').each { |node| node.remove } | ||
document.text.strip | ||
end | ||
|
||
def fragments | ||
@fragments ||= Nokogiri::HTML.fragment(@instance.content) | ||
end | ||
|
||
def contains_html? | ||
fragments.children.any? | ||
end | ||
|
||
def contains_anchor_with_href? | ||
fragments.css('a[href]').any? | ||
end | ||
|
||
def contains_user_link? | ||
fragments.css('a[href]').any? { |a| a['href'].include?('app/users') } | ||
end | ||
|
||
def extract_user_uuid | ||
user_links = fragments.css('a[href]').select { |a| a['href'].include?('app/users') } | ||
|
||
user_links.map do |a| | ||
a['href'][%r{app/users/([^/]+)}, 1] | ||
end | ||
end | ||
end | ||
|
||
def mentions | ||
@mentions ||= MentionsStruct.new(instance: self) | ||
end | ||
|
||
def has_mentions? | ||
mentions.extract_user_uuid.any? | ||
end | ||
|
||
def has_mentions! | ||
# @todo perform in a job | ||
PushNotificationTrigger.new(self, :mention, Hash.new).run | ||
end | ||
end |
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
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