From 1cfd6b80f9eb91e8efb941989eb50a0018c8573c Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 14 Apr 2015 15:18:58 +0800 Subject: [PATCH] lot of changes for groups --- app/controllers/admin/groups_controller.rb | 68 ++++++++-- app/controllers/orbit_group_controller.rb | 43 ++++++ app/controllers/page_parts_controller.rb | 2 +- app/helpers/admin/groups_helper.rb | 35 +++++ app/models/group.rb | 29 ++-- app/models/group_post.rb | 2 + app/models/group_post_comment.rb | 9 ++ app/models/group_post_file.rb | 9 ++ app/models/member_profile.rb | 2 +- app/models/user.rb | 1 + app/views/admin/groups/_group.html.erb | 2 +- app/views/admin/groups/_post.html.erb | 4 +- app/views/admin/groups/_post_comment.html.erb | 26 ++++ app/views/admin/groups/_post_form.html.erb | 23 +++- app/views/admin/groups/newpost.html.erb | 81 ++++++++++-- app/views/admin/groups/show.html.erb | 4 +- app/views/admin/groups/showpost.html.erb | 125 +++++++----------- config/routes.rb | 3 + 18 files changed, 347 insertions(+), 121 deletions(-) create mode 100644 app/controllers/orbit_group_controller.rb create mode 100644 app/helpers/admin/groups_helper.rb create mode 100644 app/models/group_post_comment.rb create mode 100644 app/models/group_post_file.rb create mode 100644 app/views/admin/groups/_post_comment.html.erb diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index 93512a8..6c87193 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -1,5 +1,5 @@ -class Admin::GroupsController < OrbitMemberController - +class Admin::GroupsController < OrbitGroupController + include Admin::GroupsHelper def index @groups = current_user.groups end @@ -13,31 +13,37 @@ class Admin::GroupsController < OrbitMemberController end def newpost + render_401 and return if !user_can_write? @no_breadcrumb = true - uid = params[:group_id].split("-").last - @group = Group.find_by(:uid => uid) @grouppost = GroupPost.new end def createpost - uid = params[:group_id].split("-").last - group = Group.find_by(:uid => uid) gp = GroupPost.new(post_params) - gp.group = group + gp.group = @group gp.save - redirect_to admin_group_path(group) + redirect_to admin_group_path(@group) end def showpost - uid = params[:id].split("-").last - @post = GroupPost.find_by(:uid => uid) + render_401 and return if !user_can_read? + if (current_user.id.to_s != @post.author.to_s) && (!@post.read_by.include?(current_user.id.to_s)) + @post.read_by << current_user.id.to_s + @post.save + end + @read_by_names = @post.read_by.collect{|rb| + user = User.find(rb) rescue nil + author = (user.member_profile.name == nil ? user.user_name : user.member_profile.name rescue "") if !user.nil? + author + } end def show @no_breadcrumb = true @no_filter = true - uid = params[:id].split("-").last - @group = Group.find_by(:uid => uid) + if !user_can_read? + render_401 and return + end end def create_category @@ -53,6 +59,22 @@ class Admin::GroupsController < OrbitMemberController render :json => {"success" => true,"id" => photo.id.to_s}.to_json end + + def upload_file + file = GroupPostFile.new(post_file_params) + file.save + render :json => {"success" => true,"id" => file.id.to_s}.to_json + end + + def download_file + file_id = params[:id] + file = GroupPostFile.find(file_id) rescue nil + if !file.nil? + redirect_to file.file.url and return + end + render :nothing => true + end + def create group = Group.new(group_params) group.save @@ -77,12 +99,22 @@ class Admin::GroupsController < OrbitMemberController end end + def new_comment + gpc = GroupPostComment.new(comment_params) + gpc.save + render :partial => "post_comment", :collection => [gpc] + end + private def category_params params.require(:category).permit! end + def comment_params + params.require(:group_post_comment).permit! + end + def post_params p = params.require(:group_post).permit! p["author"] = current_user.id @@ -93,7 +125,15 @@ class Admin::GroupsController < OrbitMemberController images << gpi end end + files = [] + p[:group_post_files].each do |id| + gpf = GroupPostFile.find(id) rescue nil + if !gpf.nil? + files << gpf + end + end p[:group_post_images] = images + p[:group_post_files] = files p end @@ -101,6 +141,10 @@ class Admin::GroupsController < OrbitMemberController params.require(:group_post_image).permit! end + def post_file_params + params.require(:group_post_file).permit! + end + def group_params p = params.require(:group).permit! p["user_ids"] << current_user.id.to_s diff --git a/app/controllers/orbit_group_controller.rb b/app/controllers/orbit_group_controller.rb new file mode 100644 index 0000000..b169264 --- /dev/null +++ b/app/controllers/orbit_group_controller.rb @@ -0,0 +1,43 @@ +class OrbitGroupController < ApplicationController + layout "member" + before_action :authenticate_user, :get_group, :set_access_rights + + def get_group + case params[:action] + when "show" + uid = params[:id].split("-").last + @group = Group.find_by(:uid => uid) + when "newpost", "createpost" + uid = params[:group_id].split("-").last + @group = Group.find_by(:uid => uid) + when "showpost" + uid = params[:id].split("-").last + @post = GroupPost.find_by(:uid => uid) + @group = @post.group + end + end + + def set_access_rights + @access_right_level = "none" + read_or_write = "write" + case params[:action] + when "show", "showpost", "newpost" + is_member = @group.users.include?(current_user) + if @group.admins.include?(current_user.id.to_s) + @access_right_level = "admin" + elsif is_member + if read_or_write == "write" + @access_right_level = "write" + elsif read_or_write == "read" + @access_right_level = "read" + end + elsif !is_member && @group.privacy == "open" + @access_right_level = "read" + end + end + end + + def render_401 + render "public/401" + end +end \ No newline at end of file diff --git a/app/controllers/page_parts_controller.rb b/app/controllers/page_parts_controller.rb index 032d7ef..b587268 100644 --- a/app/controllers/page_parts_controller.rb +++ b/app/controllers/page_parts_controller.rb @@ -35,7 +35,7 @@ class PagePartsController < ApplicationController def edit_sub_part @part = part_model.find(params[:part_id]) @subpart = SubPart.find(params[:sub_part_id]) - @select_number = @part.sub_parts.index(@subpart) + @select_number = @part.sub_parts.index{|sp| sp.id.to_s == @subpart.id.to_s} @child_page_count = 0 @kind = @subpart.kind @modules = ModuleApp.widget_enabled diff --git a/app/helpers/admin/groups_helper.rb b/app/helpers/admin/groups_helper.rb new file mode 100644 index 0000000..783bc6e --- /dev/null +++ b/app/helpers/admin/groups_helper.rb @@ -0,0 +1,35 @@ +module Admin::GroupsHelper + def remote_file_exists?(url) + url = URI.parse(url) + Net::HTTP.start(url.host, url.port) do |http| + return http.head(url.request_uri)['Content-Type'].start_with? 'image' + end + end + + def parse_for_images(content) + urls = URI.extract(content) + images = [] + urls.each do |url| + images << url if !url.nil? && remote_file_exists?(url) + end if !urls.empty? + + image_content = "
" + images.each do |img| + content = content.sub(img,"") + image_content = image_content + "
" + end if !images.empty? + return [content, image_content] + end + + def is_user_group_admin? + return (@access_right_level == "admin" ? true : false) + end + + def user_can_write? + return (@access_right_level == "admin" || @access_right_level == "write" ? true : false) + end + + def user_can_read? + return (@access_right_level == "admin" || @access_right_level == "write" || @access_right_level == "read" ? true : false) + end +end \ No newline at end of file diff --git a/app/models/group.rb b/app/models/group.rb index 426f0ad..cfd2053 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,20 +1,23 @@ class Group include Mongoid::Document - include Mongoid::Timestamps - include Slug + include Mongoid::Timestamps + include Slug - field :title, as: :slug_title, type: String, localize: true - field :description, localize: true - field :admins, type: Array, default: [] - field :privacy, default: "closed" - mount_uploader :image, ImageUploader + field :title, as: :slug_title, type: String, localize: true + field :description, localize: true + field :admins, type: Array, default: [] + field :privacy, default: "closed" + field :archive, type: Boolean, default: false + mount_uploader :image, ImageUploader - belongs_to :group_category - has_and_belongs_to_many :users - has_many :group_posts - - scope :closed, ->{ where(privacy: "closed") } - scope :open, ->{ where(privacy: "open") } + belongs_to :group_category + has_and_belongs_to_many :users + has_many :group_posts + + scope :closed, ->{ where(privacy: "closed") } + scope :open, ->{ where(privacy: "open") } + scope :archived, ->{ where(archive: true) } + scope :not_archived, ->{ where(archive: false) } end \ No newline at end of file diff --git a/app/models/group_post.rb b/app/models/group_post.rb index bfc7428..1681258 100644 --- a/app/models/group_post.rb +++ b/app/models/group_post.rb @@ -10,5 +10,7 @@ class GroupPost belongs_to :group has_many :group_post_images, :autosave => true, :dependent => :destroy + has_many :group_post_files, :autosave => true, :dependent => :destroy + has_many :group_post_comments, :autosave => true, :dependent => :destroy end \ No newline at end of file diff --git a/app/models/group_post_comment.rb b/app/models/group_post_comment.rb new file mode 100644 index 0000000..c64e85e --- /dev/null +++ b/app/models/group_post_comment.rb @@ -0,0 +1,9 @@ +class GroupPostComment + include Mongoid::Document + include Mongoid::Timestamps + + field :comment + + belongs_to :user + belongs_to :group_post +end \ No newline at end of file diff --git a/app/models/group_post_file.rb b/app/models/group_post_file.rb new file mode 100644 index 0000000..5060119 --- /dev/null +++ b/app/models/group_post_file.rb @@ -0,0 +1,9 @@ +class GroupPostFile + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + belongs_to :group_post + +end \ No newline at end of file diff --git a/app/models/member_profile.rb b/app/models/member_profile.rb index 324f19b..defc334 100644 --- a/app/models/member_profile.rb +++ b/app/models/member_profile.rb @@ -47,7 +47,7 @@ class MemberProfile def name - if self.first_name || self.last_name + if self.first_name != "" || self.last_name != "" I18n.locale.eql?(:zh_tw) ? "#{self.last_name} #{self.first_name}" : "#{self.first_name} #{self.last_name}" end end diff --git a/app/models/user.rb b/app/models/user.rb index fc44c65..a0e3c0c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,6 +21,7 @@ class User has_many :authorizations belongs_to :member_profile has_and_belongs_to_many :groups + has_many :group_post_comments has_one :facebook, :autosave => true, :dependent => :destroy has_one :google, :autosave => true, :dependent => :destroy # has_one :desktop, :dependent => :destroy diff --git a/app/views/admin/groups/_group.html.erb b/app/views/admin/groups/_group.html.erb index 6c63166..08cecf4 100644 --- a/app/views/admin/groups/_group.html.erb +++ b/app/views/admin/groups/_group.html.erb @@ -22,7 +22,7 @@ group.admins.each_with_index do |admin,i| user = User.find(admin) rescue nil if !user.nil? - author = author + (user.member_profile.name == "" ? user.user_name : user.member_profile.name rescue "") + author = author + (user.member_profile.name == nil ? user.user_name : user.member_profile.name rescue "") author = author + ", " if i != group.admins.count - 1 end end diff --git a/app/views/admin/groups/_post.html.erb b/app/views/admin/groups/_post.html.erb index b473745..0f4edd4 100644 --- a/app/views/admin/groups/_post.html.erb +++ b/app/views/admin/groups/_post.html.erb @@ -2,7 +2,7 @@
<% if !post.group_post_images.blank? %> - <%= post.title %> + <%= post.title %> <% else %> Post image <% end %> @@ -21,7 +21,7 @@ <% user = User.find(post.author) rescue nil if !user.nil? - author = (user.member_profile.name == "" ? user.user_name : user.member_profile.name rescue "") + author = (user.member_profile.name == nil ? user.user_name : user.member_profile.name rescue "") %> <%= author %> <% end %> diff --git a/app/views/admin/groups/_post_comment.html.erb b/app/views/admin/groups/_post_comment.html.erb new file mode 100644 index 0000000..d527a51 --- /dev/null +++ b/app/views/admin/groups/_post_comment.html.erb @@ -0,0 +1,26 @@ +
  • +
    +
    + <% + user = post_comment.user rescue nil + if !user.nil? + author = (user.member_profile.name == nil ? user.user_name : user.member_profile.name rescue "") + avatar = (!user.member_profile.avatar.nil? ? user.member_profile.avatar.thumb.url : "/assets/member-pic.png" rescue "/assets/member-pic.png") + end + %> + Avatar image +
    +
    +
    +
    <%= author %>
    + <% date = DateTime.parse(post_comment.created_at.to_s).strftime("%d %B") %> +
    <%= date %>
    +
    +
    + <% content_images = parse_for_images(post_comment.comment) %> + <%= content_images.first %> + <%= content_images.last.html_safe %> +
    +
    +
    +
  • \ No newline at end of file diff --git a/app/views/admin/groups/_post_form.html.erb b/app/views/admin/groups/_post_form.html.erb index e240329..5638bc5 100644 --- a/app/views/admin/groups/_post_form.html.erb +++ b/app/views/admin/groups/_post_form.html.erb @@ -29,7 +29,9 @@
    - + <% if params[:action] == "editpost" %> + + <% end %>
    @@ -59,12 +61,29 @@
    +
    + +
    + +
    +
    +
    + <% if params[:action] == "editpost" %> + + <% end %> + +
    +
    + +
    +
    +
    - <%= f.submit t('submit'), class: 'btn btn-primary' %> + <%= f.submit t('submit'), class: 'btn btn-primary', id: "create-post-form-btn" %> <%= link_to t('cancel'), admin_groups_path, :class=>"btn" %>
    diff --git a/app/views/admin/groups/newpost.html.erb b/app/views/admin/groups/newpost.html.erb index 58fb3d4..6254a1f 100644 --- a/app/views/admin/groups/newpost.html.erb +++ b/app/views/admin/groups/newpost.html.erb @@ -6,6 +6,7 @@ \ No newline at end of file diff --git a/app/views/admin/groups/show.html.erb b/app/views/admin/groups/show.html.erb index 8be8f51..57d3675 100644 --- a/app/views/admin/groups/show.html.erb +++ b/app/views/admin/groups/show.html.erb @@ -14,7 +14,9 @@

    <%= @group.title %>

    - New Post + <% if user_can_write? %> + New Post + <% end %>
    diff --git a/app/views/admin/groups/showpost.html.erb b/app/views/admin/groups/showpost.html.erb index d226c6a..da554fb 100644 --- a/app/views/admin/groups/showpost.html.erb +++ b/app/views/admin/groups/showpost.html.erb @@ -3,7 +3,8 @@ <% end -%> <% content_for :page_specific_javascript do -%> <%= javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/20140415/jquery.cycle2.min.js" %> - <%= javascript_include_tag "group.js" %> + <%= javascript_include_tag "group" %> + <%= javascript_include_tag "lib/jquery.form" %> <% end -%>
    @@ -15,8 +16,8 @@ <% user = User.find(@post.author) rescue nil if !user.nil? - author = (user.member_profile.name == "" ? user.user_name : user.member_profile.name rescue "") - avatar = (!user.member_profile.avatar.nil? ? user.member_profile.avatar.url : "/assets/member-piv.png" rescue "/assets/member-piv.png") + author = (user.member_profile.name == nil ? user.user_name : user.member_profile.name rescue "") + avatar = (!user.member_profile.avatar.nil? ? user.member_profile.avatar.thumb.url : "/assets/member-pic.png" rescue "/assets/member-pic.png") end %> @@ -51,99 +52,69 @@ <%= @post.content.html_safe %>
    + <% if !@post.group_post_files.blank? %> - My-Awesome-file - My-fantastic-file - My-lovely-file - My-stunning-file - My-Awesome-file - My-fantastic-file - My-lovely-file - My-stunning-file + <% @post.group_post_files.each do |gpf| %> + <%= File.basename(gpf.file.path) %> + <% end %> + <% end %>
    -
    - Seen by: - Harry - Ika - Joshua - Mason - Tina - Shelley - Anna - Poke - Moses -
    + <% if !@read_by_names.blank? %> +
    + Seen by: <%= @read_by_names.count %> + <% @read_by_names.each do |name| %> + <%= name %> + <% end %> +
    + <% end %>
    + <% if user_can_write? %>
    -
    +

    Leave a comment

    - + + +
    - + <% else %> +
    +

    Join the group to comment on this post.

    +
    + <% end %>

    Comments

    -
      -
    1. -
      -
      - Avatar image -
      -
      -
      -
      Joshua Milton
      -
      24 March
      -
      -
      - Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quam quos eveniet enim soluta delectus nisi modi beatae! Facilis eaque aperiam vitae dolorum at perspiciatis commodi. Error dolorem fuga aperiam. -
      -
      -
      -
    2. -
    3. -
      -
      - Avatar image -
      -
      -
      -
      Joshua Milton
      -
      24 March
      -
      -
      - Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quam quos eveniet enim soluta delectus nisi modi beatae! Facilis eaque aperiam vitae dolorum at perspiciatis commodi. Error dolorem fuga aperiam. -
      -
      -
      -
    4. -
    5. -
      -
      - Avatar image -
      -
      -
      -
      Joshua Milton
      -
      24 March
      -
      -
      - Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quam quos eveniet enim soluta delectus nisi modi beatae! Facilis eaque aperiam vitae dolorum at perspiciatis commodi. Error dolorem fuga aperiam. -
      -
      -
      -
    6. -
    +
      + <%= render :partial => "post_comment", :collection => @post.group_post_comments.desc(:created_at) %> + <% if @post.group_post_comments.length == 0 %> +
    1. No comments yet. <%= (user_can_write? ? "Be the first one!" : "") %>
    2. + <% end %> +
    -
    \ No newline at end of file + + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cf9d70d..789ef5c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,6 +126,9 @@ Orbit::Application.routes.draw do get "groups/public" => 'groups#public_groups' get "posts/:id" => 'groups#showpost' post "posts/image" => 'groups#upload_photo' + post "posts/file" => 'groups#upload_file' + get "posts/file/:id" => "groups#download_file" + post "posts/comment" => 'groups#new_comment' resources :groups do get "newpost" => 'groups#newpost'