Skip to content

Commit

Permalink
initial commit for sailpoint-embed plugin to handle showing and hidin…
Browse files Browse the repository at this point in the history
…g of replies on embedded topics
  • Loading branch information
darrell-thobe-sp committed Jun 28, 2024
0 parents commit 7d10aa9
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/discourse-plugin.yml
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
5 changes: 5 additions & 0 deletions README.md
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 added app/.gitkeep
Empty file.
59 changes: 59 additions & 0 deletions app/views/embed/comments.html.erb
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 added assets/javascripts/.gitkeep
Empty file.
Empty file added assets/stylesheets/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions config/locales/client.en.yml
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
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
en:
4 changes: 4 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sailpoint-embed:
sailpoint_embed_enabled:
default: false
client: true
14 changes: 14 additions & 0 deletions lib/discourse-embed/embed_controller_extension.rb
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
23 changes: 23 additions & 0 deletions plugin.rb
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
112 changes: 112 additions & 0 deletions public/javascripts/embed.js
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);
})();
31 changes: 31 additions & 0 deletions spec/requests/embed_controller_spec.rb
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

0 comments on commit 7d10aa9

Please sign in to comment.