From 7b621fcd6a8459896d13e09fe38f04ebd1f480c4 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Mon, 17 Nov 2014 15:59:42 -0500 Subject: Generate and update list of favorited questions. sx-favorites--ensure-favorite-list: List of favorites in format (SITE QUESTION_ID QUESTION_ID ...). (sx-favorites--update-site-favorites): Update favorites for given SITE. (sx-favorites-update): Update favorites for all networks user has an account on. --- sx-favorites.el | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 sx-favorites.el (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el new file mode 100644 index 0000000..b66b2c5 --- /dev/null +++ b/sx-favorites.el @@ -0,0 +1,87 @@ +;;; sx-site.el --- browsing sites -*- lexical-binding: t; -*- + +;; Copyright (C) 2014 Sean Allred + +;; Author: Sean Allred + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'sx-method) +(require 'sx-cache) +(require 'sx-site) +(require 'sx-networks) + +(defvar sx-favorite-list-filter + '((.backoff + .items + .quota_max + .quota_remaining + question.question_id) + nil + none)) + +(defvar sx-favorites--user-favorite-list nil + "Alist of questions favorited by the user. +Each element has the form (SITE FAVORITE-LIST). +And each element in FAVORITE-LIST has the form QUESTION_ID.") + +(defun sx-favorites--ensure-favorite-list (site) + (unless sx-favorites--user-favorite-list + (setq sx-favorites--user-favorite-list + (sx-cache-get + 'question-favorites + (let ((sites + (mapcar '(lambda (site) + `(,site)) + sx-network--user-sites))) + `(quote ,sites)))))) + +(defun sx-favorites--retrieve-favorites (site) + "Obtain list of starred QUESTION_IDs for SITE." + (sx-method-call (format "me/favorites?site=%s" site) + nil + sx-favorite-list-filter + 'warn)) + +(defun sx-favorites--update-site-favorites (site) + "Update list of starred QUESTION_IDs for SITE. + +Writes list to cache QUESTION-FAVORITES." + (sx-favorites--ensure-favorite-list site) + (let ((favs (sx-favorites--retrieve-favorites site)) + (site-cell (assoc site + sx-favorites--user-favorite-list))) + (if site-cell + (setcdr site-cell (mapcar 'cdar favs)) + (push (list site favs) sx-favorites--user-favorite-list)) + (sx-cache-set 'question-favorites sx-favorites--user-favorite-list))) + +(defun sx-favorites-update () + "Update all sites retrieved from `sx-network--user-sites'." + (sx-network--ensure-user) + (mapc #'sx-favorites--update-site-favorites + sx-network--user-sites)) + +(provide sx-favorites) +;;; sx-favorites.el ends here + +;; Local Variables: +;; indent-tabs-mode: nil +;; End: -- cgit v1.2.3 From 6a7e345bb776c715c0801674ff4cc5feaef08f4c Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 07:18:52 -0500 Subject: Refactor sx-network--get-associated to avoid double call to sx-cache-set. Fix provides lines (missed quote on symbol). --- sx-favorites.el | 2 +- sx-networks.el | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el index b66b2c5..0b9527a 100644 --- a/sx-favorites.el +++ b/sx-favorites.el @@ -79,7 +79,7 @@ Writes list to cache QUESTION-FAVORITES." (mapc #'sx-favorites--update-site-favorites sx-network--user-sites)) -(provide sx-favorites) +(provide 'sx-favorites) ;;; sx-favorites.el ends here ;; Local Variables: diff --git a/sx-networks.el b/sx-networks.el index 3a33f1a..2be764f 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -53,9 +53,9 @@ none)) (defun sx-network--get-associated () - (sx-cache-get - 'network-user - '(sx-network--update))) + (or (sx-cache-get + 'network-user) + (sx-network--update)) (defun sx-network--update () "Update user information." @@ -102,7 +102,7 @@ list of sites the user is active on." (defvar sx-network--user-sites nil "List of sites where user already has an account.") -(provide sx-networks) +(provide 'sx-networks) ;;; sx-networks.el ends here ;; Local Variables: -- cgit v1.2.3 From f813286cf44b8e1bdf7d2003f7f5b1fe870f613d Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 07:38:42 -0500 Subject: Fixes as per Github comments sx-favorites--user-favorite-list: Clarify docstring (sx-favorites--ensure-favorite-list): Remove extraneous argument --- sx-favorites.el | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el index 0b9527a..497ef1a 100644 --- a/sx-favorites.el +++ b/sx-favorites.el @@ -39,17 +39,18 @@ (defvar sx-favorites--user-favorite-list nil "Alist of questions favorited by the user. -Each element has the form (SITE FAVORITE-LIST). -And each element in FAVORITE-LIST has the form QUESTION_ID.") -(defun sx-favorites--ensure-favorite-list (site) +Each element has the form (SITE FAVORITE-LIST). And each element +in FAVORITE-LIST is the numerical QUESTION_ID.") + +(defun sx-favorites--ensure-favorite-list () (unless sx-favorites--user-favorite-list (setq sx-favorites--user-favorite-list (sx-cache-get 'question-favorites (let ((sites - (mapcar '(lambda (site) - `(,site)) + (mapcar (lambda (site) + `(,site)) sx-network--user-sites))) `(quote ,sites)))))) -- cgit v1.2.3 From a9a47da95ab06e3a68bfd037de51bead4855c1ea Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 07:42:08 -0500 Subject: (sx-favorites--update-site-favorites): Change from (list ..) to (cons ..). --- sx-favorites.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el index 497ef1a..f5a8d6c 100644 --- a/sx-favorites.el +++ b/sx-favorites.el @@ -71,7 +71,7 @@ Writes list to cache QUESTION-FAVORITES." sx-favorites--user-favorite-list))) (if site-cell (setcdr site-cell (mapcar 'cdar favs)) - (push (list site favs) sx-favorites--user-favorite-list)) + (push (cons site favs) sx-favorites--user-favorite-list)) (sx-cache-set 'question-favorites sx-favorites--user-favorite-list))) (defun sx-favorites-update () -- cgit v1.2.3 From 16637550bff638552613aa241a023ad24e757a09 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 08:49:03 -0500 Subject: GH comment fixes: Correct file header. Use sharp quote for function. --- sx-favorites.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el index f5a8d6c..9412b5b 100644 --- a/sx-favorites.el +++ b/sx-favorites.el @@ -1,4 +1,4 @@ -;;; sx-site.el --- browsing sites -*- lexical-binding: t; -*- +;;; sx-favorites.el --- Starred questions -*- lexical-binding: t; -*- ;; Copyright (C) 2014 Sean Allred @@ -70,7 +70,7 @@ Writes list to cache QUESTION-FAVORITES." (site-cell (assoc site sx-favorites--user-favorite-list))) (if site-cell - (setcdr site-cell (mapcar 'cdar favs)) + (setcdr site-cell (mapcar #'cdar favs)) (push (cons site favs) sx-favorites--user-favorite-list)) (sx-cache-set 'question-favorites sx-favorites--user-favorite-list))) -- cgit v1.2.3 From 8554d48ef764c8ca44438f35243a88f54f8386dc Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 11:44:54 -0500 Subject: Replace (sx-favorites--ensure-favorite-list) with (sx-favorites--initialize). Simplify initialization since it is run at startup rather than tested for by functions. Clean up (sx-favorites--update-site-favorites) to provide the same values for new sites as for existing. --- sx-favorites.el | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'sx-favorites.el') diff --git a/sx-favorites.el b/sx-favorites.el index 9412b5b..3aa96dd 100644 --- a/sx-favorites.el +++ b/sx-favorites.el @@ -43,16 +43,15 @@ Each element has the form (SITE FAVORITE-LIST). And each element in FAVORITE-LIST is the numerical QUESTION_ID.") -(defun sx-favorites--ensure-favorite-list () - (unless sx-favorites--user-favorite-list - (setq sx-favorites--user-favorite-list - (sx-cache-get - 'question-favorites - (let ((sites - (mapcar (lambda (site) - `(,site)) - sx-network--user-sites))) - `(quote ,sites)))))) +(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))) +;; Append to ensure `sx-network--initialize is run before it. +(add-hook 'sx-init--internal-hook #'sx-favorites--initialize 'append) (defun sx-favorites--retrieve-favorites (site) "Obtain list of starred QUESTION_IDs for SITE." @@ -65,18 +64,17 @@ in FAVORITE-LIST is the numerical QUESTION_ID.") "Update list of starred QUESTION_IDs for SITE. Writes list to cache QUESTION-FAVORITES." - (sx-favorites--ensure-favorite-list site) - (let ((favs (sx-favorites--retrieve-favorites site)) - (site-cell (assoc site - sx-favorites--user-favorite-list))) + (let* ((favs (sx-favorites--retrieve-favorites site)) + (site-cell (assoc site + sx-favorites--user-favorite-list)) + (fav-cell (mapcar #'cdar favs))) (if site-cell - (setcdr site-cell (mapcar #'cdar favs)) - (push (cons site favs) sx-favorites--user-favorite-list)) + (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))) (defun sx-favorites-update () "Update all sites retrieved from `sx-network--user-sites'." - (sx-network--ensure-user) (mapc #'sx-favorites--update-site-favorites sx-network--user-sites)) -- cgit v1.2.3