egg_mode/direct/
fun.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::common::*;
6
7use crate::user::{self, UserID};
8use crate::{auth, links};
9
10use super::*;
11
12/// Lookup a single DM by its numeric ID.
13pub async fn show(id: u64, token: &auth::Token) -> Result<Response<DirectMessage>, error::Error> {
14    let params = ParamList::default().add_param("id", id.to_string());
15    let req = get(links::direct::SHOW, token, Some(&params));
16    let resp: Response<raw::SingleEvent> = request_with_json_response(req).await?;
17    Ok(Response::into(resp))
18}
19
20/// Load the list of direct messages sent and received by the authorized user.
21///
22/// This function will only return the messages sent and received in the last 30 days. For more
23/// information, see the docs for [`Timeline`].
24///
25/// [`Timeline`]: struct.Timeline.html
26pub fn list(token: &auth::Token) -> Timeline {
27    Timeline::new(links::direct::LIST, token.clone())
28}
29
30/// Delete the direct message with the given ID.
31///
32/// The authenticated user must be the sender of this DM for this call to be successful.
33///
34/// This function will only delete the DM for the user - other users who have received the message
35/// will still see it.
36///
37/// Twitter does not return anything upon a successful deletion, so this function will return an
38/// empty `Response` upon success.
39pub async fn delete(id: u64, token: &auth::Token) -> Result<Response<()>, error::Error> {
40    let params = ParamList::new().add_param("id", id.to_string());
41    let req = auth::raw::delete(links::direct::DELETE, token, Some(&params));
42    request_with_empty_response(req).await
43}
44
45/// Marks the given message as read in the sender's interface.
46///
47/// This function sends a read receipt for the given message ID, marking it and all messages before
48/// it as read. The Twitter Web Client and other first-party Twitter clients can display an
49/// indicator to show the last message that was read.  This function can also be used to clear an
50/// "unread" indicator in these clients for the message.
51///
52/// Note that while this function accepts any `UserID`, the underlying Twitter API call only
53/// accepts a numeric ID for the sender. If you pass a string Screen Name to this function, a
54/// separate user lookup will occur prior to sending the read receipt. To avoid this extra lookup,
55/// pass a numeric ID (or the `UserID::ID` variant of `UserID`) to this function.
56pub async fn mark_read(
57    id: u64,
58    sender: impl Into<UserID>,
59    token: &auth::Token,
60) -> Result<Response<()>, error::Error> {
61    let recipient_id = match sender.into() {
62        UserID::ID(id) => id,
63        UserID::ScreenName(name) => {
64            let user = user::show(name, token).await?;
65            user.id
66        }
67    };
68    let params = ParamList::new()
69        .add_param("last_read_event_id", id.to_string())
70        .add_param("recipient_id", recipient_id.to_string());
71    let req = post(links::direct::MARK_READ, token, Some(&params));
72    request_with_empty_response(req).await
73}
74
75/// Displays a visual typing indicator for the recipient.
76///
77/// The typing indicator will display for 3 seconds or until the authenticated user sends a message
78/// to the recipient, whichever comes first.
79///
80/// Twitter warns that sending this request for every typing event will likely quickly come across
81/// rate limits (1000 requests per 15 minutes). Instead, they recommend capturing these input
82/// events and limiting API requests to some slower rate based on the behavior of your users and
83/// the Twitter rate limit constraints.
84///
85/// Note that while this function accepts any `UserID`, the underlying Twitter API call only
86/// accepts a numeric ID for the sender. If you pass a string Screen Name to this function, a
87/// separate user lookup will occur prior to sending the read receipt. To avoid this extra lookup,
88/// pass a numeric ID (or the `UserID::ID` variant of `UserID`) to this function.
89pub async fn indicate_typing(
90    recipient: impl Into<UserID>,
91    token: &auth::Token,
92) -> Result<Response<()>, error::Error> {
93    let recipient_id = match recipient.into() {
94        UserID::ID(id) => id,
95        UserID::ScreenName(name) => {
96            let user = user::show(name, token).await?;
97            user.id
98        }
99    };
100
101    let params = ParamList::new().add_param("recipient_id", recipient_id.to_string());
102    let req = post(links::direct::INDICATE_TYPING, token, Some(&params));
103    request_with_empty_response(req).await
104}