Add support for chat transcripts
(still needs to be implemented by providers)
This commit is contained in:
parent
0d9b251030
commit
2d7f25d7e6
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
|
@ -17,4 +17,8 @@ module DiscourseChat
|
|||
|
||||
get "/:provider" => "chat#respond"
|
||||
end
|
||||
|
||||
PublicEngine.routes.draw do
|
||||
get '/:secret' => "public#post_transcript"
|
||||
end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
export default function() {
|
||||
this.route('transcript', {path: '/chat-transcript/:secret'});
|
||||
};
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
});
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue