UserOperation::GetReportCount => {
do_websocket_operation::<GetReportCount>(context, id, op, data).await
}
+ UserOperation::GetUnreadCount => {
+ do_websocket_operation::<GetUnreadCount>(context, id, op, data).await
+ }
// Private Message ops
UserOperation::MarkPrivateMessageAsRead => {
};
use lemmy_db_views::{
comment_report_view::CommentReportView,
- comment_view::CommentQueryBuilder,
+ comment_view::{CommentQueryBuilder, CommentView},
local_user_view::LocalUserView,
post_report_view::PostReportView,
+ private_message_view::PrivateMessageView,
};
use lemmy_db_views_actor::{
community_moderator_view::CommunityModeratorView,
Ok(res)
}
}
+
+#[async_trait::async_trait(?Send)]
+impl Perform for GetUnreadCount {
+ type Response = GetUnreadCountResponse;
+
+ async fn perform(
+ &self,
+ context: &Data<LemmyContext>,
+ _websocket_id: Option<ConnectionId>,
+ ) -> Result<Self::Response, LemmyError> {
+ let data = self;
+ let local_user_view =
+ get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+
+ let person_id = local_user_view.person.id;
+
+ let replies = blocking(context.pool(), move |conn| {
+ CommentView::get_unread_replies(conn, person_id)
+ })
+ .await??;
+
+ let mentions = blocking(context.pool(), move |conn| {
+ PersonMentionView::get_unread_mentions(conn, person_id)
+ })
+ .await??;
+
+ let private_messages = blocking(context.pool(), move |conn| {
+ PrivateMessageView::get_unread_messages(conn, person_id)
+ })
+ .await??;
+
+ let res = Self::Response {
+ replies,
+ mentions,
+ private_messages,
+ };
+
+ Ok(res)
+ }
+}
pub comment_reports: i64,
pub post_reports: i64,
}
+
+#[derive(Deserialize)]
+pub struct GetUnreadCount {
+ pub auth: String,
+}
+
+#[derive(Serialize, Clone)]
+pub struct GetUnreadCountResponse {
+ pub replies: i64,
+ pub mentions: i64,
+ pub private_messages: i64,
+}
None => self.post.creator_id,
}
}
+
+ /// Gets the number of unread replies
+ pub fn get_unread_replies(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
+ use diesel::dsl::*;
+
+ comment::table
+ // recipient here
+ .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
+ .left_join(person_alias_1::table.on(person_alias_1::id.eq(comment_alias_1::creator_id)))
+ .inner_join(post::table)
+ .inner_join(community::table.on(post::community_id.eq(community::id)))
+ .left_join(
+ person_block::table.on(
+ comment::creator_id
+ .eq(person_block::target_id)
+ .and(person_block::person_id.eq(my_person_id)),
+ ),
+ )
+ .left_join(
+ community_block::table.on(
+ community::id
+ .eq(community_block::community_id)
+ .and(community_block::person_id.eq(my_person_id)),
+ ),
+ )
+ .filter(person_alias_1::id.eq(my_person_id)) // Gets the comment replies
+ .or_filter(
+ comment::parent_id
+ .is_null()
+ .and(post::creator_id.eq(my_person_id)),
+ ) // Gets the top level replies
+ .filter(comment::read.eq(false))
+ .filter(comment::deleted.eq(false))
+ .filter(comment::removed.eq(false))
+ // Don't show blocked communities or persons
+ .filter(community_block::person_id.is_null())
+ .filter(person_block::person_id.is_null())
+ .select(count(comment::id))
+ .first::<i64>(conn)
+ }
}
pub struct CommentQueryBuilder<'a> {
recipient,
})
}
+
+ /// Gets the number of unread messages
+ pub fn get_unread_messages(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
+ use diesel::dsl::*;
+ private_message::table
+ .filter(private_message::read.eq(false))
+ .filter(private_message::recipient_id.eq(my_person_id))
+ .filter(private_message::deleted.eq(false))
+ .select(count(private_message::id))
+ .first::<i64>(conn)
+ }
}
pub struct PrivateMessageQueryBuilder<'a> {
my_vote,
})
}
+
+ /// Gets the number of unread mentions
+ pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
+ use diesel::dsl::*;
+
+ person_mention::table
+ .filter(person_mention::recipient_id.eq(my_person_id))
+ .filter(person_mention::read.eq(false))
+ .select(count(person_mention::id))
+ .first::<i64>(conn)
+ }
}
pub struct PersonMentionQueryBuilder<'a> {
ResolvePostReport,
ListPostReports,
GetReportCount,
+ GetUnreadCount,
FollowCommunity,
GetReplies,
GetPersonMentions,
"/change_password",
web::put().to(route_post::<ChangePassword>),
)
- .route("/report_count", web::get().to(route_get::<GetReportCount>)),
+ .route("/report_count", web::get().to(route_get::<GetReportCount>))
+ .route("/unread_count", web::get().to(route_get::<GetUnreadCount>)),
)
// Admin Actions
.service(