Bläddra i källkod

Implementation sans test

PuercoPop 11 år sedan
förälder
incheckning
0be9a17cfa
1 ändrade filer med 45 tillägg och 0 borttagningar
  1. 45 0
      plugins/twitter.lisp

+ 45 - 0
plugins/twitter.lisp

@@ -0,0 +1,45 @@
1
+(:eval-when (:compile-toplevel :load-toplevel)
2
+  (ql:quickload :chirp))
3
+
4
+(defpackage :coleslaw-twitter
5
+  (:use :cl)
6
+  (:export #:enable))
7
+
8
+(in-package :coleslaw-twitter)
9
+
10
+(defvar *tweet-format* '("~A" (title post) (text content))
11
+  "Controls what the tweet annoucing the post looks like. It is the made up of a format control string followed with the pertinent variables.")
12
+
13
+(defun enable (&key api-key api-secret access-token access-secret tweet-format)
14
+  (if (and api-key api-secret access-token access-secret)
15
+      (setf chirp:*oauth-api-key* api-key
16
+            chirp:*oauth-api-secret* api-secret
17
+            chirp:*oauth-access-token* access-token
18
+            chirp:*oauth-access-secret* access-secret)
19
+      (error 'plugin-conf-error :plugin "twitter"
20
+             :message "Credentials missing.")
21
+      ;; fallback to chirp for credential erros
22
+      (chirp:account/verify-credentials))
23
+  
24
+  (when tweet-format
25
+    (setf *tweet-format* tweet-format)))
26
+
27
+(defmethod render :after (post (eql (find-class 'coleslaw:post)))
28
+  (format-post post))
29
+
30
+(defun format-post (post)
31
+  "Take a post and return a string of 140 character length, at most. Urls have 23 len and are a must."
32
+ (chirp:statuses/update (%format-post post)))
33
+
34
+(defun %format-post (offset post)
35
+  "Garauntee that the tweet content is 140 chars at most."
36
+  (let* ((content-prefix (subseq (render-tweet post) 0 (- 117 offset)))
37
+         (content (format nil "~A ~A" content-prefix (coleslaw:page-url post)))
38
+         (content-length (chirp:compute-status-length content)))
39
+    (cond
40
+      ((>= 140 content-length) content)
41
+      ((< 140 content-length) (%format-post (1- offset) post)))))
42
+
43
+(defun render-tweet (post)
44
+  "Sans the url, which is a must."
45
+  (apply #'format (append '(nil) *tweet-format*)))