Skip to content

Commit da5c284

Browse files
committed
publish: Trigger OG image generation after version updates
1 parent 9996564 commit da5c284

File tree

9 files changed

+56
-3
lines changed

9 files changed

+56
-3
lines changed

src/controllers/krate/delete.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ mod tests {
253253
assert_snapshot!(app.stored_files().await.join("\n"), @r"
254254
crates/foo/foo-1.0.0.crate
255255
index/3/f/foo
256+
og-images/foo.png
256257
rss/crates.xml
257258
rss/crates/foo.xml
258259
rss/updates.xml
@@ -268,6 +269,7 @@ mod tests {
268269
assert_crate_exists(&anon, "foo", false).await;
269270
assert!(!upstream.crate_exists("foo")?);
270271
assert_snapshot!(app.stored_files().await.join("\n"), @r"
272+
og-images/foo.png
271273
rss/crates.xml
272274
rss/updates.xml
273275
");
@@ -290,6 +292,7 @@ mod tests {
290292
assert_snapshot!(app.stored_files().await.join("\n"), @r"
291293
crates/foo/foo-1.0.0.crate
292294
index/3/f/foo
295+
og-images/foo.png
293296
rss/crates.xml
294297
rss/crates/foo.xml
295298
rss/updates.xml
@@ -305,6 +308,7 @@ mod tests {
305308
assert_crate_exists(&anon, "foo", false).await;
306309
assert!(!upstream.crate_exists("foo")?);
307310
assert_snapshot!(app.stored_files().await.join("\n"), @r"
311+
og-images/foo.png
308312
rss/crates.xml
309313
rss/updates.xml
310314
");
@@ -327,6 +331,7 @@ mod tests {
327331
assert_snapshot!(app.stored_files().await.join("\n"), @r"
328332
crates/foo/foo-1.0.0.crate
329333
index/3/f/foo
334+
og-images/foo.png
330335
rss/crates.xml
331336
rss/crates/foo.xml
332337
rss/updates.xml
@@ -342,6 +347,7 @@ mod tests {
342347
assert_crate_exists(&anon, "foo", false).await;
343348
assert!(!upstream.crate_exists("foo")?);
344349
assert_snapshot!(app.stored_files().await.join("\n"), @r"
350+
og-images/foo.png
345351
rss/crates.xml
346352
rss/updates.xml
347353
");

src/controllers/krate/publish.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::app::AppState;
44
use crate::auth::{AuthCheck, AuthHeader, Authentication};
55
use crate::worker::jobs::{
6-
self, CheckTyposquat, SendPublishNotificationsJob, UpdateDefaultVersion,
6+
self, CheckTyposquat, GenerateOgImage, SendPublishNotificationsJob, UpdateDefaultVersion,
77
};
88
use axum::Json;
99
use axum::body::{Body, Bytes};
@@ -549,14 +549,14 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
549549
// Compared to only using a background job, this prevents us from getting into a
550550
// situation where a crate exists in the `crates` table but doesn't have a default
551551
// version in the `default_versions` table.
552-
if let Some((existing_default_version, _)) = existing_default_version {
552+
if let Some((existing_default_version, _)) = &existing_default_version {
553553
let published_default_version = DefaultVersion {
554554
id: version.id,
555555
num: semver,
556556
yanked: false,
557557
};
558558

559-
if existing_default_version < published_default_version {
559+
if existing_default_version < &published_default_version {
560560
diesel::update(default_versions::table)
561561
.filter(default_versions::crate_id.eq(krate.id))
562562
.set(default_versions::version_id.eq(version.id))
@@ -631,6 +631,14 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
631631
}),
632632
)?;
633633

634+
// Enqueue OG image generation job if not handled by UpdateDefaultVersion
635+
if existing_default_version.is_none() {
636+
let og_image_job = GenerateOgImage::new(krate.name.clone());
637+
if let Err(error) = og_image_job.enqueue(conn).await {
638+
error!("Failed to enqueue `GenerateOgImage` job: {error}");
639+
}
640+
};
641+
634642
// Experiment: check new crates for potential typosquatting.
635643
if existing_crate.is_none() {
636644
let crates_feed_job = jobs::rss::SyncCratesFeed;

src/tests/krate/publish/auth.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ async fn new_krate_with_bearer_token() {
7373
assert_snapshot!(app.stored_files().await.join("\n"), @r"
7474
crates/foo_new/foo_new-1.0.0.crate
7575
index/fo/o_/foo_new
76+
og-images/foo_new.png
7677
rss/crates.xml
7778
rss/crates/foo_new.xml
7879
rss/updates.xml

src/tests/krate/publish/basics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async fn new_krate() {
2525
assert_snapshot!(app.stored_files().await.join("\n"), @r"
2626
crates/foo_new/foo_new-1.0.0.crate
2727
index/fo/o_/foo_new
28+
og-images/foo_new.png
2829
rss/crates.xml
2930
rss/crates/foo_new.xml
3031
rss/updates.xml
@@ -55,6 +56,7 @@ async fn new_krate_with_token() {
5556
assert_snapshot!(app.stored_files().await.join("\n"), @r"
5657
crates/foo_new/foo_new-1.0.0.crate
5758
index/fo/o_/foo_new
59+
og-images/foo_new.png
5860
rss/crates.xml
5961
rss/crates/foo_new.xml
6062
rss/updates.xml
@@ -76,6 +78,7 @@ async fn new_krate_weird_version() {
7678
assert_snapshot!(app.stored_files().await.join("\n"), @r"
7779
crates/foo_weird/foo_weird-0.0.0-pre.crate
7880
index/fo/o_/foo_weird
81+
og-images/foo_weird.png
7982
rss/crates.xml
8083
rss/crates/foo_weird.xml
8184
rss/updates.xml
@@ -105,6 +108,7 @@ async fn new_krate_twice() {
105108
crates/foo_twice/foo_twice-0.99.0.crate
106109
crates/foo_twice/foo_twice-2.0.0.crate
107110
index/fo/o_/foo_twice
111+
og-images/foo_twice.png
108112
rss/crates.xml
109113
rss/crates/foo_twice.xml
110114
rss/updates.xml
@@ -136,6 +140,7 @@ async fn new_krate_twice_alt() {
136140
crates/foo_twice/foo_twice-0.99.0.crate
137141
crates/foo_twice/foo_twice-2.0.0.crate
138142
index/fo/o_/foo_twice
143+
og-images/foo_twice.png
139144
rss/crates.xml
140145
rss/crates/foo_twice.xml
141146
rss/updates.xml

src/tests/krate/publish/git.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async fn new_krate_git_upload_with_conflicts() {
1414
assert_snapshot!(app.stored_files().await.join("\n"), @r"
1515
crates/foo_conflicts/foo_conflicts-1.0.0.crate
1616
index/fo/o_/foo_conflicts
17+
og-images/foo_conflicts.png
1718
rss/crates.xml
1819
rss/crates/foo_conflicts.xml
1920
rss/updates.xml

src/tests/krate/publish/max_size.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async fn tarball_between_default_axum_limit_and_max_upload_size() {
5353
assert_snapshot!(app.stored_files().await.join("\n"), @r"
5454
crates/foo/foo-1.1.0.crate
5555
index/3/f/foo
56+
og-images/foo.png
5657
rss/crates.xml
5758
rss/crates/foo.xml
5859
rss/updates.xml
@@ -151,6 +152,7 @@ async fn new_krate_too_big_but_whitelisted() {
151152
assert_snapshot!(app.stored_files().await.join("\n"), @r"
152153
crates/foo_whitelist/foo_whitelist-1.1.0.crate
153154
index/fo/o_/foo_whitelist
155+
og-images/foo_whitelist.png
154156
rss/crates/foo_whitelist.xml
155157
rss/updates.xml
156158
");

src/tests/krate/publish/rate_limit.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async fn publish_new_crate_ratelimit_expires() {
7474
assert_snapshot!(app.stored_files().await.join("\n"), @r"
7575
crates/rate_limited/rate_limited-1.0.0.crate
7676
index/ra/te/rate_limited
77+
og-images/rate_limited.png
7778
rss/crates.xml
7879
rss/crates/rate_limited.xml
7980
rss/updates.xml
@@ -115,6 +116,7 @@ async fn publish_new_crate_override_loosens_ratelimit() {
115116
assert_snapshot!(app.stored_files().await.join("\n"), @r"
116117
crates/rate_limited1/rate_limited1-1.0.0.crate
117118
index/ra/te/rate_limited1
119+
og-images/rate_limited1.png
118120
rss/crates.xml
119121
rss/crates/rate_limited1.xml
120122
rss/updates.xml
@@ -131,6 +133,8 @@ async fn publish_new_crate_override_loosens_ratelimit() {
131133
crates/rate_limited2/rate_limited2-1.0.0.crate
132134
index/ra/te/rate_limited1
133135
index/ra/te/rate_limited2
136+
og-images/rate_limited1.png
137+
og-images/rate_limited2.png
134138
rss/crates.xml
135139
rss/crates/rate_limited1.xml
136140
rss/crates/rate_limited2.xml
@@ -151,6 +155,8 @@ async fn publish_new_crate_override_loosens_ratelimit() {
151155
crates/rate_limited2/rate_limited2-1.0.0.crate
152156
index/ra/te/rate_limited1
153157
index/ra/te/rate_limited2
158+
og-images/rate_limited1.png
159+
og-images/rate_limited2.png
154160
rss/crates.xml
155161
rss/crates/rate_limited1.xml
156162
rss/crates/rate_limited2.xml
@@ -194,6 +200,7 @@ async fn publish_new_crate_expired_override_ignored() {
194200
assert_snapshot!(app.stored_files().await.join("\n"), @r"
195201
crates/rate_limited1/rate_limited1-1.0.0.crate
196202
index/ra/te/rate_limited1
203+
og-images/rate_limited1.png
197204
rss/crates.xml
198205
rss/crates/rate_limited1.xml
199206
rss/updates.xml
@@ -211,6 +218,7 @@ async fn publish_new_crate_expired_override_ignored() {
211218
assert_snapshot!(app.stored_files().await.join("\n"), @r"
212219
crates/rate_limited1/rate_limited1-1.0.0.crate
213220
index/ra/te/rate_limited1
221+
og-images/rate_limited1.png
214222
rss/crates.xml
215223
rss/crates/rate_limited1.xml
216224
rss/updates.xml
@@ -253,6 +261,7 @@ async fn publish_existing_crate_rate_limited() {
253261
assert_snapshot!(app.stored_files().await.join("\n"), @r"
254262
crates/rate_limited1/rate_limited1-1.0.0.crate
255263
index/ra/te/rate_limited1
264+
og-images/rate_limited1.png
256265
rss/crates.xml
257266
rss/crates/rate_limited1.xml
258267
rss/updates.xml
@@ -268,6 +277,7 @@ async fn publish_existing_crate_rate_limited() {
268277
crates/rate_limited1/rate_limited1-1.0.0.crate
269278
crates/rate_limited1/rate_limited1-1.0.1.crate
270279
index/ra/te/rate_limited1
280+
og-images/rate_limited1.png
271281
rss/crates.xml
272282
rss/crates/rate_limited1.xml
273283
rss/updates.xml
@@ -287,6 +297,7 @@ async fn publish_existing_crate_rate_limited() {
287297
crates/rate_limited1/rate_limited1-1.0.0.crate
288298
crates/rate_limited1/rate_limited1-1.0.1.crate
289299
index/ra/te/rate_limited1
300+
og-images/rate_limited1.png
290301
rss/crates.xml
291302
rss/crates/rate_limited1.xml
292303
rss/updates.xml
@@ -314,6 +325,7 @@ async fn publish_existing_crate_rate_limited() {
314325
crates/rate_limited1/rate_limited1-1.0.1.crate
315326
crates/rate_limited1/rate_limited1-1.0.2.crate
316327
index/ra/te/rate_limited1
328+
og-images/rate_limited1.png
317329
rss/crates.xml
318330
rss/crates/rate_limited1.xml
319331
rss/updates.xml

src/tests/krate/publish/readme.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async fn new_krate_with_readme() {
1717
assert_snapshot!(app.stored_files().await.join("\n"), @r"
1818
crates/foo_readme/foo_readme-1.0.0.crate
1919
index/fo/o_/foo_readme
20+
og-images/foo_readme.png
2021
readmes/foo_readme/foo_readme-1.0.0.html
2122
rss/crates.xml
2223
rss/crates/foo_readme.xml
@@ -39,6 +40,7 @@ async fn new_krate_with_empty_readme() {
3940
assert_snapshot!(app.stored_files().await.join("\n"), @r"
4041
crates/foo_readme/foo_readme-1.0.0.crate
4142
index/fo/o_/foo_readme
43+
og-images/foo_readme.png
4244
rss/crates.xml
4345
rss/crates/foo_readme.xml
4446
rss/updates.xml
@@ -60,6 +62,7 @@ async fn new_krate_with_readme_and_plus_version() {
6062
assert_snapshot!(app.stored_files().await.join("\n"), @r"
6163
crates/foo_readme/foo_readme-1.0.0+foo.crate
6264
index/fo/o_/foo_readme
65+
og-images/foo_readme.png
6366
readmes/foo_readme/foo_readme-1.0.0+foo.html
6467
rss/crates.xml
6568
rss/crates/foo_readme.xml

src/worker/jobs/update_default_version.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::models::update_default_version;
2+
use crate::schema::crates;
23
use crate::worker::Environment;
4+
use crate::worker::jobs::GenerateOgImage;
35
use crates_io_worker::BackgroundJob;
6+
use diesel::prelude::*;
7+
use diesel_async::RunQueryDsl;
48
use serde::{Deserialize, Serialize};
59
use std::sync::Arc;
610
use tracing::info;
@@ -30,6 +34,17 @@ impl BackgroundJob for UpdateDefaultVersion {
3034
let mut conn = ctx.deadpool.get().await?;
3135
update_default_version(crate_id, &mut conn).await?;
3236

37+
// Get the crate name for OG image generation
38+
let crate_name: String = crates::table
39+
.filter(crates::id.eq(crate_id))
40+
.select(crates::name)
41+
.first(&mut conn)
42+
.await?;
43+
44+
// Generate OG image after updating default version
45+
info!("Enqueueing OG image generation for crate {crate_name}");
46+
GenerateOgImage::new(crate_name).enqueue(&mut conn).await?;
47+
3348
Ok(())
3449
}
3550
}

0 commit comments

Comments
 (0)