Class: Post

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ApplicationHelper, Diaspora::Guid, Diaspora::Webhooks, ROXML
Defined in:
app/models/post.rb

Overview

Copyright © 2010, Diaspora Inc. This file is

  licensed under the Affero General Public License version 3 or later.  See
  the COPYRIGHT file.

Constant Summary

@@per_page =
10

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) diaspora_initialize(params)



48
49
50
51
52
53
54
55
# File 'app/models/post.rb', line 48

def self.diaspora_initialize params
  new_post = self.new params.to_hash
  new_post.author = params[:author]
  new_post.public = params[:public] if params[:public]
  new_post.pending = params[:pending] if params[:pending]
  new_post.diaspora_handle = new_post.author.diaspora_handle
  new_post
end

Instance Method Details

- (Boolean) activity_streams?

Returns:

  • (Boolean)


118
119
120
# File 'app/models/post.rb', line 118

def activity_streams?
  false
end

- (Object) as_json(opts = {})



57
58
59
60
61
62
63
64
# File 'app/models/post.rb', line 57

def as_json(opts={})
  {
      :post => {
          :id     => self.guid,
          :author => self.author.as_json,
      }
  }
end

- (Object) diaspora_handle



32
33
34
# File 'app/models/post.rb', line 32

def diaspora_handle
  read_attribute(:diaspora_handle) || self.author.diaspora_handle
end

- (Object) diaspora_handle=(nd)



43
44
45
46
# File 'app/models/post.rb', line 43

def diaspora_handle= nd
  self.author = Person.where(:diaspora_handle => nd).first
  write_attribute(:diaspora_handle, nd)
end

- (Boolean) mutable?

Returns:

  • (Boolean)


66
67
68
# File 'app/models/post.rb', line 66

def mutable?
  false
end

- (Object) receive(user, person)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/post.rb', line 82

def receive(user, person)
  #exists locally, but you dont know about it
  #does not exsist locally, and you dont know about it
  #exists_locally?
  #you know about it, and it is mutable
  #you know about it, and it is not mutable

  local_post = Post.where(:guid => self.guid).first
  if local_post && local_post.author_id == self.author_id
    known_post = user.visible_posts.where(:guid => self.guid).first
    if known_post
      if known_post.mutable?
        known_post.update_attributes(self.attributes)
      else
        Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason=immutable existing_post=#{known_post.id}")
      end
    else
      user.contact_for(person).receive_post(local_post)
      user.notify_if_mentioned(local_post)
      Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}")
      return local_post
    end
  elsif !local_post
    if self.save
      user.contact_for(person).receive_post(self)
      user.notify_if_mentioned(self)
      Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}")
      return self
    else
      Rails.logger.info("event=receive payload_type=#{self.class} update=false status=abort sender=#{self.diaspora_handle} reason=#{self.errors.full_messages}")
    end
  else
    Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason='update not from post owner' existing_post=#{self.id}")
  end
end

- (Array<Person>) subscribers(user)

The list of people that should receive this Post.

Parameters:

  • user (User)

    The context, or dispatching user.

Returns:

  • (Array<Person>)

    The list of subscribers to this post



74
75
76
77
78
79
80
# File 'app/models/post.rb', line 74

def subscribers(user)
  if self.public?
    user.contact_people
  else
    user.people_in_aspects(user.aspects_with_post(self.id))
  end
end

- (Object) user_refs



35
36
37
38
39
40
41
# File 'app/models/post.rb', line 35

def user_refs
  if AspectVisibility.exists?(:post_id => self.id)
    self.post_visibilities.count + 1
  else
    self.post_visibilities.count
  end
end