summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMrYummy <elemental428@gmail.com>2017-05-27 00:34:47 +0200
committerMrYummy <elemental428@gmail.com>2017-05-27 00:34:47 +0200
commite378dfab02d7c113b5af068feee63c01f56295f6 (patch)
tree8a784bffca014b9cd4ab477e1f136ab6a52843ce
parent992406a20b90d47d1ef8a9b4f8d0c599590a1171 (diff)
Added messaging feature
-rw-r--r--app/assets/stylesheets/.style.css.scss.swpbin0 -> 36864 bytes
-rw-r--r--app/assets/stylesheets/style.css.scss7
-rw-r--r--app/controllers/forums_controller.rb2
-rw-r--r--app/controllers/forumthreads_controller.rb2
-rw-r--r--app/controllers/messages_controller.rb74
-rw-r--r--app/controllers/threadreplies_controller.rb2
-rw-r--r--app/models/comment.rb2
-rw-r--r--app/models/message.rb20
-rw-r--r--app/models/threadreply.rb2
-rw-r--r--app/models/user.rb2
-rw-r--r--app/views/application/_md_editor.html.erb2
-rw-r--r--app/views/application/_md_editor_user.html.erb8
-rw-r--r--app/views/blogposts/index.html.erb4
-rw-r--r--app/views/forums/show.html.erb2
-rw-r--r--app/views/forumthreads/edit.html.erb2
-rw-r--r--app/views/messages/index.html.erb23
-rw-r--r--app/views/messages/new.html.erb19
-rw-r--r--app/views/messages/show.html.erb0
-rw-r--r--app/views/users/edit.html.erb2
-rw-r--r--db/migrate/20170524181458_create_messages.rb11
-rw-r--r--db/migrate/20170525184355_change_message_to_text.rb5
-rw-r--r--db/schema.rb57
-rw-r--r--db/seeds.rb26
23 files changed, 236 insertions, 38 deletions
diff --git a/app/assets/stylesheets/.style.css.scss.swp b/app/assets/stylesheets/.style.css.scss.swp
new file mode 100644
index 0000000..cb49b61
--- /dev/null
+++ b/app/assets/stylesheets/.style.css.scss.swp
Binary files differ
diff --git a/app/assets/stylesheets/style.css.scss b/app/assets/stylesheets/style.css.scss
index 6de5aa2..de55b5b 100644
--- a/app/assets/stylesheets/style.css.scss
+++ b/app/assets/stylesheets/style.css.scss
@@ -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;
-} \ No newline at end of file
+}
diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb
index ecf570e..7d95bc6 100644
--- a/app/controllers/forums_controller.rb
+++ b/app/controllers/forums_controller.rb
@@ -92,4 +92,4 @@ class ForumsController < ApplicationController
a = [:name, :position, :role_read_id, :role_write_id] + add
params.require(:forum).permit(a)
end
-end \ No newline at end of file
+end
diff --git a/app/controllers/forumthreads_controller.rb b/app/controllers/forumthreads_controller.rb
index b9b5714..86af50c 100644
--- a/app/controllers/forumthreads_controller.rb
+++ b/app/controllers/forumthreads_controller.rb
@@ -92,4 +92,4 @@ class ForumthreadsController < ApplicationController
a += add
params.require(:forumthread).permit(a)
end
-end \ No newline at end of file
+end
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
new file mode 100644
index 0000000..e597d94
--- /dev/null
+++ b/app/controllers/messages_controller.rb
@@ -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
diff --git a/app/controllers/threadreplies_controller.rb b/app/controllers/threadreplies_controller.rb
index 946155d..dc28638 100644
--- a/app/controllers/threadreplies_controller.rb
+++ b/app/controllers/threadreplies_controller.rb
@@ -69,4 +69,4 @@ class ThreadrepliesController < ApplicationController
def reply_params
params.require(:threadreply).permit(:content)
end
-end \ No newline at end of file
+end
diff --git a/app/models/comment.rb b/app/models/comment.rb
index 35a9a60..951d684 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -61,4 +61,4 @@ class Comment < ActiveRecord::Base
background_mailer(mails)
end
-end \ No newline at end of file
+end
diff --git a/app/models/message.rb b/app/models/message.rb
new file mode 100644
index 0000000..cce952f
--- /dev/null
+++ b/app/models/message.rb
@@ -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
diff --git a/app/models/threadreply.rb b/app/models/threadreply.rb
index 47b0d97..f285073 100644
--- a/app/models/threadreply.rb
+++ b/app/models/threadreply.rb
@@ -64,4 +64,4 @@ class Threadreply < ActiveRecord::Base
end
background_mailer(mails)
end
-end \ No newline at end of file
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index c422e28..9ddd4f0 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -173,4 +173,4 @@ class User < ActiveRecord::Base
def set_email_token
self.email_token ||= SecureRandom.hex(16)
end
-end \ No newline at end of file
+end
diff --git a/app/views/application/_md_editor.html.erb b/app/views/application/_md_editor.html.erb
index 3a18645..ffe1ff1 100644
--- a/app/views/application/_md_editor.html.erb
+++ b/app/views/application/_md_editor.html.erb
@@ -8,4 +8,4 @@
<%= text_area_tag name, content, options %>
<div class="preview"><i>(Loading...)</i></div>
</div>
-</div> \ No newline at end of file
+</div>
diff --git a/app/views/application/_md_editor_user.html.erb b/app/views/application/_md_editor_user.html.erb
new file mode 100644
index 0000000..950b962
--- /dev/null
+++ b/app/views/application/_md_editor_user.html.erb
@@ -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>
diff --git a/app/views/blogposts/index.html.erb b/app/views/blogposts/index.html.erb
index 9dadf42..e838de4 100644
--- a/app/views/blogposts/index.html.erb
+++ b/app/views/blogposts/index.html.erb
@@ -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">
diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb
index 60f3185..6e8ea2f 100644
--- a/app/views/forums/show.html.erb
+++ b/app/views/forums/show.html.erb
@@ -51,4 +51,4 @@
</div>
<% end %>
<%= paginate @threads %>
-</div> \ No newline at end of file
+</div>
diff --git a/app/views/forumthreads/edit.html.erb b/app/views/forumthreads/edit.html.erb
index 5297249..66ffd80 100644
--- a/app/views/forumthreads/edit.html.erb
+++ b/app/views/forumthreads/edit.html.erb
@@ -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> \ No newline at end of file
+<div class="clear"></div>
diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb
new file mode 100644
index 0000000..a3d08b2
--- /dev/null
+++ b/app/views/messages/index.html.erb
@@ -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>
diff --git a/app/views/messages/new.html.erb b/app/views/messages/new.html.erb
new file mode 100644
index 0000000..e40eb35
--- /dev/null
+++ b/app/views/messages/new.html.erb
@@ -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 %>
diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/views/messages/show.html.erb
diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb
index 6a6fe4d..1e27977 100644
--- a/app/views/users/edit.html.erb
+++ b/app/views/users/edit.html.erb
@@ -87,4 +87,4 @@
<span class='red-alert'>This user has not confirmed his email!</span>
<% end %>
<% end %>
-<% end %> \ No newline at end of file
+<% end %>
diff --git a/db/migrate/20170524181458_create_messages.rb b/db/migrate/20170524181458_create_messages.rb
new file mode 100644
index 0000000..a07fd9d
--- /dev/null
+++ b/db/migrate/20170524181458_create_messages.rb
@@ -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
diff --git a/db/migrate/20170525184355_change_message_to_text.rb b/db/migrate/20170525184355_change_message_to_text.rb
new file mode 100644
index 0000000..9bb06a6
--- /dev/null
+++ b/db/migrate/20170525184355_change_message_to_text.rb
@@ -0,0 +1,5 @@
+class ChangeMessageToText < ActiveRecord::Migration
+ def change
+ rename_column :messages, :message, :text
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2c68029..9c631a8 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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
diff --git a/db/seeds.rb b/db/seeds.rb
index 780ddb5..78097d8 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -38,4 +38,28 @@ User.create!(
password: "123456789", # high seructity!
password_confirmation: "123456789",
role: Role.get(:superadmin)
-) \ No newline at end of file
+)
+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
+)