Skip to content

Commit c4728c6

Browse files
committed
feat: policy changes API
chore: bump version to 0.8.7
1 parent 8153f5f commit c4728c6

File tree

33 files changed

+298
-64
lines changed

33 files changed

+298
-64
lines changed

Cargo.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bonfire/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "revolt-bonfire"
3-
version = "0.8.6"
3+
version = "0.8.7"
44
license = "AGPL-3.0-or-later"
55
edition = "2021"
66

@@ -41,7 +41,7 @@ revolt-result = { path = "../core/result" }
4141
revolt-models = { path = "../core/models" }
4242
revolt-config = { path = "../core/config" }
4343
revolt-database = { path = "../core/database" }
44-
revolt-permissions = { version = "0.8.6", path = "../core/permissions" }
44+
revolt-permissions = { version = "0.8.7", path = "../core/permissions" }
4545
revolt-presence = { path = "../core/presence", features = ["redis-is-patched"] }
4646

4747
# redis

crates/bonfire/src/events/impl.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ impl State {
100100
let user = self.clone_user();
101101
self.cache.is_bot = user.bot.is_some();
102102

103+
// Fetch pending policy changes.
104+
let policy_changes = if user.bot.is_some() {
105+
vec![]
106+
} else {
107+
db.fetch_policy_changes()
108+
.await?
109+
.into_iter()
110+
.filter(|policy| policy.created_time > user.last_acknowledged_policy_change)
111+
.map(Into::into)
112+
.collect()
113+
};
114+
103115
// Find all relationships to the user.
104116
let mut user_ids: HashSet<String> = user
105117
.relations
@@ -227,6 +239,7 @@ impl State {
227239
for channel in &channels {
228240
self.insert_subscription(channel.id().to_string()).await;
229241
}
242+
230243
Ok(EventV1::Ready {
231244
users: if fields.contains(&ReadyPayloadFields::Users) {
232245
Some(users)
@@ -252,6 +265,8 @@ impl State {
252265

253266
user_settings,
254267
channel_unreads: channel_unreads.map(|vec| vec.into_iter().map(Into::into).collect()),
268+
269+
policy_changes,
255270
})
256271
}
257272

crates/core/config/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "revolt-config"
3-
version = "0.8.6"
3+
version = "0.8.7"
44
edition = "2021"
55
license = "MIT"
66
authors = ["Paul Makles <[email protected]>"]
@@ -36,4 +36,4 @@ sentry = "0.31.5"
3636
sentry-anyhow = { version = "0.38.1", optional = true }
3737

3838
# Core
39-
revolt-result = { version = "0.8.6", path = "../result", optional = true }
39+
revolt-result = { version = "0.8.7", path = "../result", optional = true }

crates/core/database/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "revolt-database"
3-
version = "0.8.6"
3+
version = "0.8.7"
44
edition = "2021"
55
license = "AGPL-3.0-or-later"
66
authors = ["Paul Makles <[email protected]>"]
@@ -24,19 +24,19 @@ default = ["mongodb", "async-std-runtime", "tasks"]
2424

2525
[dependencies]
2626
# Core
27-
revolt-config = { version = "0.8.6", path = "../config", features = [
27+
revolt-config = { version = "0.8.7", path = "../config", features = [
2828
"report-macros",
2929
] }
30-
revolt-result = { version = "0.8.6", path = "../result" }
31-
revolt-models = { version = "0.8.6", path = "../models", features = [
30+
revolt-result = { version = "0.8.7", path = "../result" }
31+
revolt-models = { version = "0.8.7", path = "../models", features = [
3232
"validator",
3333
] }
34-
revolt-presence = { version = "0.8.6", path = "../presence" }
35-
revolt-permissions = { version = "0.8.6", path = "../permissions", features = [
34+
revolt-presence = { version = "0.8.7", path = "../presence" }
35+
revolt-permissions = { version = "0.8.7", path = "../permissions", features = [
3636
"serde",
3737
"bson",
3838
] }
39-
revolt-parser = { version = "0.8.6", path = "../parser" }
39+
revolt-parser = { version = "0.8.7", path = "../parser" }
4040

4141
# Utility
4242
log = "0.4"

crates/core/database/src/drivers/reference.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use futures::lock::Mutex;
44

55
use crate::{
66
Bot, Channel, ChannelCompositeKey, ChannelUnread, Emoji, File, FileHash, Invite, Member,
7-
MemberCompositeKey, Message, RatelimitEvent, Report, Server, ServerBan, Snapshot, User,
8-
UserSettings, Webhook,
7+
MemberCompositeKey, Message, PolicyChange, RatelimitEvent, Report, Server, ServerBan, Snapshot,
8+
User, UserSettings, Webhook,
99
};
1010

1111
database_derived!(
@@ -21,6 +21,7 @@ database_derived!(
2121
pub file_hashes: Arc<Mutex<HashMap<String, FileHash>>>,
2222
pub files: Arc<Mutex<HashMap<String, File>>>,
2323
pub messages: Arc<Mutex<HashMap<String, Message>>>,
24+
pub policy_changes: Arc<Mutex<HashMap<String, PolicyChange>>>,
2425
pub ratelimit_events: Arc<Mutex<HashMap<String, RatelimitEvent>>>,
2526
pub user_settings: Arc<Mutex<HashMap<String, UserSettings>>>,
2627
pub users: Arc<Mutex<HashMap<String, User>>>,

crates/core/database/src/events/client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use revolt_models::v0::{
66
AppendMessage, Channel, ChannelUnread, Emoji, FieldsChannel, FieldsMember, FieldsMessage,
77
FieldsRole, FieldsServer, FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message,
88
PartialChannel, PartialMember, PartialMessage, PartialRole, PartialServer, PartialUser,
9-
PartialWebhook, RemovalIntention, Report, Server, User, UserSettings, Webhook,
9+
PartialWebhook, PolicyChange, RemovalIntention, Report, Server, User, UserSettings, Webhook,
1010
};
1111

1212
use crate::Database;
@@ -62,6 +62,8 @@ pub enum EventV1 {
6262
user_settings: Option<UserSettings>,
6363
#[serde(skip_serializing_if = "Option::is_none")]
6464
channel_unreads: Option<Vec<ChannelUnread>>,
65+
66+
policy_changes: Vec<PolicyChange>,
6567
},
6668

6769
/// Ping response

crates/core/database/src/models/admin_migrations/ops/mongodb/init.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ pub async fn create_database(db: &MongoDb) {
6464
.await
6565
.expect("Failed to create user_settings collection.");
6666

67+
db.create_collection("policy_changes")
68+
.await
69+
.expect("Failed to create policy_changes collection.");
70+
6771
db.create_collection("safety_reports")
6872
.await
6973
.expect("Failed to create safety_reports collection.");

crates/core/database/src/models/admin_migrations/ops/mongodb/scripts.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::{
55
bson::{doc, from_bson, from_document, to_document, Bson, DateTime, Document},
66
options::FindOptions,
77
},
8-
AbstractChannels, AbstractServers, Channel, Invite, MongoDb, DISCRIMINATOR_SEARCH_SPACE,
8+
AbstractChannels, AbstractServers, Channel, Invite, MongoDb, User, DISCRIMINATOR_SEARCH_SPACE,
99
};
10-
use bson::oid::ObjectId;
10+
use bson::{oid::ObjectId, to_bson};
1111
use futures::StreamExt;
12+
use iso8601_timestamp::Timestamp;
1213
use rand::seq::SliceRandom;
1314
use revolt_permissions::DEFAULT_WEBHOOK_PERMISSIONS;
1415
use revolt_result::{Error, ErrorType};
@@ -21,7 +22,7 @@ struct MigrationInfo {
2122
revision: i32,
2223
}
2324

24-
pub const LATEST_REVISION: i32 = 33; // MUST BE +1 to last migration
25+
pub const LATEST_REVISION: i32 = 41; // MUST BE +1 to last migration
2526

2627
pub async fn migrate_database(db: &MongoDb) {
2728
let migrations = db.col::<Document>("migrations");
@@ -1139,6 +1140,31 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
11391140
.unwrap();
11401141
}
11411142

1143+
if revision <= 40 {
1144+
info!(
1145+
"Running migration [revision |> 40 / 30-05-2025]: Set last policy acknowlegement date to now and create policy changes collection."
1146+
);
1147+
1148+
db.db()
1149+
.create_collection("policy_changes")
1150+
.await
1151+
.expect("Failed to create policy_changes collection.");
1152+
1153+
db.db()
1154+
.collection::<User>("users")
1155+
.update_many(
1156+
doc! {},
1157+
doc! {
1158+
"$set": {
1159+
"last_acknowledged_policy_change": to_bson(&Timestamp::now_utc())
1160+
.expect("failed to serialise timestamp")
1161+
}
1162+
},
1163+
)
1164+
.await
1165+
.expect("failed to update users");
1166+
}
1167+
11421168
// Reminder to update LATEST_REVISION when adding new migrations.
11431169
LATEST_REVISION.max(revision)
11441170
}

crates/core/database/src/models/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod emojis;
88
mod file_hashes;
99
mod files;
1010
mod messages;
11+
mod policy_changes;
1112
mod ratelimit_events;
1213
mod safety_reports;
1314
mod safety_snapshots;
@@ -27,6 +28,7 @@ pub use emojis::*;
2728
pub use file_hashes::*;
2829
pub use files::*;
2930
pub use messages::*;
31+
pub use policy_changes::*;
3032
pub use ratelimit_events::*;
3133
pub use safety_reports::*;
3234
pub use safety_snapshots::*;
@@ -51,6 +53,7 @@ pub trait AbstractDatabase:
5153
+ file_hashes::AbstractAttachmentHashes
5254
+ files::AbstractAttachments
5355
+ messages::AbstractMessages
56+
+ policy_changes::AbstractPolicyChange
5457
+ ratelimit_events::AbstractRatelimitEvents
5558
+ safety_reports::AbstractReport
5659
+ safety_snapshots::AbstractSnapshot
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod model;
2+
mod ops;
3+
4+
pub use model::*;
5+
pub use ops::*;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use iso8601_timestamp::Timestamp;
2+
3+
auto_derived!(
4+
/// Platform policy change
5+
pub struct PolicyChange {
6+
/// Unique Id
7+
#[serde(rename = "_id")]
8+
pub id: String,
9+
10+
/// Time at which this policy was created
11+
pub created_time: Timestamp,
12+
/// Time at which this policy is effective
13+
pub effective_time: Timestamp,
14+
15+
/// Message shown to users
16+
pub description: String,
17+
/// URL with details about changes
18+
pub url: String,
19+
}
20+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use revolt_result::Result;
2+
3+
use crate::PolicyChange;
4+
5+
mod mongodb;
6+
mod reference;
7+
8+
#[async_trait]
9+
pub trait AbstractPolicyChange: Sync + Send {
10+
/// Fetch all policy changes
11+
async fn fetch_policy_changes(&self) -> Result<Vec<PolicyChange>>;
12+
13+
/// Acknowledge policy changes
14+
async fn acknowledge_policy_changes(&self, user_id: &str) -> Result<()>;
15+
}

0 commit comments

Comments
 (0)