-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquery.rs
More file actions
1060 lines (971 loc) · 35.3 KB
/
Copy pathquery.rs
File metadata and controls
1060 lines (971 loc) · 35.3 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This piece of the project exposes a GraphQL endpoint that allows one to access DAILP data in a federated manner with specific queries.
use dailp::{
async_graphql::InputType,
auth::{AuthGuard, GroupGuard, NotGroupGuard, UserGroup, UserInfo},
collection,
comment::{CommentParent, CommentUpdate, DeleteCommentInput, PostCommentInput},
page::{NewPageInput, Page},
slugify_ltree,
user::{User, UserUpdate},
AnnotatedForm, AnnotatedSeg, AttachAudioToDocumentInput, AttachAudioToWordInput,
CollectionChapter, Contributor, ContributorRole, CreateEditedCollectionInput,
CurateDocumentAudioInput, CurateWordAudioInput, Date, DeleteContributorAttribution,
DeleteDocumentAudioInput, DeleteWordAudioInput, DocumentId, DocumentMetadata,
DocumentMetadataUpdate, DocumentParagraph, PositionInDocument, SourceAttribution,
TranslatedPage, TranslatedSection, UpdateContributorAttribution, Uuid,
};
use itertools::{Itertools, Position};
use log::{debug, info};
use reqwest::{header, Client};
use {
dailp::async_graphql::{self, dataloader::DataLoader, Context, FieldResult},
dailp::{
AbstractMorphemeTag, AnnotatedDoc, AnnotatedFormUpdate, CherokeeOrthography, Database,
EditedCollection, Menu, MenuUpdate, MorphemeId, MorphemeReference, MorphemeTag,
ParagraphUpdate, WordsInDocument,
},
};
/// Home for all read-only queries
pub struct Query;
#[async_graphql::Object]
impl Query {
// List of all the edited collections available.
async fn all_edited_collections(
&self,
context: &Context<'_>,
) -> FieldResult<Vec<EditedCollection>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.all_edited_collections()
.await?)
}
async fn page_by_path(&self, context: &Context<'_>, path: String) -> FieldResult<Option<Page>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.page_by_path(&path)
.await?)
}
// query for 1 collection based on slug, and make a collection object with all the stuff in it.
async fn edited_collection(
&self,
context: &Context<'_>,
slug: String,
) -> FieldResult<Option<EditedCollection>> {
let slug = slugify_ltree(slug);
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::EditedCollectionDetails(slug))
.await?)
}
/// Retrieves a chapter and its contents by its collection and chapter slug.
async fn chapter(
&self,
context: &Context<'_>,
collection_slug: String,
chapter_slug: String,
) -> FieldResult<Option<CollectionChapter>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.chapter(slugify_ltree(collection_slug), slugify_ltree(chapter_slug))
.await?)
}
/// List of all the functional morpheme tags available
async fn all_tags(
&self,
context: &Context<'_>,
system: CherokeeOrthography,
) -> FieldResult<Vec<MorphemeTag>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.all_tags(system)
.await?)
}
/// Listing of all documents excluding their contents by default
async fn all_documents(&self, context: &Context<'_>) -> FieldResult<Vec<AnnotatedDoc>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.all_documents()
.await?)
}
/// List of all content pages
async fn all_pages(&self, context: &Context<'_>) -> FieldResult<Vec<dailp::page::Page>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.all_pages()
.await?)
}
/// List of all the document collections available.
async fn all_collections(
&self,
context: &Context<'_>,
) -> FieldResult<Vec<dailp::DocumentCollection>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.top_collections()
.await?)
}
async fn collection(
&self,
context: &Context<'_>,
slug: String,
) -> FieldResult<dailp::DocumentCollection> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.collection(slug)
.await?)
}
/// Retrieves a full document from its unique name.
pub async fn document(
&self,
context: &Context<'_>,
slug: String,
) -> FieldResult<Option<AnnotatedDoc>> {
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentShortName(slug.to_ascii_uppercase()))
.await?)
}
/// Retrieves all documents that are bookmarked by the current user.
#[graphql(guard = "AuthGuard")]
pub async fn bookmarked_documents(
&self,
context: &Context<'_>,
) -> FieldResult<Vec<AnnotatedDoc>> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let bookmarked_ids = context
.data::<DataLoader<Database>>()?
.loader()
.bookmarked_documents(&user.id)
.await?;
let annotated_docs_map = context
.data::<DataLoader<Database>>()?
.load_many(bookmarked_ids.iter().map(|&id| dailp::DocumentId(id)))
.await?;
Ok(annotated_docs_map.into_values().collect())
}
/// Retrieves a full document from its unique identifier.
pub async fn document_by_uuid(
&self,
context: &Context<'_>,
id: Uuid,
) -> FieldResult<Option<AnnotatedDoc>> {
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentId(id))
.await?)
}
/// Retrieves a full document from its unique identifier.
pub async fn page(
&self,
context: &Context<'_>,
id: String,
) -> FieldResult<Option<dailp::page::Page>> {
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::PageId(id))
.await?)
}
/// Lists all forms containing a morpheme with the given gloss.
/// Groups these words by the phonemic shape of the target morpheme.
pub async fn morphemes_by_shape(
&self,
context: &Context<'_>,
gloss: String,
#[graphql(desc = "Compare morpheme shapes in this orthography.
Choosing a simpler system like d/t will give you more general groupings.
")]
compare_by: Option<CherokeeOrthography>,
) -> FieldResult<Vec<MorphemeReference>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.morphemes(MorphemeId::parse(&gloss).unwrap(), compare_by)
.await?)
}
/// Lists all words containing a morpheme with the given gloss.
/// Groups these words by the document containing them.
async fn morphemes_by_document(
&self,
context: &Context<'_>,
document_id: Option<dailp::DocumentId>,
morpheme_gloss: String,
) -> FieldResult<Vec<WordsInDocument>> {
if let Some(document_id) = document_id {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.connected_forms(Some(document_id), &morpheme_gloss)
.await?
.into_iter()
.group_by(|w| w.position.document_id)
.into_iter()
.map(|(document_id, forms)| WordsInDocument {
document_type: None,
document_id: Some(document_id),
forms: forms.collect(),
})
.collect())
} else {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.words_by_doc(document_id, &morpheme_gloss)
.await?)
}
}
/// Forms containing the given morpheme gloss or related ones clustered over time.
async fn morpheme_time_clusters(
&self,
context: &Context<'_>,
gloss: String,
#[graphql(default = 10)] cluster_years: i32,
) -> FieldResult<Vec<FormsInTime>> {
use dailp::chrono::Datelike;
use itertools::Itertools as _;
let db = context.data::<DataLoader<Database>>()?.loader();
let morpheme = dailp::MorphemeId::parse(&gloss).unwrap();
let doc_id = if let Some(short_name) = morpheme.document_name {
db.document_id_from_name(&short_name).await?
} else {
None
};
let forms = db.connected_forms(doc_id, &morpheme.gloss).await?;
// Cluster forms by the decade they were recorded in.
let clusters = forms
.into_iter()
.map(|form| {
(
form.date_recorded
.as_ref()
.map(|d| d.0.year() / cluster_years),
form,
)
})
.into_group_map();
Ok(clusters
.into_values()
.map(|forms| {
let dates = forms.iter().filter_map(|f| f.date_recorded.as_ref());
let start = dates.clone().min();
let end = dates.max();
FormsInTime {
start: start.cloned(),
end: end.cloned(),
// Sort forms from oldest to newest.
forms: forms
.into_iter()
.sorted_by(|a, b| Ord::cmp(&b.date_recorded, &a.date_recorded))
.collect(),
}
})
// Sort the clusters from oldest to newest.
.sorted_by(|a, b| Ord::cmp(&b.start, &a.start))
.collect())
}
/// Retrieve information for the morpheme that corresponds to the given tag
/// string. For example, "3PL.B" is the standard string referring to a 3rd
/// person plural prefix.
async fn morpheme_tag(
&self,
context: &Context<'_>,
id: String,
system: CherokeeOrthography,
) -> FieldResult<Option<MorphemeTag>> {
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::TagId(id, system))
.await?
.unwrap_or_default()
.into_iter()
.next())
}
/// Search for words that match any one of the given queries.
/// Each query may match against multiple fields of a word.
async fn word_search(
&self,
context: &Context<'_>,
query: String,
) -> FieldResult<Vec<dailp::AnnotatedForm>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.search_words_any_field(query)
.await?)
}
/// Get a single word given the word ID
async fn word_by_id(
&self,
context: &Context<'_>,
id: Uuid,
) -> FieldResult<dailp::AnnotatedForm> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.word_by_id(&id)
.await?)
}
/// Get a single paragraph given the paragraph ID
async fn paragraph_by_id(
&self,
context: &Context<'_>,
id: Uuid,
) -> FieldResult<dailp::DocumentParagraph> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.paragraph_by_id(&id)
.await?)
}
/// Search for words with the exact same syllabary string, or with very
/// similar looking characters.
async fn syllabary_search(
&self,
context: &Context<'_>,
query: String,
) -> FieldResult<Vec<dailp::AnnotatedForm>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.potential_syllabary_matches(&query)
.await?)
}
/// Basic information about the currently authenticated user, if any.
#[graphql(guard = "AuthGuard")]
async fn user_info<'a>(&self, context: &'a Context<'_>) -> Option<&'a UserInfo> {
context.data_opt()
}
/// Gets a dailp_user by their id
async fn dailp_user_by_id(&self, context: &Context<'_>, id: Uuid) -> FieldResult<User> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.dailp_user_by_id(&id)
.await?)
}
/// Gets all dailp_user with their id, username, and role for now
async fn list_users(&self, context: &Context<'_>) -> FieldResult<Vec<User>> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.all_users()
.await?)
}
async fn abbreviation_id_from_short_name(
&self,
context: &Context<'_>,
short_name: String,
) -> FieldResult<Uuid> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.abbreviation_id_from_short_name(&short_name)
.await?)
}
async fn menu_by_slug(&self, context: &Context<'_>, slug: String) -> FieldResult<Menu> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.get_menu_by_slug(slug)
.await?)
}
}
pub struct Mutation;
#[async_graphql::Object]
impl Mutation {
/// Mutation must have at least one visible field for introspection to work
/// correctly, so we just provide an API version which might be useful in
/// the future.
async fn api_version(&self) -> &str {
"1.0"
}
/// Delete a comment.
/// Will fail if the user making the request is not the poster.
#[graphql(guard = "AuthGuard")]
async fn delete_comment(
&self,
context: &Context<'_>,
input: DeleteCommentInput,
) -> FieldResult<CommentParent> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
// We could theoretically do this in one round trip, if we have ever
// have performance issues. The query would roughly be:
// delete from comment where user_id and comment_id
// returning parent_type, parent_id
let db = context.data::<DataLoader<Database>>()?.loader();
let comment = db.comment_by_id(&input.comment_id).await?;
if comment.posted_by.id.0 != user.id.to_string() {
return Err("User attempted to delete another user's comment".into());
}
db.delete_comment(&input.comment_id).await?;
// We return the parent object, for GraphCache interop
comment.parent(context).await
}
/// Post a new comment on a given object
#[graphql(guard = "AuthGuard")]
async fn post_comment(
&self,
context: &Context<'_>,
input: PostCommentInput,
) -> FieldResult<CommentParent> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let db = context.data::<DataLoader<Database>>()?.loader();
db.insert_comment(
&user.id,
input.text_content,
&input.parent_id,
&input.parent_type,
&input.comment_type,
)
.await?;
// We return the parent object, for GraphCache interop
input.parent_type.resolve(db, &input.parent_id).await
}
/// Update a comment
#[graphql(guard = "AuthGuard")]
async fn update_comment(
&self,
context: &Context<'_>,
comment: CommentUpdate,
) -> FieldResult<CommentParent> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let db = context.data::<DataLoader<Database>>()?.loader();
let comment_object = db.comment_by_id(&comment.id).await?;
if comment_object.posted_by.id.0 != user.id.to_string() {
return Err("User attempted to edit another user's comment".into());
}
// Note: We should probably handle an error here more gracefully.
let _ = db.update_comment(comment).await;
// We return the parent object, for GraphCache interop
return comment_object.parent(context).await;
}
/// Mutation for adding/changing contributor attributions
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn update_contributor_attribution(
&self,
context: &Context<'_>,
contribution: UpdateContributorAttribution,
) -> FieldResult<Uuid> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.update_contributor_attribution(contribution)
.await?)
}
///Mutation for deleting contributor attributions
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn delete_contributor_attribution(
&self,
context: &Context<'_>,
contribution: DeleteContributorAttribution,
) -> FieldResult<Uuid> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.delete_contributor_attribution(contribution)
.await?)
}
/// Mutation for paragraph and translation editing
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn update_paragraph(
&self,
context: &Context<'_>,
paragraph: ParagraphUpdate,
) -> FieldResult<DocumentParagraph> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.update_paragraph(paragraph)
.await?)
}
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn update_page(
&self,
context: &Context<'_>,
// Data encoded as JSON for now.
data: async_graphql::Json<dailp::page::Page>,
) -> FieldResult<bool> {
context
.data::<DataLoader<Database>>()?
.loader()
.update_page(data.0)
.await?;
Ok(true)
}
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn update_annotation(
&self,
context: &Context<'_>,
// Data encoded as JSON for now.
data: async_graphql::Json<dailp::annotation::Annotation>,
) -> FieldResult<bool> {
context
.data::<DataLoader<Database>>()?
.loader()
.update_annotation(data.0)
.await?;
Ok(true)
}
#[graphql(guard = "NotGroupGuard::new(UserGroup::Readers)")]
async fn update_word(
&self,
context: &Context<'_>,
word: AnnotatedFormUpdate,
) -> FieldResult<AnnotatedForm> {
let database = context.data::<DataLoader<Database>>()?.loader();
Ok(database
.word_by_id(&database.update_word(word).await?)
.await?)
}
/// Updates a dailp_user's information
#[graphql(guard = "AuthGuard")]
async fn update_user(&self, context: &Context<'_>, user: UserUpdate) -> FieldResult<User> {
let user_id = Uuid::from(&user.id);
let db = context.data::<DataLoader<Database>>()?.loader();
db.update_dailp_user(user).await?;
let user_object = db.dailp_user_by_id(&user_id).await?;
// We return the user object, for GraphCache interop
return Ok(user_object);
}
/// Adds a bookmark to the user's list of bookmarks.
#[graphql(guard = "AuthGuard")]
async fn add_bookmark(
&self,
context: &Context<'_>,
document_id: Uuid,
) -> FieldResult<AnnotatedDoc> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
context
.data::<DataLoader<Database>>()?
.loader()
.add_bookmark(document_id, user.id)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentId(document_id))
.await?
.ok_or_else(|| anyhow::format_err!("Failed to load document"))?)
}
/// Removes a bookmark from a user's list of bookmarks
#[graphql(guard = "AuthGuard")]
async fn remove_bookmark(
&self,
context: &Context<'_>,
document_id: Uuid,
) -> FieldResult<AnnotatedDoc> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
context
.data::<DataLoader<Database>>()?
.loader()
.remove_bookmark(document_id, user.id)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentId(document_id))
.await?
.ok_or_else(|| anyhow::format_err!("Failed to load document"))?)
}
/// Decide if a piece of word audio should be included in edited collection
#[graphql(guard = "GroupGuard::new(UserGroup::Editors)")]
async fn curate_word_audio(
&self,
context: &Context<'_>,
input: CurateWordAudioInput,
) -> FieldResult<dailp::AnnotatedForm> {
// TODO: should this return a typed id ie. AudioSliceId?
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let word_id = context
.data::<DataLoader<Database>>()?
.loader()
.update_word_audio_visibility(
&input.word_id,
&input.audio_slice_id,
input.include_in_edited_collection,
&user.id,
)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.word_by_id(&word_id.ok_or_else(|| anyhow::format_err!("Word audio not found"))?)
.await?)
}
/// Decide if a piece of document audio should be included in edited collection
#[graphql(guard = "GroupGuard::new(UserGroup::Editors)")]
async fn curate_document_audio(
&self,
context: &Context<'_>,
input: CurateDocumentAudioInput,
) -> FieldResult<dailp::AnnotatedDoc> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let document_id = context
.data::<DataLoader<Database>>()?
.loader()
.update_document_audio_visibility(
&input.document_id,
&input.audio_slice_id,
input.include_in_edited_collection,
&user.id,
)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentId(
document_id.ok_or_else(|| anyhow::format_err!("Document not found"))?,
))
.await?
.ok_or_else(|| anyhow::format_err!("Document not found"))?)
}
/// Attach audio that has already been uploaded to S3 to a particular word
/// Assumes user requesting mutation recoreded the audio
#[graphql(
guard = "GroupGuard::new(UserGroup::Contributors).or(GroupGuard::new(UserGroup::Editors))"
)]
async fn attach_audio_to_word(
&self,
context: &Context<'_>,
input: AttachAudioToWordInput,
) -> FieldResult<dailp::AnnotatedForm> {
// TODO: should this return a typed id ie. AudioSliceId?
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let _media_slice_id = context
.data::<DataLoader<Database>>()?
.loader()
.attach_audio_to_word(&input, &user.id)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.word_by_id(&input.word_id)
.await?)
}
/// Attach audio that has already been uploaded to S3 to a particular document
/// Assumes user requesting mutation recorded the audio
#[graphql(
guard = "GroupGuard::new(UserGroup::Contributors).or(GroupGuard::new(UserGroup::Editors))"
)]
async fn attach_audio_to_document(
&self,
context: &Context<'_>,
input: AttachAudioToDocumentInput,
) -> FieldResult<dailp::AnnotatedDoc> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let _media_slice_id = context
.data::<DataLoader<Database>>()?
.loader()
.attach_audio_to_document(&input, &user.id)
.await?;
Ok(context
.data::<DataLoader<Database>>()?
.load_one(dailp::DocumentId(input.document_id))
.await?
.ok_or_else(|| anyhow::format_err!("Document not found"))?)
}
#[graphql(guard = "GroupGuard::new(UserGroup::Editors)")]
async fn update_document_metadata(
&self,
context: &Context<'_>,
document: DocumentMetadataUpdate,
) -> FieldResult<Uuid> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.update_document_metadata(document)
.await?)
}
/// Minimal mutation to add a document with only essential fields
#[graphql(
guard = "GroupGuard::new(UserGroup::Editors).or(GroupGuard::new(UserGroup::Contributors))"
)]
async fn add_document(
&self,
context: &Context<'_>,
input: CreateDocumentFromFormInput,
) -> FieldResult<AddDocumentPayload> {
let title = input.document_name;
// Get info for the user currently signed in
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let user_profile_data = context
.data::<DataLoader<Database>>()?
.loader()
.dailp_user_by_id(&user.id)
.await?;
let contributor = Contributor {
id: user.id,
name: user_profile_data.display_name,
// get users display name. TODO we should more rigorously check this value for errors
role: Some(ContributorRole::Transcriber), // TODO Ask Ellen, Cara, Shireen about this terminology
};
let today = dailp::chrono::Utc::now().date_naive();
let document_date = dailp::Date::new(today);
let document_id = Uuid::new_v4();
let short_name = dailp::slugify(&title).to_ascii_uppercase();
let source = SourceAttribution {
name: input.source_name,
link: input.source_url,
};
let mut sections = Vec::new();
for (i, _raw_text_line) in input.raw_text_lines.iter().enumerate() {
let translation: Option<String> = Some(input.english_translation_lines[i].join(" "));
let mut segs = Vec::new();
for (j, src_word) in input.raw_text_lines[i].iter().enumerate() {
let form = AnnotatedForm {
id: None,
source: src_word.clone(),
normalized_source: None,
simple_phonetics: None,
phonemic: None,
segments: None,
english_gloss: vec![],
commentary: None,
line_break: None,
page_break: None,
position: PositionInDocument {
document_id: dailp::DocumentId(document_id),
page_number: "1".to_string(),
index: j as i64,
geometry: None,
},
date_recorded: None,
ingested_audio_track: None,
};
segs.push(AnnotatedSeg::Word(form));
}
let section = TranslatedSection {
translation,
source: segs,
};
sections.push(section);
}
let page = TranslatedPage {
paragraphs: sections,
};
let meta = DocumentMetadata {
id: dailp::DocumentId(document_id),
short_name: short_name.clone(),
title: title.clone(),
sources: vec![source],
collection: None,
genre_id: None,
keywords_ids: None,
languages_ids: None,
subject_headings_ids: None,
creators_ids: None,
format_id: None,
contributors: Some(vec![contributor]),
spatial_coverage_ids: None,
translation: None,
page_images: None,
date: Some(document_date),
is_reference: false,
audio_recording: None,
order_index: 0,
};
let annotated_doc = AnnotatedDoc {
meta: meta.clone(),
segments: Some(vec![page]),
};
let database = context.data::<DataLoader<Database>>()?.loader();
let (document_id, _chapter_id) = database
.insert_document_into_edited_collection(annotated_doc.clone(), input.collection_id)
.await?;
// Update the annotated_doc with the correct document_id from the database
let mut updated_annotated_doc = annotated_doc.clone();
updated_annotated_doc.meta.id = document_id;
// Insert the document contents (words and paragraphs) into the database
database
.insert_document_contents(updated_annotated_doc)
.await?;
let collection_slug = database.collection_slug_by_id(input.collection_id).await?;
Ok(AddDocumentPayload {
id: document_id.0,
title,
slug: short_name.clone(),
collection_slug: collection_slug
.ok_or_else(|| anyhow::format_err!("Failed to load collection"))?
.to_string(), // All user-created documents go to user_documents collection
chapter_slug: dailp::slugify_ltree(&short_name), // Chapter slug must be ltree-compatible
})
}
#[graphql(
guard = "GroupGuard::new(UserGroup::Contributors).or(GroupGuard::new(UserGroup::Editors))"
)]
async fn insert_custom_morpheme_tag(
&self,
context: &Context<'_>,
tag: String,
title: String,
system: String,
) -> FieldResult<bool> {
//first get id of custom morpheme tag
let abstract_id = context
.data::<DataLoader<Database>>()?
.loader()
.insert_custom_abstract_tag(AbstractMorphemeTag {
//TODO: can just make it CUS once we remove the unique constraint
id: "CUS:".to_string() + &title,
morpheme_type: "custom".to_string(),
})
.await?;
//construct the morpheme tag
//todo: need to figure out why tag and title are the same thing :(
//its a frontend issue
let tag = MorphemeTag {
internal_tags: vec![abstract_id.to_string()],
tag: tag,
title: title.clone(),
shape: None,
details_url: None,
definition: title,
morpheme_type: String::new(),
role_override: None,
};
let system_id = context
.data::<DataLoader<Database>>()?
.loader()
.abbreviation_id_from_short_name("CUS")
.await?;
context
.data::<DataLoader<Database>>()?
.loader()
.insert_custom_morpheme_tag(tag, system_id)
.await?;
Ok(true)
}
#[graphql(
//TODO ADD ADMIN ROLES WHEN IT IS READY
guard = "GroupGuard::new(UserGroup::Editors)"
)]
async fn create_edited_collection(
&self,
context: &Context<'_>,
input: CreateEditedCollectionInput,
) -> FieldResult<String> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.insert_edited_collection(input)
.await?
.to_string())
}
#[graphql(guard = "GroupGuard::new(UserGroup::Editors)")]
async fn upsert_page(&self, context: &Context<'_>, page: NewPageInput) -> FieldResult<String> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.upsert_page(page)
.await?)
}
// dennis todo: should be admin, but admin accs not implemented yet
#[graphql(guard = "GroupGuard::new(UserGroup::Editors)")]
async fn update_menu(&self, context: &Context<'_>, menu: MenuUpdate) -> FieldResult<Menu> {
Ok(context
.data::<DataLoader<Database>>()?
.loader()
.update_menu(menu)
.await?)
}
async fn validate_turnstile_token(
&self,
context: &Context<'_>,
token: String,
) -> FieldResult<bool> {
// POST to SiteVerify API directly unless an override is provided. Used for AWS Infra testing
let turnstile_api = std::env::var("TURNSTILE_API")
.unwrap_or("https://challenges.cloudflare.com/turnstile/v0/siteverify".to_string());
let secret = std::env::var("TURNSTILE_SECRET_KEY").unwrap();
let params = [("secret", secret), ("response", token)];
let client = reqwest::Client::new();
info!("Sending POST to SiteVerify API");
debug!("Payload: {:?}", params);
let response = client
.post(turnstile_api)
.header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.form(¶ms)
.send()
.await?;
info!("Response recieved from SiteVerify API");
debug!("Status Code: {}", response.status());
let body = response.text().await?;
debug!("Body Content: {}", body);