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