diff --git a/app/controllers/public_controller.rb b/app/controllers/public_controller.rb new file mode 100644 index 0000000..266878e --- /dev/null +++ b/app/controllers/public_controller.rb @@ -0,0 +1,18 @@ +class DiscourseChat::PublicController < ApplicationController + requires_plugin DiscourseChat::PLUGIN_NAME + + def post_transcript + params.require(:secret) + + redis_key = "chat_integration:transcript:" + params[:secret] + content = $redis.get(redis_key) + + if content + render json: {content: content} + return + end + + raise Discourse::NotFound + + end +end \ No newline at end of file diff --git a/app/helpers/helper.rb b/app/helpers/helper.rb index d27d587..7deca25 100644 --- a/app/helpers/helper.rb +++ b/app/helpers/helper.rb @@ -178,6 +178,14 @@ module DiscourseChat end + def self.save_transcript(transcript) + secret = SecureRandom.hex + redis_key = "chat_integration:transcript:" + secret + $redis.set(redis_key, transcript, {:ex => 3600}) # Expire in 1 hour + + return secret + end + end end diff --git a/app/initializers/discourse_chat.rb b/app/initializers/discourse_chat.rb index 59cd785..0ab00a7 100644 --- a/app/initializers/discourse_chat.rb +++ b/app/initializers/discourse_chat.rb @@ -6,6 +6,11 @@ module ::DiscourseChat isolate_namespace DiscourseChat end + class PublicEngine < ::Rails::Engine + engine_name DiscourseChat::PLUGIN_NAME+"-public" + isolate_namespace DiscourseChat + end + def self.plugin_name DiscourseChat::PLUGIN_NAME end @@ -31,6 +36,7 @@ require_relative "../serializers/channel_serializer" require_relative "../serializers/rule_serializer" require_relative "../controllers/chat_controller" +require_relative "../controllers/public_controller" require_relative "../routes/discourse_chat" require_relative "../routes/discourse" diff --git a/app/routes/discourse.rb b/app/routes/discourse.rb index 01b0685..5eee9a3 100644 --- a/app/routes/discourse.rb +++ b/app/routes/discourse.rb @@ -1,4 +1,5 @@ Discourse::Application.routes.append do mount ::DiscourseChat::AdminEngine, at: '/admin/plugins/chat', constraints: AdminConstraint.new + mount ::DiscourseChat::PublicEngine, at: '/chat-transcript/', as: 'chat-transcript' mount ::DiscourseChat::Provider::HookEngine, at: '/chat-integration/' end \ No newline at end of file diff --git a/app/routes/discourse_chat.rb b/app/routes/discourse_chat.rb index 1b65c53..80b68b7 100644 --- a/app/routes/discourse_chat.rb +++ b/app/routes/discourse_chat.rb @@ -17,4 +17,8 @@ module DiscourseChat get "/:provider" => "chat#respond" end + + PublicEngine.routes.draw do + get '/:secret' => "public#post_transcript" + end end \ No newline at end of file diff --git a/assets/javascripts/discourse/public-route-map.js.es6 b/assets/javascripts/discourse/public-route-map.js.es6 new file mode 100644 index 0000000..acff9f8 --- /dev/null +++ b/assets/javascripts/discourse/public-route-map.js.es6 @@ -0,0 +1,3 @@ +export default function() { + this.route('transcript', {path: '/chat-transcript/:secret'}); +}; \ No newline at end of file diff --git a/assets/javascripts/discourse/routes/transcript.js.es6 b/assets/javascripts/discourse/routes/transcript.js.es6 new file mode 100644 index 0000000..c4cdee2 --- /dev/null +++ b/assets/javascripts/discourse/routes/transcript.js.es6 @@ -0,0 +1,26 @@ +import { ajax } from 'discourse/lib/ajax'; +import { popupAjaxError } from 'discourse/lib/ajax-error'; + +export default Discourse.Route.extend({ + beforeModel: function(transition) { + + if (Discourse.User.current()) { + var secret = transition.params.transcript.secret; + // User is logged in + this.replaceWith('discovery.latest').then(e => { + if (this.controllerFor('navigation/default').get('canCreateTopic')) { + // User can create topic + Ember.run.next(() => { + ajax("/chat-transcript/"+secret).then(result => { + e.send('createNewTopicViaParams', null, result['content'], null, null, null); + }, popupAjaxError); + }); + } + }); + } else { + // User is not logged in + this.session.set("shouldRedirectToUrl", window.location.href); + this.replaceWith('login'); + } + } +}); \ No newline at end of file diff --git a/spec/controllers/public_controller_spec.rb b/spec/controllers/public_controller_spec.rb new file mode 100644 index 0000000..f823938 --- /dev/null +++ b/spec/controllers/public_controller_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +describe 'Public Controller', type: :request do + + before do + SiteSetting.chat_integration_enabled = true + end + + describe 'loading a transcript' do + + it 'should be able to load a transcript' do + key = DiscourseChat::Helper.save_transcript("Some content here") + + get "/chat-transcript/#{key}.json" + + expect(response).to be_success + + expect(response.body).to eq('{"content":"Some content here"}') + end + + it 'should 404 for non-existant transcript' do + key = 'abcdefghijk' + get "/chat-transcript/#{key}.json" + + expect(response).not_to be_success + end + + end + +end diff --git a/spec/helpers/helper_spec.rb b/spec/helpers/helper_spec.rb index 86cdb6e..d43f7de 100644 --- a/spec/helpers/helper_spec.rb +++ b/spec/helpers/helper_spec.rb @@ -301,4 +301,14 @@ RSpec.describe DiscourseChat::Manager do end end + describe '.save_transcript' do + + it 'saves a transcript to redis' do + key = DiscourseChat::Helper.save_transcript("Some content here") + + expect($redis.get("chat_integration:transcript:#{key}")).to eq("Some content here") + expect($redis.pttl("chat_integration:transcript:#{key}")).to eq(3600*1000) + end + end + end \ No newline at end of file