Skip to main content

apb/
target.rs

1use crate::Object;
2
3pub const PUBLIC : &str = "https://www.w3.org/ns/activitystreams#Public";
4pub const PUBLIC_COMPACT: &str = "as:Public";
5
6pub fn is_public(target: &str) -> bool {
7	target == PUBLIC || target == PUBLIC_COMPACT
8}
9
10pub trait Addressed {
11	fn addressed(&self) -> Vec<String>; // TODO rename this? remate others? idk
12	fn mentioning(&self) -> Vec<String>;
13	// fn secondary_targets(&self) -> Vec<String>;
14	// fn public_targets(&self) -> Vec<String>;
15	// fn private_targets(&self) -> Vec<String>;
16}
17
18impl<T: Object> Addressed for T {
19	fn addressed(&self) -> Vec<String> {
20		let mut to : Vec<String> = self.to().all_ids();
21		to.append(&mut self.bto().all_ids());
22		to.append(&mut self.cc().all_ids());
23		to.append(&mut self.bcc().all_ids());
24		to
25	}
26
27	fn mentioning(&self) -> Vec<String> {
28		let mut to : Vec<String> = self.to().all_ids();
29		to.append(&mut self.bto().all_ids());
30		to
31	}
32
33	// fn secondary_targets(&self) -> Vec<String> {
34	// 	let mut to : Vec<String> = self.cc().ids();
35	// 	to.append(&mut self.bcc().ids());
36	// 	to
37	// }
38
39	// fn public_targets(&self) -> Vec<String> {
40	// 	let mut to : Vec<String> = self.to().ids();
41	// 	to.append(&mut self.cc().ids());
42	// 	to
43	// }
44
45	// fn private_targets(&self) -> Vec<String> {
46	// 	let mut to : Vec<String> = self.bto().ids();
47	// 	to.append(&mut self.bcc().ids());
48	// 	to
49	// }
50}
51
52#[cfg(test)]
53mod test {
54	use super::Addressed;
55
56	#[test]
57	#[cfg(feature = "unstructured")]
58	fn addressed_trait_finds_all_targets_on_json_objects() {
59		let obj = serde_json::json!({
60			"id": "http://localhost:8080/obj/1",
61			"type": "Note",
62			"content": "hello world!",
63			"published": "2024-06-04T17:09:20+00:00",
64			"to": ["http://localhost:8080/usr/root/followers"],
65			"bto": ["https://localhost:8080/usr/secret"],
66			"cc": [crate::target::PUBLIC],
67			"bcc": [],
68		});
69
70		let addressed = obj.addressed();
71
72		assert_eq!(
73			addressed,
74			vec![
75				"http://localhost:8080/usr/root/followers".to_string(), 
76				"https://localhost:8080/usr/secret".to_string(),
77				crate::target::PUBLIC.to_string(),
78			]
79		);
80	}
81
82	#[test]
83	#[cfg(feature = "unstructured")]
84	fn primary_targets_only_finds_to_and_bto() {
85		let obj = serde_json::json!({
86			"id": "http://localhost:8080/obj/1",
87			"type": "Note",
88			"content": "hello world!",
89			"published": "2024-06-04T17:09:20+00:00",
90			"to": ["http://localhost:8080/usr/root/followers"],
91			"bto": ["https://localhost:8080/usr/secret"],
92			"cc": [crate::target::PUBLIC],
93			"bcc": [],
94		});
95
96		let addressed = obj.mentioning();
97
98		assert_eq!(
99			addressed,
100			vec![
101				"http://localhost:8080/usr/root/followers".to_string(), 
102				"https://localhost:8080/usr/secret".to_string(),
103			]
104		);
105	}
106}