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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! Twitter feed
//!
//! This module includes the [`Twitter`] struct that is a source that is able to parse a twitter feed via twitter API

use crate::entry::Entry;
use crate::error::source::TwitterError;
use crate::sink::Media;
use crate::sink::Message;

use egg_mode::entities::MediaType;
use egg_mode::{auth::bearer_token, tweet::user_timeline, KeyPair, Token};

/// A source that fetches from a Twitter feed using the Twitter API
pub struct Twitter {
	handle: String,
	api_key: String,
	api_secret: String,
	token: Option<Token>,
	filter: Vec<String>,
}

impl Twitter {
	/// Creates a new [`Twitter`] source
	#[must_use]
	pub fn new(handle: String, api_key: String, api_secret: String, filter: Vec<String>) -> Self {
		Self {
			handle,
			api_key,
			api_secret,
			token: None,
			filter,
		}
	}

	/// Fetches all tweets from the feed
	#[tracing::instrument(skip_all)]
	pub async fn get(&mut self) -> Result<Vec<Entry>, TwitterError> {
		tracing::debug!("Getting tweets");

		let token = match &self.token {
			Some(t) => t,
			None => {
				self.token = Some(
					bearer_token(&KeyPair::new(self.api_key.clone(), self.api_secret.clone()))
						.await
						.map_err(TwitterError::Auth)?,
				);

				self.token
					.as_ref()
					.expect("token should have been init just up above")
			}
		};

		// TODO: keep a tweet id -> message id hashmap and handle enable with_replies from below
		let (_, tweets) = user_timeline(self.handle.clone(), false, true, token)
			/*
			// TODO: is this doing what I think it is doing or have I gotten it wrong? The docs aren't clear enough
			.older(
				// read_filter
				// 	.and_then(ReadFilter::last_read)
				// 	.and_then(|x| x.parse().ok()),
				if let Some(rf) = &self.read_filter {
					if let Some(last_read_id) = rf.read().await.last_read() {
						last_read_id.parse().ok()
					} else {
						None
					}
				} else {
					None
				},
			)
			*/
			.start()
			.await?;

		tracing::debug!(
			"Got {num} tweets older than the last one read",
			num = tweets.len()
		);

		let messages = tweets
			.iter()
			.filter_map(|tweet| {
				if !self.filter.is_empty()
					&& !Self::tweet_contains_filters(&tweet.text, self.filter.as_slice())
				{
					return None;
				}

				Some(Entry {
					id: Some(tweet.id.to_string()),
					msg: Message {
						body: Some(tweet.text.clone()),
						link: Some(
							format!(
								"https://twitter.com/{handle}/status/{id}",
								handle = self.handle,
								id = tweet.id
							)
							.as_str()
							.try_into()
							.expect("The URL is hand crafted and should always be valid"),
						),
						media: tweet.entities.media.as_ref().and_then(|x| {
							x.iter()
								.map(|x| match x.media_type {
									MediaType::Photo => {
										Some(Media::Photo(x.media_url.as_str().try_into().expect("The tweet URL provided by the Tweeter API should always be a valid URL")))
									}
									MediaType::Video => {
										Some(Media::Video(x.media_url.as_str().try_into().expect("The tweet URL provided by the Tweeter API should always be a valid URL")))
									}
									MediaType::Gif => None,
								})
								.collect::<Option<Vec<Media>>>()
						}),
						..Default::default()
					},
					..Default::default()
				})
			})
			.collect::<Vec<_>>();

		let unread_num = messages.len();
		if unread_num > 0 {
			tracing::debug!("Got {unread_num} unread filtered tweets");
		} else {
			tracing::debug!("All tweets have already been read, none remaining to send");
		}

		Ok(messages)
	}

	fn tweet_contains_filters(tweet: &str, filters: &[String]) -> bool {
		for filter in filters {
			if !tweet.to_lowercase().contains(&filter.to_lowercase()) {
				return false;
			}
		}

		true
	}
}

impl std::fmt::Debug for Twitter {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Twitter")
			.field("handle", &self.handle)
			.field("filter", &self.filter)
			.finish_non_exhaustive()
	}
}