Impl /auth/user/list

This commit is contained in:
2022-10-16 02:10:35 +00:00
committed by GitHub
parent e2a1dde4a6
commit 43f8a27172
7 changed files with 248 additions and 2 deletions

View File

@@ -319,6 +319,38 @@ impl PixivDownloaderSqlite {
.optional()?)
}
#[cfg(feature = "server")]
async fn _list_users(&self, offset: u64, limit: u64) -> Result<Vec<User>, SqliteError> {
let con = self.db.lock().await;
let mut stmt = con.prepare("SELECT * FROM users LIMIT ?, ?;")?;
let mut rows = stmt.query([offset, limit])?;
let mut users = Vec::new();
while let Some(row) = rows.next()? {
let password: Vec<u8> = row.get(3)?;
let password: &[u8] = &password;
users.push(User {
id: row.get(0)?,
name: row.get(1)?,
username: row.get(2)?,
password: BytesMut::from(password),
is_admin: row.get(4)?,
});
}
Ok(users)
}
#[cfg(feature = "server")]
async fn _list_users_id(&self, offset: u64, count: u64) -> Result<Vec<u64>, SqliteError> {
let con = self.db.lock().await;
let mut stmt = con.prepare("SELECT id FROM users LIMIT ?, ?;")?;
let mut rows = stmt.query([offset, count])?;
let mut ids = Vec::new();
while let Some(row) = rows.next()? {
ids.push(row.get(0)?);
}
Ok(ids)
}
async fn _read_version(&self) -> Result<Option<[u8; 4]>, SqliteError> {
let con = self.db.lock().await;
let mut stmt = con.prepare("SELECT v1, v2, v3, v4 FROM version WHERE id='main';")?;
@@ -596,6 +628,24 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
Ok(())
}
#[cfg(feature = "server")]
async fn list_users(
&self,
offset: u64,
limit: u64,
) -> Result<Vec<User>, PixivDownloaderDbError> {
Ok(self._list_users(offset, limit).await?)
}
#[cfg(feature = "server")]
async fn list_users_id(
&self,
offset: u64,
count: u64,
) -> Result<Vec<u64>, PixivDownloaderDbError> {
Ok(self._list_users_id(offset, count).await?)
}
#[cfg(feature = "server")]
async fn revoke_expired_tokens(&self) -> Result<usize, PixivDownloaderDbError> {
let mut db = self.db.lock().await;

View File

@@ -78,6 +78,24 @@ pub trait PixivDownloaderDb {
/// Initialize the database (create tables, migrate data, etc.)
async fn init(&self) -> Result<(), PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// List users
/// * `offset` - The offset of the first user
/// * `limit` - The maximum number of users to return
async fn list_users(
&self,
offset: u64,
limit: u64,
) -> Result<Vec<User>, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// List users' id
/// * `offset` - The offset of the list
/// * `count` - The maximum count of the list
async fn list_users_id(
&self,
offset: u64,
count: u64,
) -> Result<Vec<u64>, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Remove all expired tokens
/// Return the number of removed tokens
async fn revoke_expired_tokens(&self) -> Result<usize, PixivDownloaderDbError>;