context.rs
1 use std::collections::HashMap; 2 3 use matrix_sdk::{Room, ruma::OwnedUserId}; 4 use reqwest::Url; 5 6 use crate::{ 7 aoc::client::AocClient, 8 config::{Config, User}, 9 mastodon, 10 utils::store::Store, 11 }; 12 13 pub struct Context { 14 pub config: Config, 15 pub store: Store, 16 pub room: Room, 17 pub aoc_client: AocClient, 18 pub garygrady: ContextGarygrady, 19 pub users: ContextUsers, 20 } 21 22 pub struct ContextUsers { 23 pub by_aoc: HashMap<u64, User>, 24 pub by_matrix: HashMap<OwnedUserId, User>, 25 } 26 27 pub struct ContextGarygrady { 28 pub server: Url, 29 pub user_id: mastodon::Id, 30 } 31 32 impl Context { 33 pub fn new( 34 config: Config, 35 store: Store, 36 room: Room, 37 aoc_client: AocClient, 38 garygrady: ContextGarygrady, 39 ) -> Self { 40 let users = ContextUsers::from_config(&config); 41 42 Self { 43 config, 44 store, 45 room, 46 aoc_client, 47 garygrady, 48 users, 49 } 50 } 51 } 52 53 impl ContextUsers { 54 fn from_config(config: &Config) -> Self { 55 let by_aoc = config 56 .users 57 .iter() 58 .flat_map(|user| Some((user.aoc?, user.clone()))) 59 .collect(); 60 let by_matrix = config 61 .users 62 .iter() 63 .flat_map(|user| Some((user.matrix.clone()?, user.clone()))) 64 .collect(); 65 66 Self { by_aoc, by_matrix } 67 } 68 }