Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/backend/env/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use super::{
State,
};

const STATUS_IMPLEMENTED: u8 = 1;
const STATUS_OPEN: u8 = 0;

#[derive(Default, Serialize, Deserialize)]
pub struct Feature {
// Users who voted for increased priority
Expand All @@ -33,7 +36,7 @@ pub fn features<'a>(
now: Time,
) -> Box<dyn DoubleEndedIterator<Item = ((&'a Post, Meta<'a>), Token, Feature)> + 'a> {
let transform_feature = move |(post_id, feature): (&PostId, Feature)| {
if feature.last_activity + YEAR <= now {
if feature.status == STATUS_OPEN && feature.last_activity + YEAR <= now {
return None;
}
let tokens = feature
Expand Down Expand Up @@ -101,7 +104,7 @@ pub fn create_feature(caller: Principal, post_id: PostId, now: Time) -> Result<(
post_id,
Feature {
supporters: Default::default(),
status: 0,
status: STATUS_OPEN,
last_activity: now,
},
)
Expand All @@ -121,7 +124,7 @@ pub fn create_feature(caller: Principal, post_id: PostId, now: Time) -> Result<(

pub fn close_feature(state: &mut State, post_id: PostId) -> Result<(), String> {
let mut feature = state.memory.features.remove(&post_id)?;
feature.status = 1;
feature.status = STATUS_IMPLEMENTED;
state
.memory
.features
Expand Down
40 changes: 27 additions & 13 deletions src/backend/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,33 @@ impl State {
return Err("seed too long".into());
}

let user = self.principal_to_user_mut(caller).ok_or("user not found")?;
user.change_credits(
CONFIG.feature_cost,
CreditsDelta::Minus,
"profile privacy change",
)?;
let len = user.posts.len();
let (username, len, is_deactivated) = {
let user = self.principal_to_user_mut(caller).ok_or("user not found")?;
user.change_credits(
CONFIG.feature_cost,
CreditsDelta::Minus,
"profile privacy change",
)?;
let len = user.posts.len();
let username = user.name.clone();
user.deactivated = !user.deactivated;
let is_deactivated = user.deactivated;

user.deactivated = !user.deactivated;
for post_id in user.posts.clone() {
Post::crypt(self, post_id, &seed);
}

for post_id in user.posts.clone() {
Post::crypt(self, post_id, &seed);
}
(username, len, is_deactivated)
};

let _ = self.system_message(
if is_deactivated {
format!("Account @{} was deactivated. 😢", username)
} else {
format!("Account @{} is active again! 🎉", username)
},
CONFIG.dao_realm.into(),
);

Ok(len)
}
Expand Down Expand Up @@ -802,7 +816,7 @@ impl State {
let tipper_id = tipper.id;
let tipper_name = tipper.name.clone();
// DoS protection
self.charge(tipper_id, CONFIG.tipping_cost, "tipping".to_string())?; // DoS protection
self.charge(tipper_id, CONFIG.tipping_cost, "tipping".to_string())?;
let author_id = Post::get(self, &post_id).ok_or("post not found")?.user;
let author = self.users.get(&author_id).ok_or("user not found")?;
token::transfer(
Expand Down Expand Up @@ -863,7 +877,7 @@ impl State {
self.set_pfp(id, Default::default())
.expect("couldn't set default pfp");
let _ = self.system_message(
format!("`@{}` joined {}!", name, CONFIG.name),
format!("@{} joined {}!", name, CONFIG.name),
CONFIG.dao_realm.into(),
);
Ok(id)
Expand Down
6 changes: 5 additions & 1 deletion src/backend/env/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ impl Post {
let user_id = user.id;
let controversial = user.controversial();
let user_balance = user.balance;
let is_organic = user.organic();
let tags = tags(CONFIG.max_tag_length, &body).collect::<BTreeSet<_>>();
let mut post = Post::new(
user_id,
Expand Down Expand Up @@ -662,7 +663,10 @@ impl Post {
_ => (),
};

notify_about(state, &post);
// Don't notify about system messages
if is_organic {
notify_about(state, &post);
}

if post.parent.is_none() {
state.root_posts_index.push(post.id);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/env/realms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn create_realm(

let _ = state.system_message(
format!(
"Realm [{}](/#/realm/{0}) was created by `@{}`",
"Realm [{}](/#/realm/{0}) was created by @{}",
realm_id, user_name
),
CONFIG.dao_realm.into(),
Expand Down
13 changes: 12 additions & 1 deletion src/backend/env/tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,21 @@ fn memo_to_u64(memo: Vec<u8>) -> Result<u64, String> {

pub async fn add_tip(
post_id: PostId,
canister_id: Principal,
canister_id: String,
caller: Principal,
start_index: u64,
) -> Result<Tip, String> {
mutate(|state| match state.principal_to_user(caller) {
Some(user) => {
// DoS protection
state.charge(user.id, CONFIG.tipping_cost, "tipping".to_string())?;
Ok::<(), String>(())
}
_ => Err("user not found".into()),
})?;

let canister_id =
Principal::from_text(&canister_id).map_err(|_| "invalid canister id".to_string())?;
match try_tip(post_id, canister_id, caller, start_index).await {
Ok(tip) => Ok(tip),
Err(e) => {
Expand Down
12 changes: 5 additions & 7 deletions src/backend/env/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,12 @@ impl User {
if *revenue > 0 && credits_needed > 0 {
let e8s_needed = credits_needed * e8s_for_one_xdr / CONFIG.credits_per_xdr;
let top_up_e8s = e8s_needed.min(*revenue);
let credits =
top_up_e8s as f32 / e8s_for_one_xdr as f32 * CONFIG.credits_per_xdr as f32;
let credits = (top_up_e8s as f32 / e8s_for_one_xdr as f32
* CONFIG.credits_per_xdr as f32) as Credits;
*revenue = (*revenue).saturating_sub(top_up_e8s);
self.change_credits(
credits as Credits,
CreditsDelta::Plus,
"credits top-up from revenue",
)?;
if credits > 0 {
self.change_credits(credits, CreditsDelta::Plus, "credits top-up from revenue")?;
}
}
Ok(())
}
Expand Down
43 changes: 2 additions & 41 deletions src/backend/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,44 +109,7 @@ fn post_upgrade() {
}

#[allow(clippy::all)]
fn sync_post_upgrade_fixtures() {
mutate(|state| {
// Update last_activity for features
let ids = state
.memory
.features
.iter()
.map(|(id, _)| id)
.cloned()
.collect::<Vec<_>>();
for id in ids.into_iter() {
let mut feature = state.memory.features.remove(&id).unwrap();
let post = Post::get(state, &id).expect("post not found for feature");
feature.last_activity = post.timestamp();
state
.memory
.features
.insert(id, feature)
.expect("couldn't re-insert feature");
}

// Cleanup old logs
state.logger.clean_up(time() - MONTH * 6);

// Clear feeds if they exceed 200 chars in total
for u in state.users.values_mut() {
if u.feeds
.iter()
.flat_map(|feed| feed.iter())
.map(|tag| tag.len())
.sum::<usize>()
>= 200
{
u.feeds.clear();
}
}
});
}
fn sync_post_upgrade_fixtures() {}

#[allow(clippy::all)]
async fn async_post_upgrade_fixtures() {}
Expand Down Expand Up @@ -745,9 +708,7 @@ fn cancel_bid() {

#[export_name = "canister_update add_external_icrc_transaction"]
fn add_external_icrc_transaction() {
let (canister_id_as_str, start_index, post_id): (String, u64, PostId) = parse(&arg_data_raw());

let canister_id = Principal::from_text(canister_id_as_str).unwrap();
let (canister_id, start_index, post_id): (String, u64, PostId) = parse(&arg_data_raw());
spawn(async move { reply(add_tip(post_id, canister_id, read(caller), start_index).await) });
}

Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ export const PostView = ({
);

const isComment = post ? !isRoot(post) : false;
const expanded =
isComment || focused || (!isFeedItem && !repost) || isThreadView;
const expanded = focused || isThreadView || prime;
const costTable = reactionCosts();
const isInactive = React.useMemo(
() =>
Expand Down
12 changes: 7 additions & 5 deletions src/frontend/src/realms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -791,29 +791,31 @@ export const RealmHeader = ({
)
)
return;
return window.api
await window.api
.call(
"toggle_realm_membership",
name,
)
.then(window.reloadUser)
.then(loadRealm);
location.href = `#/realm/${name}`;
}}
/>
)}
{user.realms.includes(name) && (
<ButtonWithLoading
classNameArg="active"
label="LEAVE"
onClick={async () =>
window.api
onClick={async () => {
await window.api
.call(
"toggle_realm_membership",
name,
)
.then(window.reloadUser)
.then(loadRealm)
}
.then(loadRealm);
location.href = `#/home`;
}}
/>
)}
</div>
Expand Down