)
}
- pub fn jwt(user: User_, hostname: String) -> Jwt {
+ pub fn jwt(user: User_, hostname: String) -> Result<Jwt, jsonwebtoken::errors::Error> {
let my_claims = Claims {
id: user.id,
iss: hostname,
&my_claims,
&EncodingKey::from_secret(Settings::get().jwt_secret.as_ref()),
)
- .unwrap()
}
}
DbPool,
};
use actix_web::client::Client;
+use anyhow::Context;
use lemmy_db::{
diesel_option_overwrite,
naive_now,
use lemmy_utils::{
generate_actor_keypair,
is_valid_community_name,
+ location_info,
make_apub_endpoint,
naive_from_unix,
EndpointType,
let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
- let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
+ let creator_index = admins
+ .iter()
+ .position(|r| r.id == site_creator_id)
+ .context(location_info!())?;
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
let creator_index = community_mods
.iter()
.position(|r| r.user_id == data.user_id)
- .unwrap();
+ .context(location_info!())?;
let creator_user = community_mods.remove(creator_index);
community_mods.insert(0, creator_user);
LemmyError,
};
use actix_web::client::Client;
+use anyhow::Context;
use lemmy_db::{
category::*,
comment_view::*,
SearchType,
SortType,
};
-use lemmy_utils::settings::Settings;
+use lemmy_utils::{location_info, settings::Settings};
use log::{debug, info};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
let creator_index = admins
.iter()
.position(|r| r.id == site_view.creator_id)
- .unwrap();
+ .context(location_info!())?;
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
LemmyError,
};
use actix_web::client::Client;
+use anyhow::Context;
use bcrypt::verify;
use captcha::{gen, Difficulty};
use chrono::Duration;
generate_random_string,
is_valid_preferred_username,
is_valid_username,
+ location_info,
make_apub_endpoint,
naive_from_unix,
remove_slurs,
// Return the jwt
Ok(LoginResponse {
- jwt: Claims::jwt(user, Settings::get().hostname),
+ jwt: Claims::jwt(user, Settings::get().hostname)?,
})
}
}
// Return the jwt
Ok(LoginResponse {
- jwt: Claims::jwt(inserted_user, Settings::get().hostname),
+ jwt: Claims::jwt(inserted_user, Settings::get().hostname)?,
})
}
}
// Return the jwt
Ok(LoginResponse {
- jwt: Claims::jwt(updated_user, Settings::get().hostname),
+ jwt: Claims::jwt(updated_user, Settings::get().hostname)?,
})
}
}
blocking(pool, move |conn| Site::read(conn, 1).map(|s| s.creator_id)).await??;
let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
- let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
+ let creator_index = admins
+ .iter()
+ .position(|r| r.id == site_creator_id)
+ .context(location_info!())?;
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
// Return the jwt
Ok(LoginResponse {
- jwt: Claims::jwt(updated_user, Settings::get().hostname),
+ jwt: Claims::jwt(updated_user, Settings::get().hostname)?,
})
}
}
.sort(sort_type)
.list()?;
- let items = create_post_items(posts);
+ let items = create_post_items(posts)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder
channel_builder.description(&site_desc);
}
- Ok(channel_builder.build().unwrap().to_string())
+ Ok(channel_builder.build().map_err(|e| anyhow!(e))?.to_string())
}
async fn get_feed(
.for_creator_id(user.id)
.list()?;
- let items = create_post_items(posts);
+ let items = create_post_items(posts)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder
.for_community_id(community.id)
.list()?;
- let items = create_post_items(posts);
+ let items = create_post_items(posts)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder
.my_user_id(user_id)
.list()?;
- let items = create_post_items(posts);
+ let items = create_post_items(posts)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder
.sort(&sort)
.list()?;
- let items = create_reply_and_mention_items(replies, mentions);
+ let items = create_reply_and_mention_items(replies, mentions)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder
fn create_reply_and_mention_items(
replies: Vec<ReplyView>,
mentions: Vec<UserMentionView>,
-) -> Vec<Item> {
+) -> Result<Vec<Item>, LemmyError> {
let mut reply_items: Vec<Item> = replies
.iter()
.map(|r| {
);
build_item(&r.creator_name, &r.published, &reply_url, &r.content)
})
- .collect();
+ .collect::<Result<Vec<Item>, LemmyError>>()?;
let mut mention_items: Vec<Item> = mentions
.iter()
);
build_item(&m.creator_name, &m.published, &mention_url, &m.content)
})
- .collect();
+ .collect::<Result<Vec<Item>, LemmyError>>()?;
reply_items.append(&mut mention_items);
- reply_items
+ Ok(reply_items)
}
-fn build_item(creator_name: &str, published: &NaiveDateTime, url: &str, content: &str) -> Item {
+fn build_item(
+ creator_name: &str,
+ published: &NaiveDateTime,
+ url: &str,
+ content: &str,
+) -> Result<Item, LemmyError> {
let mut i = ItemBuilder::default();
i.title(format!("Reply from {}", creator_name));
let author_url = format!("https://{}/u/{}", Settings::get().hostname, creator_name);
let dt = DateTime::<Utc>::from_utc(*published, Utc);
i.pub_date(dt.to_rfc2822());
i.comments(url.to_owned());
- let guid = GuidBuilder::default().permalink(true).value(url).build();
- i.guid(guid.unwrap());
+ let guid = GuidBuilder::default()
+ .permalink(true)
+ .value(url)
+ .build()
+ .map_err(|e| anyhow!(e))?;
+ i.guid(guid);
i.link(url.to_owned());
// TODO add images
let html = markdown_to_html(&content.to_string());
i.description(html);
- i.build().unwrap()
+ Ok(i.build().map_err(|e| anyhow!(e))?)
}
-fn create_post_items(posts: Vec<PostView>) -> Vec<Item> {
+fn create_post_items(posts: Vec<PostView>) -> Result<Vec<Item>, LemmyError> {
let mut items: Vec<Item> = Vec::new();
for p in posts {
let guid = GuidBuilder::default()
.permalink(true)
.value(&post_url)
- .build();
- i.guid(guid.unwrap());
+ .build()
+ .map_err(|e| anyhow!(e))?;
+ i.guid(guid);
let community_url = format!(
"https://{}/c/{}",
p.community_name, community_url
))
.domain(Settings::get().hostname.to_owned())
- .build();
- i.categories(vec![category.unwrap()]);
+ .build()
+ .map_err(|e| anyhow!(e))?;
+
+ i.categories(vec![category]);
if let Some(url) = p.url {
i.link(url);
i.description(description);
- items.push(i.build().unwrap());
+ items.push(i.build().map_err(|e| anyhow!(e))?);
}
- items
+ Ok(items)
}
UserId,
};
use actix_web::client::Client;
+use anyhow::Context as acontext;
use lemmy_db::naive_now;
+use lemmy_utils::location_info;
/// Chat server sends this messages to session
#[derive(Message)]
}
}
- pub fn join_community_room(&mut self, community_id: CommunityId, id: ConnectionId) {
+ pub fn join_community_room(
+ &mut self,
+ community_id: CommunityId,
+ id: ConnectionId,
+ ) -> Result<(), LemmyError> {
// remove session from all rooms
for sessions in self.community_rooms.values_mut() {
sessions.remove(&id);
self
.community_rooms
.get_mut(&community_id)
- .unwrap()
+ .context(location_info!())?
.insert(id);
+ Ok(())
}
- pub fn join_post_room(&mut self, post_id: PostId, id: ConnectionId) {
+ pub fn join_post_room(&mut self, post_id: PostId, id: ConnectionId) -> Result<(), LemmyError> {
// remove session from all rooms
for sessions in self.post_rooms.values_mut() {
sessions.remove(&id);
self.post_rooms.insert(post_id, HashSet::new());
}
- self.post_rooms.get_mut(&post_id).unwrap().insert(id);
+ self
+ .post_rooms
+ .get_mut(&post_id)
+ .context(location_info!())?
+ .insert(id);
+
+ Ok(())
}
- pub fn join_user_room(&mut self, user_id: UserId, id: ConnectionId) {
+ pub fn join_user_room(&mut self, user_id: UserId, id: ConnectionId) -> Result<(), LemmyError> {
// remove session from all rooms
for sessions in self.user_rooms.values_mut() {
sessions.remove(&id);
self.user_rooms.insert(user_id, HashSet::new());
}
- self.user_rooms.get_mut(&user_id).unwrap().insert(id);
+ self
+ .user_rooms
+ .get_mut(&user_id)
+ .context(location_info!())?
+ .insert(id);
+
+ Ok(())
}
fn send_post_room_message<Response>(
fn handle(&mut self, msg: SendAllMessage<Response>, _: &mut Context<Self>) {
self
.send_all_message(&msg.op, &msg.response, msg.my_id)
- .unwrap();
+ .ok();
}
}
fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) {
self
.send_user_room_message(&msg.op, &msg.response, msg.recipient_id, msg.my_id)
- .unwrap();
+ .ok();
}
}
fn handle(&mut self, msg: SendCommunityRoomMessage<Response>, _: &mut Context<Self>) {
self
.send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.my_id)
- .unwrap();
+ .ok();
}
}
type Result = ();
fn handle(&mut self, msg: SendPost, _: &mut Context<Self>) {
- self.send_post(&msg.op, &msg.post, msg.my_id).unwrap();
+ self.send_post(&msg.op, &msg.post, msg.my_id).ok();
}
}
type Result = ();
fn handle(&mut self, msg: SendComment, _: &mut Context<Self>) {
- self.send_comment(&msg.op, &msg.comment, msg.my_id).unwrap();
+ self.send_comment(&msg.op, &msg.comment, msg.my_id).ok();
}
}
type Result = ();
fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
- self.join_user_room(msg.user_id, msg.id);
+ self.join_user_room(msg.user_id, msg.id).ok();
}
}
type Result = ();
fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
- self.join_community_room(msg.community_id, msg.id);
+ self.join_community_room(msg.community_id, msg.id).ok();
}
}
type Result = ();
fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
- self.join_post_room(msg.post_id, msg.id);
+ self.join_post_room(msg.post_id, msg.id).ok();
}
}