denshobato_chat_panel 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8eea5004f950d0d29cd47978c95b089d619a4035
4
+ data.tar.gz: d21710259e35a919da41708114689570753c83f4
5
+ SHA512:
6
+ metadata.gz: 7f5950ba318c59b5cb00ecb7a2d3edf135a3eaf67ca801cba995e0c5db2f351ea3e9f63064eca948a3be391af0578b0e2ad03f450239b7aed20f29f3550c0796
7
+ data.tar.gz: d6eeb16458b851bbec292659f46a37a434987d3c546c96a7fb02c9184f53ce6842538618b368dd0576e426a518b86ea1a11dc45c0b158f6516832c79169fcb16
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /node_modules
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in denshobato_chat_panel.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # DenshobatoChatPanel
2
+
3
+ ![alt text](http://i.imgur.com/0sUUfDl.jpg "Screen")
4
+ DenshobatoChatPanel, is a official addon for [Denshobato Gem](https://github.com/ID25/denshobato), it provides simple chat panel for you. If you don't need any special customization for dialog panel, or if you want to try messaging quickly, you can use chat panel.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'denshobato_chat_panel'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install denshobato_chat_panel
21
+
22
+ ## Usage
23
+
24
+ ### Install Chat
25
+
26
+ ```shell
27
+ rails g denshobato_chat_panel:install
28
+ ```
29
+
30
+ This command copies assets to vendor/public/assets
31
+
32
+ #####1. Copy this line to your `config/initializers/assets.rb`
33
+ ```ruby
34
+ Rails.application.config.assets.precompile += %w( denshobato.js )
35
+ ```
36
+ #####2. In your application.scss import css
37
+ ```scss
38
+ @import 'denshobato';
39
+ ```
40
+
41
+ #####3. In `layouts/application.erb` include javascript file in the bottom
42
+
43
+ ```erb
44
+ <body>
45
+ <div class='container'>
46
+ <%= render 'layouts/header' %>
47
+ <%= yield %>
48
+ </div>
49
+
50
+ <%= javascript_include_tag 'denshobato' %>
51
+ </body>
52
+ ```
53
+ #####4. Mount API route in your routes.rb
54
+ ```ruby
55
+ mount Denshobato::DenshobatoApi => '/'
56
+ ```
57
+
58
+ #####5. On the page with your conversation, e.g `localhost:3000/conversation/32`, add this helper with arguments
59
+ ```slim
60
+ = render_denshobato_messages(@conversation, current_user)
61
+ // => When @conversation = Denshobato::Conversation.find(params[:id])
62
+ // => and current_user is your signed in user, e.g Devise current_user etc.
63
+ ```
64
+
65
+ ### A few methods for Chat Panel
66
+
67
+ You need to set up name and image for chat independently.
68
+ By default, name show a class name of model, e.g Customer, or User, and a gravatar.
69
+
70
+ Go to your user model and rewrite these methods.
71
+
72
+ ```ruby
73
+ class User
74
+ denshobato_for :user
75
+
76
+ def full_name
77
+ "#{self.first_name}, #{self.last_name}"
78
+ end
79
+
80
+ def image
81
+ self.avatar.url
82
+ end
83
+ end
84
+ ```
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ID25/denshobato_chat_panel.
89
+
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "denshobato_chat_panel"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'denshobato_chat_panel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'denshobato_chat_panel'
8
+ spec.version = DenshobatoChatPanel::VERSION
9
+ spec.authors = ['ID25']
10
+ spec.email = ['xid25x@gmail.com']
11
+
12
+ spec.summary = 'Chat Panel for Denshobato messaging.'
13
+ spec.description = 'Chat Panel for Denshobato messaging.'
14
+ spec.homepage = 'https://github.com/ID25/denshobato_chat_panel'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'grape'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'activerecord'
28
+ spec.add_development_dependency 'sqlite3'
29
+ end
@@ -0,0 +1,32 @@
1
+ require 'grape'
2
+
3
+ class ConversationApi < Grape::API
4
+ format :json
5
+ prefix :api
6
+
7
+ helpers do
8
+ def take_current_user(params)
9
+ params[:class].constantize.find(params[:user])
10
+ end
11
+
12
+ def class_name(klass)
13
+ klass.class.name
14
+ end
15
+
16
+ def conversation
17
+ Denshobato::Conversation
18
+ end
19
+ end
20
+
21
+ resource :conversations do
22
+ desc 'Get info from conversation'
23
+ get :conversation_info do
24
+ # Get current user id, and Conversation id
25
+
26
+ current_user = take_current_user(params)
27
+ room = conversation.find(params[:id])
28
+
29
+ { author: room.sender.email, conversation_id: room.id, sender_id: current_user.id, sender_class: class_name(current_user), recipient: room.recipient.full_name, recipient_class: class_name(room.recipient) }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,6 @@
1
+ require 'grape'
2
+
3
+ class DenshobatoApi < Grape::API
4
+ mount MessageApi
5
+ mount ConversationApi
6
+ end
@@ -0,0 +1,96 @@
1
+ require 'grape'
2
+
3
+ class MessageApi < Grape::API
4
+ format :json
5
+ prefix :api
6
+
7
+ helpers do
8
+ def take_current_user(params)
9
+ params[:class].constantize.find(params[:user])
10
+ end
11
+
12
+ def formated_messages(klass)
13
+ # Prepare JSON for React
14
+
15
+ { body: klass.body, id: klass.id, author: klass.author.email, full_name: klass.author.full_name, avatar: klass.author.image, time: klass.message_time }
16
+ end
17
+
18
+ def send_notification(id, message)
19
+ # Find current conversation
20
+ conversation = densh_conversation.find(id)
21
+
22
+ # Create Notifications
23
+ create_notifications_for(conversation, message)
24
+ end
25
+
26
+ def create_notifications_for(conversation, message)
27
+ # Take sender and recipient
28
+ sender = conversation.sender
29
+ recipient = conversation.recipient
30
+
31
+ # Find conversation, where sender it's recipient
32
+ conversation_2 = recipient.find_conversation_with(sender)
33
+
34
+ # If recipient deletes the conversation, create it again
35
+ conversation_2 = create_conversation_for_recipient(sender, recipient) if conversation_2.nil?
36
+
37
+ # Send notifications of new messages to sender and recipient
38
+ [conversation.id, conversation_2.id].each { |id| message.notifications.create(conversation_id: id) }
39
+ end
40
+
41
+ def message_class
42
+ Denshobato::Message
43
+ end
44
+
45
+ def densh_conversation
46
+ Denshobato::Conversation
47
+ end
48
+ end
49
+
50
+ resource :messages do
51
+ desc 'Return messages for current conversation'
52
+ get :get_conversation_messages do
53
+ # Fetch all messages from conversation
54
+
55
+ messages = Denshobato::Conversation.find(params[:id]).messages.includes(:author)
56
+
57
+ messages.each_with_object([]) { |msg, arr| arr << formated_messages(msg) }
58
+ end
59
+
60
+ desc 'Create Message in conversation'
61
+ post :create_message do
62
+ # Create a message in conversation
63
+
64
+ params do
65
+ requires :message, type: Hash do
66
+ requires :body, type: String
67
+ requires :sender_class, type: String
68
+ requires :sender, type: Integer
69
+ requires :conversation_id, type: Integer
70
+ end
71
+ end
72
+
73
+ message_params = params[:message]
74
+ user = message_params[:sender_class].constantize.find(message_params[:sender])
75
+ message = user.hato_messages.build(body: message_params[:body])
76
+
77
+ if message.save
78
+ id = message_params[:conversation_id]
79
+ send_notification(id, message)
80
+ formated_messages(message)
81
+ else
82
+ error!({ error: message.errors.full_messages.join(' ') }, 422)
83
+ end
84
+ end
85
+
86
+ desc 'Delete Message'
87
+ delete :delete_message do
88
+ # Delete message from DB
89
+
90
+ message = message_class.find(params[:id])
91
+ conversation = params[:conversation]
92
+
93
+ densh_conversation.where(id: conversation).includes(:denshobato_notifications).where(denshobato_notifications: { message_id: message.id }).first.notifications.first.destroy
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ module DenshobatoChatPanel
2
+ class Engine < Rails::Engine
3
+ config.autoload_paths += Dir["#{config.root}/lib/**/"]
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module DenshobatoChatPanel
2
+ module ReactHelper
3
+ def render_denshobato_messages(room, user)
4
+ content_tag(:div, '', id: 'denshobato-message-panel', data: { room: room.id, current_user_id: user.id, current_user_class: user.class.name })
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module DenshobatoChatPanel
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'denshobato_chat_panel/version'
2
+ require 'denshobato_chat_panel/engine' if defined?(Rails)
3
+
4
+ # Helpers for React-Redux
5
+ DenshobatoChatPanel.autoload :ReactHelper, 'denshobato_chat_panel/react_helper'
6
+
7
+ module DenshobatoChatPanel
8
+ ActionView::Base.include DenshobatoChatPanel::ReactHelper if defined?(ActionView::Base)
9
+ end