activityforge 0.1.0-pre-alpha.2

ActivityForge federated git forges over ActivityPub
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use serde::{Deserialize, Serialize};

use crate::db::{
    Db, Follower, Inbox, Iri, Key, Like, Name, Outbox, PatchTracker, Repository, TableEntry,
    TableType, Team, TicketTracker, Uuid,
};
use crate::{Error, Result, util};

/// Represents a builder struct for a [Repository].
///
/// Allows for users to have a high-level interface for creating all of the
/// sub-record entries that make up a [Repository] entry.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct RepositoryBuilder {
    #[serde(serialize_with = "util::ser_uuid", deserialize_with = "util::de_uuid")]
    uuid: Uuid,
    id: Iri,
    name: Name,
    inbox: Inbox,
    outbox: Outbox,
    clone_uris: Vec<Iri>,
    push_uris: Vec<Iri>,
    forks: Vec<Repository>,
    likes: Vec<Like>,
    followers: Vec<Follower>,
    keys: Vec<Key>,
    patches_tracked_by: Option<PatchTracker>,
    tickets_tracked_by: Option<TicketTracker>,
    is_archived: Option<bool>,
    moved_to: Option<Repository>,
    mirrors: Option<Repository>,
    team: Option<Team>,
}

impl RepositoryBuilder {
    /// Creates a new [RepositoryBuilder].
    ///
    /// Creates a default inbox and outbox based on the supplied `id`.
    pub fn new<I: Into<Iri>, N: Into<Name>>(id: I, name: N) -> Result<Self> {
        let uuid = util::rand_uuid();

        let id = Repository::TABLE.id_from_uuid(&id.into(), uuid)?;
        let name = name.into();

        let inbox_id = Iri::try_from(format!("{id}/inbox")).unwrap_or_default();
        let outbox_id = Iri::try_from(format!("{id}/outbox")).unwrap_or_default();

        let actor = TableEntry::create(Self::table(), uuid);

        let inbox = Inbox::new()
            .with_uuid(util::rand_uuid())
            .with_id(inbox_id)
            .with_actor(actor);

        let outbox = Outbox::new()
            .with_uuid(util::rand_uuid())
            .with_id(outbox_id)
            .with_actor(actor);

        Ok(Self {
            uuid,
            id,
            name,
            inbox,
            outbox,
            clone_uris: Vec::new(),
            push_uris: Vec::new(),
            forks: Vec::new(),
            likes: Vec::new(),
            followers: Vec::new(),
            keys: Vec::new(),
            patches_tracked_by: None,
            tickets_tracked_by: None,
            is_archived: None,
            moved_to: None,
            mirrors: None,
            team: None,
        })
    }

    /// Gets the table type.
    #[inline]
    pub const fn table() -> TableType {
        Repository::TABLE
    }

    /// Gets the table entry.
    #[inline]
    pub const fn table_entry(&self) -> TableEntry {
        TableEntry::create(Self::table(), self.uuid)
    }

    /// Sets the [Uuid] for the [Repository].
    ///
    /// Optional, a random [Uuid] is assigned in [RepositoryBuilder::new].
    pub fn uuid(self, uuid: Uuid) -> Result<Self> {
        Self::table()
            .id_from_uuid(&self.id, uuid)
            .map(|id| {
                Self {
                    uuid,
                    id,
                    name: self.name,
                    clone_uris: self.clone_uris,
                    push_uris: self.push_uris,
                    is_archived: self.is_archived,
                    moved_to: self.moved_to,
                    mirrors: self.mirrors,
                    patches_tracked_by: self.patches_tracked_by,
                    tickets_tracked_by: self.tickets_tracked_by,
                    team: self.team,
                    inbox: Inbox::new(),
                    outbox: Outbox::new(),
                    forks: Vec::new(),
                    likes: Vec::new(),
                    keys: Vec::new(),
                    followers: Vec::new(),
                }
                .inbox(self.inbox)
                .outbox(self.outbox)
                .likes(self.likes)
                .forks(self.forks)
                .followers(self.followers)
            })
            .and_then(|s| s.keys(self.keys))
    }

    /// Sets the ID IRI for the [Repository].
    ///
    /// Optional, set a different ID than provided in [RepositoryBuilder::new].
    pub fn id<I: Into<Iri>>(self, id: I) -> Result<Self> {
        Repository::TABLE
            .id_from_uuid(&id.into(), self.uuid)
            .map(|id| Self { id, ..self })
    }

    /// Sets the name for the [Repository].
    ///
    /// Optional, set a different name than provided in [RepositoryBuilder::new].
    pub fn name<N: Into<Name>>(self, name: N) -> Self {
        Self {
            name: name.into(),
            ..self
        }
    }

    /// Sets the [Inbox] for the [Repository].
    ///
    /// Only needed if the default [Inbox] created by [RepositoryBuilder::new] is incorrect.
    pub fn inbox(self, inbox: Inbox) -> Self {
        let actor = self.table_entry();

        let inbox = if inbox.actor() != actor {
            inbox.with_actor(actor)
        } else {
            inbox
        };

        Self { inbox, ..self }
    }

    /// Sets the [Outbox] for the [Repository].
    ///
    /// Only needed if the default [Outbox] created by [RepositoryBuilder::new] is incorrect.
    pub fn outbox(self, outbox: Outbox) -> Self {
        let actor = self.table_entry();

        let outbox = if outbox.actor() != actor {
            outbox.with_actor(actor)
        } else {
            outbox
        };

        Self { outbox, ..self }
    }

    /// Sets the list of [Repository] clone URIs.
    pub fn clone_uris<T, I>(self, val: I) -> Self
    where
        T: Into<Iri>,
        I: IntoIterator<Item = T>,
    {
        let mut clone_uris: Vec<Iri> = val.into_iter().map(|i| i.into()).collect();
        clone_uris.sort();
        clone_uris.dedup();

        Self { clone_uris, ..self }
    }

    /// Sets the list of [Repository] push URIs.
    pub fn push_uris<T, I>(self, val: I) -> Self
    where
        T: Into<Iri>,
        I: IntoIterator<Item = T>,
    {
        let mut push_uris: Vec<Iri> = val.into_iter().map(|i| i.into()).collect();
        push_uris.sort();
        push_uris.dedup();

        Self { push_uris, ..self }
    }

    /// Sets the list of [Repository] forks.
    pub fn forks<T, I>(self, val: I) -> Self
    where
        T: Into<Repository>,
        I: IntoIterator<Item = T>,
    {
        let mut forks: Vec<Repository> = val.into_iter().map(|i| i.into()).collect();
        forks.sort();
        forks.dedup();

        Self { forks, ..self }
    }

    /// Sets the list of [Repository] likes.
    pub fn likes<T, I>(self, val: I) -> Self
    where
        T: Into<Like>,
        I: IntoIterator<Item = T>,
    {
        let mut likes: Vec<Like> = val.into_iter().map(|i| i.into()).collect();
        likes.sort();
        likes.dedup();

        Self { likes, ..self }
    }

    /// Sets the list of [Repository] [Follower]s.
    pub fn followers<T, I>(self, val: I) -> Self
    where
        T: Into<Follower>,
        I: IntoIterator<Item = T>,
    {
        let actor = self.table_entry();

        let mut followers: Vec<Follower> = val
            .into_iter()
            .map(|i| {
                let mut follower = i.into();

                if follower.following().contains(&actor) {
                    follower
                } else {
                    follower.add_following_entry(actor).ok();
                    follower
                }
            })
            .collect();

        followers.sort();
        followers.dedup();

        Self { followers, ..self }
    }

    /// Sets the list of [Repository] [Key]s.
    pub fn keys<T, I>(self, val: I) -> Result<Self>
    where
        T: Into<Key>,
        I: IntoIterator<Item = T>,
    {
        let actor = self.table_entry();
        let table = TableType::Key;

        val.into_iter()
            .map(|i| {
                let key = i.into();

                table.id_from_uuid(key.id(), key.uuid()).map(|id| {
                    if key.actor() == actor {
                        key.with_id(id).with_actor_id(&self.id)
                    } else {
                        key.with_id(id).with_actor_id(&self.id).with_actor(actor)
                    }
                })
            })
            .collect::<Result<Vec<Key>>>()
            .map(|keys| Self { keys, ..self })
    }

    /// Sets the [Repository] patch tracker.
    ///
    /// Optional: if unset, the [Repository] is assumed to be its own [PatchTracker].
    pub fn patches_tracked_by<T>(self, val: T) -> Self
    where
        T: Into<PatchTracker>,
    {
        Self {
            patches_tracked_by: Some(val.into()),
            ..self
        }
    }

    /// Sets the [Repository] ticket tracker.
    ///
    /// Optional: if unset, the [Repository] is assumed to be its own [TicketTracker].
    pub fn tickets_tracked_by<T>(self, val: T) -> Self
    where
        T: Into<TicketTracker>,
    {
        Self {
            tickets_tracked_by: Some(val.into()),
            ..self
        }
    }

    /// Sets where [Repository] moved to, if `is_archived` is true.
    ///
    /// Optional: only set if the [Repository] is archived, sets `is_archived` to true.
    pub fn moved_to<T>(self, val: T) -> Self
    where
        T: Into<Repository>,
    {
        Self {
            moved_to: Some(val.into()),
            is_archived: Some(true),
            ..self
        }
    }

    /// Sets which [Repository] this [Repository] mirrors.
    ///
    /// Optional: only set if this [Repository] is a mirror of another.
    pub fn mirrors<T>(self, val: T) -> Self
    where
        T: Into<Repository>,
    {
        Self {
            mirrors: Some(val.into()),
            ..self
        }
    }

    /// Sets the [Repository] team.
    ///
    /// Optional: only set if this [Repository] has a [Team].
    pub fn team<T>(self, val: T) -> Self
    where
        T: Into<Team>,
    {
        Self {
            team: Some(val.into()),
            ..self
        }
    }

    /// Builds the [Repository] record, and inserts all sub-records into the database.
    pub async fn build(mut self, db: &Db) -> Result<Repository> {
        let uuid = self.uuid;

        let pool = db.pool()?;
        let db_key = db.key()?;

        let mut dbtx = pool.begin().await?;

        let inbox_uuid = self.inbox.find_or_create_tx(&mut dbtx).await?;
        let outbox_uuid = self.outbox.find_or_create_tx(&mut dbtx).await?;

        for fork in self.forks.iter_mut() {
            fork.find_or_create_tx(&mut dbtx).await?;
        }

        for like in self.likes.iter_mut() {
            like.find_or_create_tx(&mut dbtx).await?;
        }

        for follower in self.followers.iter_mut() {
            follower.find_or_create_tx(&mut dbtx).await?;
        }

        for key in self.keys.iter_mut() {
            key.find_or_create_tx(&mut dbtx, &db_key).await?;
        }

        let patches_tracked_by = if let Some(mut p) = self.patches_tracked_by {
            p.find_or_create_tx(&mut dbtx)
                .await
                .map(|_| Some(p.id().clone()))?
        } else {
            None
        };

        let tickets_tracked_by = if let Some(mut p) = self.tickets_tracked_by {
            p.find_or_create_tx(&mut dbtx)
                .await
                .map(|_| Some(p.id().clone()))?
        } else {
            None
        };

        let moved_to = if let Some(mut m) = self.moved_to {
            m.find_or_create_tx(&mut dbtx)
                .await
                .map(|_| Some(m.id().clone()))?
        } else {
            None
        };

        let mirrors = if let Some(mut m) = self.mirrors {
            m.find_or_create_tx(&mut dbtx)
                .await
                .map(|_| Some(m.id().clone()))?
        } else {
            None
        };

        let team = if let Some(mut t) = self.team {
            t.find_or_create_tx(&mut dbtx)
                .await
                .map(|_| Some(t.id().clone()))?
        } else {
            None
        };

        let forks = self
            .forks
            .into_iter()
            .map(|f| f.id().clone())
            .collect::<Vec<_>>();
        let likes = self.likes.into_iter().map(|l| l.uuid()).collect::<Vec<_>>();
        let followers = self
            .followers
            .into_iter()
            .map(|f| f.id().clone())
            .collect::<Vec<_>>();
        let key_ids = self.keys.into_iter().map(|k| k.uuid()).collect::<Vec<_>>();

        let mut repository = Repository::new()
            .with_uuid(uuid)
            .with_id(self.id)
            .with_name(self.name)
            .with_inbox(inbox_uuid)
            .with_outbox(outbox_uuid)
            .with_clone_uris(self.clone_uris)
            .and_then(|p| p.with_push_uris(self.push_uris))
            .and_then(|p| p.with_forks(forks))
            .and_then(|p| p.with_likes(likes))
            .and_then(|p| p.with_followers(followers))
            .and_then(|p| p.with_key_ids(key_ids))?;

        if let Some(p) = patches_tracked_by {
            repository.set_send_patches_to(p);
        }

        if let Some(t) = tickets_tracked_by {
            repository.set_tickets_tracked_by(t);
        }

        if let Some(m) = self.is_archived {
            repository.set_is_archived(m);
        }

        if let Some(m) = moved_to {
            repository.set_moved_to(m);
        }

        if let Some(m) = mirrors {
            repository.set_mirrors(m);
        }

        if let Some(t) = team {
            repository.set_team(t);
        }

        repository.find_or_create_tx(&mut dbtx).await?;

        dbtx.commit().await.map(|_| repository).map_err(Error::from)
    }
}