Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamped caching system #199

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ before_install:
- cask --version

script:
- emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
- cask exec emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit

notifications:
webhooks:
Expand Down
2 changes: 2 additions & 0 deletions Cask
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; -*- mode: emacs-lisp -*-

(source gnu)
(source melpa-stable)

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $(VERSIONS) :: clean
evm use emacs-24.$@-bin
emacs --version
cask install
emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
cask exec emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit

clean:
rm -rf .sx/
Expand Down
25 changes: 12 additions & 13 deletions sx-auth.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@

(require 'sx)
(require 'sx-request)
(require 'sx-cache)
(require 'stash)

(defconst sx-auth-root
"https://stackexchange.com/oauth/dialog")
(defconst sx-auth-redirect-uri
"http://vermiculus.github.io/sx.el/auth/auth.htm")
(defconst sx-auth-client-id
"3291")
(defvar sx-auth-access-token
nil

(defstash sx-auth-access-token
"auth.el" sx nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how catastrophic it would be to have two stashes with the same file name, maybe we should use more specific names for future stashes. Or (even better) maybe stash.el could take care of choosing unique file names based on variable name.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter option seems good, but what else would auth be used for (in our case specifically)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I don't know. I don't think we should change this variable specifically. It just occurred to me we might want to use longer names on future stashes.

"Your access token.
This is needed to use your account to write questions, make
comments, and read your inbox. Do not alter this unless you know
what you are doing!

This variable is set with `sx-auth-authenticate'.")
(sx-init-variable sx-auth-access-token)

(defconst sx-auth-method-auth
'((me . t)
Expand Down Expand Up @@ -74,12 +76,10 @@ This variable is set with `sx-auth-authenticate'.")
render
upvote
(unanswered my-tags)))
"List of methods that require auth.
Methods are of the form \(METHOD . SUBMETHODS) where SUBMETHODS
is \(METHOD METHOD METHOD ...).

If all SUBMETHODS require auth or there are no submethods, form
will be \(METHOD . t)")
"List of methods that require authentication.
Methods are of the form \(METHOD SUBMETHOD...). If all
SUBMETHODS require authentication or there are no submethods,
form will be \(METHOD . t)")

(defconst sx-auth-filter-auth
'(question.upvoted
Expand Down Expand Up @@ -133,10 +133,9 @@ parsed and displayed prominently on the page)."
","))))
(browse-url url)
(read-string "Enter the access token displayed on the webpage: ")))
(if (string-equal "" sx-auth-access-token)
(progn (setq sx-auth-access-token nil)
(error "You must enter this code to use this client fully"))
(sx-cache-set 'auth `((access_token . ,sx-auth-access-token)))))
(when (string-equal "" sx-auth-access-token)
(progn (setq sx-auth-access-token nil)
(error "You must enter this code to use this client fully"))))

(defun sx-auth--method-p (method &optional submethod)
"Check if METHOD is one that may require authentication.
Expand Down
117 changes: 0 additions & 117 deletions sx-cache.el

This file was deleted.

16 changes: 7 additions & 9 deletions sx-favorites.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,25 @@
;;; Code:

(require 'sx-method)
(require 'sx-cache)
(require 'sx-site)
(require 'sx-networks)
(require 'sx-filter)
(require 'stash)

(defconst sx-favorite-list-filter
(sx-filter-from-nil
(question.question_id)))

(defvar sx-favorites--user-favorite-list nil
(defstash sx-favorites--user-favorite-list "favorites.el" sx nil
"Alist of questions favorited by the user.
Each element has the form (SITE FAVORITE-LIST). And each element
in FAVORITE-LIST is the numerical QUESTION_ID.")

(defun sx-favorites--initialize ()
"Ensure question-favorites cache is available.
Added as hook to initialization."
(or (setq sx-favorites--user-favorite-list
(sx-cache-get 'question-favorites))
(sx-favorites-update)))
(or (stash-load 'sx-favorites--user-favorite-list)
(sx-favorites-update)))
;; ;; Append to ensure `sx-network--initialize' is run before it.
;; This is removed for now because it performs a lot of API calls and
;; was never used.
Expand All @@ -62,13 +61,12 @@ Added as hook to initialization."
"Update list of starred QUESTION_IDs for SITE.
Writes list to cache QUESTION-FAVORITES."
(let* ((favs (sx-favorites--retrieve-favorites site))
(site-cell (assoc site
sx-favorites--user-favorite-list))
(site-cell (assoc site sx-favorites--user-favorite-list))
(fav-cell (mapcar #'cdar favs)))
(if site-cell
(setcdr site-cell fav-cell)
(push (cons site fav-cell) sx-favorites--user-favorite-list))
(sx-cache-set 'question-favorites sx-favorites--user-favorite-list)))
(push (cons site fav-cell)
sx-favorites--user-favorite-list))))

(defun sx-favorites-update ()
"Update all sites retrieved from `sx-network--user-sites'."
Expand Down
24 changes: 10 additions & 14 deletions sx-filter.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@
;;; Dependencies

(require 'sx)
(require 'sx-cache)
(require 'sx-request)
(require 'stash)


;;; Customizations

(defvar sx--filter-alist
(sx-cache-get 'filter)
"An alist of known filters. See `sx-filter-compile'.
(defstash sx--filter-alist "filter.el" sx nil
"An alist of known filters. See `sx-filter-compile'.
Structure:

(((INCLUDE EXCLUDE BASE ) . \"compiled filter \")
((INCLUDE2 EXCLUDE2 BASE2) . \"compiled filter2\")
...)")
(((INCLUDE EXCLUDE BASE) . \"compiled filter\")...)")


;;; Customizations


;;; Creation
Expand Down Expand Up @@ -94,13 +91,12 @@ Returns the compiled filter as a string."
If the filter data exists in `sx--filter-alist', that value will
be returned. Otherwise, compile INCLUDE, EXCLUDE, and BASE into
a filter with `sx-filter-compile' and push the association onto
`sx--filter-alist'. Re-cache the alist with `sx-cache-set' and
return the compiled filter."
`sx--filter-alist'. Return the compiled filter."
(or (cdr (assoc (list include exclude base) sx--filter-alist))
(let ((filter (sx-filter-compile include exclude base)))
(when filter
(push (cons (list include exclude base) filter) sx--filter-alist)
(sx-cache-set 'filter sx--filter-alist)
(push (cons (list include exclude base) filter)
sx--filter-alist)
filter))))


Expand Down
1 change: 0 additions & 1 deletion sx-load.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
sx-auth
sx-button
sx-babel
sx-cache
sx-compose
sx-encoding
sx-favorites
Expand Down
14 changes: 8 additions & 6 deletions sx-method.el
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ See `sx-request-make' and `sx-request-all-items'.

Return the entire response as a complex alist."
(declare (indent 1))
(let ((access-token (sx-cache-get 'auth))
(method-auth (sx-auth--method-p method submethod))
(let ((method-auth (sx-auth--method-p method submethod))
(filter-auth (sx-auth--filter-p filter))
(full-method (concat (format "%s" method)
(when id
Expand All @@ -111,14 +110,17 @@ Return the entire response as a complex alist."
(cond
((eq get-all t) #'sx-request-all-stop-when-no-more)
(t get-all))))
(lwarn "sx-call-method" :debug "A: %S T: %S. M: %S,%s. F: %S" (equal 'warn auth)
access-token method-auth full-method filter-auth)
(unless access-token
(lwarn "sx-call-method"
:debug "A: %S T: %S. M: %S,%s. F: %S"
(eq 'warn auth) sx-auth-access-token
method-auth full-method filter-auth)
(unless sx-auth-access-token
(cond
;; 1. Need auth and warn user (interactive use)
((and method-auth (equal 'warn auth))
(sx-user-error
"This request requires authentication. Please run `M-x sx-authenticate' and try again."))
"This request requires authentication. \
Please run `M-x sx-authenticate' and try again."))
;; 2. Need auth to populate UI, cannot provide subset
((and method-auth auth)
(setq call 'sx-request-fallback))
Expand Down
20 changes: 10 additions & 10 deletions sx-networks.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
;;; Code:

(require 'sx-method)
(require 'sx-cache)
(require 'sx-site)
(require 'sx-filter)
(require 'stash)

(defconst sx-network--user-filter
(sx-filter-from-nil
Expand All @@ -48,21 +48,21 @@
(defun sx-network--get-associated ()
"Retrieve cached information for network user.
If cache is not available, retrieve current data."
(or (and (setq sx-network--user-information (sx-cache-get 'network-user)
sx-network--user-sites
(or (and sx-network--user-information
(setq sx-network--user-sites
(sx-network--map-site-url-to-site-api)))
(sx-network--update)))

(defun sx-network--update ()
"Update user information.
Sets cache and then uses `sx-network--get-associated' to update
the variables."
(sx-cache-set 'network-user
(sx-method-call 'me
:submethod 'associated
:keywords '((types . (main_site meta_site)))
:filter sx-network--user-filter
:auth t))
(setq sx-network--user-information
(sx-method-call 'me
:submethod 'associated
:keywords '((types . (main_site meta_site)))
:filter sx-network--user-filter
:auth t))
(sx-network--get-associated))

(defun sx-network--initialize ()
Expand All @@ -88,7 +88,7 @@ list of sites the user is active on."
(cdr (assoc u-site sites-info)))))
sx-network--user-information)))

(defvar sx-network--user-information nil
(defstash sx-network--user-information "network-user.el" sx nil
"User information for the various sites.")

(defvar sx-network--user-sites nil
Expand Down
Loading