From 0d0f566f3e70214582ba0036f6fe53d141b60eb5 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sat, 14 Apr 2012 23:41:35 +0200 Subject: Refactor news feed generation. --- src/mulk/benki/book_marx.clj | 33 ++++++++--------------------- src/mulk/benki/feed.clj | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/mulk/benki/lazychat.clj | 38 ++++++++++++---------------------- 3 files changed, 70 insertions(+), 50 deletions(-) create mode 100644 src/mulk/benki/feed.clj (limited to 'src') diff --git a/src/mulk/benki/book_marx.clj b/src/mulk/benki/book_marx.clj index 5c556c2..16496d3 100644 --- a/src/mulk/benki/book_marx.clj +++ b/src/mulk/benki/book_marx.clj @@ -7,7 +7,7 @@ [hiccup.core :only [escape-html]] [ring.util.codec :only [url-encode]] noir.core - [mulk.benki util db auth config webutil]) + [mulk.benki util db auth config webutil feed]) (:require [clojure.algo.monads :as m] [clojure.java.jdbc :as sql] [clojure.string :as string] @@ -16,12 +16,9 @@ [noir.response :as response] [noir.session :as session] hiccup.core) - (:import [org.jsoup Jsoup] - [org.apache.abdera Abdera])) + (:import [org.jsoup Jsoup])) -(defonce abdera (Abdera.)) - (def bookmark_tags (cq/table :bookmark_tags)) (def bookmarks (cq/table :bookmarks)) (def tags (cq/table :tags)) @@ -113,32 +110,18 @@ " [" [:a {:href (resolve-uri (authlink feed-link))} "Atom auth"] "]" " [" [:a {:href (authlink (:uri (request/ring-request)))} "authlink"] "]"))])])))) - (defn marx-feed-for-user [user] (let [marks (bookmarks-visible-by user)] (with-dbt (let [last-updated (sql/with-query-results results ["SELECT MAX(date) AS maxdate FROM bookmarks"] (:maxdate (first results))) - feed (doto (.newFeed abdera) - (.setId (fmt nil "tag:~A,2012:/marx" - (:tag-base @benki-config))) - (.setTitle "Book Marx") - (.setUpdated last-updated) - (.addLink (link :marx)))] - (doseq [mark @marks] - (doto (.addEntry feed) - (.setId (fmt nil "tag:~A,2012:/marx/~D" - (:tag-base benki-config) - (:id mark))) - (.setTitle (:title mark)) - (.setSummaryAsHtml (hiccup.core/html (htmlize-description (:description mark)))) - ;;(.setUpdated (:updated mark)) - (.setPublished (:date mark)) - ;;(.setAuthor (fmt nil "~A ~A" (:first_name mark) (:last_name mark))) - ;;(.addLink (link :marx (:id mark))) - (.addLink (:uri mark)))) - (.toString feed))))) + items (map #(with-meta + (assoc % + (hiccup.core/html (htmlize-description (:description %)))) + {:type ::bookmark}) @marks)] + (generate-feed "Book Marx" last-updated "marx" (link :marx) + items))))) (defpage "/marx/feed" {} (response/content-type "application/atom+xml; charset=UTF-8" diff --git a/src/mulk/benki/feed.clj b/src/mulk/benki/feed.clj new file mode 100644 index 0000000..31125dd --- /dev/null +++ b/src/mulk/benki/feed.clj @@ -0,0 +1,49 @@ +(ns mulk.benki.feed + (:refer-clojure) + (:use [clojure repl] + [mulk.benki util config webutil]) + (:require [clojure.algo.monads :as m] + [clojure.string :as string] + [hiccup.core]) + (:import [org.apache.abdera Abdera])) + + +(defonce abdera (Abdera.)) + + +(defmulti feed-add-entry #(type (second %))) + +(defmethod feed-add-entry :mulk.benki.book_marx/bookmark [feed item] + (doto (.addEntry feed) + (.setId (fmt nil "tag:~A,2012:/marx/~D" + (:tag-base benki-config) + (:id item))) + (.setTitle (:title item)) + (.setSummaryAsHtml (:html item)) + ;;(.setUpdated (:updated item)) + (.setPublished (:date item)) + ;;(.setAuthor (fmt nil "~A ~A" (:first_name item) (:last_name item))) + ;;(.addLink (link :marx (:id item))) + (.addLink (:uri item)))) + +(defmethod feed-add-entry :mulk.benki.lazychat/lazychat-message [feed item] + (doto (.addEntry feed) + (.setId (fmt nil "tag:~A,2012:/lafargue/~D" + (:tag-base benki-config) + (:id item))) + (.setSummaryAsHtml (sanitize-html (markdown->html (:content item)))) + (.setPublished (:date item)) + ;;(.setAuthor (fmt nil "~A ~A" (:first_name item) (:last_name item))) + ;;(.addLink (link :lafargue (:id item))) + )) + +(defn generate-feed [title last-updated tag link items] + (let [feed (doto (.newFeed abdera) + (.setId (fmt nil "tag:~A,2012:/~A" + (:tag-base @benki-config) + tag)) + (.setTitle title) + (.setUpdated last-updated) + (.addLink link))] + (doall (map #(feed-add-entry feed %) items)) + (.toString feed))) diff --git a/src/mulk/benki/lazychat.clj b/src/mulk/benki/lazychat.clj index ecc4ac1..b2cb128 100644 --- a/src/mulk/benki/lazychat.clj +++ b/src/mulk/benki/lazychat.clj @@ -4,7 +4,7 @@ [hiccup core page-helpers] [noir core] [noir-async core] - [mulk.benki auth config db util webutil] + [mulk.benki auth config db util webutil feed] ;; [clojure.core.match :only [match]] [hiccup.core :only [escape-html]] @@ -22,11 +22,9 @@ [aleph.http :as ahttp] [aleph.formats :as aformats] [clojure.data.json :as json] - [mulk.benki.xmpp :as xmpp]) - (:import [org.apache.abdera Abdera])) + [mulk.benki.xmpp :as xmpp])) -(defonce abdera (Abdera.)) (defonce lafargue-events (channel)) @@ -53,6 +51,9 @@ (defn start-xmpp-pump [] (receive-all lafargue-events push-message-to-xmpp)) +(defn fill-in-author-details [] + ) + (defn create-lazychat-message! [{content :content, visibility :visibility format :format, targets :targets, referees :referees, id :id}] @@ -78,10 +79,11 @@ [id (int target)])) (enqueue lafargue-events (with-meta - {:content content, :visibility visibility, - :format format, :targets targets, - :referees referees, :id id, - :author *user*, :date (java.util.Date.)} + (fill-in-author-details + {:content content, :visibility visibility, + :format format, :targets targets, + :referees referees, :id id, + :author *user*, :date (java.util.Date.)}) {:type ::lafargue-message})))))) (defn select-message [id] @@ -189,23 +191,9 @@ (let [last-updated (sql/with-query-results results ["SELECT MAX(date) AS maxdate FROM lazychat_messages"] (:maxdate (first results))) - feed (doto (.newFeed abdera) - (.setId (fmt nil "tag:~A,2012:/lafargue" - (:tag-base @benki-config))) - (.setTitle "Lafargue Lazy Chat") - (.setUpdated last-updated) - (.addLink (link :lafargue)))] - (doseq [item messages] - (doto (.addEntry feed) - (.setId (fmt nil "tag:~A,2012:/lafargue/~D" - (:tag-base benki-config) - (:id item))) - (.setSummaryAsHtml (sanitize-html (markdown->html (:content item)))) - (.setPublished (:date item)) - ;;(.setAuthor (fmt nil "~A ~A" (:first_name item) (:last_name item))) - ;;(.addLink (link :lafargue (:id item))) - )) - (.toString feed))))) + items (map #(with-meta % {:type ::lazychat-message}) messages)] + (generate-feed "Lafargue Lazy Chat" last-updated "lafargue" (link :lafargue) + items))))) (defpage "/lafargue/feed" {} -- cgit v1.2.3