Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reshare.rb « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72e3ccbca5574dd01beb65c43e81ae83c54f9f7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Reshare < Post

  belongs_to :root, :class_name => 'Post'
  validate :root_must_be_public
  attr_accessible :root_id, :public
  validates_presence_of :root, :on => :create

  xml_attr :root_diaspora_id
  xml_attr :root_guid

  before_validation do
    self.public = true
  end

  def root_guid
    self.root.guid
  end

  def root_diaspora_id
    self.root.author.diaspora_handle
  end

  def receive(user, person)
    local_reshare = Reshare.where(:guid => self.guid).first
    if local_reshare && local_reshare.root.author_id == user.person.id
      local_reshare.root.reshares << local_reshare

      if user.contact_for(person)
        local_reshare.receive(user, person)
      end

    else
      super(user, person)
    end
  end

  private

  def after_parse
    root_author = Webfinger.new(@root_diaspora_id).fetch
    root_author.save!

    local_post = Post.where(:guid => @root_guid).select('id').first

    unless local_post && self.root_id = local_post.id
      response = Faraday.get(root_author.url + "/p/#{@root_guid}.xml")
      received_post = Diaspora::Parser.from_xml(response.body)

      unless post = received_post.class.where(:guid => received_post.guid).first
        post = received_post

        if root_author.diaspora_handle != post.diaspora_handle
          raise "Diaspora ID (#{post.diaspora_handle}) in the root does not match the Diaspora ID (#{root_author.diaspora_handle}) specified in the reshare!"
        end

        post.author_id = root_author.id
        post.save!
      end

      self.root_id = post.id
    end
  end

  def root_must_be_public
    if self.root && !self.root.public
      errors[:base] << "you must reshare public posts"
      return false
    end
  end
end