Skip to content

Commit

Permalink
respect js/ts devs
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Jan 15, 2024
1 parent e8c0f81 commit b647957
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 146 deletions.
36 changes: 18 additions & 18 deletions src/handle/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use sms4_backend::{
verify::{self, Captcha},
Account, Permission, Tag, TagEntry, Unverified,
},
Error,
Error, Id,
};

use crate::{Auth, Global};
Expand Down Expand Up @@ -107,7 +107,7 @@ pub struct LoginReq {

#[derive(Serialize, Deserialize)]
pub struct LoginRes {
pub id: u64,
pub id: Id,
pub token: String,
pub expire_at: Option<i64>,
}
Expand All @@ -129,7 +129,7 @@ pub async fn login<Io: IoHandle>(
})?;

Ok(axum::Json(LoginRes {
id: lazy.id(),
id: lazy.id().into(),
token,
expire_at: exp_time,
}))
Expand Down Expand Up @@ -180,7 +180,7 @@ pub async fn reset_password<Io: IoHandle>(
let mut lazy = gd!(select, unverified.email_hash()).ok_or(Error::PermissionDenied)?;
let account = lazy.get_mut().await?;
account.reset_password(captcha, new_password)?;
// Clear all tokens after reseting password
// Clear all tokens after resetting password
account.clear_tokens();
Ok(())
}
Expand Down Expand Up @@ -269,7 +269,7 @@ pub async fn logout<Io: IoHandle>(

#[derive(Deserialize)]
pub struct SetPermissionsReq {
pub target_account: u64,
pub target_account: Id,
pub permissions: Vec<Permission>,
}

Expand All @@ -294,8 +294,8 @@ pub async fn set_permissions<Io: IoHandle>(
.filter_map(Tag::as_permission)
.copied();

let select_t = sd!(worlds.account, target_account);
let mut lazy_t = gd!(select_t, target_account).ok_or(Error::AccountNotFound)?;
let select_t = sd!(worlds.account, target_account.0);
let mut lazy_t = gd!(select_t, target_account.0).ok_or(Error::AccountNotFound)?;
let target = lazy_t.get_mut().await?;
if this
.tags()
Expand Down Expand Up @@ -391,14 +391,14 @@ impl Info {
}

pub async fn get_info<Io: IoHandle>(
Path(target): Path<u64>,
Path(target): Path<Id>,
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
) -> Result<Json<Info>, Error> {
let select = sd!(worlds.account, auth.account);
let this_lazy = va!(auth, select);
let select = sd!(worlds.account, target);
let lazy = gd!(select, target).ok_or(Error::AccountNotFound)?;
let select = sd!(worlds.account, target.0);
let lazy = gd!(select, target.0).ok_or(Error::AccountNotFound)?;
let account = lazy.get().await?;

if auth.account == account.id() {
Expand All @@ -424,7 +424,7 @@ pub async fn get_info<Io: IoHandle>(

#[derive(Deserialize)]
pub struct BulkGetInfoReq {
pub ids: Vec<u64>,
pub ids: Vec<Id>,
}

/// Bulk gets account info, returns a map from account id to simple account info,
Expand All @@ -433,21 +433,21 @@ pub async fn bulk_get_info<Io: IoHandle>(
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
Json(BulkGetInfoReq { ids }): Json<BulkGetInfoReq>,
) -> Result<Json<HashMap<u64, Info>>, Error> {
) -> Result<Json<HashMap<Id, Info>>, Error> {
let select = sd!(worlds.account, auth.account);
va!(auth, select => ViewSimpleAccount);
if let Some(last) = ids.first().copied() {
let mut select = worlds.account.select(0, last);
if let Some(first) = ids.first().copied() {
let mut select = worlds.account.select(0, first.0);
for account in &ids[1..] {
select = select.plus(0, *account);
select = select.plus(0, account.0);
}
select = select.hints(ids[..].into_iter().copied());
select = select.hints(ids.iter().copied().map(From::from));
let mut iter = select.iter();
let mut res = HashMap::with_capacity(ids.len());
while let Some(Ok(lazy)) = iter.next().await {
if ids.contains(&lazy.id()) {
if ids.contains(&Id(lazy.id())) {
if let Ok(account) = lazy.get().await {
res.insert(account.id(), Info::from_simple(account));
res.insert(account.id().into(), Info::from_simple(account));
}
}
}
Expand Down
59 changes: 31 additions & 28 deletions src/handle/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use sms4_backend::{
account::{Permission, Tag},
notification::Notification,
Id,
};
use time::{Date, Duration, OffsetDateTime};

Expand Down Expand Up @@ -59,7 +60,7 @@ pub struct NotifyReq {
#[derive(Serialize)]
pub struct NotifyRes {
/// Id of the notification.
pub id: u64,
pub id: Id,
}

/// Creates a new notification.
Expand All @@ -84,7 +85,7 @@ pub async fn notify<Io: IoHandle>(
va!(auth, select => ManageNotifications);

let notification = Notification::new(title, body, time, auth.account);
let id = notification.id();
let id = Id(notification.id());
worlds.notification.insert(notification).await?;
Ok(Json(NotifyRes { id }))
}
Expand All @@ -104,7 +105,7 @@ pub struct FilterNotificationParams {
/// Filter notifications after this id.\
/// The field can be omitted.
#[serde(default)]
pub from: Option<u64>,
pub from: Option<Id>,
/// Max notifications to return.\
/// The field can be omitted,
/// and the default value is **16**.
Expand All @@ -116,7 +117,7 @@ pub struct FilterNotificationParams {
///
/// This only works with the permission [`Permission::ManageNotifications`].
#[serde(default)]
pub sender: Option<u64>,
pub sender: Option<Id>,
}

impl FilterNotificationParams {
Expand All @@ -127,7 +128,7 @@ impl FilterNotificationParams {
#[derive(Serialize)]
pub struct FilterNotificationRes {
/// Notifications ids.
pub notifications: Vec<u64>,
pub notifications: Box<[Id]>,
}

/// Filters notifications.
Expand All @@ -152,7 +153,7 @@ pub async fn filter<Io: IoHandle>(

let mut select = worlds.notification.select_all();
if let Some(from) = from {
select = select.and(0, from..);
select = select.and(0, from.0..);
}
if let Some((before, after)) = after.zip(before) {
if after + Duration::days(365) > before {
Expand All @@ -170,24 +171,26 @@ pub async fn filter<Io: IoHandle>(
let mut notifications = Vec::new();
let now = OffsetDateTime::now_utc();
while let Some(Ok(lazy)) = iter.next().await {
if from.is_some_and(|a| lazy.id() <= a) {
if from.is_some_and(|a| lazy.id() <= a.0) {
continue;
}
if let Ok(val) = lazy.get().await {
if sender.is_some_and(|c| val.sender() != c && permitted_manage)
if sender.is_some_and(|c| Id(val.sender()) != c && permitted_manage)
|| after.is_some_and(|d| val.time().date() >= d)
|| before.is_some_and(|d| val.time().date() <= d)
|| (!permitted_manage && val.time() > now)
{
continue;
}
notifications.push(val.id());
notifications.push(Id(val.id()));
if notifications.len() == limit {
break;
}
}
}
Ok(Json(FilterNotificationRes { notifications }))
Ok(Json(FilterNotificationRes {
notifications: notifications.into_boxed_slice(),
}))
}

#[derive(Serialize)]
Expand Down Expand Up @@ -220,7 +223,7 @@ impl Info {

/// Gets a notification.
pub async fn get_info<Io: IoHandle>(
Path(id): Path<u64>,
Path(Id(id)): Path<Id>,
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
) -> Result<Json<Info>, Error> {
Expand All @@ -246,7 +249,7 @@ pub async fn get_info<Io: IoHandle>(

#[derive(Deserialize)]
pub struct BulkGetInfoReq {
pub notifications: Box<[u64]>,
pub notifications: Box<[Id]>,
}

pub async fn bulk_get_info<Io: IoHandle>(
Expand All @@ -267,16 +270,16 @@ pub async fn bulk_get_info<Io: IoHandle>(
};
let mut select = worlds
.notification
.select(0, first)
.hints(notifications.iter().copied());
.select(0, first.0)
.hints(notifications.iter().copied().map(From::from));
for id in notifications[1..].iter().copied() {
select = select.plus(0, id);
select = select.plus(0, id.0);
}
let mut iter = select.iter();
let mut res = HashMap::with_capacity(notifications.len().max(64));
let now = OffsetDateTime::now_utc();
while let Some(Ok(lazy)) = iter.next().await {
if notifications.contains(&lazy.id()) {
if notifications.contains(&Id(lazy.id())) {
if let Ok(val) = lazy.get().await {
if val.time() <= now && !permitted_manage {
continue;
Expand All @@ -298,16 +301,16 @@ pub async fn bulk_get_info<Io: IoHandle>(
///
/// The request must be authorized with [`Permission::ManageNotifications`].
pub async fn remove<Io: IoHandle>(
Path(id): Path<u64>,
Path(id): Path<Id>,
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
) -> Result<(), Error> {
let select = sd!(worlds.account, auth.account);
va!(auth, select => ManageNotifications);

let select = sd!(worlds.notification, id);
gd!(select, id)
.ok_or(Error::NotificationNotFound(id))?
let select = sd!(worlds.notification, id.0);
gd!(select, id.0)
.ok_or(Error::NotificationNotFound(id.0))?
.destroy()
.await?;

Expand All @@ -317,7 +320,7 @@ pub async fn remove<Io: IoHandle>(
/// Request body for bulk removing notifications.
#[derive(Deserialize)]
pub struct BulkRemoveReq {
pub notifications: Box<[u64]>,
pub notifications: Box<[Id]>,
}

/// Bulk removes notifications.
Expand All @@ -340,15 +343,15 @@ pub async fn bulk_remove<Io: IoHandle>(
if let Some(first) = notifications.first().copied() {
let mut select = worlds
.notification
.select(0, first)
.hints(notifications.iter().copied());
.select(0, first.0)
.hints(notifications.iter().copied().map(From::from));
for id in notifications[1..].iter().copied() {
select = select.plus(0, id);
select = select.plus(0, id.0);
}

let mut iter = select.iter();
while let Some(Ok(lazy)) = iter.next().await {
if notifications.contains(&lazy.id()) {
if notifications.contains(&Id(lazy.id())) {
lazy.destroy().await?;
}
}
Expand Down Expand Up @@ -377,15 +380,15 @@ pub struct ModifyReq {
///
/// The request body is declared as [`ModifyReq`].
pub async fn modify<Io: IoHandle>(
Path(id): Path<u64>,
Path(id): Path<Id>,
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
Json(ModifyReq { title, body, time }): Json<ModifyReq>,
) -> Result<(), Error> {
let select = sd!(worlds.account, auth.account);
va!(auth, select => ManageNotifications);
let select = sd!(worlds.notification, id);
let mut lazy = gd!(select, id).ok_or(Error::NotificationNotFound(id))?;
let select = sd!(worlds.notification, id.0);
let mut lazy = gd!(select, id.0).ok_or(Error::NotificationNotFound(id.0))?;
let val = lazy.get_mut().await?;

if let Some(title) = title {
Expand Down
Loading

0 comments on commit b647957

Please sign in to comment.