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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
;;; mastodon-search.el --- Search functions for mastodon.el -*- lexical-binding: t -*-
;; Copyright (C) 2017-2019 Marty Hiatt
;; Author: Marty Hiatt <martianhiatus@riseup.net>
;; Maintainer: Marty Hiatt <martianhiatus@riseup.net>
;; Version: 1.0.0
;; Homepage: https://codeberg.org/martianh/mastodon.el
;; This file is not part of GNU Emacs.
;; This file is part of mastodon.el.
;; mastodon.el 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.
;; mastodon.el 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 mastodon.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A basic search function for mastodon.el
;;; Code:
(require 'json)
(eval-when-compile
(require 'mastodon-tl))
(autoload 'mastodon-auth--access-token "mastodon-auth")
(autoload 'mastodon-http--api "mastodon-http")
(autoload 'mastodon-http--get-json "mastodon-http")
(autoload 'mastodon-http--get-search-json "mastodon-http")
(autoload 'mastodon-mode "mastodon")
(autoload 'mastodon-tl--as-string "mastodon-tl")
(autoload 'mastodon-tl--as-string "mastodon-tl")
(autoload 'mastodon-tl--render-text "mastodon-tl")
(autoload 'mastodon-tl--set-buffer-spec "mastodon-tl")
(autoload 'mastodon-tl--set-face "mastodon-tl")
(autoload 'mastodon-tl--timeline "mastodon-tl")
(autoload 'mastodon-tl--toot "mastodon-tl")
(autoload 'mastodon-tl--buffer-property "mastodon-tl")
(autoload 'mastodon-http--api-search "mastodon-http")
(defvar mastodon-toot--completion-style-for-mentions)
(defvar mastodon-instance-url)
(defvar mastodon-tl--link-keymap)
(defvar mastodon-tl--horiz-bar)
;; functions for completion of mentions in mastodon-toot
(defun mastodon-search--get-user-info-@ (account)
"Get user handle, display name and account URL from ACCOUNT."
(list (concat "@" (cdr (assoc 'acct account)))
(cdr (assoc 'url account))
(cdr (assoc 'display_name account))))
(defun mastodon-search--search-accounts-query (query)
"Prompt for a search QUERY and return accounts synchronously.
Returns a nested list containing user handle, display name, and URL."
(let* ((url (mastodon-http--api "accounts/search"))
(response
(if (equal mastodon-toot--completion-style-for-mentions "following")
(mastodon-http--get-json
url `(("q" . ,query) ("following" . "true"))
:silent)
(mastodon-http--get-json url `(("q" . ,query)) :silent))))
(mapcar #'mastodon-search--get-user-info-@ response)))
;; functions for tags completion:
(defun mastodon-search--search-tags-query (query)
"Return an alist containing tag strings plus their URLs.
QUERY is the string to search."
(let* ((url (mastodon-http--api-search))
(params `(("q" . ,query) ("type" . "hashtags")))
(response (mastodon-http--get-json url params :silent))
(tags (alist-get 'hashtags response)))
(mapcar #'mastodon-search--get-hashtag-info tags)))
;; trending tags
(defun mastodon-search--trending-tags ()
"Display a list of tags trending on your instance."
(interactive)
(mastodon-search--view-trending "tags"
#'mastodon-search--print-tags-list))
(defun mastodon-search--trending-statuses ()
"Display a list of statuses trending on your instance."
(interactive)
(mastodon-search--view-trending "statuses"
#'mastodon-tl--timeline))
(defun mastodon-search--view-trending (type print-fun)
"Display a list of tags trending on your instance.
TYPE is a string, either tags, statuses, or links.
PRINT-FUN is the function used to print the data from the response."
(let* ((url (mastodon-http--api
(format "trends/%s" type)))
;; max for statuses = 40, for others = 20
(limit (if (equal type "statuses")
'("limit" . "40")
'("limit" . "20")))
(offset '(("offset" . "0")))
(params (push limit offset))
(response (mastodon-http--get-json url params))
(data (cond ((equal type "tags")
(mapcar #'mastodon-search--get-hashtag-info response))
((equal type "statuses")
response) ; no longer needs further processing
((equal type "links")
(message "todo"))))
(buffer (get-buffer-create (format "*mastodon-trending-%s*" type))))
(with-mastodon-buffer buffer #'mastodon-mode nil
(mastodon-tl--set-buffer-spec (buffer-name buffer)
(format "trends/%s" type)
print-fun nil
params)
(insert (mastodon-tl--set-face
(concat "\n " mastodon-tl--horiz-bar "\n"
(upcase (format " TRENDING %s\n" type))
" " mastodon-tl--horiz-bar "\n\n")
'success))
(funcall print-fun data)
(unless (equal type "statuses")
(goto-char (point-min))))))
;; functions for mastodon search
(defun mastodon-search--format-heading (heading)
"Format HEADING as a heading."
(insert
(mastodon-tl--set-face (concat "\n " mastodon-tl--horiz-bar "\n "
heading "\n"
" " mastodon-tl--horiz-bar "\n")
'success)))
(defvar mastodon-search-types
'("statuses" "accounts" "hashtags"))
(defun mastodon-search--query (query
&optional type limit
following account-id offset)
"Prompt for a search QUERY and return accounts, statuses, and hashtags.
TYPE is a member of `mastodon-search-types'.
LIMIT is a number as string, up to 40, with 40 the default.
FOLLOWING means limit to accounts followed, for \"accounts\" type only.
A single prefix arg also sets FOLLOWING to true.
ACCOUNT-ID means limit search to that account, for \"statuses\" type only.
OFFSET is a number as string, means to skip that many results. It
is used for pagination."
;; TODO: handle no results
(interactive "sSearch mastodon for: ")
(let* ((url (mastodon-http--api-search))
(following (when (or following
(equal current-prefix-arg '(4)))
"true"))
(type (or type
(if (equal current-prefix-arg '(4))
"accounts" ; if FOLLOWING, must be "accounts"
(completing-read "Search type: "
mastodon-search-types
nil t))))
(limit (or limit "40"))
(offset (or offset "0"))
(buffer (format "*mastodon-search-%s-%s*" type query))
(params (cl-remove nil
`(("q" . ,query)
,(when type `("type" . ,type))
,(when limit `("limit" . ,limit))
,(when offset `("offset" . ,offset))
,(when following `("following" . ,following))
,(when account-id `("account_id" . ,account-id)))))
(response (mastodon-http--get-json url params))
(accts (when (equal type "accounts")
(alist-get 'accounts response)))
(tags (when (equal type "hashtags")
(alist-get 'hashtags response)))
(statuses (when (equal type "statuses")
(alist-get 'statuses response))))
(with-mastodon-buffer buffer #'mastodon-mode nil
(mastodon-search-mode)
(mastodon-search--format-heading (upcase type))
;; user results:
(cond ((equal type "accounts")
(mastodon-search--render-response accts type buffer params
'mastodon-views--insert-users-propertized-note
'mastodon-views--insert-users-propertized-note))
((equal type "hashtags")
(mastodon-search--render-response tags type buffer params
'mastodon-search--print-tags
'mastodon-search--print-tags))
((equal type "statuses")
(mastodon-search--render-response statuses type buffer params
#'mastodon-tl--timeline
#'mastodon-tl--timeline)))
(goto-char (point-min))
(message
(substitute-command-keys
"\\[mastodon-search--query-cycle] to cycle result types.")))))
(defun mastodon-search-insert-no-results (&optional thing)
"Insert a no results message for object THING."
(let ((thing (or thing "nothing")))
(insert
(propertize (format "Looks like search returned no %s." thing)
'face 'font-lock-comment-face))))
(defun mastodon-search--render-response (data type buffer params
insert-fun update-fun)
"Call INSERT-FUN on DATA of result TYPE if non-nil.
BUFFER, PARAMS, and UPDATE-FUN are for `mastodon-tl--buffer-spec'."
(if (not data)
(mastodon-search-insert-no-results type)
(funcall insert-fun data))
;; (mapc #'mastodon-tl--toot data))
(mastodon-tl--set-buffer-spec buffer "search"
update-fun
nil params))
(defun mastodon-search--buf-type ()
"Return search buffer type, a member of `mastodon-search-types'."
;; called in `mastodon-tl--get-buffer-type'
(let* ((spec (mastodon-tl--buffer-property 'update-params)))
(alist-get "type" spec nil nil #'equal)))
(defun mastodon-search--query-cycle ()
"Cycle through search types: accounts, hashtags, and statuses."
(interactive)
(let* ((spec (mastodon-tl--buffer-property 'update-params))
(type (alist-get "type" spec nil nil #'equal))
(query (alist-get "q" spec nil nil #'equal)))
(cond ((equal type "hashtags")
(mastodon-search--query query "accounts"))
((equal type "accounts")
(mastodon-search--query query "statuses"))
((equal type "statuses")
(mastodon-search--query query "hashtags")))))
(defun mastodon-serach--query-accounts-followed (query)
"Run an accounts search QUERY, limited to your followers."
(interactive "sSearch mastodon for: ")
(mastodon-search--query query "accounts" :following))
(defun mastodon-search--insert-users-propertized (json &optional note)
"Insert users list into the buffer.
JSON is the data from the server.
If NOTE is non-nil, include user's profile note. This is also
called by `mastodon-tl--get-follow-suggestions' and
`mastodon-profile--insert-follow-requests'."
(mapc (lambda (acct)
(insert (concat (mastodon-search--propertize-user acct note)
mastodon-tl--horiz-bar
"\n\n")))
json))
(defun mastodon-search--propertize-user (acct &optional note)
"Propertize display string for ACCT, optionally including profile NOTE."
(let* ((user (mastodon-search--get-user-info acct))
(id (alist-get 'id acct)))
(propertize
(concat
(propertize (car user)
'face 'mastodon-display-name-face
'byline t
'toot-id id) ; for prev/next nav
" : \n : "
(propertize (concat "@" (cadr user))
'face 'mastodon-handle-face
'mouse-face 'highlight
'mastodon-tab-stop 'user-handle
'keymap mastodon-tl--link-keymap
'mastodon-handle (concat "@" (cadr user))
'help-echo (concat "Browse user profile of @" (cadr user)))
" : \n"
(if note
(mastodon-tl--render-text (cadddr user) acct)
"")
"\n")
'toot-json acct))) ; for compat w other processing functions
(defun mastodon-search--print-tags (tags)
"Print TAGS data as returned from a \"hashtags\" search query."
(let ((tags-list (mapcar #'mastodon-search--get-hashtag-info tags)))
(mastodon-search--print-tags-list tags-list)))
(defun mastodon-search--print-tags-list (tags-list)
"Insert a propertized list of TAGS-LIST."
(mapc (lambda (el)
(insert
" : "
(propertize (concat "#" (car el))
'face '(:box t)
'mouse-face 'highlight
'mastodon-tag (car el)
'mastodon-tab-stop 'hashtag
'help-echo (concat "Browse tag #" (car el))
'keymap mastodon-tl--link-keymap)
" : \n\n"))
tags-list))
(defun mastodon-search--get-user-info (account)
"Get user handle, display name, account URL and profile note from ACCOUNT."
(list (if (not (string-empty-p (alist-get 'display_name account)))
(alist-get 'display_name account)
(alist-get 'username account))
(alist-get 'acct account)
(alist-get 'url account)
(alist-get 'note account)))
(defun mastodon-search--get-hashtag-info (tag)
"Get hashtag name and URL from TAG."
(list (alist-get 'name tag)
(alist-get 'url tag)))
(defun mastodon-search--get-status-info (status)
"Get ID, timestamp, content, and spoiler from STATUS."
(list (alist-get 'id status)
(alist-get 'created_at status)
(alist-get 'spoiler_text status)
(alist-get 'content status)))
(defun mastodon-search--id-from-status (status)
"Fetch the id from a STATUS returned by a search call to the server.
We use this to fetch the complete status from the server."
(alist-get 'id status))
(defun mastodon-search--full-status-from-id (id)
"Fetch the full status with id ID from the server.
This allows us to access the full account etc. details and to
render them properly."
(let* ((url (concat mastodon-instance-url "/api/v1/statuses/" (mastodon-tl--as-string id)))
(json (mastodon-http--get-json url)))
json))
(defvar mastodon-search-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'mastodon-search--query-cycle)
map)
"Keymap for `mastodon-search-mode'.")
(define-minor-mode mastodon-search-mode
"Toggle mastodon search minor mode.
This minor mode is used for mastodon search pages to adds a keybinding."
:init-value nil
:lighter " Search"
:keymap mastodon-search-mode-map
:group 'mastodon
:global nil)
(provide 'mastodon-search)
;;; mastodon-search.el ends here
|