From e6bb22b2f0666dbd9ef229d46dc95b9aecfa3d0a Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Mon, 17 Nov 2014 15:48:02 -0500 Subject: Create `sx-networks` and the associated cache. sx-network--user-information: User query from site. sx-network--user-sites: List of sites user is active on for use when querying sites. (sx-network--ensure-user): Ensures network-user cache is available for use. (sx-network--update): Retrieve most recent network user information. --- sx-networks.el | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 sx-networks.el diff --git a/sx-networks.el b/sx-networks.el new file mode 100644 index 0000000..41c7aa1 --- /dev/null +++ b/sx-networks.el @@ -0,0 +1,84 @@ +;;; sx-networks.el --- user network information -*- 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) + +(defun sx-network--get-associated () + (sx-cache-get + 'network-user + '(sx-network--update))) + +(defun sx-network--update () + "Update user information." + (setq sx-network--user-information + (sx-method-call "me/associated" + '((types . "main_site;meta_site")) + 'nil + 'warn)) + (setq sx-network--user-sites (sx-network--map-site-url-to-site-api)) + (sx-cache-set 'network-user sx-network--user-information)) + +(defun sx-network--ensure-user () + "Ensure user-cache is available. + +This should be called during initialization." + (cond + ((not sx-network--user-information) + (or (sx-network--get-associated) + (sx-network--update))) + ((not sx-network--user-sites) + (sx-network--map-site-url-to-site-api)))) + +(defun sx-network--map-site-url-to-site-api () + "Convert `me/associations' to a set of `api_site_parameter's. + +`me/associations' does not return `api_site_parameter' so cannot +be directly used to retrieve content per site. This creates a +list of sites the user is active on." + (let ((sites-info (mapcar (lambda (x) + (cons (cdr (assoc 'site_url x)) + (cdr (assoc 'api_site_parameter + x)))) + (sx-site--get-site-list)))) + (mapcar '(lambda (loc) + (let ((u-site (cdr (assoc 'site_url loc)))) + (when (member u-site (mapcar 'car sites-info)) + (cdr (assoc u-site sites-info))))) + (sx-network--user-information)))) + +(defvar sx-network--user-information nil + "User information for the various sites.") + +(defvar sx-network--user-sites nil + "List of sites where user already has an account.") + +(provide sx-networks) +;;; sx-networks.el ends here + +;; Local Variables: +;; indent-tabs-mode: nil +;; End: -- cgit v1.2.3 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 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 7e6d1a3663b2d9fb21c104c8cc145cb9bf65c85d Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Mon, 17 Nov 2014 16:04:40 -0500 Subject: Use sx-network--user-filter to include `user_type' property (non-default). --- sx-networks.el | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sx-networks.el b/sx-networks.el index 41c7aa1..5e6a96d 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -27,6 +27,31 @@ (require 'sx-cache) (require 'sx-site) +(defvar sx-network--user-filter + '((.backoff + .error_id + .error_message + .error_name + .has_more + .items + .quota_max + .quota_remaining + badge_count.bronze + badge_count.silver + badge_count.gold + network_user.account_id + network_user.answer_count + network_user.badge_counts + network_user.creation_date + network_user.last_access_date + network_user.reputation + network_user.site_name + network_user.site_url + network_user.user_id + network_user.user_type) + nil + none)) + (defun sx-network--get-associated () (sx-cache-get 'network-user @@ -37,7 +62,7 @@ (setq sx-network--user-information (sx-method-call "me/associated" '((types . "main_site;meta_site")) - 'nil + sx-network--user-filter 'warn)) (setq sx-network--user-sites (sx-network--map-site-url-to-site-api)) (sx-cache-set 'network-user sx-network--user-information)) -- cgit v1.2.3 From 49ccfdc2202cb8d71638e115509fc20d487a0da1 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Mon, 17 Nov 2014 17:47:20 -0500 Subject: Fix mistakes: - sx-network--user-information is a var not a function - remember to setq sx-network--user-sites. --- sx-networks.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sx-networks.el b/sx-networks.el index 5e6a96d..3a33f1a 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -76,7 +76,8 @@ This should be called during initialization." (or (sx-network--get-associated) (sx-network--update))) ((not sx-network--user-sites) - (sx-network--map-site-url-to-site-api)))) + (setq sx-network--user-sites + (sx-network--map-site-url-to-site-api))))) (defun sx-network--map-site-url-to-site-api () "Convert `me/associations' to a set of `api_site_parameter's. @@ -93,7 +94,7 @@ list of sites the user is active on." (let ((u-site (cdr (assoc 'site_url loc)))) (when (member u-site (mapcar 'car sites-info)) (cdr (assoc u-site sites-info))))) - (sx-network--user-information)))) + sx-network--user-information))) (defvar sx-network--user-information nil "User information for the various sites.") -- 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(-) 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 1715dfecb488bcfd487e1150adc92875aa2f8c10 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 07:26:51 -0500 Subject: Fixes as per github comments. (sx-network--get-associated): Ensure user-sites is also set when retrieving cache. (sx-network--ensure-user): Simplify. Used to ensure cache is loaded before attempting to use it. Fix quoted lambda in sx-network--map-site-url-to-site-api. --- sx-networks.el | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/sx-networks.el b/sx-networks.el index 2be764f..ead4d6d 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -53,15 +53,19 @@ none)) (defun sx-network--get-associated () - (or (sx-cache-get - 'network-user) - (sx-network--update)) + "Retrieve cached information for network user. + +If cache is not available, retrieve current data." + (or (and (sx-cache-get 'network-user) + (setq sx-network--user-sites + (sx-network--map-site-url-to-site-api))) + (sx-network--update))) (defun sx-network--update () "Update user information." (setq sx-network--user-information (sx-method-call "me/associated" - '((types . "main_site;meta_site")) + '((types . (main_site meta_site))) sx-network--user-filter 'warn)) (setq sx-network--user-sites (sx-network--map-site-url-to-site-api)) @@ -71,13 +75,9 @@ "Ensure user-cache is available. This should be called during initialization." - (cond - ((not sx-network--user-information) - (or (sx-network--get-associated) - (sx-network--update))) - ((not sx-network--user-sites) - (setq sx-network--user-sites - (sx-network--map-site-url-to-site-api))))) + ;; Cache was not retrieved, retrieve it. + (unless sx-network--user-information + (sx-network--get-associated))) (defun sx-network--map-site-url-to-site-api () "Convert `me/associations' to a set of `api_site_parameter's. @@ -90,10 +90,10 @@ list of sites the user is active on." (cdr (assoc 'api_site_parameter x)))) (sx-site--get-site-list)))) - (mapcar '(lambda (loc) - (let ((u-site (cdr (assoc 'site_url loc)))) - (when (member u-site (mapcar 'car sites-info)) - (cdr (assoc u-site sites-info))))) + (mapcar (lambda (loc) + (let ((u-site (cdr (assoc 'site_url loc)))) + (when (member u-site (mapcar 'car sites-info)) + (cdr (assoc u-site sites-info))))) sx-network--user-information))) (defvar sx-network--user-information nil -- 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(-) 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(-) 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(-) 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 876ab80a8b62214bb608716241c525ffc2ea0cf3 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 08:50:22 -0500 Subject: GH comment fix: Use sharp quote for function. --- sx-networks.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sx-networks.el b/sx-networks.el index ead4d6d..315daba 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -92,7 +92,7 @@ list of sites the user is active on." (sx-site--get-site-list)))) (mapcar (lambda (loc) (let ((u-site (cdr (assoc 'site_url loc)))) - (when (member u-site (mapcar 'car sites-info)) + (when (member u-site (mapcar #'car sites-info)) (cdr (assoc u-site sites-info))))) sx-network--user-information))) -- cgit v1.2.3