Sample user and forum data structure
class ForumUser:
def __init__(self, username):
self.username = username
self.ignored_users = set() # Set of usernames the user wants to ignore
def ignore_user(self, username_to_ignore):
"""Adds a user to the ignore list."""
self.ignored_users.add(username_to_ignore)
print(f"{username_to_ignore} has been ignored by {self.username}.")
def unignore_user(self, username_to_unignore):
"""Removes a user from the ignore list."""
if username_to_unignore in self.ignored_users:
self.ignored_users.remove(username_to_unignore)
print(f"{username_to_unignore} has been unignored by {self.username}.")
else:
print(f"{username_to_unignore} was not in the ignore list of {self.username}.")
def is_ignored(self, username):
"""Checks if a user is ignored."""
return username in self.ignored_users
class ForumPost:
def __init__(self, author, content):
self.author = author
self.content = content
class Forum:
def __init__(self):
self.users = {} # Dictionary of username -> ForumUser
self.posts = [] # List of ForumPost
def add_user(self, username):
"""Adds a new user to the forum."""
self.users[username] = ForumUser(username)
def post_message(self, author, content):
"""Allows a user to post a message on the forum."""
if author in self.users:
post = ForumPost(author, content)
self.posts.append(post)
else:
print(f"User {author} does not exist in the forum.")
def display_posts(self, viewer_username):
"""Displays posts, filtering out those by ignored users."""
if viewer_username not in self.users:
print(f"User {viewer_username} does not exist in the forum.")
return
viewer = self.users[viewer_username]
print(f"Posts visible to {viewer_username}:\n")
for post in self.posts:
if not viewer.is_ignored(post.author):
print(f"{post.author}: {post.content}")
else:
print(f"{post.author}'s post is hidden (ignored).")
# Example Usage
forum = Forum()
forum.add_user("Alice")
forum.add_user("Bob")
forum.add_user("Charlie")
# Posting messages
forum.post_message("Alice", "Hello, everyone!")
forum.post_message("Bob", "Hi, Alice!")
forum.post_message("Charlie", "Hey Alice and Bob!")
# Ignoring users
forum.users["Alice"].ignore_user("Charlie")
# Display posts from Alice's perspective (Charlie's posts should be hidden)
forum.display_posts("Alice")