Module: Async::Caldav::Handlers::Proppatch

Defined in:
lib/async/caldav/handlers/proppatch.rb

Class Method Summary collapse

Class Method Details

.call(path:, body:, storage:) ⇒ Object



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
# File 'lib/async/caldav/handlers/proppatch.rb', line 9

def call(path:, body:, storage:, **)
  col_path = path.ensure_trailing_slash
  col = storage.get_collection(col_path.to_s)

  if col
    updates = {}
    is_remove = body&.match?(/<[^>]*remove[^>]*>/m)
    dn = Protocol::Caldav::Xml.extract_value(body, 'displayname')
    desc = Protocol::Caldav::Xml.extract_value(body, 'calendar-description')
    color = Protocol::Caldav::Xml.extract_value(body, 'calendar-color')
    if is_remove
      if body.match?(/displayname/i) && !dn
        updates[:displayname] = nil
      end
      if body.match?(/calendar-description/i) && !desc
        updates[:description] = nil
      end
      if body.match?(/calendar-color/i) && !color
        updates[:color] = nil
      end
    end
    if dn
      updates[:displayname] = dn
    end
    if desc
      updates[:description] = desc
    end
    if color
      updates[:color] = color
    end
    storage.update_collection(col_path.to_s, updates)
    xml = Protocol::Caldav::Multistatus.new.to_xml do |x|
      Protocol::Caldav::XmlBuilder.response(x, href: col_path.to_s) do
        x.tag!("d:propstat") do
          x.tag!("d:prop")
          x.tag!("d:status", "HTTP/1.1 200 OK")
        end
      end
    end
    [207, Protocol::Caldav::Constants::DAV_HEADERS, [xml]]
  else
    [404, { 'content-type' => 'text/plain' }, ['Not Found']]
  end
end