From 0be9a17cfa1f8ea9e34c55c63cfa9d4c6602f504 Mon Sep 17 00:00:00 2001 From: PuercoPop Date: Wed, 7 May 2014 01:37:27 -0500 Subject: [PATCH] Implementation sans test --- plugins/twitter.lisp | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugins/twitter.lisp diff --git a/plugins/twitter.lisp b/plugins/twitter.lisp new file mode 100644 index 0000000..60e77f1 --- /dev/null +++ b/plugins/twitter.lisp @@ -0,0 +1,45 @@ +(:eval-when (:compile-toplevel :load-toplevel) + (ql:quickload :chirp)) + +(defpackage :coleslaw-twitter + (:use :cl) + (:export #:enable)) + +(in-package :coleslaw-twitter) + +(defvar *tweet-format* '("~A" (title post) (text content)) + "Controls what the tweet annoucing the post looks like. It is the made up of a format control string followed with the pertinent variables.") + +(defun enable (&key api-key api-secret access-token access-secret tweet-format) + (if (and api-key api-secret access-token access-secret) + (setf chirp:*oauth-api-key* api-key + chirp:*oauth-api-secret* api-secret + chirp:*oauth-access-token* access-token + chirp:*oauth-access-secret* access-secret) + (error 'plugin-conf-error :plugin "twitter" + :message "Credentials missing.") + ;; fallback to chirp for credential erros + (chirp:account/verify-credentials)) + + (when tweet-format + (setf *tweet-format* tweet-format))) + +(defmethod render :after (post (eql (find-class 'coleslaw:post))) + (format-post post)) + +(defun format-post (post) + "Take a post and return a string of 140 character length, at most. Urls have 23 len and are a must." + (chirp:statuses/update (%format-post post))) + +(defun %format-post (offset post) + "Garauntee that the tweet content is 140 chars at most." + (let* ((content-prefix (subseq (render-tweet post) 0 (- 117 offset))) + (content (format nil "~A ~A" content-prefix (coleslaw:page-url post))) + (content-length (chirp:compute-status-length content))) + (cond + ((>= 140 content-length) content) + ((< 140 content-length) (%format-post (1- offset) post))))) + +(defun render-tweet (post) + "Sans the url, which is a must." + (apply #'format (append '(nil) *tweet-format*)))