;; Federation link-card fragment handler
;;
;; Renders actor profile preview card(s) by username.
;; Supports single mode (?slug=x or ?username=x) and batch mode (?keys=x,y,z).

(defhandler link-card (&key username slug keys)
  (if keys
    (let ((usernames (filter (fn (u) (not (empty? u)))
                      (map trim (split keys ",")))))
      (<> (map (fn (u)
        (let ((actor (service "federation" "get-actor-by-username" :username u)))
          (<> (str "<!-- fragment:" u " -->")
              (when (not (nil? actor))
                (~link-card
                  :link (app-url "federation"
                          (str "/users/" (get actor "preferred_username")))
                  :title (or (get actor "display_name")
                             (get actor "preferred_username"))
                  :image nil
                  :icon "fas fa-user"
                  :subtitle (when (get actor "preferred_username")
                              (str "@" (get actor "preferred_username")))
                  :detail (get actor "summary")
                  :data-app "federation"))))) usernames)))
    (let ((lookup (or username slug)))
      (when (not (empty? (or lookup "")))
        (let ((actor (service "federation" "get-actor-by-username"
                       :username lookup)))
          (when (not (nil? actor))
            (~link-card
              :link (app-url "federation"
                      (str "/users/" (get actor "preferred_username")))
              :title (or (get actor "display_name")
                         (get actor "preferred_username"))
              :image nil
              :icon "fas fa-user"
              :subtitle (when (get actor "preferred_username")
                          (str "@" (get actor "preferred_username")))
              :detail (get actor "summary")
              :data-app "federation")))))))
