2023-10-19 17:49:54 +02:00
|
|
|
;; tinmop: a multiprotocol client
|
2023-10-19 17:46:22 +02:00
|
|
|
;; Copyright © cage
|
2020-06-07 11:50:36 +02:00
|
|
|
|
|
|
|
;; 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/>.
|
|
|
|
|
|
|
|
(in-package :api-pleroma)
|
|
|
|
|
2020-09-02 16:30:13 +02:00
|
|
|
(defgeneric create-chat (object user-id))
|
|
|
|
|
|
|
|
(defmethod create-chat ((object tooter:client) (account-id string))
|
|
|
|
"Connect with `client' and create a new chat with user identified by `account-id'"
|
|
|
|
(decode-chat (tooter:submit object
|
|
|
|
(format nil
|
|
|
|
"/api/v1/pleroma/chats/by-account-id/~a"
|
|
|
|
account-id))))
|
|
|
|
|
2021-02-19 16:34:38 +01:00
|
|
|
|
2021-03-14 11:04:20 +01:00
|
|
|
(defgeneric get-chats-list (object &key with-muted-p max-id min-id since-id offset limit))
|
2021-02-19 16:34:38 +01:00
|
|
|
|
|
|
|
(defmethod get-chats-list ((object tooter:client)
|
|
|
|
&key
|
|
|
|
(with-muted-p t)
|
|
|
|
(max-id nil)
|
|
|
|
(min-id nil)
|
|
|
|
(since-id nil)
|
|
|
|
(offset 0)
|
2021-03-14 11:04:20 +01:00
|
|
|
(limit 200))
|
2021-02-19 16:34:38 +01:00
|
|
|
"Get a list of all chats, ordered from the more recent updated. Note: uses version 2 of the API."
|
|
|
|
(decode-chat (tooter:query object
|
|
|
|
"/api/v2/pleroma/chats"
|
|
|
|
:with-muted with-muted-p
|
|
|
|
:max-id max-id
|
|
|
|
:min-id min-id
|
|
|
|
:since-id since-id
|
|
|
|
:offset offset
|
2021-03-14 11:04:20 +01:00
|
|
|
:limit limit)))
|
2021-02-19 16:34:38 +01:00
|
|
|
|
2023-09-24 11:47:30 +02:00
|
|
|
(let ((cached-value nil))
|
|
|
|
(api-client:defun-api-call instance-type ()
|
|
|
|
(or cached-value
|
|
|
|
(handler-case
|
|
|
|
(progn
|
|
|
|
(get-all-chats api-client:*client*)
|
|
|
|
(setf cached-value :pleroma)
|
|
|
|
:pleroma)
|
|
|
|
(error ()
|
|
|
|
(setf cached-value :mastodon)
|
|
|
|
:mastodon)))))
|
|
|
|
|
|
|
|
(defun instance-pleroma-p ()
|
|
|
|
(eq (instance-type) :pleroma))
|
|
|
|
|
2021-03-13 20:50:04 +01:00
|
|
|
(defgeneric get-all-chats (object &key &allow-other-keys))
|
2021-02-19 16:34:38 +01:00
|
|
|
|
2022-11-21 20:30:58 +01:00
|
|
|
(defun sort-chat-id< (chats)
|
|
|
|
(sort chats #'string< :key #'chat-id))
|
|
|
|
|
2021-03-14 13:26:06 +01:00
|
|
|
(defmethod get-all-chats ((object tooter:client) &key (min-id nil) (accum ()))
|
2021-02-19 16:34:38 +01:00
|
|
|
"Get a list of all chats, ordered from the more recent updated."
|
2022-11-21 20:30:58 +01:00
|
|
|
(let ((chats (sort-chat-id< (get-chats-list object :min-id min-id))))
|
2021-02-19 16:34:38 +01:00
|
|
|
(if chats
|
2021-03-14 13:26:06 +01:00
|
|
|
(let ((new-min-id (chat-id (last-elt chats))))
|
|
|
|
(get-all-chats object
|
|
|
|
:min-id new-min-id
|
|
|
|
:accum (append chats accum)))
|
2022-11-21 20:30:58 +01:00
|
|
|
(sort-chat-id< accum))))
|
2021-02-19 16:34:38 +01:00
|
|
|
|
2020-09-02 16:30:13 +02:00
|
|
|
(defgeneric post-chat-message (object chat-id content media))
|
|
|
|
|
|
|
|
(defun post-chat-path (chat-id)
|
|
|
|
(format nil "/api/v1/pleroma/chats/~a/messages" chat-id))
|
|
|
|
|
2020-09-06 14:42:16 +02:00
|
|
|
(defmethod post-chat-message ((object tooter:client)
|
|
|
|
(chat-id string)
|
|
|
|
(content string)
|
|
|
|
(media null))
|
2020-09-02 16:30:13 +02:00
|
|
|
"Post a message to chat identified by `chat-id' with text `content` or
|
|
|
|
media `media'."
|
|
|
|
(decode-chat-message (tooter:submit object
|
|
|
|
(post-chat-path chat-id)
|
|
|
|
:content content
|
|
|
|
:media-id media)))
|
|
|
|
|
2020-09-06 14:42:16 +02:00
|
|
|
(defmethod post-chat-message ((object tooter:client)
|
|
|
|
(chat-id string)
|
|
|
|
(content null)
|
|
|
|
(media string))
|
2020-09-02 16:30:13 +02:00
|
|
|
"Post a message to chat identified by `chat-id' with text `content` or
|
|
|
|
media `media'. Returns a `chat-message' instance"
|
|
|
|
(let ((path-media (fs:namestring->pathname media)))
|
|
|
|
(decode-chat-message (tooter:submit object
|
|
|
|
(post-chat-path chat-id)
|
|
|
|
:content content
|
|
|
|
:media-id (tooter:id (tooter:make-media object
|
|
|
|
path-media))))))
|
|
|
|
|
2020-09-02 17:17:16 +02:00
|
|
|
(defgeneric fetch-chat-messages (object chat-id &key min-id))
|
|
|
|
|
|
|
|
(defmethod fetch-chat-messages ((object tooter:client) chat-id &key (min-id nil))
|
|
|
|
(decode-chat-message (tooter:query object
|
|
|
|
(format nil
|
|
|
|
"/api/v1/pleroma/chats/~a/messages"
|
|
|
|
chat-id)
|
|
|
|
:min-id min-id)))
|
|
|
|
|
2020-09-02 16:30:13 +02:00
|
|
|
(defgeneric delete-chat-message (object chat-id message-id))
|
|
|
|
|
|
|
|
(defmethod delete-chat-message ((object tooter:client) (chat-id string) (message-id string))
|
|
|
|
(decode-chat-message (tooter:submit object
|
|
|
|
(format nil
|
|
|
|
"/api/v1/pleroma/chats/~a/messages/~a"
|
|
|
|
chat-id
|
|
|
|
message-id)
|
|
|
|
:http-method :delete)))
|
2020-09-05 17:02:00 +02:00
|
|
|
|
2021-01-12 20:28:21 +01:00
|
|
|
(api-client:defun-api-call get-chat-messages (chat-id min-id)
|
2020-09-05 17:02:00 +02:00
|
|
|
(fetch-chat-messages api-client:*client* chat-id :min-id min-id))
|
|
|
|
|
2021-01-12 20:28:21 +01:00
|
|
|
(api-client:defun-api-call get-chats ()
|
2020-09-05 17:02:00 +02:00
|
|
|
(get-all-chats api-client:*client*))
|
2020-09-06 14:42:16 +02:00
|
|
|
|
2021-01-12 20:28:21 +01:00
|
|
|
(api-client:defun-api-call post-on-chat (chat-id message)
|
2020-09-06 14:42:16 +02:00
|
|
|
(if (cl-ppcre:scan "^/" message)
|
|
|
|
(api-pleroma:post-chat-message api-client:*client* chat-id nil message)
|
|
|
|
(api-pleroma:post-chat-message api-client:*client* chat-id message nil)))
|
2020-09-10 17:50:22 +02:00
|
|
|
|
2021-01-12 20:28:21 +01:00
|
|
|
(api-client:defun-api-call create-new-chat (user-id)
|
2020-09-10 17:50:22 +02:00
|
|
|
(create-chat api-client:*client* user-id))
|