Skip to content

Commit

Permalink
add user group features
Browse files Browse the repository at this point in the history
  • Loading branch information
xixilive committed Jun 1, 2014
1 parent abcc9cf commit 33c64c1
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 6 deletions.
47 changes: 45 additions & 2 deletions controllers/apis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ def self.read_body io, parse_json = true
def self.included base
base.class_eval do

before /^\/api\/(message|user|menu|media)/ do
before /^\/api\/(?<!token)/ do
@app ||= Wecheat::Models::App.find_by_access_token(params[:access_token])
halt(json errcode: 40012) if @app.nil?
end

# to receive message from out-site app
post '/api/message/custom/send' do
Wecheat::Utils.log_received_message({app: @app.label, response: Apis.read_body(request.body, false)}.to_json)
json errcode: 0
Expand All @@ -35,6 +34,50 @@ def self.included base
json @app.user(params[:openid]) || {errcode: 46004}
end

post '/api/groups/create' do
group = Wecheat::Models::Group.new(params[:group])
if group
@app.groups << group
@app.save
json group: group
else
json errcode: 40050
end
end

get '/api/groups/get' do
json groups: @app.groups.collect{|g| {id: g.id, name: g.name, count: @app.users.select{|u| u.group_id.to_s == g.id.to_s }.size } }
end

post '/api/groups/getid' do
if user = @app.user(params[:openid])
json group_id: user.group_id
else
json errcode: 40003
end
end

post '/api/groups/update' do
group = @app.group((params[:group]||{})[:id])
if group
group.name = params[:group][:name]
@app.save
json errcode: 0
else
json json errcode: 40050
end
end

post '/api/groups/members/update' do
if user = @app.user(params[:openid])
user.group_id = params[:to_groupid]
@app.save
json errcode: 0
else
json errcode: 40050
end
end

post '/api/menu/create' do
if data = Apis.read_body(request.body)
@app.button = Wecheat::Models::Button.new(data)
Expand Down
14 changes: 11 additions & 3 deletions models/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class App < Hashie::Dash
property :url, default: ''
property :users, default: []
property :medias, default: []
property :articles, default: []
property :button
property :groups, default: []

def self.find_by_access_token token
self.all.select{|app| app.access_token == token }.first
Expand Down Expand Up @@ -61,8 +61,16 @@ def medias_by_type type
self.medias.select{|m| m.type.to_s == type.to_s }
end

def article id
find_resource :articles, :id, id
def group id
find_resource :groups, :id, id
end

def group_users gid
users.select{|u| u.group_id.to_s == gid.to_s }
end

def user_group_name u
(group(u.group_id)||{})[:name]
end

private
Expand Down
13 changes: 13 additions & 0 deletions models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Wecheat::Models
class Group < Hashie::Dash
include Hashie::Extensions::IgnoreUndeclared
property :id, required: true
property :name, required: true, default: 'unamed'

def initialize(attributes = {}, &block)
attributes[:id] ||= Time.now.strftime('%s%L')
super(attributes, &block)
end

end
end
2 changes: 1 addition & 1 deletion models/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Media < Hashie::Dash
property :path, required: true, default: ''

def initialize(attributes = {}, &block)
attributes[:id] ||= (Time.now.to_i | rand(100000))
attributes[:id] ||= Time.now.strftime('%s%L')
super(attributes, &block)
end
end
Expand Down
1 change: 1 addition & 0 deletions models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < Hashie::Dash
property :latitude, required: true
property :longitude, required: true
property :precision, required: true
property :group_id

def initialize(attributes = {}, &block)
attributes[:openid] ||= Wecheat::Utils.rand_openid
Expand Down
12 changes: 12 additions & 0 deletions views/_app_groups.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="panel panel-default">
<div class="panel-heading">Groups<span class="badge pull-right"><%= app.groups.size %></span></div>
<ul class="list-group">
<% app.groups.each do |group| %>
<li class="list-group-item">
<span class="label label-default">ID</span> <%= group.id %>
<span class="label label-default">Name</span> <%= group.name %>
<span class="badge"><%= app.group_users(group.id).size %> users</span>
</li>
<% end %>
</ul>
</div>
1 change: 1 addition & 0 deletions views/_app_users.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<li class="list-group-item clearfix">
<span class="label label-default">OpenID</span> <span class="text-muted"><%= user.openid %></span>
<span class="label label-default">Nickname</span> <a class="btn btn-link" data-toggle="modal" href="#user-modal-<%= user.openid %>"><%= user.nickname %></a>
<span class="label label-default">Group</span> <%= app.user_group_name(user) %>
</li>
<li class="list-group-item">
<div class="row">
Expand Down
1 change: 1 addition & 0 deletions views/app.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</div>

<div class="col-sm-7">
<%= erb :_app_groups, locals: {app: app} %>
<%= erb :_app_users, locals: {app: app} %>
<%= erb :_app_medias, locals: {app: app} %>
</div>
Expand Down

0 comments on commit 33c64c1

Please sign in to comment.