lockbook_server_lib/
debug_info.rs1use crate::ServerError::ClientError;
2use crate::guard::ServerTx;
3use std::fmt::Debug;
4
5use lb_rs::model::{
6 account::BETA_USERS,
7 api::{UpsertDebugInfoError, UpsertDebugInfoRequest},
8 file_metadata::Owner,
9};
10use libsecp256k1::PublicKey;
11use reqwest::multipart;
12use serde_json::json;
13use tracing::{info, warn};
14
15use crate::{
16 RequestContext, ServerError, ServerState,
17 billing::{
18 app_store_client::AppStoreClient, google_play_client::GooglePlayClient,
19 stripe_client::StripeClient,
20 },
21 document_service::DocumentService,
22 schema::ServerV6,
23};
24
25impl<S, A, G, D> ServerState<S, A, G, D>
26where
27 S: StripeClient,
28 A: AppStoreClient,
29 G: GooglePlayClient,
30 D: DocumentService,
31{
32 pub async fn upsert_debug_info(
33 &self, context: RequestContext<UpsertDebugInfoRequest>,
34 ) -> Result<(), ServerError<UpsertDebugInfoError>> {
35 let mut lock = self.index_db.lock().await;
36
37 let mut tx = ServerTx::begin(&mut lock)?;
38 let db = &mut *tx;
39
40 if !Self::is_beta_user::<UpsertDebugInfoError>(db, &context.public_key) {
41 return Err(ClientError(UpsertDebugInfoError::NotPermissioned));
42 }
43
44 let owner = Owner(context.public_key);
45
46 let debug_info = context.request.debug_info.clone();
47 let new_panics_count = debug_info.panics.len();
48
49 let maybe_old_debug_info = db.debug_info.insert(
50 owner,
51 context.request.debug_info.lb_id,
52 context.request.debug_info,
53 )?;
54
55 let old_panics_count =
56 if let Some(debug_info) = maybe_old_debug_info { debug_info.panics.len() } else { 0 };
57
58 if new_panics_count > old_panics_count {
59 if let Some(panic) = debug_info.panics.first() {
60 warn!(?debug_info, "beta user experienced a panic");
61
62 self.send_panic_to_discord(&debug_info, panic).await?;
63 }
64 }
65
66 tx.end()?;
67
68 Ok(())
69 }
70
71 async fn send_panic_to_discord(
72 &self, debug_info: &lb_rs::service::debug::DebugInfo, panic: &str,
73 ) -> Result<(), ServerError<UpsertDebugInfoError>> {
74 let discord_webhook_url = match &self.config.server.discord_webhook_url {
75 Some(url) => url,
76 None => return Ok(()),
77 };
78
79 let maybe_new_line_index = panic.find('\n');
80 let mut panic_title = None;
81
82 if let Some(new_line_index) = maybe_new_line_index {
83 panic_title = Some(panic[0..new_line_index].to_string());
84 }
85
86 let payload = json!({
87 "username": "Panic Reporter",
88 "embeds": [{
89 "color": 14622784,
90 "author": { "name": debug_info.name },
91 "title": panic_title.unwrap_or("".to_string()),
92 }]
93 });
94
95 let debug_info_part = multipart::Part::bytes(serde_json::to_vec_pretty(&debug_info)?)
96 .file_name(format!("{}_{}_debug_info.json", debug_info.name, debug_info.panics.len()))
97 .mime_str("application/json")?;
98
99 let form = multipart::Form::new()
100 .part("file", debug_info_part)
101 .text("payload_json", payload.to_string());
102
103 let response = self
104 .discord_client
105 .post(discord_webhook_url)
106 .multipart(form)
107 .send()
108 .await?;
109
110 if response.status().is_success() {
111 info!("Notifed discord of a panic!");
112 } else {
113 warn!("Failed to notify discord: {:?} {:?}", response.status(), response.text().await?);
114 }
115 Ok(())
116 }
117
118 pub fn is_beta_user<E: Debug>(db: &ServerV6, public_key: &PublicKey) -> bool {
119 let is_beta = match db.accounts.get(&Owner(*public_key)) {
120 None => false,
121 Some(account) => BETA_USERS.contains(&account.username.as_str()),
122 };
123
124 is_beta
125 }
126}