-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b46f84
commit 028074e
Showing
8 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* Styles for messages/conversations*/ | ||
.messages-container { | ||
max-width: 800px; | ||
margin: 40px auto; | ||
padding: 20px; | ||
} | ||
|
||
.conversations-list { | ||
margin-top: 20px; | ||
} | ||
|
||
.conversation-item { | ||
display: block; | ||
padding: 15px; | ||
border: 1px solid #e1e1e1; | ||
margin-bottom: 10px; | ||
border-radius: 8px; | ||
text-decoration: none; | ||
color: inherit; | ||
transition: background-color 0.2s; | ||
} | ||
|
||
.conversation-item:hover { | ||
background-color: #f5f5f5; | ||
} | ||
|
||
.conversation-container { | ||
max-width: 800px; | ||
margin: 40px auto; | ||
padding: 20px; | ||
} | ||
|
||
.messages-list { | ||
margin: 20px 0; | ||
padding: 20px; | ||
border: 1px solid #e1e1e1; | ||
border-radius: 8px; | ||
height: 400px; | ||
overflow-y: auto; | ||
} | ||
|
||
.message { | ||
margin-bottom: 15px; | ||
max-width: 70%; | ||
} | ||
|
||
.message.sent { | ||
margin-left: auto; | ||
} | ||
|
||
.message.received { | ||
margin-right: auto; | ||
} | ||
|
||
.message-content { | ||
padding: 10px 15px; | ||
border-radius: 15px; | ||
background-color: #f0f0f0; | ||
} | ||
|
||
.message.sent .message-content { | ||
background-color: #007bff; | ||
color: white; | ||
} | ||
|
||
.message-timestamp { | ||
font-size: 12px; | ||
color: #666; | ||
margin-top: 5px; | ||
text-align: right; | ||
} | ||
|
||
.message-form { | ||
margin-top: 20px; | ||
} | ||
|
||
.message-form textarea { | ||
width: 100%; | ||
padding: 10px; | ||
border: 1px solid #ddd; | ||
border-radius: 8px; | ||
resize: vertical; | ||
} | ||
|
||
.send-button { | ||
background-color: #007bff; | ||
color: white; | ||
padding: 10px 20px; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
margin-top: 10px; | ||
} | ||
|
||
.send-button:hover { | ||
background-color: #0056b3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
class MessageController < ApplicationController | ||
before_action :require_login | ||
|
||
# Get unique users that current user has sent/received messages to/from | ||
def index | ||
sent_to_users = Message.where(from_id: session[:user]["id"]).includes(:to).map(&:to) | ||
received_from_users = Message.where(to_id: session[:user]["id"]).includes(:from).map(&:from) | ||
@users = (sent_to_users + received_from_users).uniq | ||
end | ||
|
||
# Query and get all messages between current and other user | ||
def show_messages | ||
@user2 = User.find(params[:user_id]) | ||
@messages = Message.where( | ||
"(from_id = ? AND to_id = ?) OR (from_id = ? AND to_id = ?)", | ||
session[:user]["id"], @user2.id, | ||
@user2.id, session[:user]["id"] | ||
).order(created_at: :asc) | ||
@new_message = Message.new | ||
end | ||
|
||
def create | ||
@message = Message.new(message_params) | ||
@message.from_id = session[:user]["id"] | ||
@message.to_id = params[:user_id] | ||
|
||
if @message.save | ||
redirect_to conversation_path(params[:user_id]), notice: "Message sent!" | ||
else | ||
redirect_to conversation_path(params[:user_id]), alert: "Failed to send message." | ||
end | ||
end | ||
|
||
def require_login | ||
unless session[:user] | ||
redirect_to login_path | ||
end | ||
end | ||
|
||
private | ||
|
||
# require content for message | ||
def message_params | ||
params.require(:message).permit(:content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div class="messages-container"> | ||
<h1>Your Messages</h1> | ||
|
||
<% if @users.empty? %> | ||
<p>No messages yet!</p> | ||
<% else %> | ||
<div class="conversations-list"> | ||
<% @users.each do |user| %> | ||
<%= link_to conversation_path(user), class: "conversation-item" do %> | ||
<div class="user-info"> | ||
<h3><%= user.username %></h3> | ||
</div> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<div class="conversation-container"> | ||
<h1>Conversation with <%= @user2.username %></h1> | ||
|
||
<% if flash[:notice] %> | ||
<div class="alert alert-success"> | ||
<%= flash[:notice] %> | ||
</div> | ||
<% end %> | ||
|
||
<% if flash[:alert] %> | ||
<div class="alert alert-danger"> | ||
<%= flash[:alert] %> | ||
</div> | ||
<% end %> | ||
|
||
<div class="messages-list"> | ||
<% @messages.each do |message| %> | ||
<div class="message <%= message.from_id == session[:user]["id"] ? 'sent' : 'received' %>"> | ||
<div class="message-content"> | ||
<%= message.content %> | ||
</div> | ||
<div class="message-timestamp"> | ||
<%= time_ago_in_words(message.created_at) %> ago | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<%= form_with(model: @new_message, url: conversation_path(@user2), local: true) do |f| %> | ||
<div class="message-form"> | ||
<%= f.text_area :content, placeholder: "Enter message...", class: "message-input" %> | ||
<%= f.submit "Send", class: "send-button" %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters