
There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
164 lines
5 KiB
Rust
164 lines
5 KiB
Rust
use std::sync::Arc;
|
|
|
|
use anyhow::{anyhow, Context as _, Result};
|
|
use chrono::{DateTime, Utc};
|
|
use util::ResultExt;
|
|
|
|
use crate::db::Database;
|
|
use crate::executor::Executor;
|
|
use crate::{AppState, Config};
|
|
|
|
pub fn spawn_user_backfiller(app_state: Arc<AppState>) {
|
|
let Some(user_backfiller_github_access_token) =
|
|
app_state.config.user_backfiller_github_access_token.clone()
|
|
else {
|
|
log::info!("no USER_BACKFILLER_GITHUB_ACCESS_TOKEN set; not spawning user backfiller");
|
|
return;
|
|
};
|
|
|
|
let executor = app_state.executor.clone();
|
|
executor.spawn_detached({
|
|
let executor = executor.clone();
|
|
async move {
|
|
let user_backfiller = UserBackfiller::new(
|
|
app_state.config.clone(),
|
|
user_backfiller_github_access_token,
|
|
app_state.db.clone(),
|
|
executor,
|
|
);
|
|
|
|
log::info!("backfilling users");
|
|
|
|
user_backfiller
|
|
.backfill_github_user_created_at()
|
|
.await
|
|
.log_err();
|
|
}
|
|
});
|
|
}
|
|
|
|
const GITHUB_REQUESTS_PER_HOUR_LIMIT: usize = 5_000;
|
|
const SLEEP_DURATION_BETWEEN_USERS: std::time::Duration = std::time::Duration::from_millis(
|
|
(GITHUB_REQUESTS_PER_HOUR_LIMIT as f64 / 60. / 60. * 1000.) as u64,
|
|
);
|
|
|
|
struct UserBackfiller {
|
|
config: Config,
|
|
github_access_token: Arc<str>,
|
|
db: Arc<Database>,
|
|
http_client: reqwest::Client,
|
|
executor: Executor,
|
|
}
|
|
|
|
impl UserBackfiller {
|
|
fn new(
|
|
config: Config,
|
|
github_access_token: Arc<str>,
|
|
db: Arc<Database>,
|
|
executor: Executor,
|
|
) -> Self {
|
|
Self {
|
|
config,
|
|
github_access_token,
|
|
db,
|
|
http_client: reqwest::Client::new(),
|
|
executor,
|
|
}
|
|
}
|
|
|
|
async fn backfill_github_user_created_at(&self) -> Result<()> {
|
|
let initial_channel_id = self.config.auto_join_channel_id;
|
|
|
|
let users_missing_github_user_created_at =
|
|
self.db.get_users_missing_github_user_created_at().await?;
|
|
|
|
for user in users_missing_github_user_created_at {
|
|
match self
|
|
.fetch_github_user(&format!(
|
|
"https://api.github.com/user/{}",
|
|
user.github_user_id
|
|
))
|
|
.await
|
|
{
|
|
Ok(github_user) => {
|
|
self.db
|
|
.get_or_create_user_by_github_account(
|
|
&user.github_login,
|
|
github_user.id,
|
|
user.email_address.as_deref(),
|
|
user.name.as_deref(),
|
|
github_user.created_at,
|
|
initial_channel_id,
|
|
)
|
|
.await?;
|
|
|
|
log::info!("backfilled user: {}", user.github_login);
|
|
}
|
|
Err(err) => {
|
|
log::error!("failed to fetch GitHub user {}: {err}", user.github_login);
|
|
}
|
|
}
|
|
|
|
self.executor.sleep(SLEEP_DURATION_BETWEEN_USERS).await;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_github_user(&self, url: &str) -> Result<GithubUser> {
|
|
let response = self
|
|
.http_client
|
|
.get(url)
|
|
.header(
|
|
"authorization",
|
|
format!("Bearer {}", self.github_access_token),
|
|
)
|
|
.header("user-agent", "zed")
|
|
.send()
|
|
.await
|
|
.with_context(|| format!("failed to fetch '{url}'"))?;
|
|
|
|
let rate_limit_remaining = response
|
|
.headers()
|
|
.get("x-ratelimit-remaining")
|
|
.and_then(|value| value.to_str().ok())
|
|
.and_then(|value| value.parse::<i32>().ok());
|
|
let rate_limit_reset = response
|
|
.headers()
|
|
.get("x-ratelimit-reset")
|
|
.and_then(|value| value.to_str().ok())
|
|
.and_then(|value| value.parse::<i64>().ok())
|
|
.and_then(|value| DateTime::from_timestamp(value, 0));
|
|
|
|
if rate_limit_remaining == Some(0) {
|
|
if let Some(reset_at) = rate_limit_reset {
|
|
let now = Utc::now();
|
|
if reset_at > now {
|
|
let sleep_duration = reset_at - now;
|
|
log::info!(
|
|
"rate limit reached. Sleeping for {} seconds",
|
|
sleep_duration.num_seconds()
|
|
);
|
|
self.executor.sleep(sleep_duration.to_std().unwrap()).await;
|
|
}
|
|
}
|
|
}
|
|
|
|
let response = match response.error_for_status() {
|
|
Ok(response) => response,
|
|
Err(err) => return Err(anyhow!("failed to fetch GitHub user: {err}")),
|
|
};
|
|
|
|
response
|
|
.json()
|
|
.await
|
|
.with_context(|| format!("failed to deserialize GitHub user from '{url}'"))
|
|
}
|
|
}
|
|
|
|
#[derive(serde::Deserialize)]
|
|
struct GithubUser {
|
|
id: i32,
|
|
created_at: DateTime<Utc>,
|
|
name: Option<String>,
|
|
}
|