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 (limited to '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 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(-) (limited to 'sx-networks.el') 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(-) (limited to 'sx-networks.el') 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(-) (limited to 'sx-networks.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 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(-) (limited to 'sx-networks.el') 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 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(-) (limited to 'sx-networks.el') 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 From 6a1d561e3af81aaa67dd834192697e39e3e4bc5d Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 11:21:51 -0500 Subject: Fix logic for updating cache and setting variables. Turned (sx-network--ensure-user) into (sx-network--initialize) and added as hook for initialization. --- sx-networks.el | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'sx-networks.el') diff --git a/sx-networks.el b/sx-networks.el index 315daba..fbb2d78 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -56,28 +56,30 @@ "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 + (or (and (setq sx-network--user-information (sx-cache-get 'network-user) + 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))) - 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)) - -(defun sx-network--ensure-user () + "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/associated" + '((types . (main_site meta_site))) + sx-network--user-filter + 'warn)) + (sx-network--get-associated)) + +(defun sx-network--initialize () "Ensure user-cache is available. -This should be called during initialization." +Added as hook to initialization." ;; Cache was not retrieved, retrieve it. - (unless sx-network--user-information - (sx-network--get-associated))) + (sx-network--get-associated)) +(add-hook 'sx-init--internal-hook #'sx-network--initialize) (defun sx-network--map-site-url-to-site-api () "Convert `me/associations' to a set of `api_site_parameter's. -- cgit v1.2.3 From cb4d53d57f5ee68aced5ff1a73829eea8a2d662b Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Tue, 18 Nov 2014 11:44:16 -0500 Subject: Correct naming of cache to correspond to sx-cache-get. --- sx-networks.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-networks.el') diff --git a/sx-networks.el b/sx-networks.el index fbb2d78..755d62c 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -74,7 +74,7 @@ the variables." (sx-network--get-associated)) (defun sx-network--initialize () - "Ensure user-cache is available. + "Ensure network-user cache is available. Added as hook to initialization." ;; Cache was not retrieved, retrieve it. -- cgit v1.2.3