diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..c2c566e --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: bundle exec puma -C config/puma.rb diff --git a/README.md b/README.md index b106979..1259433 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,24 @@ # README -これは、LINEを模して製作された簡易チャットアプリです +LINEを模して製作された簡易チャットアプリです。 ProgateでRuby on Railsを学んだ直後に作られました。 -製作者:Taishi Murakami -製作時間:1週間 +製作者:Taishi Murakami +製作時間:1週間 -主な機能: - メール認証によるユーザー登録機能 - ログイン/ログアウト機能 - 登録情報の変更機能 - 1対1でのチャット機能(メイン) +-使用言語・バージョン + -Ruby 2.5.1 + -Rails 5.2.0 -参考にしたサイト: - Deviseの導入:https://qiita.com/cigalecigales/items/f4274088f20832252374 - ActionCableの導入:https://qiita.com/jnchito/items/aec75fab42804287d71b - 1対1チャット機能の実装:https://qiita.com/YN6127yn/items/7ddd966141cca195b4da - DB設計:https://railsguides.jp/association_basics.html +-主な機能: + -メール認証によるユーザー登録機能 + -ログイン/ログアウト機能 + -登録情報の変更機能 + -1対1でのチャット機能(メイン) + +-備考 + -ローカル環境での一作目なのでかろうじて動くだけのダメアプリです。テストもリファクタリングもしていません。悪しからず。 + +-参考にしたサイト: + -Deviseの導入:[deviseの使い方(rails5版)](https://qiita.com/cigalecigales/items/f4274088f20832252374) + -ActionCableの導入:[Rails 5 + ActionCableで作る!シンプルなチャットアプリ(DHH氏のデモ動画より)](https://qiita.com/jnchito/items/aec75fab42804287d71b) + -1対1チャット機能の実装:[Ruby on Rails チュートリアル 機能拡張5(メッセージ機能)](https://qiita.com/YN6127yn/items/7ddd966141cca195b4da) + -DB設計:[Rails Guide](https://railsguides.jp/association_basics.html) diff --git a/app/assets/javascripts/channels/room.coffee b/app/assets/javascripts/channels/room.coffee index 843155f..158006a 100644 --- a/app/assets/javascripts/channels/room.coffee +++ b/app/assets/javascripts/channels/room.coffee @@ -1,4 +1,6 @@ App.room = App.cable.subscriptions.create "RoomChannel", + + connected: -> # Called when the subscription is ready for use on the server @@ -10,10 +12,12 @@ App.room = App.cable.subscriptions.create "RoomChannel", # Called when there's incoming data on the websocket for this channel speak:(message) -> - @perform 'speak', message: message + to_id =$('input:hidden[name="to_id"]').val() + @perform 'speak', {message: message,to_id:to_id} $(document).on 'keypress', '[data-behavior~=room_speaker]', (event) -> if event.keyCode is 13 # return = send + # Roomチャンネルのspeakメソッドを実行 App.room.speak event.target.value event.target.value = '' event.preventDefault() diff --git a/app/assets/stylesheets/custom.scss b/app/assets/stylesheets/custom.scss new file mode 100644 index 0000000..33e18d5 --- /dev/null +++ b/app/assets/stylesheets/custom.scss @@ -0,0 +1,169 @@ +/* reset ================================ */ +* { + box-sizing: border-box; + text-align: center; +} + +html { + font: 100%/1.5 'Avenir Next', 'Hiragino Sans', sans-serif; + line-height: 1.7; + letter-spacing: 1px; +} + +ul, li { + list-style-type: none; + padding: 0; + margin: 0; +} + +a { + text-decoration: none; + color: #2d3133; + font-size: 14px; + font-weight: bold; +} + +h1, h2, h3, h4, h5, h6, p { + margin: 0; +} + +input { + background-color: transparent; + outline-width: 0; +} + +form input[type="submit"] { + border: none; + cursor: pointer; +} + +/* 共通レイアウト ================================ */ +body { + color: #2d3133; + margin: 0; + min-height: 1vh; +} + +.main { + position: absolute; + top: 64px; + width: 100%; + height: auto; + min-height: 100%; + background-color: #f5f8fa; +} + +.container { + max-width: 600px; + margin: 30px auto; + padding-left: 15px; + padding-right: 15px; + clear: both; +} + +/* ヘッダー ================================ */ +header { + height: 64px; + z-index: 1; + width: 100%; + background-color: #5ae628; +} + +.header-logo { + float: left; + padding-left: 20px; + color: white; + font-size: 22px; + line-height: 64px; +} + +.header-logo a{ + color: white; + font-size: 40px; +} + +.header-menus { + float: right; + padding-right: 20px; +} + +.header-menus li { + float: left; + line-height: 64px; + font-size: 13px; + color: white; + padding-left: 15px; +} + +.header-menus a { + float: left; + font-size: 20px; + color: white; +} + +.header-menus .fa { + padding-right: 5px; +} + +.header-menus input[type="submit"] { + padding: 0 20px; + float: left; + line-height: 64px; + color: white; + margin: 0; + font-size: 13px; +} + +.notice,.alert{ + color:white; + background-color:orange; +} + +.top-page{ + margin-top:30px; +} + +.top-page h1,h2{ + padding:20px; +} + +.top-page p{ + padding:5px 10px; + background-color:#5ae628; + display:inline-block; +} + +.top-page a{ + color:white; +} + +.my-page{ + padding-top: 10px; +} + +.user-right { + width: 50%; + text-align: left; + display: table-cell; + vertical-align: middle; +} + +.user-right a{ + font-size: 30px; + font-weight: normal; +} + +.users-index-item { + font-size: 30px; + padding: 20px 30px; + background-color: white; + overflow: hidden; + box-shadow: 0 2px 6px #c1ced7; + display: table; + width: 100%; +} + +#messages{ + width: 100%; + border-top:1px solid gray; +} diff --git a/app/channels/room_channel.rb b/app/channels/room_channel.rb index b70036f..9b0c355 100644 --- a/app/channels/room_channel.rb +++ b/app/channels/room_channel.rb @@ -8,6 +8,20 @@ def unsubscribed end def speak(data) - Message.create! content: data['message'], user_id: current_user.id + user=User.find_by(id: data['to_id']) + room_id = message_room_id(current_user, user) + Message.create! content: data['message'], user_id: current_user.id, + from_id: current_user.id, to_id: user.id, + room_id: room_id + end + + def message_room_id(first_user, second_user) + first_id = first_user.id.to_i + second_id = second_user.id.to_i + if first_id < second_id + "#{first_user.id}-#{second_user.id}" + else + "#{second_user.id}-#{first_user.id}" + end end end diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 5c4b2a9..b3ba933 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -2,6 +2,18 @@ class RoomsController < ApplicationController before_action :sign_in_required, only: [:show] def show - @messages = Message.all + @user=User.find_by(id: params[:id]) + @room_id = message_room_id(current_user, @user) + @messages = Message.where(room_id: @room_id) + end + + def message_room_id(first_user, second_user) + first_id = first_user.id.to_i + second_id = second_user.id.to_i + if first_id < second_id + "#{first_user.id}-#{second_user.id}" + else + "#{second_user.id}-#{first_user.id}" + end end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b223..769f77a 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -2,3 +2,4 @@ class ApplicationMailer < ActionMailer::Base default from: 'from@example.com' layout 'mailer' end + diff --git a/app/models/message.rb b/app/models/message.rb index 663196c..e31d133 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,4 +1,11 @@ class Message < ApplicationRecord after_create_commit { MessageBroadcastJob.perform_later self } belongs_to :user + + # Validations + validates :from_id, presence: true + validates :to_id, presence: true + validates :room_id, presence: true + validates :content, presence: true, length: {maximum: 50} + end diff --git a/app/models/user.rb b/app/models/user.rb index f500414..4a9cf74 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,4 +4,9 @@ class User < ApplicationRecord :confirmable, :lockable, :timeoutable has_many :messages,dependent: :destroy + + # Validations + validates :email, presence: true + validates :username, presence: true + validates :encrypted_password, presence: true end diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index e0c63be..56b7aeb 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -2,7 +2,12 @@

Chat room

-
+ + <% if current_user.id==@user.id %> +
+ <% else%> +
+ <% end %>
diff --git a/app/views/users/top.html.erb b/app/views/users/top.html.erb index 1c18b79..eef3a12 100644 --- a/app/views/users/top.html.erb +++ b/app/views/users/top.html.erb @@ -6,7 +6,7 @@ <% @users.each do |user|%>
-

<%= link_to user.username,root_url%>

+

<%= link_to user.username,"/rooms/#{user.id}"%>

<% end %> diff --git a/config/environments/production.rb b/config/environments/production.rb index b6b4e5e..08fbb31 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -47,7 +47,7 @@ config.action_cable.allowed_request_origins = [ /http:\/\/.*/ ] # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - # config.force_ssl = true + config.force_ssl = true # Use the lowest log level to ensure availability of diagnostic information # when problems arise. diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index b3a7835..d67c9e7 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -8,8 +8,8 @@ # confirmation, reset password and unlock tokens in the database. # Devise will use the `secret_key_base` as its `secret_key` # by default. You can change it below and use your own secret key. - # config.secret_key = '36225be56b61efa00ddd192276401308319f57986469c4f528653eda4f44aed253fc887a07711360b0b3da4e641427cf3e02bd71350b62c10b83407f6acd7d1d' - + config.secret_key = '6ff05a2a5b47194687fe711bcec8a4dfed7f7350c2ab7842cb7cfe8ce0bf52b259b2b72f7635341c80f6437df9fe39b0e5f75a4bbce788bc249a5b6ce1de2f38' + # ==> Controller configuration # Configure the parent class to the devise controllers. # config.parent_controller = 'DeviseController' diff --git a/config/routes.rb b/config/routes.rb index 206dde9..2a089bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do devise_for :user - get 'rooms/show'=> 'rooms#show' + get 'rooms/:id'=> 'rooms#show' get 'users/:id'=>'users#top' diff --git a/db/migrate/20180630084154_add_column_to_messages.rb b/db/migrate/20180630084154_add_column_to_messages.rb deleted file mode 100644 index 1b491d3..0000000 --- a/db/migrate/20180630084154_add_column_to_messages.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddColumnToMessages < ActiveRecord::Migration[5.2] - def change - add_column :messages, :user_id, :integer - end -end diff --git a/db/migrate/20180703042018_add_column_to_messages.rb b/db/migrate/20180703042018_add_column_to_messages.rb new file mode 100644 index 0000000..a091634 --- /dev/null +++ b/db/migrate/20180703042018_add_column_to_messages.rb @@ -0,0 +1,8 @@ +class AddColumnToMessages < ActiveRecord::Migration[5.2] + def change + add_column :messages, :user_id, :integer + add_column :messages, :from_id, :integer + add_column :messages, :to_id, :integer + add_column :messages, :room_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 9e12d2d..dd54dee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_06_30_084154) do +ActiveRecord::Schema.define(version: 2018_07_03_042018) do create_table "messages", force: :cascade do |t| t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" + t.integer "from_id" + t.integer "to_id" + t.string "room_id" end create_table "users", force: :cascade do |t|