Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to tag users #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions app/helpers/forum_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

module ForumHelper
include SimpleDiscussion::ForumPostsHelper

def formatted_content(text)
def formatted_content(document)
options = {
hard_wrap: true,
filter_html: true,
autolink: true,
tables: true
}
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
sanitize(md.render(text))

# Initialize each renderer separately
video_renderer = VideoEmbeddingHelper::VideoMarkdownRenderer.new(preprocess: true)
user_tag_renderer = UserTagHelper::UserTagMarkdownRenderer.new(preprocess: true)
circuit_renderer = CircuitEmbeddingHelper::CircuitMarkdownRenderer.new(preprocess: true)

# Preprocess the document separately using each renderer
video_html = video_renderer.preprocess(document)
user_tag_html = user_tag_renderer.preprocess(video_html)
circuit_html = circuit_renderer.preprocess(user_tag_html)

# Allow 'iframe' tags and 'src' attributes in addition to 'strong', 'em', and 'a'
sanitized_html = sanitize(circuit_html, tags: %w(strong em a iframe), attributes: %w(href src))

sanitized_html
end
end
24 changes: 24 additions & 0 deletions app/helpers/user_tag_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module UserTagHelper
class UserTagMarkdownRenderer < Redcarpet::Render::HTML
include Rails.application.routes.url_helpers

def preprocess(document)
# Parse user mentions and replace them with links to user profiles
document.gsub!(/@([\w\s]+)/) do |match|
username = $1
user = User.find_by(name: username)
if user
notify_tagged_user(user) # Trigger notification for tagged user
"<a href='/users/#{user.uid}'>@#{username}</a>"
else
match # Leave the mention unchanged if the user doesn't exist
end
end
document
end
def notify_tagged_user(user)
# Trigger TagNotification for the tagged user
TagNotification.with(tagged_user: user).deliver(user)
end
end
end
15 changes: 15 additions & 0 deletions app/notifications/tag_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class TagNotification < Noticed::Base
deliver_by :database, association: :noticed_notifications

def message
tagged_user = params[:tagged_user]
t("users.notifications.tag_notif", user: tagged_user.name)
end

def icon
"far fa-user-tag"
end
end

2 changes: 2 additions & 0 deletions app/services/notify_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def type_check
Result.new("true", "forum_comment", @thread, @post.id)
when "ForumThreadNotification"
Result.new("true", "forum_thread", @thread)
when "TagNotification" # Add this case for handling tag notifications
Result.new("true", "tag", @tagged_user_id, @tagged_user_name)
else
Result.new("false", "no_type", root_path)
end
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
en:
users:
notifications:
tag_notif: "You have been tagged by %{user} in the thread"
edit: "Edit"
show: "Show"
delete: "Delete"
Expand Down