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



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 13

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

  return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless col

  updates = {}

  # Check for removals
  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
    updates[:displayname] = nil if body.match?(/displayname/i) && !dn
    updates[:description] = nil if body.match?(/calendar-description/i) && !desc
    updates[:color] = nil if body.match?(/calendar-color/i) && !color
  end

  updates[:displayname] = dn if dn
  updates[:description] = desc if desc
  updates[:color] = color if color

  storage.update_collection(col_path.to_s, updates)

  response_xml = <<~XML
    <d:response>
      <d:href>#{Protocol::Caldav::Xml.escape(col_path.to_s)}</d:href>
      <d:propstat>
        <d:prop/>
        <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
    </d:response>
  XML

  xml = Protocol::Caldav::Multistatus.new([response_xml]).to_xml
  [207, Protocol::Caldav::Constants::DAV_HEADERS, [xml]]
end