forked from jmug/stoatchat
change how webhook information is in the webhook
This commit is contained in:
@@ -11,7 +11,7 @@ pub async fn req(db: &Db, user: User, target: Ref, msg: Ref) -> Result<EmptyResp
|
|||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.author.as_deref().unwrap_or_default() != user.id {
|
if message.author != user.id {
|
||||||
perms(&user)
|
perms(&user)
|
||||||
.channel(&target.as_channel(db).await?)
|
.channel(&target.as_channel(db).await?)
|
||||||
.throw_permission(db, Permission::ManageMessages)
|
.throw_permission(db, Permission::ManageMessages)
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ pub async fn req(
|
|||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.author.as_deref().unwrap_or_default() != user.id {
|
if message.author != user.id {
|
||||||
return Err(Error::CannotEditMessage);
|
return Err(Error::CannotEditMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ pub async fn req(db: &Db, user: User, target: Ref, data: Json<CreateWebhookBody>
|
|||||||
name: data.name,
|
name: data.name,
|
||||||
avatar,
|
avatar,
|
||||||
channel: channel.id().to_string(),
|
channel: channel.id().to_string(),
|
||||||
token: nanoid::nanoid!(64)
|
token: Some(nanoid::nanoid!(64))
|
||||||
};
|
};
|
||||||
|
|
||||||
webhook.create(db).await?;
|
webhook.create(db).await?;
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ pub async fn report_content(db: &Db, user: User, data: Json<DataReportContent>)
|
|||||||
let message = db.fetch_message(id).await?;
|
let message = db.fetch_message(id).await?;
|
||||||
|
|
||||||
// Users cannot report themselves
|
// Users cannot report themselves
|
||||||
if message.author.as_ref() == Some(&user.id) {
|
if message.author == user.id {
|
||||||
return Err(Error::CannotReportYourself);
|
return Err(Error::CannotReportYourself);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use revolt_quark::{Db, Ref, Result, EmptyResponse, Error};
|
|||||||
pub async fn req(db: &Db, target: Ref, token: String) -> Result<EmptyResponse> {
|
pub async fn req(db: &Db, target: Ref, token: String) -> Result<EmptyResponse> {
|
||||||
let webhook = target.as_webhook(db).await?;
|
let webhook = target.as_webhook(db).await?;
|
||||||
|
|
||||||
(webhook.token == token)
|
(webhook.token.as_deref() == Some(&token))
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(Error::InvalidCredentials)?;
|
.ok_or(Error::InvalidCredentials)?;
|
||||||
|
|
||||||
|
|||||||
@@ -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?;
|
let mut webhook = target.as_webhook(db).await?;
|
||||||
|
|
||||||
(webhook.token == token)
|
(webhook.token.as_deref() == Some(&token))
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(Error::InvalidCredentials)?;
|
.ok_or(Error::InvalidCredentials)?;
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ pub async fn req(db: &Db, target: Ref, token: String, data: Json<DataMessageSend
|
|||||||
|
|
||||||
let webhook = target.as_webhook(db).await?;
|
let webhook = target.as_webhook(db).await?;
|
||||||
|
|
||||||
(webhook.token == token)
|
(webhook.token.as_deref() == Some(&token))
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(Error::InvalidCredentials)?;
|
.ok_or(Error::InvalidCredentials)?;
|
||||||
|
|
||||||
|
|||||||
@@ -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<()> {
|
pub async fn req(db: &Db, target: Ref, token: String, event: EventHeader<'_>, data: String) -> Result<()> {
|
||||||
let webhook = target.as_webhook(db).await?;
|
let webhook = target.as_webhook(db).await?;
|
||||||
|
|
||||||
(webhook.token == token)
|
(webhook.token.as_deref() == Some(&token))
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(Error::InvalidCredentials)?;
|
.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 {
|
let mut message = Message {
|
||||||
id: message_id,
|
id: message_id,
|
||||||
|
author: webhook.id.clone(),
|
||||||
channel: webhook.channel.clone(),
|
channel: webhook.channel.clone(),
|
||||||
embeds: Some(vec![embed]),
|
embeds: Some(vec![embed]),
|
||||||
webhook: Some(webhook.id.clone()),
|
webhook: Some(webhook.clone()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use rocket::serde::json::Json;
|
|||||||
pub async fn req(db: &Db, target: Ref, token: String) -> Result<Json<Webhook>> {
|
pub async fn req(db: &Db, target: Ref, token: String) -> Result<Json<Webhook>> {
|
||||||
let webhook = target.as_webhook(db).await?;
|
let webhook = target.as_webhook(db).await?;
|
||||||
|
|
||||||
(webhook.token == token)
|
(webhook.token.as_deref() == Some(&token))
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(Error::InvalidCredentials)?;
|
.ok_or(Error::InvalidCredentials)?;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ impl AbstractMessage for DummyDb {
|
|||||||
Ok(Message {
|
Ok(Message {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
channel: "channel".into(),
|
channel: "channel".into(),
|
||||||
author: Some("author".into()),
|
author: "author".into(),
|
||||||
content: Some("message content".into()),
|
content: Some("message content".into()),
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ impl AbstractWebhook for DummyDb {
|
|||||||
name: "".to_string(),
|
name: "".to_string(),
|
||||||
avatar: None,
|
avatar: None,
|
||||||
channel: "0".to_string(),
|
channel: "0".to_string(),
|
||||||
token: "".to_owned(),
|
token: Some("".to_owned()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// Start constructing the message
|
||||||
let message_id = Ulid::new().to_string();
|
let message_id = Ulid::new().to_string();
|
||||||
let mut message = Message {
|
let mut message = Message {
|
||||||
@@ -435,14 +440,11 @@ impl Channel {
|
|||||||
channel: self.id().to_string(),
|
channel: self.id().to_string(),
|
||||||
masquerade: data.masquerade,
|
masquerade: data.masquerade,
|
||||||
interactions: data.interactions.unwrap_or_default(),
|
interactions: data.interactions.unwrap_or_default(),
|
||||||
|
author: author_id,
|
||||||
|
webhook,
|
||||||
..Default::default()
|
..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.
|
// Parse mentions in message.
|
||||||
let mut mentions = HashSet::new();
|
let mut mentions = HashSet::new();
|
||||||
if let Some(content) = &data.content {
|
if let Some(content) = &data.content {
|
||||||
@@ -464,7 +466,7 @@ impl Channel {
|
|||||||
let message = Ref::from_unchecked(id).as_message(db).await?;
|
let message = Ref::from_unchecked(id).as_message(db).await?;
|
||||||
|
|
||||||
if mention {
|
if mention {
|
||||||
mentions.insert(message.author_id().to_owned());
|
mentions.insert(message.author.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
replies.insert(message.id);
|
replies.insert(message.id);
|
||||||
|
|||||||
@@ -272,12 +272,8 @@ impl Message {
|
|||||||
db.clear_reaction(&self.id, emoji).await
|
db.clear_reaction(&self.id, emoji).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets either the user or the webhook id which sent this message
|
pub fn is_webhook(&self) -> bool {
|
||||||
pub fn author_id(&self) -> &str {
|
self.webhook.is_some()
|
||||||
match &self.author {
|
|
||||||
Some(id) => id,
|
|
||||||
None => self.webhook.as_deref().unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,8 +285,8 @@ impl IntoUsers for Message {
|
|||||||
fn get_user_ids(&self) -> Vec<String> {
|
fn get_user_ids(&self) -> Vec<String> {
|
||||||
let mut ids = Vec::new();
|
let mut ids = Vec::new();
|
||||||
|
|
||||||
if let Some(author) = &self.author {
|
if !self.is_webhook() {
|
||||||
ids.push(author.clone());
|
ids.push(self.author.clone());
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(msg) = &self.system {
|
if let Some(msg) = &self.system {
|
||||||
@@ -331,7 +327,7 @@ impl SystemMessage {
|
|||||||
Message {
|
Message {
|
||||||
id: Ulid::new().to_string(),
|
id: Ulid::new().to_string(),
|
||||||
channel,
|
channel,
|
||||||
author: Some("00000000000000000000000000".to_string()),
|
author: "00000000000000000000000000".to_string(),
|
||||||
system: Some(self),
|
system: Some(self),
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::util::regex::RE_COLOUR;
|
use crate::{util::regex::RE_COLOUR, models::Webhook};
|
||||||
|
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
use iso8601_timestamp::Timestamp;
|
use iso8601_timestamp::Timestamp;
|
||||||
@@ -120,12 +120,11 @@ pub struct Message {
|
|||||||
pub nonce: Option<String>,
|
pub nonce: Option<String>,
|
||||||
/// Id of the channel this message was sent in
|
/// Id of the channel this message was sent in
|
||||||
pub channel: String,
|
pub channel: String,
|
||||||
/// Id of the user that sent this message, will be none when a webhook sent the message
|
/// Id of the user or webhook that sent this message
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
pub author: String,
|
||||||
pub author: Option<String>,
|
|
||||||
/// Id of the webhook that sent this message, mutually exclusive with `author`
|
/// Id of the webhook that sent this message, mutually exclusive with `author`
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub webhook: Option<String>,
|
pub webhook: Option<Webhook>,
|
||||||
/// Message content
|
/// Message content
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub content: Option<String>,
|
pub content: Option<String>,
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ pub struct Webhook {
|
|||||||
pub channel: String,
|
pub channel: String,
|
||||||
|
|
||||||
/// The private token for the webhook
|
/// The private token for the webhook
|
||||||
pub token: String
|
pub token: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
|
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
|
||||||
|
|||||||
Reference in New Issue
Block a user