update

in www/project/icla/views/actions/update.json.rb [78:171]


def update()
  
  raise ArgumentError.new('token must not be nil') unless @token
  raise ArgumentError.new("Invalid action: '#{@action}'") unless VALID_ACTIONS.include? @action
  raise ArgumentError.new('expectedPhase must not be nil') unless @expectedPhase
  if @newPhase and not VALID_PHASES.include? @newPhase
    raise ArgumentError.new("Invalid @newPhase: '#{@newPhase}'")
  end

  timestamp = Time.now.utc.to_s
  addComment = nil
  voteinfo = nil
  if @action == 'submitVote'
    raise ArgumentError.new("Invalid vote: '#{@vote}'") unless VALID_VOTES.include? @vote
    raise ArgumentError.new('member must not be nil') unless @member
    if @vote == '-1'
      raise ArgumentError.new('-1 vote must have comment') unless @comment
    end
    if @comment 
      voteinfo = {
        'vote' => @vote,
        'comment' => @comment,
        'member' => @member,
        'timestamp' => timestamp,
      }
    else
      voteinfo = {
        'vote' => @vote,
        'member' => @member,
        'timestamp' => timestamp,
      }
    end
  elsif HAS_COMMENT.include? @action
    if @comment
      addComment =
      {
        'comment' => @comment,
        'member' => @member,
        'timestamp' => timestamp,
      }
    else
      raise ArgumentError.new("comment must not be nil for '#{@action}'")
    end
  end

  file = "/srv/icla/#{@token}.json"

  
  contents = {} 
  rewrite = false 
  phases = *@expectedPhase 

  LockFile.lockfile(file, 'r+', File::LOCK_EX) do |f|
    contents = JSON::parse(f.read)
    phase = contents['phase']
    raise ArgumentError.new("Phase '#{phase}': expected '#{@expectedPhase}'") unless @expectedPhase == '*' or phases.include? phase
    if @newPhase && @newPhase != phase
      contents['phase'] = @newPhase
      rewrite = true
    end
    if @action == 'startVoting' 
      comment0 = contents['comments'][0]['comment'] 
      voteinfo = {
        'vote' => '+1',
        'comment' => "#{comment0}\nHere is my +1", 
        'member' => @member,
        'timestamp' => timestamp,
      }
      addComment['comment'] += "\n**Starting the vote.**"
    end
    if voteinfo
      contents['votes'] << voteinfo
      rewrite = true
    end
    if addComment
      contents['comments'] << addComment
      rewrite = true
    end
    if rewrite
      f.rewind 
      f.truncate(0) 
      f.write(JSON.pretty_generate(contents))
    end
  end

  if @action == 'tallyVote'
    sendTally(contents)
  end

  
  _rewrite rewrite 
  contents
end