function handle()

in site/api/atom.lua [50:198]


function handle(r)
    cross.contentType(r, "application/xhtml+xml; charset=UTF-8")
    local get = r:parseargs()

    
    if not get.list and not get.mid then
        r:puts("<>")
        return cross.OK
    end

    
    
    local qs = "*"
    local dd = 30
    local maxresults = 40
    local account = user.get(r)
    local rights = nil
    local listid = r:escape_html(get.list or "")
    local listraw = "<" .. listid:gsub("@", ".") .. ">"

    
    local sterm = {
                    term = {
                        list_raw = listraw
                    }
                }

    local emls = {}
    emls_thrd = {}

    
    if get.list then
        local threads = {}
        local emails = {}
        local emails_full = {}
        local doc = elastic.raw {
            _source = {'message-id','body','from','subject','epoch','list_raw', 'private'},
            query = {
                bool = {
                    must = {
                        sterm,
                        {
                            query_string = {
                                default_field = "subject",
                                query = qs
                            }
                        },
                        {
                            range = {
                                date = {
                                    gt = "now-1M"
                                }
                            }
                        }
                    }
                }
            },
            sort = {
                {
                    epoch = {
                        order = "desc"
                    }
                }
            },
            size = maxresults
        }

        
        for k = #doc.hits.hits, 1, -1 do
            local v = doc.hits.hits[k]
            local email = v._source
            if aaa.canAccessDoc(r, email, account) then
                local mid = email['message-id']
                local irt = email['in-reply-to']
                email.id = v._id
                email.irt = irt
                email.references = nil
                email.to = nil
                email['in-reply-to'] = nil
                table.insert(emls, 1, email)
            end
        end

    
    elseif get.mid then
        
        local doc = elastic.get("mbox", get.mid)
        if doc then
            
            local parent = utils.findParent(r, doc, elastic)

            
            if parent then
                table.insert(emls_thrd, parent)
                fetchChildren(r, parent)
                
                for k, doc in pairs(emls_thrd) do
                    if aaa.canAccessDoc(r, doc, account) then
                        table.insert(emls, doc)
                    end
                end
            end
        end
    end

    
    local scheme = "https"
    if r.port == 80 then
        scheme = "http"
    end
    local hostname = ("%s://%s:%u"):format(scheme, r.hostname, r.port)
    r:puts(([[<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>%s Archives</title>
<link rel="self" href="%s/atom.lua?list=%s"/>
<link href="%s/list.html?%s"/>
<id>%s/list.html?%s</id>
    ]]):format(listid, hostname, listid, hostname, listid, hostname, listid) )
    for k, eml in pairs(emls) do
        
        local date = os.date("!%Y-%m-%dT%H:%M:%S", eml.epoch) .. "Z"
        r:puts(([[
<entry>
<title>%s</title>
<author><name>%s</name></author>
<link rel="alternate" href="%s/thread.html/%s"/>
<id>urn:uuid:%s</id>
<updated>%s</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<pre>
%s
</pre>
</div>
</content>
</entry>
]]):format(r
           :escape_html(eml.subject),
           r:escape_html(eml.from),
           hostname,
           r:escape_html(eml['message-id']),
           r:escape_html(eml['message-id']),
           date,
           r:escape_html(eml.body:gsub("\x0F", ""))
           ))
    end
    r:puts[[</feed>]]
    return cross.OK
end