Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
3
Indexable
Never
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ChatGroup {
    struct User {
        address userAddress;
        bool isAdmin;
    }

    struct Message {
        address sender;
        string text;
    }

    mapping(address => User) public users;
    Message[] public messages;

    event UserAdded(address user, bool isAdmin);
    event MessageSent(address sender, string text);

    constructor() {
        // Initialize the chat group with the contract creator as the initial admin
        users[msg.sender] = User(msg.sender, true);
        emit UserAdded(msg.sender, true);
    }

    function addUser(address newUser, bool isAdmin) public {
        // Add a new user to the group (requires admin approval)
        require(users[msg.sender].isAdmin, "Only admins can add users");
        users[newUser] = User(newUser, isAdmin);
        emit UserAdded(newUser, isAdmin);
    }

    function sendMessage(string memory text) public {
        // Send a message to the chat group
        require(users[msg.sender].userAddress != address(0), "User not found");
        messages.push(Message(msg.sender, text));
        emit MessageSent(msg.sender, text);
    }
}