diff --git a/db/manifest b/db/manifest index 232cb12ef..e8485d6cc 100755 --- a/db/manifest +++ b/db/manifest @@ -143,3 +143,4 @@ emails.sql email_batch.sql user_progress_tracking2.sql bands_did_session.sql +chat_messages.sql \ No newline at end of file diff --git a/db/up/add_chat_message.sql b/db/up/add_chat_message.sql new file mode 100644 index 000000000..e69de29bb diff --git a/db/up/chat_messages.sql b/db/up/chat_messages.sql new file mode 100644 index 000000000..01a0fcd14 --- /dev/null +++ b/db/up/chat_messages.sql @@ -0,0 +1,8 @@ +CREATE TABLE chat_messages +( + id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), + user_id character varying(64), + music_session_id character varying(64), + messsage TEXT NOT NULL, + created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/pb/src/client_container.proto b/pb/src/client_container.proto index 1df3bebf8..51ec3308d 100644 --- a/pb/src/client_container.proto +++ b/pb/src/client_container.proto @@ -52,6 +52,7 @@ message ClientMessage { // text message TEXT_MESSAGE = 236; + CHAT_MESSAGE = 237; MUSICIAN_SESSION_FRESH = 240; MUSICIAN_SESSION_STALE = 245; @@ -130,6 +131,7 @@ message ClientMessage { // text message optional TextMessage text_message = 236; + optional ChatMessage chat_message = 237; optional MusicianSessionFresh musician_session_fresh = 240; optional MusicianSessionStale musician_session_stale = 245; @@ -397,6 +399,14 @@ message TextMessage { optional bool clipped_msg = 7; } +message ChatMessage { + optional string sender_name = 1; + optional string sender_id = 2; + optional string msg = 3; + optional string msg_id = 4; + optional string created_at = 5; +} + // route_to: client: // sent by server to let the rest of the participants know a client has become active again after going stale message MusicianSessionFresh { diff --git a/ruby/lib/jam_ruby/message_factory.rb b/ruby/lib/jam_ruby/message_factory.rb index 491e1b3e5..fbcb1673d 100644 --- a/ruby/lib/jam_ruby/message_factory.rb +++ b/ruby/lib/jam_ruby/message_factory.rb @@ -580,6 +580,23 @@ module JamRuby ) end + # creates the session chat message + def chat_message(session_id, sender_name, sender_id, msg, msg_id, created_at) + chat_message = Jampb::ChatMessage.new( + :sender_id => sender_id, + :sender_name => sender_name, + :msg => msg, + :msg_id => msg_id, + :created_at => created_at + ) + + Jampb::ClientMessage.new( + :type => ClientMessage::Type::CHAT_MESSAGE, + :route_to => SESSION_TARGET_PREFIX + session_id, + :chat_message => chat_message + ) + end + # create a musician fresh session message def musician_session_fresh(session_id, user_id, username, photo_url) fresh = Jampb::MusicianSessionFresh.new( diff --git a/ruby/lib/jam_ruby/models/chat_message.rb b/ruby/lib/jam_ruby/models/chat_message.rb new file mode 100644 index 000000000..786b48642 --- /dev/null +++ b/ruby/lib/jam_ruby/models/chat_message.rb @@ -0,0 +1,27 @@ +module JamRuby + class ChatMessage < ActiveRecord::Base + + self.primary_key = 'id' + + default_scope order('created_at DESC') + + belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id" + belongs_to :session, :class_name => "JamRuby::MusicSession", :foreign_key => "session_id" + + validates :message, length: {minimum: 1, maximum: 255}, no_profanity: true + + def self.send_chat_msg(music_session, chat_msg, user) + msg = @@message_factory.chat_message( + music_session.id, + user.name, + user.id, + chat_msg.message, + chat_msg.id, + chat_msg.created_at + ) + + @@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => chat_msg.user_id}) + end + + end +end \ No newline at end of file diff --git a/web/app/assets/javascripts/AAB_message_factory.js b/web/app/assets/javascripts/AAB_message_factory.js index 04bd51344..3f974b0a2 100644 --- a/web/app/assets/javascripts/AAB_message_factory.js +++ b/web/app/assets/javascripts/AAB_message_factory.js @@ -51,6 +51,7 @@ // text message TEXT_MESSAGE : "TEXT_MESSAGE", + CHAT_MESSAGE : "CHAT_MESSAGE", // broadcast notifications SOURCE_UP_REQUESTED : "SOURCE_UP_REQUESTED", diff --git a/web/app/assets/javascripts/chatPanel.js b/web/app/assets/javascripts/chatPanel.js new file mode 100644 index 000000000..e69de29bb diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index fbdf74a2a..f08cb340f 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -944,6 +944,24 @@ }); } + function createChatMessage(session_id, options) { + return $.ajax({ + type: "POST", + url: '/api/sessions/' + session_id + '/chat?' + $.param(options), + dataType: "json", + contentType: 'application/json' + }); + } + + function getChatMessages(session_id, options) { + return $.ajax({ + type: "GET", + url: '/api/sessions/' + session_id + '/chats' + $.param(options), + dataType: "json", + contentType: 'application/json' + }); + } + function initialize() { return self; } @@ -1026,6 +1044,8 @@ this.createFbInviteUrl = createFbInviteUrl; this.createTextMessage = createTextMessage; this.getNotifications = getNotifications; + this.createChatMessage = createChatMessage; + this.getChatMessages = getChatMessages; return this; }; diff --git a/web/app/assets/javascripts/sidebar.js b/web/app/assets/javascripts/sidebar.js index 57253d053..41aa9e3f6 100644 --- a/web/app/assets/javascripts/sidebar.js +++ b/web/app/assets/javascripts/sidebar.js @@ -10,6 +10,7 @@ var invitationDialog = null; var textMessageDialog = null; var notificationPanel = null; + var chatPanel = null; var me = null; function initializeSearchPanel() { @@ -95,6 +96,8 @@ } function initializeChatPanel() { +// chatPanel = new context.JK.ChatPanel(app); +// chatPanel.initialize(me, textMessageDialog); } function search(query) { diff --git a/web/app/controllers/api_chats_controller.rb b/web/app/controllers/api_chats_controller.rb new file mode 100644 index 000000000..b639ee15a --- /dev/null +++ b/web/app/controllers/api_chats_controller.rb @@ -0,0 +1,35 @@ +class ApiChatsController < ApiController + + before_filter :api_signed_in_user, :check_session + + respond_to :json + + def create + @chat_msg = ChatMessage.new + @chat_msg.user_id = current_user.id + @chat_msg.session_id = params[:music_session] + @chat_msg.message = params[:msg] + if @chat_msg.save + ChatMessage.send_chat_msg @music_session, @chat_msg, current_user + end + + respond_with_model(@chat_msg) + end + + def index + @chat_msgs = ChatMessage.find_by_session_id(params[:music_session]) + .paginate(page: params[:page], per_page: pararms[:per_page] || 20) + end + + def check_session + @music_session = MusicSession.find(params[:music_session]) + if @music_session.nil? + raise ArgumentError, 'specified session not found' + end + + unless @music_session.access? current_user + raise PermissionError, 'not allowed to join the specified session' + end + end + +end \ No newline at end of file diff --git a/web/config/routes.rb b/web/config/routes.rb index b225e907f..508fd3170 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -259,6 +259,10 @@ SampleApp::Application.routes.draw do match '/users/:id/share/session/:provider' => 'api_users#share_session', :via => :get match '/users/:id/share/recording/:provider' => 'api_users#share_recording', :via => :get + # session chat + match '/sessions/:id/chat' => 'api_chats#create', :via => :post + match '/sessions/:id/chats' => 'api_chats#index', :via => :get + # user recordings # match '/users/:id/recordings' => 'api_users#recording_index', :via => :get # match '/users/:id/recordings/:recording_id' => 'api_users#recording_show', :via => :get, :as => 'api_recording_detail'