`GET /user/mentions`
-#### Edit User Mention
+#### Mark User Mention as read
##### Request
```rust
{
- op: "EditUserMention",
+ op: "MarkUserMentionAsRead",
data: {
user_mention_id: i32,
- read: Option<bool>,
+ read: bool,
auth: String,
}
}
##### Response
```rust
{
- op: "EditUserMention",
+ op: "MarkUserMentionAsRead",
data: {
mention: UserMentionView,
}
```
##### HTTP
-`PUT /user/mention`
+`POST /user/mention/mark_as_read`
#### Get Private Messages
##### Request
}
}
+impl UserMention {
+ pub fn update_read(
+ conn: &PgConnection,
+ user_mention_id: i32,
+ new_read: bool,
+ ) -> Result<Self, Error> {
+ use crate::schema::user_mention::dsl::*;
+ diesel::update(user_mention.find(user_mention_id))
+ .set(read.eq(new_read))
+ .get_result::<Self>(conn)
+ }
+
+ pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
+ use crate::schema::user_mention::dsl::*;
+ diesel::update(
+ user_mention
+ .filter(recipient_id.eq(for_recipient_id))
+ .filter(read.eq(false)),
+ )
+ .set(read.eq(true))
+ .get_results::<Self>(conn)
+ }
+}
+
#[cfg(test)]
mod tests {
use crate::{
}
#[derive(Serialize, Deserialize)]
-pub struct EditUserMention {
+pub struct MarkUserMentionAsRead {
user_mention_id: i32,
- read: Option<bool>,
+ read: bool,
auth: String,
}
}
#[async_trait::async_trait(?Send)]
-impl Perform for Oper<EditUserMention> {
+impl Perform for Oper<MarkUserMentionAsRead> {
type Response = UserMentionResponse;
async fn perform(
pool: &DbPool,
_websocket_info: Option<WebsocketInfo>,
) -> Result<UserMentionResponse, LemmyError> {
- let data: &EditUserMention = &self.data;
+ let data: &MarkUserMentionAsRead = &self.data;
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
return Err(APIError::err("couldnt_update_comment").into());
}
- let user_mention_form = UserMentionForm {
- recipient_id: read_user_mention.recipient_id,
- comment_id: read_user_mention.comment_id,
- read: data.read.to_owned(),
- };
-
let user_mention_id = read_user_mention.id;
- let update_mention =
- move |conn: &'_ _| UserMention::update(conn, user_mention_id, &user_mention_form);
+ let read = data.read;
+ let update_mention = move |conn: &'_ _| UserMention::update_read(conn, user_mention_id, read);
if blocking(pool, update_mention).await?.is_err() {
return Err(APIError::err("couldnt_update_comment").into());
};
}
}
- // Mentions
- let mentions = blocking(pool, move |conn| {
- UserMentionQueryBuilder::create(conn, user_id)
- .unread_only(true)
- .page(1)
- .limit(999)
- .list()
- })
- .await??;
-
- // TODO: this should probably be a bulk operation
- for mention in &mentions {
- let mention_form = UserMentionForm {
- recipient_id: mention.to_owned().recipient_id,
- comment_id: mention.to_owned().id,
- read: Some(true),
- };
-
- let user_mention_id = mention.user_mention_id;
- let update_mention =
- move |conn: &'_ _| UserMention::update(conn, user_mention_id, &mention_form);
- if blocking(pool, update_mention).await?.is_err() {
- return Err(APIError::err("couldnt_update_comment").into());
- }
+ // Mark all user mentions as read
+ let update_user_mentions = move |conn: &'_ _| UserMention::mark_all_as_read(conn, user_id);
+ if blocking(pool, update_user_mentions).await?.is_err() {
+ return Err(APIError::err("couldnt_update_comment").into());
}
// Mark all private_messages as read
.wrap(rate_limit.message())
.route("", web::get().to(route_get::<GetUserDetails>))
.route("/mention", web::get().to(route_get::<GetUserMentions>))
- .route("/mention", web::put().to(route_post::<EditUserMention>))
+ .route(
+ "/mention/mark_as_read",
+ web::post().to(route_post::<MarkUserMentionAsRead>),
+ )
.route("/replies", web::get().to(route_get::<GetReplies>))
.route(
"/followed_communities",
GetUserDetails,
GetReplies,
GetUserMentions,
- EditUserMention,
+ MarkUserMentionAsRead,
GetModlog,
BanFromCommunity,
AddModToCommunity,
UserOperation::AddAdmin => do_user_operation::<AddAdmin>(args).await,
UserOperation::BanUser => do_user_operation::<BanUser>(args).await,
UserOperation::GetUserMentions => do_user_operation::<GetUserMentions>(args).await,
- UserOperation::EditUserMention => do_user_operation::<EditUserMention>(args).await,
+ UserOperation::MarkUserMentionAsRead => {
+ do_user_operation::<MarkUserMentionAsRead>(args).await
+ }
UserOperation::MarkAllAsRead => do_user_operation::<MarkAllAsRead>(args).await,
UserOperation::DeleteAccount => do_user_operation::<DeleteAccount>(args).await,
UserOperation::PasswordReset => do_user_operation::<PasswordReset>(args).await,
CommentNode as CommentNodeI,
CommentLikeForm,
CommentForm as CommentFormI,
- EditUserMentionForm,
+ MarkUserMentionAsReadForm,
SaveCommentForm,
BanFromCommunityForm,
BanUserForm,
handleMarkRead(i: CommentNode) {
// if it has a user_mention_id field, then its a mention
if (i.props.node.comment.user_mention_id) {
- let form: EditUserMentionForm = {
+ let form: MarkUserMentionAsReadForm = {
user_mention_id: i.props.node.comment.user_mention_id,
read: !i.props.node.comment.read,
};
- WebSocketService.Instance.editUserMention(form);
+ WebSocketService.Instance.markUserMentionAsRead(form);
} else {
let form: CommentFormI = {
content: i.props.node.comment.content,
this.sendUnreadCount();
this.setState(this.state);
setupTippy();
- } else if (res.op == UserOperation.EditUserMention) {
+ } else if (res.op == UserOperation.MarkUserMentionAsRead) {
let data = res.data as UserMentionResponse;
let found = this.state.mentions.find(c => c.id == data.mention.id);
GetUserDetails,
GetReplies,
GetUserMentions,
- EditUserMention,
+ MarkUserMentionAsRead,
GetModlog,
BanFromCommunity,
AddModToCommunity,
mentions: Array<Comment>;
}
-export interface EditUserMentionForm {
+export interface MarkUserMentionAsReadForm {
user_mention_id: number;
- read?: boolean;
+ read: boolean;
auth?: string;
}
| GetUserDetailsForm
| GetRepliesForm
| GetUserMentionsForm
- | EditUserMentionForm
+ | MarkUserMentionAsReadForm
| GetModlogForm
| SiteForm
| SearchForm
UserView,
GetRepliesForm,
GetUserMentionsForm,
- EditUserMentionForm,
+ MarkUserMentionAsReadForm,
SearchForm,
UserSettingsForm,
DeleteAccountForm,
this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
}
- public editUserMention(form: EditUserMentionForm) {
+ public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
this.setAuth(form);
- this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
+ this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
}
public getModlog(form: GetModlogForm) {