Skip to main content

canvas_lms_api/resources/
user.rs

1use crate::{
2    http::Requester,
3    pagination::PageStream,
4    resources::{course::Course, enrollment::Enrollment},
5};
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10/// A Canvas user.
11#[derive(Debug, Clone, Deserialize, Serialize)]
12pub struct User {
13    pub id: u64,
14    pub name: Option<String>,
15    pub sortable_name: Option<String>,
16    pub short_name: Option<String>,
17    pub sis_user_id: Option<String>,
18    pub login_id: Option<String>,
19    pub email: Option<String>,
20    pub avatar_url: Option<String>,
21    pub locale: Option<String>,
22    pub last_login: Option<DateTime<Utc>>,
23    pub time_zone: Option<String>,
24    pub bio: Option<String>,
25
26    #[serde(skip)]
27    pub(crate) requester: Option<Arc<Requester>>,
28}
29
30impl User {
31    fn req(&self) -> &Arc<Requester> {
32        self.requester.as_ref().expect("requester not initialized")
33    }
34
35    /// Stream all courses for this user.
36    ///
37    /// # Canvas API
38    /// `GET /api/v1/users/:id/courses`
39    pub fn get_courses(&self) -> PageStream<Course> {
40        PageStream::new(
41            Arc::clone(self.req()),
42            &format!("users/{}/courses", self.id),
43            vec![],
44        )
45    }
46
47    /// Stream all enrollments for this user.
48    ///
49    /// # Canvas API
50    /// `GET /api/v1/users/:id/enrollments`
51    pub fn get_enrollments(&self) -> PageStream<Enrollment> {
52        PageStream::new(
53            Arc::clone(self.req()),
54            &format!("users/{}/enrollments", self.id),
55            vec![],
56        )
57    }
58}
59
60/// The currently authenticated user (extends User with additional fields).
61#[derive(Debug, Clone, Deserialize, Serialize)]
62pub struct CurrentUser {
63    pub id: u64,
64    pub name: Option<String>,
65    pub sortable_name: Option<String>,
66    pub short_name: Option<String>,
67    pub sis_user_id: Option<String>,
68    pub login_id: Option<String>,
69    pub email: Option<String>,
70    pub avatar_url: Option<String>,
71    pub locale: Option<String>,
72    pub last_login: Option<DateTime<Utc>>,
73    pub time_zone: Option<String>,
74    pub bio: Option<String>,
75    pub effective_locale: Option<String>,
76}
77
78/// A user display stub (id + name only) used in nested contexts.
79#[derive(Debug, Clone, Deserialize, Serialize)]
80pub struct UserDisplay {
81    pub id: u64,
82    pub display_name: Option<String>,
83    pub avatar_image_url: Option<String>,
84    pub html_url: Option<String>,
85}
86
87/// Identifies a user by numeric ID or as the currently authenticated user.
88pub enum UserId {
89    Id(u64),
90    Current,
91}
92
93impl UserId {
94    pub(crate) fn to_path_segment(&self) -> String {
95        match self {
96            UserId::Id(id) => id.to_string(),
97            UserId::Current => "self".to_string(),
98        }
99    }
100}