aboutsummaryrefslogtreecommitdiff
path: root/sx-question.el
blob: 344b54c8b7c38fe0ca687dd2bec6fefd2c66cd0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
;;; sx-question.el --- question logic for stack-mode  -*- lexical-binding: t; -*-

;; Copyright (C) 2014  Sean Allred

;; Author: Sean Allred <code@seanallred.com>

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:


;;; Code:

(require 'sx)
(require 'sx-filter)
(require 'sx-method)

(defvar sx-question-browse-filter
  '((question.body_markdown
     question.comments
     question.answers
     question.last_editor
     question.accepted_answer_id
     question.link
     user.display_name
     comment.owner
     comment.body_markdown
     comment.body
     answer.last_editor
     answer.link
     answer.owner
     answer.body_markdown
     answer.comments)
    (user.profile_image shallow_user.profile_image))
  "The filter applied with `sx-question-get-questions' and
  `sx-question-get-question'.")

(defun sx-question-get-questions (site &optional page)
  "Get the page PAGE of questions from SITE.

Return a list of questions, each consed with (site SITE).

`sx-method-call' is used with `sx-question-browse-filter'."
  (mapcar
   (lambda (question) (cons (cons 'site site) question))
   (sx-method-call
    "questions"
    `((site . ,site)
      (page . ,page))
    sx-question-browse-filter)))

(defun sx-question-get-question (site question-id)
  "Query SITE for a QUESTION-ID and return it.

If QUESTION-ID doesn't exist on SITE, raise an error."
  (let ((res (sx-method-call
              (format "questions/%s" question-id)
              `((site . ,site))
              sx-question-browse-filter)))
    (if (vectorp res)
        (elt res 0)
      (error "Couldn't find question %S in %S"
             question-id site))))


;;; Question Properties

;;;; Read/unread
(defvar sx-question--user-read-list nil 
  "Alist of questions read by the user.

Each element has the form

    (SITE . QUESTION-LIST)

where each element in QUESTION-LIST has the form

    (QUESTION_ID . LAST-VIEWED-DATE).")

(defun sx-question--ensure-read-list (site)
  "Ensure `sx-question--user-read-list' has been read from cache.

If no cache exists for it, initialize one with SITE."
  (unless sx-question--user-read-list
    (setq sx-question--user-read-list
          (sx-cache-get 'read-questions `'((,site))))))

(defun sx-question--read-p (question)
  "Non-nil if QUESTION has been read since last updated.

See `sx-question--user-read-list'."
  (sx-assoc-let question
    (sx-question--ensure-read-list .site)
    (let ((ql (cdr (assoc .site sx-question--user-read-list))))
      (and ql
           (>= (or (cdr (assoc .question_id ql)) 0)
               .last_activity_date)))))

(defun sx-question--mark-read (question)
  "Mark QUESTION as being read until it is updated again.

See `sx-question--user-read-list'."
  (sx-assoc-let question
    (sx-question--ensure-read-list .site)
    (let ((site-cell (assoc .site sx-question--user-read-list))
          (q-cell (cons .question_id .last_activity_date))
          cell)
      (cond
       ;; First question from this site.
       ((null site-cell)
        (push (list .site q-cell) sx-question--user-read-list))
       ;; Question already has an older time.
       ((setq cell (assoc .question_id site-cell))
        (setcdr cell .last_activity_date))
       ;; Question wasn't present.
       (t
        (sx-sorted-insert-skip-first
         q-cell site-cell (lambda (x y) (> (car x) (car y))))))))
  ;; Save the results.
  ;; @TODO This causes a small lag on `j' and `k' as the list gets
  ;; large.  Should we do this on a timer?
  (sx-cache-set 'read-questions sx-question--user-read-list))


;;;; Hidden
(defvar sx-question--user-hidden-list nil 
  "Alist of questions hidden by the user.

Each element has the form

  (SITE QUESTION_ID QUESTION_ID ...)")

(defun sx-question--ensure-hidden-list (site)
  "Ensure the `sx-question--user-hidden-list' has been read from cache.

If no cache exists for it, initialize one with SITE."
  (unless sx-question--user-hidden-list
    (setq sx-question--user-hidden-list
          (sx-cache-get 'hidden-questions `'((,site))))))

(defun sx-question--hidden-p (question)
  "Non-nil if QUESTION has been hidden."
  (sx-assoc-let question
    (sx-question--ensure-hidden-list .site)
    (let ((ql (cdr (assoc .site sx-question--user-hidden-list))))
      (and ql (memq .question_id ql)))))

(defun sx-question--mark-hidden (question)
  "Mark QUESTION as being hidden."
  (sx-assoc-let question
    (sx-question--ensure-hidden-list .site)
    (let ((site-cell (assoc .site sx-question--user-hidden-list))
          cell)
      ;; If question already hidden, do nothing.
      (unless (memq .question_id site-cell)
        ;; First question from this site.
        (if (null site-cell)
            (push (list .site .question_id) sx-question--user-hidden-list)
          ;; Question wasn't present.
          ;; Add it in, but make sure it's sorted (just in case we need
          ;; it later).
          (sx-sorted-insert-skip-first .question_id site-cell >))
        ;; This causes a small lag on `j' and `k' as the list gets large.
        ;; Should we do this on a timer?
        ;; Save the results.
        (sx-cache-set 'hidden-questions sx-question--user-hidden-list)))))


;;;; Other data

(defun sx-question--accepted-answer-id (question)
  "Return accepted answer in QUESTION or nil if none exists."
  (sx-assoc-let question
    (and (integerp .accepted_answer_id)
         .accepted_answer_id)))

(defun sx-question--tag-format (tag)
  "Formats TAG for display."
  (concat "[" tag "]"))

(provide 'sx-question)
;;; sx-question.el ends here

;; Local Variables:
;; indent-tabs-mode: nil
;; End: