2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2023-06-20 22:50:26 +00:00
|
|
|
use crate::dbs::Notification;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Action, Transaction};
|
2023-09-10 10:58:50 +00:00
|
|
|
use crate::doc::CursorDoc;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2023-09-10 10:58:50 +00:00
|
|
|
use crate::sql::paths::SC;
|
|
|
|
use crate::sql::paths::SD;
|
|
|
|
use crate::sql::paths::TK;
|
|
|
|
use crate::sql::permission::Permission;
|
2023-06-20 22:50:26 +00:00
|
|
|
use crate::sql::Value;
|
2023-09-10 10:58:50 +00:00
|
|
|
use std::ops::Deref;
|
2023-08-24 14:04:57 +00:00
|
|
|
use std::sync::Arc;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn lives(
|
|
|
|
&self,
|
2023-09-10 10:58:50 +00:00
|
|
|
_ctx: &Context<'_>,
|
2023-02-22 18:04:20 +00:00
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2023-06-20 22:50:26 +00:00
|
|
|
stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
2023-02-22 18:04:20 +00:00
|
|
|
// Check if forced
|
|
|
|
if !opt.force && !self.changed() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Get the record id
|
2023-07-05 21:26:13 +00:00
|
|
|
let rid = self.id.as_ref().unwrap();
|
|
|
|
// Check if we can send notifications
|
|
|
|
if let Some(chn) = &opt.sender {
|
|
|
|
// Clone the sending channel
|
|
|
|
let chn = chn.clone();
|
|
|
|
// Loop through all index statements
|
2023-07-06 14:57:42 +00:00
|
|
|
for lv in self.lv(opt, txn).await?.iter() {
|
2023-07-05 21:26:13 +00:00
|
|
|
// Create a new statement
|
|
|
|
let lq = Statement::from(lv);
|
2023-09-10 10:58:50 +00:00
|
|
|
// Get the event action
|
|
|
|
let met = if stm.is_delete() {
|
|
|
|
Value::from("DELETE")
|
|
|
|
} else if self.is_new() {
|
|
|
|
Value::from("CREATE")
|
|
|
|
} else {
|
|
|
|
Value::from("UPDATE")
|
|
|
|
};
|
|
|
|
// Check if this is a delete statement
|
|
|
|
let doc = match stm.is_delete() {
|
|
|
|
true => &self.initial,
|
|
|
|
false => &self.current,
|
|
|
|
};
|
|
|
|
// Ensure that a session exists on the LIVE query
|
|
|
|
let sess = lv.session.as_ref().ok_or(Error::UnknownAuth)?;
|
|
|
|
// Ensure that auth info exists on the LIVE query
|
|
|
|
let auth = lv.auth.clone().ok_or(Error::UnknownAuth)?;
|
|
|
|
// We need to create a new context which we will
|
|
|
|
// use for processing this LIVE query statement.
|
|
|
|
// This ensures that we are using the session
|
|
|
|
// of the user who created the LIVE query.
|
|
|
|
let mut lqctx = Context::background();
|
|
|
|
lqctx.add_value("auth", sess.pick(SD.as_ref()));
|
|
|
|
lqctx.add_value("scope", sess.pick(SC.as_ref()));
|
|
|
|
lqctx.add_value("token", sess.pick(TK.as_ref()));
|
|
|
|
lqctx.add_value("session", sess);
|
|
|
|
// We need to create a new options which we will
|
|
|
|
// use for processing this LIVE query statement.
|
|
|
|
// This ensures that we are using the auth data
|
|
|
|
// of the user who created the LIVE query.
|
|
|
|
let lqopt = Options::new_with_perms(opt, true)
|
|
|
|
.with_auth_enabled(true)
|
|
|
|
.with_auth(Arc::from(auth));
|
|
|
|
// Add $before, $after, $value, and $event params
|
|
|
|
// to this LIVE query so that user can use these
|
|
|
|
// within field projections and WHERE clauses.
|
|
|
|
lqctx.add_value("event", met);
|
|
|
|
lqctx.add_value("value", self.current.doc.deref());
|
|
|
|
lqctx.add_value("after", self.current.doc.deref());
|
|
|
|
lqctx.add_value("before", self.initial.doc.deref());
|
|
|
|
// First of all, let's check to see if the WHERE
|
|
|
|
// clause of the LIVE query is matched by this
|
|
|
|
// document. If it is then we can continue.
|
|
|
|
match self.lq_check(&lqctx, &lqopt, txn, &lq, doc).await {
|
|
|
|
Err(Error::Ignore) => continue,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(_) => (),
|
2023-07-05 21:26:13 +00:00
|
|
|
}
|
2023-09-10 10:58:50 +00:00
|
|
|
// Secondly, let's check to see if any PERMISSIONS
|
|
|
|
// clause for this table allows this document to
|
|
|
|
// be viewed by the user who created this LIVE
|
|
|
|
// query. If it does, then we can continue.
|
|
|
|
match self.lq_allow(&lqctx, &lqopt, txn, &lq, doc).await {
|
|
|
|
Err(Error::Ignore) => continue,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(_) => (),
|
2023-08-24 14:04:57 +00:00
|
|
|
}
|
2023-09-10 10:58:50 +00:00
|
|
|
// Finally, let's check what type of statement
|
|
|
|
// caused this LIVE query to run, and send the
|
|
|
|
// relevant notification based on the statement.
|
2023-07-05 21:26:13 +00:00
|
|
|
if stm.is_delete() {
|
|
|
|
// Send a DELETE notification
|
2023-07-24 16:15:45 +00:00
|
|
|
if opt.id()? == lv.node.0 {
|
2023-07-05 21:26:13 +00:00
|
|
|
let thing = (*rid).clone();
|
|
|
|
chn.send(Notification {
|
2023-07-24 16:15:45 +00:00
|
|
|
id: lv.id.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
action: Action::Delete,
|
|
|
|
result: Value::Thing(thing),
|
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
|
|
|
} else if self.is_new() {
|
|
|
|
// Send a CREATE notification
|
2023-07-24 16:15:45 +00:00
|
|
|
if opt.id()? == lv.node.0 {
|
2023-07-05 21:26:13 +00:00
|
|
|
chn.send(Notification {
|
2023-07-24 16:15:45 +00:00
|
|
|
id: lv.id.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
action: Action::Create,
|
2023-09-10 10:58:50 +00:00
|
|
|
result: self.pluck(&lqctx, &lqopt, txn, &lq).await?,
|
2023-06-20 22:50:26 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
2023-06-20 22:50:26 +00:00
|
|
|
} else {
|
2023-07-05 21:26:13 +00:00
|
|
|
// Send a UPDATE notification
|
2023-07-24 16:15:45 +00:00
|
|
|
if opt.id()? == lv.node.0 {
|
2023-07-05 21:26:13 +00:00
|
|
|
chn.send(Notification {
|
2023-07-24 16:15:45 +00:00
|
|
|
id: lv.id.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
action: Action::Update,
|
2023-09-10 10:58:50 +00:00
|
|
|
result: self.pluck(&lqctx, &lqopt, txn, &lq).await?,
|
2023-06-20 22:50:26 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-02-22 18:04:20 +00:00
|
|
|
}
|
|
|
|
// Carry on
|
2022-02-06 01:14:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-10 10:58:50 +00:00
|
|
|
/// Check the WHERE clause for a LIVE query
|
|
|
|
async fn lq_check(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
stm: &Statement<'_>,
|
|
|
|
doc: &CursorDoc<'_>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
// Check where condition
|
|
|
|
if let Some(cond) = stm.conds() {
|
|
|
|
// Check if the expression is truthy
|
|
|
|
if !cond.compute(ctx, opt, txn, Some(doc)).await?.is_truthy() {
|
|
|
|
// Ignore this document
|
|
|
|
return Err(Error::Ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
/// Check any PERRMISSIONS for a LIVE query
|
|
|
|
async fn lq_allow(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
stm: &Statement<'_>,
|
|
|
|
doc: &CursorDoc<'_>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
// Should we run permissions checks?
|
|
|
|
if opt.check_perms(stm.into()) {
|
|
|
|
// Get the table
|
|
|
|
let tb = self.tb(opt, txn).await?;
|
|
|
|
// Get the permission clause
|
|
|
|
let perms = &tb.permissions.select;
|
|
|
|
// Process the table permissions
|
|
|
|
match perms {
|
|
|
|
Permission::None => return Err(Error::Ignore),
|
|
|
|
Permission::Full => return Ok(()),
|
|
|
|
Permission::Specific(e) => {
|
|
|
|
// Disable permissions
|
|
|
|
let opt = &opt.new_with_perms(false);
|
|
|
|
// Process the PERMISSION clause
|
|
|
|
if !e.compute(ctx, opt, txn, Some(doc)).await?.is_truthy() {
|
|
|
|
return Err(Error::Ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|