Class: TentD::Model::ProfileInfo

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, Permissible, TypeProperties, UserScoped
Defined in:
lib/tentd/model/profile_info.rb

Constant Summary collapse

TENT_PROFILE_TYPE_URI =
'https://tent.io/types/info/core/v0.1.0'
TENT_PROFILE_TYPE =
TentType.new(TENT_PROFILE_TYPE_URI)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Permissible

#assign_permissions, included, #permissions_json

Methods included from UserScoped

included

Methods included from TypeProperties

included, #type, #type=

Class Method Details

.create_update_post(id) ⇒ Object



61
62
63
# File 'lib/tentd/model/profile_info.rb', line 61

def self.create_update_post(id)
  first(:id => id).create_update_post
end

.get_profile(authorized_scopes = [], current_auth = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tentd/model/profile_info.rb', line 32

def self.get_profile(authorized_scopes = [], current_auth = nil)
  h = if (authorized_scopes.include?(:read_profile) || authorized_scopes.include?(:write_profile)) && current_auth.respond_to?(:profile_info_types)
    current_auth.profile_info_types.include?('all') ? all : all(:type_base => current_auth.profile_info_types.map { |t| TentType.new(t).base }) + all(:public => true)
  else
    fetch_with_permissions({}, current_auth)
  end.inject({}) do |memo, info|
    memo[info.type.uri] = info.content.merge(:permissions => info.permissions_json(authorized_scopes.include?(:read_permissions)))
    memo
  end
  h
end

.tent_infoObject



28
29
30
# File 'lib/tentd/model/profile_info.rb', line 28

def self.tent_info
  first(:type_base => TENT_PROFILE_TYPE.base, :order => :type_version.desc) || Hashie::Mash.new
end

.update_profile(type, data) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tentd/model/profile_info.rb', line 44

def self.update_profile(type, data)
  data = Hashie::Mash.new(data) unless data.kind_of?(Hashie::Mash)
  type = TentType.new(type)
  perms = data.delete(:permissions)
  if (infos = all(:type_base => type.base)) && (info = infos.pop)
    infos.to_a.each(&:destroy)
    info.type = type
    info.public = data.delete(:public)
    info.content = data
    info.save
  else
    info = create(:type => type, :public => data.delete(:public), :content => data)
  end
  info.assign_permissions(perms)
  info
end

Instance Method Details

#create_update_postObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tentd/model/profile_info.rb', line 65

def create_update_post
  post = user.posts.create(
    :type => 'https://tent.io/types/post/profile/v0.1.0',
    :entity => user.profile_entity,
    :original => true,
    :content => {
      :action => 'update',
      :types => [self.type.uri],
    }
  )
  Permission.copy(self, post)
  Notifications.trigger(:type => post.type.uri, :post_id => post.id)
end