-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit for sailpoint-embed plugin to handle showing and hidin…
…g of replies on embedded topics
- Loading branch information
0 parents
commit 7d10aa9
Showing
13 changed files
with
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: Discourse Plugin | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
ci: | ||
uses: discourse/.github/.github/workflows/discourse-plugin.yml@v1 |
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,5 @@ | ||
# **Plugin Name** Plugin | ||
|
||
**Plugin Summary** | ||
|
||
For more information, please see: **url to meta topic** |
Empty file.
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,59 @@ | ||
<header class='discourse' data-embed-state='loaded'> | ||
<% if @reply_count < 1 %> | ||
<%= link_to(I18n.t('embed.start_discussion'), @topic_view.topic.url, class: 'button', target: '_blank') %> | ||
<% elsif @reply_count >= 10 && @show_comments %> | ||
<%= link_to(I18n.t('embed.continue'), @second_post_url, class: 'button', target: '_blank') %> | ||
<% end %> | ||
<% if @show_comments %> | ||
<span class='replies'><%= I18n.t('embed.replies', count: @reply_count) %></span> | ||
<% end %> | ||
</header> | ||
|
||
<%- if @topic_view.posts.present? %> | ||
<%- @topic_view.posts.each do |post| %> | ||
<% if @show_comments %> | ||
<article class='post<%- if post.trashed? %> deleted<% end %>' id='post-<%= post.id.to_s %>'> | ||
<%= link_to embed_post_date(post.created_at), post.full_url, title: post.created_at.strftime("%B %e, %Y %l:%M%P"), class: 'post-date', target: "_blank" %> | ||
<%- if post.reply_to_post.present? && !post.cooked.index('aside') %> | ||
<%= link_to I18n.t('embed.in_reply_to', username: post.reply_to_post.username), post.reply_to_post.full_url, 'data-link-to-post' => post.reply_to_post.id.to_s, :class => 'in-reply-to', target: "_blank" %> | ||
<%- end %> | ||
|
||
<div class='author'> | ||
<a href='<%= Discourse.base_url %>/u/<%= post.username %>' target="_blank"><img src='<%= post.user.small_avatar_url %>' alt=''></a> | ||
</div> | ||
<div class='cooked'> | ||
<h3 class='username'> | ||
<a href='<%= Discourse.base_url %>/u/<%= post.username %>' target="_blank" class='<% if post.user.staff? %>staff<% end %><% if post.user.new_user? %>new-user<% end %>'><%= post.user.username %></a> | ||
<%- if post.user.title.present? %> | ||
<span class='title'><%= post.user.title %></span> | ||
<%- end %> | ||
</h3> | ||
<%= get_html(post) %> | ||
<%- if post.reply_count > 0 && post.replies.exists? %> | ||
<%- if post.reply_count == 1 %> | ||
<%= link_to I18n.t('embed.replies', count: post.reply_count), post.full_url, 'data-link-to-post' => post.replies.first.id.to_s, :class => 'post-replies button', target: "_blank" %> | ||
<% else %> | ||
<%= link_to I18n.t('embed.replies', count: post.reply_count), post.full_url, class: 'post-replies button', target: "_blank" %> | ||
<%- end %> | ||
<%- end %> | ||
</div> | ||
</article> | ||
<% end %> | ||
<%- end %> | ||
<% if @topic_view.topic.posts_count > 0 %> | ||
<footer class="clearfix"> | ||
<%= link_to(image_tag(SiteSetting.site_logo_url, class: 'logo'), Discourse.base_url, target: '_blank') %> | ||
<%= link_to(I18n.t('embed.continue'), @topic_view.posts.last.full_url, class: 'button', target: '_blank') %> | ||
<%- if @posts_left > 0 %> | ||
<span class='replies'><%= I18n.t('embed.more_replies', count: @posts_left) %></span> | ||
<%- end %> | ||
</footer> | ||
<div class="clearfix"></div> | ||
<% end %> | ||
<% end %> |
Empty file.
Empty file.
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,9 @@ | ||
en: | ||
admin_js: | ||
admin: | ||
site_settings: | ||
categories: | ||
TODO_plugin_name: "Plugin Name" | ||
js: | ||
discourse_plugin_name: | ||
placeholder: placeholder |
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 @@ | ||
en: |
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,4 @@ | ||
sailpoint-embed: | ||
sailpoint_embed_enabled: | ||
default: false | ||
client: true |
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,14 @@ | ||
# frozen_string_literal: true | ||
module SailPointEmbed | ||
module EmbedControllerExtension | ||
extend ActiveSupport::Concern | ||
|
||
prepended do | ||
before_action :set_show_comments, only: [:comments] | ||
|
||
def set_show_comments | ||
@show_comments = ActiveModel::Type::Boolean.new.cast(params[:show_comments]) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# name: sailpoint-embed | ||
# about: Embedding settings to hide/show the replies in the embed | ||
# meta_topic_id: TODO | ||
# version: 0.0.1 | ||
# authors: Darrell Thobe | ||
# url: TODO | ||
|
||
enabled_site_setting :sailpoint_embed_enabled | ||
|
||
module ::SailPointEmbed | ||
PLUGIN_NAME = "sailpoint-embed" | ||
end | ||
|
||
require_relative "lib/discourse-embed/embed_controller_extension" | ||
|
||
after_initialize do | ||
reloadable_patch do | ||
::ActionController::Base.prepend_view_path File.expand_path("../app/views/", __FILE__) | ||
EmbedController.prepend SailPointEmbed::EmbedControllerExtension | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
(function () { | ||
var DE = window.DiscourseEmbed || {}; | ||
var comments = document.getElementById("discourse-comments"); | ||
var iframe = document.createElement("iframe"); | ||
|
||
[ | ||
"showComments", | ||
"discourseUrl", | ||
"discourseEmbedUrl", | ||
"discourseUserName", | ||
"discourseReferrerPolicy", | ||
].forEach(function (i) { | ||
if (window[i]) { | ||
DE[i] = DE[i] || window[i]; | ||
} | ||
}); | ||
|
||
var queryParams = {}; | ||
|
||
if (DE.discourseEmbedUrl) { | ||
if (DE.discourseEmbedUrl.startsWith("/")) { | ||
console.error( | ||
"discourseEmbedUrl must be a full URL, not a relative path" | ||
); | ||
} | ||
|
||
queryParams.embed_url = encodeURIComponent(DE.discourseEmbedUrl); | ||
} | ||
|
||
if (DE.discourseUserName) { | ||
queryParams.discourse_username = DE.discourseUserName; | ||
} | ||
|
||
if (DE.topicId) { | ||
queryParams.topic_id = DE.topicId; | ||
} | ||
|
||
if (DE.className) { | ||
queryParams.class_name = DE.className; | ||
} | ||
|
||
if (DE.showComments !== undefined && DE.showComments !== null) { | ||
queryParams.show_comments = DE.showComments; | ||
} else { | ||
queryParams.show_comments = true; | ||
} | ||
|
||
var src = DE.discourseUrl + "embed/comments"; | ||
var keys = Object.keys(queryParams); | ||
if (keys.length > 0) { | ||
src += "?"; | ||
|
||
for (var i = 0; i < keys.length; i++) { | ||
if (i > 0) { | ||
src += "&"; | ||
} | ||
|
||
var k = keys[i]; | ||
src += k + "=" + queryParams[k]; | ||
} | ||
} | ||
|
||
iframe.src = src; | ||
iframe.id = "discourse-embed-frame"; | ||
iframe.width = "100%"; | ||
iframe.frameBorder = "0"; | ||
iframe.scrolling = "no"; | ||
iframe.referrerPolicy = | ||
DE.discourseReferrerPolicy || "no-referrer-when-downgrade"; | ||
comments.appendChild(iframe); | ||
|
||
// Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html | ||
function findPosY(obj) { | ||
var top = 0; | ||
if (obj.offsetParent) { | ||
while (1) { | ||
top += obj.offsetTop; | ||
if (!obj.offsetParent) break; | ||
obj = obj.offsetParent; | ||
} | ||
} else if (obj.y) { | ||
top += obj.y; | ||
} | ||
return top; | ||
} | ||
|
||
function normalizeUrl(url) { | ||
return url.replace(/^https?(\:\/\/)?/, ""); | ||
} | ||
|
||
function postMessageReceived(e) { | ||
if (!e) { | ||
return; | ||
} | ||
if (!normalizeUrl(DE.discourseUrl).includes(normalizeUrl(e.origin))) { | ||
return; | ||
} | ||
|
||
if (e.data) { | ||
if (e.data.type === "discourse-resize" && e.data.height) { | ||
iframe.height = e.data.height + "px"; | ||
} | ||
|
||
if (e.data.type === "discourse-scroll" && e.data.top) { | ||
// find iframe offset | ||
var destY = findPosY(iframe) + e.data.top; | ||
window.scrollTo(0, destY); | ||
} | ||
} | ||
} | ||
window.addEventListener("message", postMessageReceived, false); | ||
})(); |
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe EmbedController do | ||
describe "#comments" do | ||
let!(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" } | ||
fab!(:embeddable_host) | ||
|
||
fab!(:topic) | ||
fab!(:topic_embed) { Fabricate(:topic_embed, embed_url: "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html") } | ||
fab!(:post) { Fabricate(:post, topic: topic_embed.topic) } | ||
|
||
before { Jobs.run_immediately! } | ||
|
||
it "hiding replies on embed" do | ||
get "/embed/comments", params: { embed_url: embed_url, show_comments: false }, headers: { "REFERER" => embed_url } | ||
|
||
html = Nokogiri::HTML5.fragment(response.body) | ||
posts_count = html.css("article.post").count | ||
|
||
expect(posts_count).to eq(0) | ||
end | ||
|
||
it "showing replies on embed" do | ||
get "/embed/comments", params: { embed_url: embed_url, show_comments: true }, headers: { "REFERER" => embed_url } | ||
|
||
html = Nokogiri::HTML5.fragment(response.body) | ||
posts_count = html.css("article.post").count | ||
expect(posts_count).to eq(1) | ||
end | ||
end | ||
end |