Handle account and reply mode in same namespace, and html generation in own namespace

This commit is contained in:
Clemens 2024-05-29 11:25:37 +02:00
parent 0b85045158
commit 1cde7289c5
5 changed files with 36 additions and 209 deletions

View file

@ -76,7 +76,7 @@ Including replies of one of your posts will work as follows:
<body> <body>
<div id="masto-embed" <div id="masto-embed"
account_name="team" account_name="team"
replies_to="107937234506182462" replies_to="112432461907918517"
filter_favorited=false filter_favorited=false
host_url="https://social.meissa-gmbh.de"> host_url="https://social.meissa-gmbh.de">
Here the timeline will appear. Here the timeline will appear.

View file

@ -16,8 +16,7 @@
(ns dda.masto-embed.app (ns dda.masto-embed.app
(:require (:require
[dda.masto-embed.browser :as b] [dda.masto-embed.browser :as b]
[dda.masto-embed.reply-mode :as rm] [dda.masto-embed.modes :as m]))
[dda.masto-embed.to-html :as am]))
(defn init [] (defn init []
(let [host-url (b/host-url-from-document) (let [host-url (b/host-url-from-document)
@ -25,5 +24,5 @@
replies-to (b/replies-to-from-document) replies-to (b/replies-to-from-document)
filter-favorited (b/filter-favorited-from-document)] filter-favorited (b/filter-favorited-from-document)]
(if (nil? replies-to) (if (nil? replies-to)
(am/to-html host-url account-name) (m/account-mode host-url account-name)
(rm/replies-mode host-url account-name replies-to filter-favorited)))) (m/replies-mode host-url account-name replies-to filter-favorited))))

View file

@ -13,7 +13,7 @@
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and ; See the License for the specific language governing permissions and
; limitations under the License. ; limitations under the License.
(ns dda.masto-embed.reply-mode (ns dda.masto-embed.modes
(:require (:require
[cljs.core.async :refer [go close! put! take! timeout chan <! >!]] [cljs.core.async :refer [go close! put! take! timeout chan <! >!]]
[cljs.core.async.interop :refer-macros [<p!]] [cljs.core.async.interop :refer-macros [<p!]]
@ -24,45 +24,9 @@
[dda.masto-embed.browser :as b] [dda.masto-embed.browser :as b]
[dda.masto-embed.to-html :as th] [dda.masto-embed.to-html :as th]
)) ))
; ToDo: Possibility to present replies to more than one post
(defn mastocard->html [card media]
(when (some? card)
(let [{:keys [title description image url]} card
{:keys [type preview_url url]} (first media)]
[:div {:class "card" :url url}
(when (some? image)
[:img {:class "card-img-top" :src image}])
(when (and (some? type) (= type "image"))
[:img {:class "card-img-top" :src preview_url}])
[:p media]
[:h3 {:class "card-title"} title]
[:p {:class "card-body"} description]])))
; TODO?: Functions in this ns mix business logic and api calls.
; Should we separate better etween business and infra logic?
(defn mastomedia->html [media]
(when (some? media)
(let [{:keys [id type preview_url url]} (first media)]
[:div {:class "media"}
(when (and (some? type) (= type "image"))
[:img {:class "img-thumbnail" :width "100" :height "100"
:src preview_url}])])))
(defn masto->html [statuses]
[:ul {:class "list-group"}
(map (fn [status]
(let [{:keys [created_at card media_attachments]} status
date (t/parse created_at)]
[:li {:class "list-group-item, card"}
[:div {:class "card-body row"}
[:div {:class "col-sm"}
[:h2 {:class "card-title"}
[:a {:href (get-in status [:url])}
(t/unparse (t/formatter "dd.MM.yyyy") date) " "]]
[:div {:class "card-text"}
(:content status)]]
[:div {:class "col-sm"} (mastomedia->html media_attachments)]]]))
statuses)])
(defn favorited-replies? [host-url account-name reply-id] (defn favorited-replies? [host-url account-name reply-id]
(let [out (chan)] (let [out (chan)]
@ -88,6 +52,18 @@
(conj result (<! (favorited-replies? host-url account-name (first loc-replies))))))))) (conj result (<! (favorited-replies? host-url account-name (first loc-replies)))))))))
out)) out))
(defn find-account-id [host-url account-name]
(let [out (chan)]
(go
(>! out
(->>
(<p! (api/get-directory host-url))
api/mastojs->edn
(filter #(= account-name (:acct %)))
(map :id)
first)))
out))
(defn replies-mode [host-url account-name post-id filter-favorited] (defn replies-mode [host-url account-name post-id filter-favorited]
(go (go
(let [replies (-> (let [replies (->
@ -99,6 +75,20 @@
(filter #(or (not filter-favorited) (:favorited %))) (filter #(or (not filter-favorited) (:favorited %)))
(reverse) (reverse)
(map :status) (map :status)
(masto->html) (th/masto->html)
(render-html)
(b/render-to-document)))))
(defn account-mode [host-url account-name]
(go
(let [account-id (<! (find-account-id host-url account-name))
status (->
(<p! (api/get-account-statuses host-url account-id))
api/mastojs->edn)]
(->> status
(filter #(= nil (:reblog %)))
(filter #(= nil (:in_reply_to_account_id %)))
(take 4)
(th/masto->html)
(render-html) (render-html)
(b/render-to-document))))) (b/render-to-document)))))

View file

@ -101,7 +101,8 @@
(defn masto->html [statuses] (defn masto->html [statuses]
(let [html (b/post-html-hiccup)] (let [html (b/post-html-hiccup)]
(map (fn [status] (map (fn [status]
(let [{:keys [account created_at content media_attachments replies_count reblogs_count favourites_count card url]} status] (let [{:keys [account created_at content media_attachments replies_count reblogs_count favourites_count card url]} status
abc (js/console.log card)]
(-> html (-> html
(masto-header->html account created_at url) (masto-header->html account created_at url)
(masto-content->html content) (masto-content->html content)
@ -109,43 +110,3 @@
(masto-link-prev->html card) (masto-link-prev->html card)
(masto-footer->html replies_count reblogs_count favourites_count)))) (masto-footer->html replies_count reblogs_count favourites_count))))
statuses))) statuses)))
(defn find-account-id [host-url account-name]
(let [out (chan)]
(go
(>! out
(->>
(<p! (api/get-directory host-url))
api/mastojs->edn
(filter #(= account-name (:acct %)))
(map :id)
first)))
out))
(defn to-html [host-url account-name]
(go
(let [account-id (<! (find-account-id host-url account-name))
status (->
(<p! (api/get-account-statuses host-url account-id))
api/mastojs->edn)]
(->> status
(filter #(= nil (:reblog %)))
(filter #(= nil (:in_reply_to_account_id %)))
(take 4)
(masto->html)
(render-html)
(b/render-to-document)))))
; Execute in browser repl to get some infos about incoming data
(defn account-mode-debug [host-url account-name]
(go
(let [account-id (<! (find-account-id host-url account-name))
status (->
(<p! (api/get-account-statuses host-url account-id))
api/mastojs->edn)]
(->> status
(filter #(= nil (:reblog %)))
(filter #(= nil (:in_reply_to_account_id %)))
(take 1)
(infra/debug)
))))

View file

@ -1,123 +0,0 @@
; Licensed to the Apache Software Foundation (ASF) under one
; or more contributor license agreements. See the NOTICE file
; distributed with this work for additional information
; regarding copyright ownership. The ASF licenses this file
; to you under the Apache License, Version 2.0 (the
; "License"); you may not use this file except in compliance
; with the License. You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
(ns dda.masto-embed.reply-mode-test
(:require
[cljs.test :refer (deftest is)]
[dda.masto-embed.reply-mode :as sut]))
(def statuses [{:mentions
[{:id "2",
:username "team",
:url "https://social.meissa-gmbh.de/@team",
:acct "team"}],
:emojis [],
:tags [],
:reblog nil,
:replies_count 0,
:in_reply_to_account_id "2",
:reblogs_count 0,
:application nil,
:content
"<p><span class=\"h-card\"><a href=\"https://social.meissa-gmbh.de/@team\" class=\"u-url mention\">@<span>team</span></a></span> Hier mein erstes Bild :-)</p>",
:sensitive false,
:favourites_count 2,
:in_reply_to_id "107937234506182462",
:poll nil,
:account
{:acct "jerger",
:last_status_at "2022-03-11",
:emojis [],
:bot false,
:group false,
:following_count 220,
:avatar_static
"https://cdn.masto.host/socialmeissagmbhde/accounts/avatars/000/000/001/original/794ca61bfd71bbe1.jpg",
:fields
[{:name "blog",
:value
"<a href=\"https://domaindrivenarchitecture.org/\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">domaindrivenarchitecture.org/</span><span class=\"invisible\"></span></a>",
:verified_at nil}
{:name "interests",
:value "Clojure, sci-fi, tech, DevOps, public weal",
:verified_at nil}
{:name "location", :value "Reutlingen, de, eu", :verified_at nil}
{:name "OpenPGP",
:value "4BBFF05ED0F97346DF15033D1A2CCFAF0C8C2BB6",
:verified_at nil}],
:username "jerger",
:header_static
"https://cdn.masto.host/socialmeissagmbhde/accounts/headers/000/000/001/original/2a45f78fa1af0815.jpg",
:discoverable true,
:statuses_count 295,
:header
"https://cdn.masto.host/socialmeissagmbhde/accounts/headers/000/000/001/original/2a45f78fa1af0815.jpg",
:note
"<p>meissa GmbH, Maintainer, dda-pallet, Clojure, OpenSource, DevOps, DomainDrivenDesign, Demokratie, Bürgerbeteiligung, Europa, Klettern, Wandern</p>",
:locked false,
:id "1",
:avatar
"https://cdn.masto.host/socialmeissagmbhde/accounts/avatars/000/000/001/original/794ca61bfd71bbe1.jpg",
:url "https://social.meissa-gmbh.de/@jerger",
:display_name "jerger",
:followers_count 58,
:created_at "2019-06-02T00:00:00.000Z"},
:card nil,
:language nil,
:id "107937257700481042",
:url "https://social.meissa-gmbh.de/@jerger/107937257700481042",
:media_attachments
[{:description nil,
:meta
{:original
{:width 1247,
:height 1663,
:size "1247x1663",
:aspect 0.7498496692723993},
:small
{:width 346,
:height 461,
:size "346x461",
:aspect 0.7505422993492408}},
:type "image",
:blurhash "UIJkAFt,%gIv}~9tIUWYOvRkD*xZw6$hRj%1",
:preview_url
"https://cdn.masto.host/socialmeissagmbhde/media_attachments/files/107/937/248/257/634/217/small/923e75c7684a2c31.jpg",
:preview_remote_url nil,
:id "107937248257634217",
:url
"https://cdn.masto.host/socialmeissagmbhde/media_attachments/files/107/937/248/257/634/217/original/923e75c7684a2c31.jpg",
:remote_url nil,
:text_url nil}],
:uri
"https://social.meissa-gmbh.de/users/jerger/statuses/107937257700481042",
:edited_at nil,
:visibility "public",
:created_at "2022-03-11T09:44:07.235Z",
:spoiler_text ""}])
(deftest test-mastodon->html
(is (= [:ul {:class "list-group"}
'([:li {:class "list-group-item, card"}
[:div {:class "card-body row"}
[:div {:class "col-sm"}
[:h2 {:class "card-title"}
[:a {:href "https://social.meissa-gmbh.de/@jerger/107937257700481042"} "11.03.2022" " "]]
[:div {:class "card-text"} "<p><span class=\"h-card\"><a href=\"https://social.meissa-gmbh.de/@team\" class=\"u-url mention\">@<span>team</span></a></span> Hier mein erstes Bild :-)</p>"]]
[:div {:class "col-sm"}
[:div {:class "media"}
[:img {:class "img-thumbnail", :width "100", :height "100", :src "https://cdn.masto.host/socialmeissagmbhde/media_attachments/files/107/937/248/257/634/217/small/923e75c7684a2c31.jpg"}]]]]])]
(sut/masto->html statuses))))