The contract I made this week is a decentralized social media platform where users can post tweets, send messages, follow other users, and manage operators. Below is a breakdown of its key components:
Here is the Github link : Click Me
1. Data Structures
Tweet Struct: Stores information about a tweet including its id, author, content, and createdAt timestamp.
Message Struct: Stores details of a message including its id, content, sender (from), receiver (to), and createdAt timestamp.
2. Mappings
tweets: Maps tweet id to the Tweet struct, storing all tweets by their unique id.
tweetsOf: Maps an address (user) to an array of tweet IDs, indicating which tweets were authored by that user.
conversations: Maps an address (user) to an array of Message structs, representing the messages sent or received by that user.
operators: Maps a user address to another address and a boolean value to indicate if the latter address is allowed to tweet or send messages on behalf of the former.
following: Maps a user address to an array of addresses representing the accounts that the user is following.
3. State Variables
nextId: Tracks the ID for the next tweet.
nextMessageId: Tracks the ID for the next message.
4. Internal Functions
_tweet(address _from, string memory _content): Handles the creation of a tweet, ensuring the sender has permission. Adds the tweet to the tweets mapping and updates tweetsOf.
_sendMessage(address _from, address _to, string memory _content): Handles sending a message, ensuring the sender has permission. Adds the message to the conversations mapping.
5. Public Functions
tweet(string memory _content): Creates a tweet from msg.sender.
tweet(address _from, string memory _content): Allows an authorized operator to create a tweet on behalf of another user.
sendMessage(string memory _content, address _to): Sends a message from msg.sender to _to.
sendMessage(address _from, address _to, string memory _content): Allows an authorized operator to send a message on behalf of another user.
follow(address _followed): Allows msg.sender to follow another user.
allow(address _operator): Grants another address permission to tweet or message on behalf of msg.sender.
disallow(address _operator): Revokes the permission granted to an operator.
getLatestTweets(uint count): Returns the latest count tweets. Ensures count is valid and retrieves tweets starting from the most recent.
6. Security Considerations
Access Control: The _tweet and _sendMessage functions require that either the caller is the user themselves or they are an authorized operator.
Validation: The getLatestTweets function ensures the count is within valid bounds before returning tweets.
That's all folks.
Stay tuned for more......
Top comments (0)