Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/private/conversations.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
24 changes: 24 additions & 0 deletions app/assets/stylesheets/partials/posts/branch_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@
.infinite-scroll {
display: none;
}

.send-message-to-user {
background-color: $navbarColor;
padding: 10px;
color: white;
border-radius: 10px;
margin-top: 10px;
&:hover {
background-color: black;
color: white;
}
}

.contact-user {
text-align: center;
}

.contacted-user {
display: inline-block;
border-radius: 10px;
padding: 10px;
background-color: $navbarColor;
color: white;
}

#branch-main-content {
background: white;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/private/conversations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the private/conversations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
9 changes: 8 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def create
end

def show
@post = Post.find(params[:id])
@post = Post.find(params[:id])
if user_signed_in?
@message_has_been_sent = conversation_exist?
end
end

def study
Expand Down Expand Up @@ -56,4 +59,8 @@ def get_posts

}).call
end

def conversation_exist?
Private::Conversation.between_users(current_user.id, @post.user.id).present?
end
end
33 changes: 33 additions & 0 deletions app/controllers/private/conversations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Private::ConversationsController < ApplicationController
def create
recipient_id = Post.find(params[:post_id]).user.id
@conversation = Private::Conversation.new(sender_id: current_user.id,
recipient_id: recipient_id)
if @conversation.save
Private::Message.create(user_id: recipient_id,
conversation_id: @conversation.id,
body: params[:message_body])

add_to_conversations unless already_added?

respond_to do |format|
format.js {render partial: 'posts/show/contact_user/message_form/success'}
end
else
respond_to do |format|
format.js {render partial: 'posts/show/contact_user/message_form/fail'}
end
end
end

private

def add_to_conversations
session[:private_conversations] ||= []
session[:private_conversations] << @conversation.id
end

def already_added?
session[:private_conversations].include?(@conversation.id)
end
end
16 changes: 16 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ def update_pagination_partial_path
'posts/posts_pagination_page/remove_pagination'
end
end

def contact_user_partial_path
if user_signed_in?
@post.user.id != current_user.id ? 'posts/show/contact_user' : 'shared/empty_partial'
else
'posts/show/login_required'
end
end

def leave_message_partial_path
if @message_has_been_sent
'posts/show/contact_user/already_in_touch'
else
'posts/show/contact_user/message_form'
end
end
end
13 changes: 13 additions & 0 deletions app/helpers/private/conversations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Private::ConversationsHelper
def private_conv_recipient(conversation)
conversation.opposed_user(current_user)
end

def load_private_messages(conversation)
if conversation.messages.count > 0
'private/conversations/conversation/messages_list/link_to_previous_messages'
else
'shared/empty_partial'
end
end
end
19 changes: 19 additions & 0 deletions app/models/private/conversation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Private::Conversation < ApplicationRecord
self.table_name = 'private_conversations'

has_many :messages,
class_name: "Private::Message",
foreign_key: :conversation_id
belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'

scope :between_users, -> (user1_id, user2_id) do
where(sender_id: user1_id, recipient_id: user2_id).or(
where(sender_id: user2_id, recipient_id: user1_id)
)
end

def opposed_user(user)
user == recipient ? sender : recipient
end
end
8 changes: 8 additions & 0 deletions app/models/private/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Private::Message < ApplicationRecord
self.table_name = 'private_messages'

belongs_to :user
belongs_to :conversation,
class_name: "Private::Conversation",
foreign_key: :conversation_id
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable

has_many :posts, dependent: :destroy
has_many :private_messages, class_name: "Private::Message"
has_many :private_conversations,
foreign_key: :sender_id,
class_name: "Private::Conversation"
end
1 change: 1 addition & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="posted-by">Posted by <%= @post.user.name %></div>
<h3><%= @post.title %></h3>
<p><%= @post.content %></p>
<%= render contact_user_partial_path %>
</div>
</div><!-- row -->
</div>
3 changes: 3 additions & 0 deletions app/views/posts/show/_contact_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="contact-user">
<%= render leave_message_partial_path %>
</div><!-- contact-user -->
3 changes: 3 additions & 0 deletions app/views/posts/show/_login_required.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="text-center">
To contact the user you have to <%= link_to 'Login', login_path %>
</div>
3 changes: 3 additions & 0 deletions app/views/posts/show/contact_user/_already_in_touch.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="contacted-user">
You are already in touch with this user
</div>
12 changes: 12 additions & 0 deletions app/views/posts/show/contact_user/_message_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= form_tag({controller: "private/conversations", action: "create"},
method: "post",
remote: true) do %>
<%= hidden_field_tag(:post_id, @post.id) %>
<%= text_area_tag(:message_body,
nil,
rows: 3,
class: 'form-control',
placeholder: 'Send a message to the user') %>
<%= submit_tag('Send a message', class: 'btn send-message-to-user') %>
<% end %>

1 change: 1 addition & 0 deletions app/views/posts/show/contact_user/message_form/_fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('.contact-user').replaceWith('<div>Message has not been sent</div>');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$('.contact-user').replaceWith('\
<div class="contact-user">\
<div class="contacted-user">Message has been sent</div>\
</div>');
20 changes: 20 additions & 0 deletions app/views/private/conversations/_conversation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% @recipient = private_conv_recipient(conversation) %>
<% @is_messenger = false %>
<li class="conversation-window"
id="pc<%= conversation.id %>"
data-pconversation-user-name="<%= @recipient.name %>"
data-turbolinks-permanent>
<div class="panel panel-default" data-pconversation-id="<%= conversation.id %>">
<%= render 'private/conversations/conversation/heading',
conversation: conversation %>

<!-- Conversation window's content -->
<div class="panel-body">
<%= render 'private/conversations/conversation/messages_list',
conversation: conversation %>
<%= render 'private/conversations/conversation/new_message_form',
conversation: conversation,
user: user %>
</div><!-- panel-body -->
</div>
</li><!-- conversation-window -->
11 changes: 11 additions & 0 deletions app/views/private/conversations/conversation/_heading.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="panel-heading conversation-heading">
<span class="contact-name-notif"><%= @recipient.name %></span>
</div> <!-- conversation-heading -->

<!-- Close conversation button -->
<%= link_to "X",
close_private_conversation_path(conversation),
class: 'close-conversation',
title: 'Close',
remote: true,
method: :post %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="messages-list">
<%= render load_private_messages(conversation), conversation: conversation %>
<div class="loading-more-messages">
<i class="fa fa-spinner" aria-hidden="true"></i>
</div>
<!-- messages -->
<ul>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= link_to "Load messages",
private_messages_path(:conversation_id => conversation.id,
:messages_to_display_offset => @messages_to_display_offset,
:is_messenger => @is_messenger),
class: 'load-more-messages',
remote: true %>
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@
get 'team'
end
end

namespace :private do
resources :conversations, only: [:create] do
member do
post :close
end
end
resources :messages, only: [:index, :create]
end
end
14 changes: 14 additions & 0 deletions db/migrate/20211211044212_create_private_conversations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreatePrivateConversations < ActiveRecord::Migration[5.1]
def change
create_table :private_conversations do |t|
t.integer :recipient_id
t.integer :sender_id

t.timestamps
end

add_index :private_conversations, :recipient_id
add_index :private_conversations, :sender_id
add_index :private_conversations, [:recipient_id, :sender_id], unique: true
end
end
12 changes: 12 additions & 0 deletions db/migrate/20211211044226_create_private_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreatePrivateMessages < ActiveRecord::Migration[5.1]
def change
create_table :private_messages do |t|
t.text :body
t.references :user, foreign_key: true
t.belongs_to :conversation, index: true
t.boolean :seen, default: false

t.timestamps
end
end
end
24 changes: 23 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20211111154915) do
ActiveRecord::Schema.define(version: 20211211044226) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -31,6 +31,27 @@
t.index ["user_id"], name: "index_posts_on_user_id"
end

create_table "private_conversations", force: :cascade do |t|
t.integer "recipient_id"
t.integer "sender_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["recipient_id", "sender_id"], name: "index_private_conversations_on_recipient_id_and_sender_id", unique: true
t.index ["recipient_id"], name: "index_private_conversations_on_recipient_id"
t.index ["sender_id"], name: "index_private_conversations_on_sender_id"
end

create_table "private_messages", force: :cascade do |t|
t.text "body"
t.bigint "user_id"
t.bigint "conversation_id"
t.boolean "seen", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["conversation_id"], name: "index_private_messages_on_conversation_id"
t.index ["user_id"], name: "index_private_messages_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "name", default: "", null: false
t.string "email", default: "", null: false
Expand All @@ -44,4 +65,5 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "private_messages", "users"
end
5 changes: 5 additions & 0 deletions spec/controllers/private/conversations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Private::ConversationsController, type: :controller do

end
17 changes: 17 additions & 0 deletions spec/factories/private_conversations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FactoryGirl.define do
factory :private_conversation, class: 'Private::Conversation' do
association :recipient, factory: :user
association :sender, factory: :user

factory :private_conversation_with_messages do
transient do
messages_count 1
end

after(:create) do |private_conversation, evaluator|
create_list(:private_message, evaluator.messages_count,
conversation: private_conversation)
end
end
end
end
7 changes: 7 additions & 0 deletions spec/factories/private_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :private_message, class: 'Private::Message' do
body 'a'*20
association :conversation, factory: :private_conversation
user
end
end
Loading