Added messaging feature

This commit is contained in:
MrYummy
2017-05-27 00:34:47 +02:00
parent 992406a20b
commit e378dfab02
23 changed files with 236 additions and 38 deletions

Binary file not shown.

View File

@@ -480,6 +480,11 @@ blockquote p {
padding: 4em 1em 1em;
}
}
.field_container_user {
position: relative;
.editor_field {
}
}
}
ul.dropdown-menu {
@@ -1026,4 +1031,4 @@ nav.pagination {
padding: 0.1em 0.2em;
border-radius: 0.2em;
text-shadow: none;
}
}

View File

@@ -92,4 +92,4 @@ class ForumsController < ApplicationController
a = [:name, :position, :role_read_id, :role_write_id] + add
params.require(:forum).permit(a)
end
end
end

View File

@@ -92,4 +92,4 @@ class ForumthreadsController < ApplicationController
a += add
params.require(:forumthread).permit(a)
end
end
end

View File

@@ -0,0 +1,74 @@
class MessagesController < ApplicationController
before_filter :check_permission, only: [:destroy]
def index
if !current_user
flash[:alert] = "Please log in to see your private messages."
redirect_to blogposts_path
end
@messages = Message.where(user_target: current_user).page(params[:page])
end
def destroy
if @message.user_target.is?(current_user)
if @message.destroy
flash[:notice] = "Message deleted!"
else
flash[:alert] = "There was a problem while deleting this message"
end
else
flash[:alert] = "You are not allowed to delete this message"
end
redirect_to messages_path
end
def create
if !message_params[:user_target_id]
flash[:alert] = "Please enter a valid IGN before sending."
redirect_to new_message_path
return
end
if message_params[:text] == ""
flash[:alert] = "Please write a message before sending."
redirect_to new_message_path
return
end
@message = Message.new(message_params)
if @message.save
flash[:notice] = "Message sent!"
redirect_to messages_path
return
else
flash[:alert] = "Something went wrong while creating your message."
render action: "new"
return
end
end
def new
if !current_user
flash[:alert] = "Please log in to send a private message."
redirect_to blogposts_path
return
end
@message = Message.new
end
def message_params(add = [])
params[:message][:user_target_id] = User.find_by(ign: params[:message][:user_target].gsub(/[@ ]/,"")).try(:id)
params[:message][:user_sender_id] = User.find_by(ign: params[:message][:user_sender]).id
params.require(:message).permit([:text, :user_target_id, :user_sender_id])
end
private
def check_permission
@message = Message.find(params[:id])
unless @message.user_target == current_user
flash[:alert] = "You are not allowed to view this message"
redirect_to home_statics_path
end
end
end

View File

@@ -69,4 +69,4 @@ class ThreadrepliesController < ApplicationController
def reply_params
params.require(:threadreply).permit(:content)
end
end
end

View File

@@ -61,4 +61,4 @@ class Comment < ActiveRecord::Base
background_mailer(mails)
end
end
end

20
app/models/message.rb Normal file
View File

@@ -0,0 +1,20 @@
class Message < ActiveRecord::Base
belongs_to :user_sender, class_name: "User", foreign_key: "user_sender_id"
belongs_to :user_target, class_name: "User", foreign_key: "user_target_id"
validates_presence_of :user_sender, :user_target, :text, on: :create
def sender
@sender ||= if self.user_sender.present?
user_sender
else
User.first
end
end
def target
# can be nil
@target ||= user_target
end
end

View File

@@ -64,4 +64,4 @@ class Threadreply < ActiveRecord::Base
end
background_mailer(mails)
end
end
end

View File

@@ -173,4 +173,4 @@ class User < ActiveRecord::Base
def set_email_token
self.email_token ||= SecureRandom.hex(16)
end
end
end

View File

@@ -8,4 +8,4 @@
<%= text_area_tag name, content, options %>
<div class="preview"><i>(Loading...)</i></div>
</div>
</div>
</div>

View File

@@ -0,0 +1,8 @@
<div class="md_editor">
<div class="field_container_user">
<% options = (defined?(options) && options || {}) %>
<% options[:class] = "#{options[:class]} editor_field" %>
<% options[:placeholder] ||= "Enter user's name. prefix with \"@\" to get suggestions." %>
<%= text_field_tag name, content, options %>
</div>
</div>

View File

@@ -1,5 +1,7 @@
<% title "News" %>
<% if current_user %>
<%= link_to "Private Messages (#{Message.where(user_target: current_user).count})", messages_path, class: "btn right blue" %>
<% end %>
<h1>News</h1>
<%= link_to 'Make new Post', new_blogpost_path, class: "btn blue" if mod? %>
<div id="posts">

View File

@@ -51,4 +51,4 @@
</div>
<% end %>
<%= paginate @threads %>
</div>
</div>

View File

@@ -38,4 +38,4 @@
<p><%= f.submit "Update thread", class: "btn blue left" %></p>
<% end %>
<%= button_to "Delete thread", @thread, :method => "delete", data: {confirm: "Delete thread & comments forever?"}, class: "btn red right" %>
<div class="clear"></div>
<div class="clear"></div>

View File

@@ -0,0 +1,23 @@
<%= link_to "Create new message", new_message_path, class: "btn blue right" %>
<h3>Your private messages:</h3>
<div id="forum_groups">
<% @messages.each do |message| %>
<div class="item-group with-avatar">
<div class="header">
<%= link_to(message.user_sender.avatar(64), message.user_sender, title: message.user_sender.ign) %>
<%= render partial: "users/username", locals: { user: message.user_sender } %>
<%= ago message.created_at %>
<div class="right">
<%= link_to "Delete message", message, :method => "delete", data: {confirm: "Delete this message forever?"} %>
</div>
<div class="clear-right"></div>
</div>
<div class="items">
<div class="item">
<%= render_md(message.text).html_safe %>
</div>
</div>
</div>
<% end %>
<%= paginate @messages %>
</div>

View File

@@ -0,0 +1,19 @@
<h1>Example Text</h1>
<%= form_for @message do |f| %>
</table>
<tr>
<td>
<%= render partial: "md_editor_user", locals: {name: "message[user_target]", content: @message.user_target} %>
</td>
</tr>
<br>
<tr>
<td>
<%= render partial: "md_editor", locals: {name: "message[text]", content: @message.text} %>
</td>
</tr>
</table>
<%= f.hidden_field :user_sender, value: current_user %>
<br>
<p><%= f.submit "Send Message", class: "btn blue left" %></p>
<% end %>

View File

View File

@@ -87,4 +87,4 @@
<span class='red-alert'>This user has not confirmed his email!</span>
<% end %>
<% end %>
<% end %>
<% end %>

View File

@@ -0,0 +1,11 @@
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.text :message
t.references :user_sender
t.references :user_target
t.datetime :created_at
end
end
end

View File

@@ -0,0 +1,5 @@
class ChangeMessageToText < ActiveRecord::Migration
def change
rename_column :messages, :message, :text
end
end

View File

@@ -11,10 +11,10 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160926220738) do
ActiveRecord::Schema.define(version: 20170525184355) do
create_table "blogposts", force: :cascade do |t|
t.string "title"
t.string "title", limit: 191
t.text "content", limit: 65535
t.integer "user_author_id", limit: 4
t.integer "user_editor_id", limit: 4
@@ -32,14 +32,14 @@ ActiveRecord::Schema.define(version: 20160926220738) do
end
create_table "forumgroups", force: :cascade do |t|
t.string "name"
t.string "name", limit: 191
t.integer "position", limit: 4
t.integer "role_read_id", limit: 4
t.integer "role_write_id", limit: 4
end
create_table "forums", force: :cascade do |t|
t.string "name"
t.string "name", limit: 191
t.integer "position", limit: 4
t.integer "role_read_id", limit: 4
t.integer "role_write_id", limit: 4
@@ -52,7 +52,7 @@ ActiveRecord::Schema.define(version: 20160926220738) do
end
create_table "forumthreads", force: :cascade do |t|
t.string "title"
t.string "title", limit: 191
t.text "content", limit: 65535
t.boolean "sticky", default: false
t.boolean "locked", default: false
@@ -65,33 +65,40 @@ ActiveRecord::Schema.define(version: 20160926220738) do
end
create_table "info", force: :cascade do |t|
t.string "title"
t.string "title", limit: 191
t.text "content", limit: 65535
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "labels", force: :cascade do |t|
t.string "name"
t.string "color"
t.string "name", limit: 191
t.string "color", limit: 191
end
create_table "messages", force: :cascade do |t|
t.text "text", limit: 65535
t.integer "user_sender_id", limit: 4
t.integer "user_target_id", limit: 4
t.datetime "created_at"
end
create_table "register_tokens", force: :cascade do |t|
t.string "uuid", null: false
t.string "token", null: false
t.string "email", null: false
t.string "uuid", limit: 32, null: false
t.string "token", limit: 6, null: false
t.string "email", limit: 191, null: false
end
add_index "register_tokens", ["uuid"], name: "index_register_tokens_on_uuid", unique: true, using: :btree
create_table "roles", force: :cascade do |t|
t.string "name"
t.string "name", limit: 191
t.integer "value", limit: 4
t.string "color"
t.string "color", limit: 191
end
create_table "sessions", force: :cascade do |t|
t.string "session_id", null: false
t.string "session_id", limit: 191, null: false
t.text "data", limit: 65535
t.datetime "created_at"
t.datetime "updated_at"
@@ -110,20 +117,20 @@ ActiveRecord::Schema.define(version: 20160926220738) do
end
create_table "users", force: :cascade do |t|
t.string "uuid", null: false
t.string "name", null: false
t.string "password_digest", null: false
t.string "ign", null: false
t.string "email", null: false
t.string "uuid", limit: 191, null: false
t.string "name", limit: 191, null: false
t.string "password_digest", limit: 191, null: false
t.string "ign", limit: 191, null: false
t.string "email", limit: 191, null: false
t.text "about", limit: 65535
t.string "last_ip"
t.string "skype"
t.string "last_ip", limit: 191
t.string "skype", limit: 191
t.boolean "skype_public", default: false
t.string "youtube"
t.string "youtube_channelname"
t.string "twitter"
t.string "youtube", limit: 191
t.string "youtube_channelname", limit: 191
t.string "twitter", limit: 191
t.boolean "donor", default: false
t.string "email_token"
t.string "email_token", limit: 191
t.boolean "confirmed", default: false
t.datetime "last_seen"
t.integer "role_id", limit: 4, null: false

View File

@@ -38,4 +38,28 @@ User.create!(
password: "123456789", # high seructity!
password_confirmation: "123456789",
role: Role.get(:superadmin)
)
)
User.create!(
uuid: "7f52491ab5d64c11b4a43806db47a101",
ign: "Yummy_",
name: "Yummy",
email: "yummy@example.com",
password: "123456789", # high seructity!
password_confirmation: "123456789",
role: Role.get(:superadmin)
)
User.create!(
uuid: "62fe35da05ae437ea44b4deae1be1dc4",
ign: "Logal",
email: "logal@example.com",
password: "123456789", # high seructity!
password_confirmation: "123456789",
role: Role.get(:superadmin)
)
Message.create!(
user_sender_id: 2,
user_target_id: 2,
text: "This is a very long message that I will be using to test a plentitude of things. :)",
created_at: Time.utc(0).to_datetime
)