mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
* feat: integrate supabase auth service * chore: ignore the sercet * feat: separate and inject the auth service * chore: integrate auth service into sign in/up page * feat: integrate github and google sign in * chore: update user trait * feat: box any params in UserCloudService trait * feat: add flowy-server crate * refactor: user trait * docs: doc ThirdPartyAuthPB * feat: server provider * feat: pass the uuid to rust side * feat: pass supabase config to rust side * feat: integrate env file * feat: implement login as guest * feat: impl postgrest * test: use env * chore: upper case key * feat: optimize the file storage * fix: don't call set auth when user login in local * docs: add docs * feat: create/update/get user using postgrest * feat: optimize the login as guest * feat: create user workspace * feat: create user default workspace * feat: redesign the setting path location page * feat: use uuid as view id * feat: send auth info to rust backend * fix: sign up * fix: skip to wrong page after login * fix: integrate test error * fix: indent command error * feat: add discord login in type * fix: flutter analyze * ci: fix rust tests * ci: fix tauri build * ci: fix tauri build --------- Co-authored-by: nathan <nathan@appflowy.io>
59 lines
1.4 KiB
Rust
59 lines
1.4 KiB
Rust
use std::time::SystemTime;
|
|
|
|
const EPOCH: u64 = 1637806706000;
|
|
const NODE_ID_BITS: u64 = 10;
|
|
const SEQUENCE_BITS: u64 = 12;
|
|
const NODE_ID_SHIFT: u64 = SEQUENCE_BITS;
|
|
const TIMESTAMP_SHIFT: u64 = NODE_ID_BITS + SEQUENCE_BITS;
|
|
const SEQUENCE_MASK: u64 = (1 << SEQUENCE_BITS) - 1;
|
|
|
|
pub struct UserIDGenerator {
|
|
node_id: u64,
|
|
sequence: u64,
|
|
last_timestamp: u64,
|
|
}
|
|
|
|
impl UserIDGenerator {
|
|
pub fn new(node_id: u64) -> UserIDGenerator {
|
|
UserIDGenerator {
|
|
node_id,
|
|
sequence: 0,
|
|
last_timestamp: 0,
|
|
}
|
|
}
|
|
|
|
pub fn next_id(&mut self) -> i64 {
|
|
let timestamp = self.timestamp();
|
|
if timestamp < self.last_timestamp {
|
|
panic!("Clock moved backwards!");
|
|
}
|
|
|
|
if timestamp == self.last_timestamp {
|
|
self.sequence = (self.sequence + 1) & SEQUENCE_MASK;
|
|
if self.sequence == 0 {
|
|
self.wait_next_millis();
|
|
}
|
|
} else {
|
|
self.sequence = 0;
|
|
}
|
|
|
|
self.last_timestamp = timestamp;
|
|
let id = (timestamp - EPOCH) << TIMESTAMP_SHIFT | self.node_id << NODE_ID_SHIFT | self.sequence;
|
|
id as i64
|
|
}
|
|
|
|
fn wait_next_millis(&self) {
|
|
let mut timestamp = self.timestamp();
|
|
while timestamp == self.last_timestamp {
|
|
timestamp = self.timestamp();
|
|
}
|
|
}
|
|
|
|
fn timestamp(&self) -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.expect("Clock moved backwards!")
|
|
.as_millis() as u64
|
|
}
|
|
}
|