change how webhook information is in the webhook

This commit is contained in:
Zomatree
2023-04-01 22:49:34 +01:00
parent 01577fd798
commit 7b39f4e9d9
15 changed files with 31 additions and 33 deletions

View File

@@ -11,7 +11,7 @@ pub async fn req(db: &Db, user: User, target: Ref, msg: Ref) -> Result<EmptyResp
return Err(Error::NotFound);
}
if message.author.as_deref().unwrap_or_default() != user.id {
if message.author != user.id {
perms(&user)
.channel(&target.as_channel(db).await?)
.throw_permission(db, Permission::ManageMessages)

View File

@@ -43,7 +43,7 @@ pub async fn req(
return Err(Error::NotFound);
}
if message.author.as_deref().unwrap_or_default() != user.id {
if message.author != user.id {
return Err(Error::CannotEditMessage);
}

View File

@@ -41,7 +41,7 @@ pub async fn req(db: &Db, user: User, target: Ref, data: Json<CreateWebhookBody>
name: data.name,
avatar,
channel: channel.id().to_string(),
token: nanoid::nanoid!(64)
token: Some(nanoid::nanoid!(64))
};
webhook.create(db).await?;

View File

@@ -42,7 +42,7 @@ pub async fn report_content(db: &Db, user: User, data: Json<DataReportContent>)
let message = db.fetch_message(id).await?;
// Users cannot report themselves
if message.author.as_ref() == Some(&user.id) {
if message.author == user.id {
return Err(Error::CannotReportYourself);
}

View File

@@ -8,7 +8,7 @@ use revolt_quark::{Db, Ref, Result, EmptyResponse, Error};
pub async fn req(db: &Db, target: Ref, token: String) -> Result<EmptyResponse> {
let webhook = target.as_webhook(db).await?;
(webhook.token == token)
(webhook.token.as_deref() == Some(&token))
.then_some(())
.ok_or(Error::InvalidCredentials)?;

View File

@@ -27,7 +27,7 @@ pub async fn req(db: &Db, target: Ref, token: String, data: Json<WebhookEditBody
let mut webhook = target.as_webhook(db).await?;
(webhook.token == token)
(webhook.token.as_deref() == Some(&token))
.then_some(())
.ok_or(Error::InvalidCredentials)?;

View File

@@ -15,7 +15,7 @@ pub async fn req(db: &Db, target: Ref, token: String, data: Json<DataMessageSend
let webhook = target.as_webhook(db).await?;
(webhook.token == token)
(webhook.token.as_deref() == Some(&token))
.then_some(())
.ok_or(Error::InvalidCredentials)?;

View File

@@ -745,7 +745,7 @@ fn convert_event(data: &str, event_name: &str) -> Result<Event> {
pub async fn req(db: &Db, target: Ref, token: String, event: EventHeader<'_>, data: String) -> Result<()> {
let webhook = target.as_webhook(db).await?;
(webhook.token == token)
(webhook.token.as_deref() == Some(&token))
.then_some(())
.ok_or(Error::InvalidCredentials)?;
@@ -978,9 +978,10 @@ pub async fn req(db: &Db, target: Ref, token: String, event: EventHeader<'_>, da
let mut message = Message {
id: message_id,
author: webhook.id.clone(),
channel: webhook.channel.clone(),
embeds: Some(vec![embed]),
webhook: Some(webhook.id.clone()),
webhook: Some(webhook.clone()),
..Default::default()
};

View File

@@ -9,7 +9,7 @@ use rocket::serde::json::Json;
pub async fn req(db: &Db, target: Ref, token: String) -> Result<Json<Webhook>> {
let webhook = target.as_webhook(db).await?;
(webhook.token == token)
(webhook.token.as_deref() == Some(&token))
.then_some(())
.ok_or(Error::InvalidCredentials)?;

View File

@@ -9,7 +9,7 @@ impl AbstractMessage for DummyDb {
Ok(Message {
id: id.into(),
channel: "channel".into(),
author: Some("author".into()),
author: "author".into(),
content: Some("message content".into()),
..Default::default()

View File

@@ -22,7 +22,7 @@ impl AbstractWebhook for DummyDb {
name: "".to_string(),
avatar: None,
channel: "0".to_string(),
token: "".to_owned(),
token: Some("".to_owned()),
})
}

View File

@@ -428,6 +428,11 @@ impl Channel {
}
}
let (author_id, webhook) = match &author {
MessageAuthor::User(user) => (user.id.clone(), None),
MessageAuthor::Webhook(webhook) => (webhook.id.clone(), Some((*webhook).clone()))
};
// Start constructing the message
let message_id = Ulid::new().to_string();
let mut message = Message {
@@ -435,14 +440,11 @@ impl Channel {
channel: self.id().to_string(),
masquerade: data.masquerade,
interactions: data.interactions.unwrap_or_default(),
author: author_id,
webhook,
..Default::default()
};
match &author {
MessageAuthor::User(user) => message.author = Some(user.id.clone()),
MessageAuthor::Webhook(webhook) => message.webhook = Some(webhook.id.clone()),
}
// Parse mentions in message.
let mut mentions = HashSet::new();
if let Some(content) = &data.content {
@@ -464,7 +466,7 @@ impl Channel {
let message = Ref::from_unchecked(id).as_message(db).await?;
if mention {
mentions.insert(message.author_id().to_owned());
mentions.insert(message.author.to_owned());
}
replies.insert(message.id);

View File

@@ -272,12 +272,8 @@ impl Message {
db.clear_reaction(&self.id, emoji).await
}
/// Gets either the user or the webhook id which sent this message
pub fn author_id(&self) -> &str {
match &self.author {
Some(id) => id,
None => self.webhook.as_deref().unwrap()
}
pub fn is_webhook(&self) -> bool {
self.webhook.is_some()
}
}
@@ -289,8 +285,8 @@ impl IntoUsers for Message {
fn get_user_ids(&self) -> Vec<String> {
let mut ids = Vec::new();
if let Some(author) = &self.author {
ids.push(author.clone());
if !self.is_webhook() {
ids.push(self.author.clone());
};
if let Some(msg) = &self.system {
@@ -331,7 +327,7 @@ impl SystemMessage {
Message {
id: Ulid::new().to_string(),
channel,
author: Some("00000000000000000000000000".to_string()),
author: "00000000000000000000000000".to_string(),
system: Some(self),
..Default::default()

View File

@@ -1,4 +1,4 @@
use crate::util::regex::RE_COLOUR;
use crate::{util::regex::RE_COLOUR, models::Webhook};
use indexmap::{IndexMap, IndexSet};
use iso8601_timestamp::Timestamp;
@@ -120,12 +120,11 @@ pub struct Message {
pub nonce: Option<String>,
/// Id of the channel this message was sent in
pub channel: String,
/// Id of the user that sent this message, will be none when a webhook sent the message
#[serde(skip_serializing_if = "Option::is_none")]
pub author: Option<String>,
/// Id of the user or webhook that sent this message
pub author: String,
/// Id of the webhook that sent this message, mutually exclusive with `author`
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook: Option<String>,
pub webhook: Option<Webhook>,
/// Message content
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,

View File

@@ -24,7 +24,7 @@ pub struct Webhook {
pub channel: String,
/// The private token for the webhook
pub token: String
pub token: Option<String>
}
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]