Skip to content

Commit

Permalink
✨ Add follow feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 23, 2024
1 parent 2212235 commit a64c1d8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
21 changes: 20 additions & 1 deletion packages/society/src/components/followable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,24 @@ mod FollowableComponent {
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {}
> of InternalTrait<TContractState> {
fn follow(self: @ComponentState<TContractState>, world: WorldStorage, followed: felt252) {
// [Setup] Datastore
let mut store = StoreTrait::new(world);

// [Effect] Follow
let follower = starknet::get_caller_address().into();
let time = starknet::get_block_timestamp();
store.follow(follower, followed, time);
}

fn unfollow(self: @ComponentState<TContractState>, world: WorldStorage, followed: felt252) {
// [Setup] Datastore
let mut store = StoreTrait::new(world);

// [Effect] Unfollow
let follower = starknet::get_caller_address().into();
store.unfollow(follower, followed);
}
}
}
72 changes: 72 additions & 0 deletions packages/society/src/events/follow.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Internal imports

use society::events::index::Follow;

// Errors

pub mod errors {
pub const FOLLOW_INVALID_FOLLOWER: felt252 = 'Follow: invalid follower';
pub const FOLLOW_INVALID_FOLLOWED: felt252 = 'Follow: invalid followed';
}

// Implementations

#[generate_trait]
impl FollowImpl of FollowTrait {
#[inline]
fn new(follower: felt252, followed: felt252, time: u64,) -> Follow {
// [Check] Inputs
// [Info] We don't check points here, leave free the game to decide
FollowAssert::assert_valid_follower(follower);
FollowAssert::assert_valid_followed(followed);
// [Return] Follow
Follow { follower, followed, time }
}
}

#[generate_trait]
impl FollowAssert of AssertTrait {
#[inline]
fn assert_valid_follower(follower: felt252) {
assert(follower != 0, errors::FOLLOW_INVALID_FOLLOWER);
}

#[inline]
fn assert_valid_followed(followed: felt252) {
assert(followed != 0, errors::FOLLOW_INVALID_FOLLOWED);
}
}

#[cfg(test)]
mod tests {
// Local imports

use super::{Follow, FollowTrait};

// Constants

const FOLLOWER: felt252 = 'FOLLOWER';
const FOLLOWED: felt252 = 'FOLLOWED';
const TIME: u64 = 100;

#[test]
fn test_follow_new() {
let follow = FollowTrait::new(FOLLOWER, FOLLOWED, TIME);
assert_eq!(follow.follower, FOLLOWER);
assert_eq!(follow.followed, FOLLOWED);
assert_eq!(follow.time, TIME);
}

#[test]
#[should_panic(expected: ('Follow: invalid follower',))]
fn test_follow_new_invalid_follower() {
FollowTrait::new(0, FOLLOWED, TIME);
}

#[test]
#[should_panic(expected: ('Follow: invalid followed',))]
fn test_follow_new_invalid_followed() {
FollowTrait::new(FOLLOWER, 0, TIME);
}
}

4 changes: 2 additions & 2 deletions packages/society/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[dojo::event]
pub struct Follow {
#[key]
account_id: felt252,
follower_id: felt252,
follower: felt252,
followed: felt252,
time: u64,
}
5 changes: 5 additions & 0 deletions packages/society/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ mod types {
mod role;
}

mod events {
mod index;
mod follow;
}

mod models {
mod index;
mod member;
Expand Down
15 changes: 14 additions & 1 deletion packages/society/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use starknet::SyscallResultTrait;

use dojo::world::WorldStorage;
use dojo::model::ModelStorage;
use dojo::event::EventStorage;

// Models imports

use society::models::alliance::Alliance;
use society::models::guild::Guild;
use society::models::member::Member;

use society::events::follow::{Follow, FollowTrait};

// Structs

Expand Down Expand Up @@ -61,4 +62,16 @@ impl StoreImpl of StoreTrait {
fn set_member(ref self: Store, member: @Member) {
self.world.write_model(member);
}

#[inline]
fn follow(ref self: Store, follower: felt252, followed: felt252, time: u64) {
let event = FollowTrait::new(follower, followed, time);
self.world.emit_event(@event);
}

#[inline]
fn unfollow(ref self: Store, follower: felt252, followed: felt252) {
let event = FollowTrait::new(follower, followed, 0);
self.world.emit_event(@event);
}
}

0 comments on commit a64c1d8

Please sign in to comment.