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
use activitystreams_vocabulary::create_activity;
create_activity! {
/// Indicates that new content has been pushed to the [Repository](crate::Repository).
///
/// # Example
///
/// ```rust
/// use activityforge::{Commit, Push, Sha1Hash, context};
/// use activitystreams_vocabulary::{DateTime, Iri, OrderedCollection};
///
/// # fn main() {
/// let id = Iri::try_from("https://example.dev/aviva/myproject/outbox/reBGo").unwrap();
/// let actor = Iri::try_from("https://example.dev/aviva/myproject").unwrap();
/// let attributed_to = Iri::try_from("https://example.dev/aviva").unwrap();
///
/// let to0 = Iri::try_from("https://example.dev/aviva").unwrap();
/// let to1 = Iri::try_from("https://example.dev/aviva/followers").unwrap();
/// let to2 = Iri::try_from("https://example.dev/aviva/myproject/followers").unwrap();
///
/// let summary = "<p>Aviva pushed a commit to myproject</p>";
///
/// let commit_id = Iri::try_from("https://example.dev/aviva/myproject/commits/d96596230322716bd6f87a232a648ca9822a1c20").unwrap();
/// let commit_attributed_to = Iri::try_from("https://example.dev/aviva").unwrap();
/// let commit_context = Iri::try_from("https://example.dev/aviva/myproject").unwrap();
/// let commit_hash = Sha1Hash::try_from("d96596230322716bd6f87a232a648ca9822a1c20").unwrap();
/// let commit_created = "2019-11-03T13:43:59Z";
/// let commit_summary = "Provide hints in sign-up form fields";
///
/// let target = Iri::try_from("https://example.dev/aviva/myproject/branches/master").unwrap();
///
/// let json_str = format!(
/// r#"{{
/// "@context": [
/// "https://www.w3.org/ns/activitystreams",
/// "https://forgefed.org/ns"
/// ],
/// "type": "Push",
/// "id": "{id}",
/// "attributedTo": "{attributed_to}",
/// "summary": "{summary}",
/// "to": [
/// "{to0}",
/// "{to1}",
/// "{to2}"
/// ],
/// "actor": "{actor}",
/// "object": {{
/// "type": "OrderedCollection",
/// "orderedItems": [
/// {{
/// "type": "Commit",
/// "id": "{commit_id}",
/// "attributedTo": "{commit_attributed_to}",
/// "summary": "{commit_summary}",
/// "context": "{commit_context}",
/// "created": "{commit_created}",
/// "hash": "{commit_hash}"
/// }}
/// ],
/// "totalItems": 1
/// }},
/// "target": "{target}"
/// }}"#
/// );
///
/// let context_property = context::forgefed_context();
///
/// let commit = Commit::new_inner()
/// .with_id(commit_id)
/// .with_attributed_to(commit_attributed_to)
/// .with_context(commit_context)
/// .with_hash(commit_hash)
/// .with_created(commit_created.parse::<DateTime>().unwrap())
/// .with_summary(commit_summary);
///
/// let items = [commit];
///
/// let object = OrderedCollection::new_inner()
/// .with_total_items(items.len() as u64)
/// .with_ordered_items(items);
///
/// let push = Push::new()
/// .with_context_property(context_property)
/// .with_id(id)
/// .with_actor(actor)
/// .with_attributed_to(attributed_to)
/// .with_to([to0, to1, to2])
/// .with_summary(summary)
/// .with_object(object)
/// .with_target(target);
///
/// assert_eq!(serde_json::to_string_pretty(&push).unwrap(), json_str);
/// assert_eq!(
/// serde_json::from_str::<Push>(json_str.as_str()).unwrap(),
/// push
/// );
/// # }
/// ```
Push: crate::ActivityType::Push {}
}
#[cfg(test)]
mod tests {
use activitystreams_vocabulary::{DateTime, Iri, OrderedCollection};
use super::*;
use crate::{Commit, Sha1Hash, context};
#[test]
fn test_push() {
let id = Iri::try_from("https://example.dev/aviva/myproject/outbox/reBGo").unwrap();
let actor = Iri::try_from("https://example.dev/aviva/myproject").unwrap();
let attributed_to = Iri::try_from("https://example.dev/aviva").unwrap();
let to0 = Iri::try_from("https://example.dev/aviva").unwrap();
let to1 = Iri::try_from("https://example.dev/aviva/followers").unwrap();
let to2 = Iri::try_from("https://example.dev/aviva/myproject/followers").unwrap();
let summary = "<p>Aviva pushed a commit to myproject</p>";
let commit_id = Iri::try_from(
"https://example.dev/aviva/myproject/commits/d96596230322716bd6f87a232a648ca9822a1c20",
)
.unwrap();
let commit_attributed_to = Iri::try_from("https://example.dev/aviva").unwrap();
let commit_context = Iri::try_from("https://example.dev/aviva/myproject").unwrap();
let commit_hash = Sha1Hash::try_from("d96596230322716bd6f87a232a648ca9822a1c20").unwrap();
let commit_created = "2019-11-03T13:43:59Z";
let commit_summary = "Provide hints in sign-up form fields";
let target = Iri::try_from("https://example.dev/aviva/myproject/branches/master").unwrap();
let json_str = format!(
r#"{{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://forgefed.org/ns"
],
"type": "Push",
"id": "{id}",
"attributedTo": "{attributed_to}",
"summary": "{summary}",
"to": [
"{to0}",
"{to1}",
"{to2}"
],
"actor": "{actor}",
"object": {{
"type": "OrderedCollection",
"orderedItems": [
{{
"type": "Commit",
"id": "{commit_id}",
"attributedTo": "{commit_attributed_to}",
"summary": "{commit_summary}",
"context": "{commit_context}",
"created": "{commit_created}",
"hash": "{commit_hash}"
}}
],
"totalItems": 1
}},
"target": "{target}"
}}"#
);
let context_property = context::forgefed_context();
let commit = Commit::new_inner()
.with_id(commit_id)
.with_attributed_to(commit_attributed_to)
.with_context(commit_context)
.with_hash(commit_hash)
.with_created(commit_created.parse::<DateTime>().unwrap())
.with_summary(commit_summary);
let items = [commit];
let object = OrderedCollection::new_inner()
.with_total_items(items.len() as u64)
.with_ordered_items(items);
let push = Push::new()
.with_context_property(context_property)
.with_id(id)
.with_actor(actor)
.with_attributed_to(attributed_to)
.with_to([to0, to1, to2])
.with_summary(summary)
.with_object(object)
.with_target(target);
assert_eq!(serde_json::to_string_pretty(&push).unwrap(), json_str);
assert_eq!(
serde_json::from_str::<Push>(json_str.as_str()).unwrap(),
push
);
}
}