✨ feat: Implementierung von Benachrichtigungen und sozialen Funktionen; Hinzufügen von API-Endpunkten für Benachrichtigungen, Benutzer-Follows und soziale Interaktionen; Verbesserung des Logging-Systems zur besseren Nachverfolgbarkeit von Systemereignissen.
This commit is contained in:
308
models.py
308
models.py
@@ -45,6 +45,35 @@ user_thought_bookmark = db.Table('user_thought_bookmark',
|
||||
db.Column('created_at', db.DateTime, default=datetime.utcnow)
|
||||
)
|
||||
|
||||
# Beziehungstabelle für Benutzer-Freundschaften
|
||||
user_friendships = db.Table('user_friendships',
|
||||
db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('friend_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('created_at', db.DateTime, default=datetime.utcnow),
|
||||
db.Column('status', db.String(20), default='pending') # pending, accepted, blocked
|
||||
)
|
||||
|
||||
# Beziehungstabelle für Benutzer-Follows
|
||||
user_follows = db.Table('user_follows',
|
||||
db.Column('follower_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('created_at', db.DateTime, default=datetime.utcnow)
|
||||
)
|
||||
|
||||
# Beziehungstabelle für Post-Likes
|
||||
post_likes = db.Table('post_likes',
|
||||
db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('post_id', db.Integer, db.ForeignKey('social_post.id'), primary_key=True),
|
||||
db.Column('created_at', db.DateTime, default=datetime.utcnow)
|
||||
)
|
||||
|
||||
# Beziehungstabelle für Comment-Likes
|
||||
comment_likes = db.Table('comment_likes',
|
||||
db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
||||
db.Column('comment_id', db.Integer, db.ForeignKey('social_comment.id'), primary_key=True),
|
||||
db.Column('created_at', db.DateTime, default=datetime.utcnow)
|
||||
)
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
@@ -59,7 +88,20 @@ class User(db.Model, UserMixin):
|
||||
avatar = db.Column(db.String(200), nullable=True) # Profilbild-URL
|
||||
last_login = db.Column(db.DateTime, nullable=True) # Letzter Login
|
||||
|
||||
# Relationships
|
||||
# Social Network Felder
|
||||
display_name = db.Column(db.String(100), nullable=True) # Anzeigename
|
||||
birth_date = db.Column(db.Date, nullable=True) # Geburtsdatum
|
||||
gender = db.Column(db.String(20), nullable=True) # Geschlecht
|
||||
phone = db.Column(db.String(20), nullable=True) # Telefonnummer
|
||||
is_verified = db.Column(db.Boolean, default=False) # Verifizierter Account
|
||||
is_private = db.Column(db.Boolean, default=False) # Privater Account
|
||||
follower_count = db.Column(db.Integer, default=0) # Follower-Anzahl
|
||||
following_count = db.Column(db.Integer, default=0) # Following-Anzahl
|
||||
post_count = db.Column(db.Integer, default=0) # Post-Anzahl
|
||||
online_status = db.Column(db.String(20), default='offline') # online, offline, away
|
||||
last_seen = db.Column(db.DateTime, nullable=True) # Zuletzt gesehen
|
||||
|
||||
# Beziehungen
|
||||
threads = db.relationship('Thread', backref='creator', lazy=True)
|
||||
messages = db.relationship('Message', backref='author', lazy=True)
|
||||
projects = db.relationship('Project', backref='owner', lazy=True)
|
||||
@@ -68,6 +110,37 @@ class User(db.Model, UserMixin):
|
||||
bookmarked_thoughts = db.relationship('Thought', secondary=user_thought_bookmark,
|
||||
lazy='dynamic', backref=db.backref('bookmarked_by', lazy='dynamic'))
|
||||
|
||||
# Social Network Beziehungen
|
||||
posts = db.relationship('SocialPost', backref='author', lazy=True, cascade="all, delete-orphan")
|
||||
comments = db.relationship('SocialComment', backref='author', lazy=True, cascade="all, delete-orphan")
|
||||
notifications = db.relationship('Notification', foreign_keys='Notification.user_id', backref='user', lazy=True, cascade="all, delete-orphan")
|
||||
|
||||
# Freundschaften (bidirektional)
|
||||
friends = db.relationship(
|
||||
'User',
|
||||
secondary=user_friendships,
|
||||
primaryjoin=id == user_friendships.c.user_id,
|
||||
secondaryjoin=id == user_friendships.c.friend_id,
|
||||
backref='friend_of',
|
||||
lazy='dynamic'
|
||||
)
|
||||
|
||||
# Following/Followers
|
||||
following = db.relationship(
|
||||
'User',
|
||||
secondary=user_follows,
|
||||
primaryjoin=id == user_follows.c.follower_id,
|
||||
secondaryjoin=id == user_follows.c.followed_id,
|
||||
backref=db.backref('followers', lazy='dynamic'),
|
||||
lazy='dynamic'
|
||||
)
|
||||
|
||||
# Liked Posts und Comments
|
||||
liked_posts = db.relationship('SocialPost', secondary=post_likes,
|
||||
backref=db.backref('liked_by', lazy='dynamic'), lazy='dynamic')
|
||||
liked_comments = db.relationship('SocialComment', secondary=comment_likes,
|
||||
backref=db.backref('liked_by', lazy='dynamic'), lazy='dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<User {self.username}>'
|
||||
|
||||
@@ -84,6 +157,45 @@ class User(db.Model, UserMixin):
|
||||
@is_admin.setter
|
||||
def is_admin(self, value):
|
||||
self.role = 'admin' if value else 'user'
|
||||
|
||||
# Social Network Methoden
|
||||
def follow(self, user):
|
||||
"""Folgt einem anderen Benutzer"""
|
||||
if not self.is_following(user):
|
||||
self.following.append(user)
|
||||
user.follower_count += 1
|
||||
user.following_count += 1
|
||||
|
||||
# Notification erstellen
|
||||
notification = Notification(
|
||||
user_id=user.id,
|
||||
type='follow',
|
||||
message=f'{self.username} folgt dir jetzt',
|
||||
related_user_id=self.id
|
||||
)
|
||||
db.session.add(notification)
|
||||
|
||||
def unfollow(self, user):
|
||||
"""Entfolgt einem Benutzer"""
|
||||
if self.is_following(user):
|
||||
self.following.remove(user)
|
||||
user.follower_count -= 1
|
||||
user.following_count -= 1
|
||||
|
||||
def is_following(self, user):
|
||||
"""Prüft ob der Benutzer einem anderen folgt"""
|
||||
return self.following.filter(user_follows.c.followed_id == user.id).count() > 0
|
||||
|
||||
def get_feed_posts(self, limit=20):
|
||||
"""Holt Posts für den Feed (von gefolgten Benutzern)"""
|
||||
# Hole alle User-IDs von Benutzern, denen ich folge + meine eigene
|
||||
followed_user_ids = [user.id for user in self.following]
|
||||
all_user_ids = followed_user_ids + [self.id]
|
||||
|
||||
# Hole Posts von diesen Benutzern
|
||||
return SocialPost.query.filter(
|
||||
SocialPost.user_id.in_(all_user_ids)
|
||||
).order_by(SocialPost.created_at.desc()).limit(limit)
|
||||
|
||||
class Category(db.Model):
|
||||
"""Wissenschaftliche Kategorien für die Gliederung der öffentlichen Mindmap"""
|
||||
@@ -388,4 +500,196 @@ class MindmapShare(db.Model):
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<MindmapShare: {self.mindmap_id} - {self.shared_with_id} - {self.permission_type.name}>'
|
||||
return f'<MindmapShare: {self.mindmap_id} - {self.shared_with_id} - {self.permission_type.name}>'
|
||||
|
||||
class SocialPost(db.Model):
|
||||
"""Posts im Social Network"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
image_url = db.Column(db.String(500), nullable=True) # Bild-URL
|
||||
video_url = db.Column(db.String(500), nullable=True) # Video-URL
|
||||
link_url = db.Column(db.String(500), nullable=True) # Link-URL
|
||||
link_title = db.Column(db.String(200), nullable=True) # Link-Titel
|
||||
link_description = db.Column(db.Text, nullable=True) # Link-Beschreibung
|
||||
post_type = db.Column(db.String(20), default='text') # text, image, video, link, thought_share
|
||||
visibility = db.Column(db.String(20), default='public') # public, friends, private
|
||||
is_pinned = db.Column(db.Boolean, default=False)
|
||||
like_count = db.Column(db.Integer, default=0)
|
||||
comment_count = db.Column(db.Integer, default=0)
|
||||
share_count = db.Column(db.Integer, default=0)
|
||||
view_count = db.Column(db.Integer, default=0)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
|
||||
# Verknüpfung zu Gedanken (falls der Post einen Gedanken teilt)
|
||||
shared_thought_id = db.Column(db.Integer, db.ForeignKey('thought.id'), nullable=True)
|
||||
shared_thought = db.relationship('Thought', backref='shared_in_posts')
|
||||
|
||||
# Verknüpfung zu Mindmap-Knoten
|
||||
shared_node_id = db.Column(db.Integer, db.ForeignKey('mind_map_node.id'), nullable=True)
|
||||
shared_node = db.relationship('MindMapNode', backref='shared_in_posts')
|
||||
|
||||
# Kommentare zu diesem Post
|
||||
comments = db.relationship('SocialComment', backref='post', lazy=True, cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self):
|
||||
return f'<SocialPost {self.id} by {self.author.username}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'content': self.content,
|
||||
'post_type': self.post_type,
|
||||
'image_url': self.image_url,
|
||||
'video_url': self.video_url,
|
||||
'link_url': self.link_url,
|
||||
'link_title': self.link_title,
|
||||
'link_description': self.link_description,
|
||||
'visibility': self.visibility,
|
||||
'is_pinned': self.is_pinned,
|
||||
'like_count': self.like_count,
|
||||
'comment_count': self.comment_count,
|
||||
'share_count': self.share_count,
|
||||
'view_count': self.view_count,
|
||||
'created_at': self.created_at.isoformat(),
|
||||
'updated_at': self.updated_at.isoformat(),
|
||||
'author': {
|
||||
'id': self.author.id,
|
||||
'username': self.author.username,
|
||||
'display_name': self.author.display_name or self.author.username,
|
||||
'avatar': self.author.avatar,
|
||||
'is_verified': self.author.is_verified
|
||||
},
|
||||
'shared_thought': self.shared_thought.to_dict() if self.shared_thought else None,
|
||||
'shared_node': self.shared_node.to_dict() if self.shared_node else None
|
||||
}
|
||||
|
||||
class SocialComment(db.Model):
|
||||
"""Kommentare zu Posts"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
like_count = db.Column(db.Integer, default=0)
|
||||
reply_count = db.Column(db.Integer, default=0)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
post_id = db.Column(db.Integer, db.ForeignKey('social_post.id'), nullable=False)
|
||||
parent_id = db.Column(db.Integer, db.ForeignKey('social_comment.id'), nullable=True)
|
||||
|
||||
# Antworten auf diesen Kommentar
|
||||
replies = db.relationship('SocialComment', backref=db.backref('parent', remote_side=[id]), lazy=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<SocialComment {self.id} by {self.author.username}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'content': self.content,
|
||||
'like_count': self.like_count,
|
||||
'reply_count': self.reply_count,
|
||||
'created_at': self.created_at.isoformat(),
|
||||
'updated_at': self.updated_at.isoformat(),
|
||||
'author': {
|
||||
'id': self.author.id,
|
||||
'username': self.author.username,
|
||||
'display_name': self.author.display_name or self.author.username,
|
||||
'avatar': self.author.avatar,
|
||||
'is_verified': self.author.is_verified
|
||||
},
|
||||
'parent_id': self.parent_id
|
||||
}
|
||||
|
||||
class Notification(db.Model):
|
||||
"""Benachrichtigungen für Benutzer"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
type = db.Column(db.String(50), nullable=False) # follow, like, comment, mention, friend_request, etc.
|
||||
message = db.Column(db.String(500), nullable=False)
|
||||
is_read = db.Column(db.Boolean, default=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
|
||||
# Verknüpfungen zu anderen Entitäten
|
||||
related_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
|
||||
related_post_id = db.Column(db.Integer, db.ForeignKey('social_post.id'), nullable=True)
|
||||
related_comment_id = db.Column(db.Integer, db.ForeignKey('social_comment.id'), nullable=True)
|
||||
related_thought_id = db.Column(db.Integer, db.ForeignKey('thought.id'), nullable=True)
|
||||
|
||||
# Beziehungen
|
||||
related_user = db.relationship('User', foreign_keys=[related_user_id])
|
||||
related_post = db.relationship('SocialPost', foreign_keys=[related_post_id])
|
||||
related_comment = db.relationship('SocialComment', foreign_keys=[related_comment_id])
|
||||
related_thought = db.relationship('Thought', foreign_keys=[related_thought_id])
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Notification {self.id} for {self.user.username}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'type': self.type,
|
||||
'message': self.message,
|
||||
'is_read': self.is_read,
|
||||
'created_at': self.created_at.isoformat(),
|
||||
'related_user': self.related_user.username if self.related_user else None,
|
||||
'related_post_id': self.related_post_id,
|
||||
'related_comment_id': self.related_comment_id,
|
||||
'related_thought_id': self.related_thought_id
|
||||
}
|
||||
|
||||
class UserSettings(db.Model):
|
||||
"""Benutzereinstellungen"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, unique=True)
|
||||
|
||||
# Datenschutz-Einstellungen
|
||||
profile_visibility = db.Column(db.String(20), default='public') # public, friends, private
|
||||
show_email = db.Column(db.Boolean, default=False)
|
||||
show_birth_date = db.Column(db.Boolean, default=False)
|
||||
show_location = db.Column(db.Boolean, default=True)
|
||||
allow_friend_requests = db.Column(db.Boolean, default=True)
|
||||
allow_messages = db.Column(db.String(20), default='everyone') # everyone, friends, none
|
||||
|
||||
# Benachrichtigungs-Einstellungen
|
||||
email_notifications = db.Column(db.Boolean, default=True)
|
||||
push_notifications = db.Column(db.Boolean, default=True)
|
||||
notify_on_follow = db.Column(db.Boolean, default=True)
|
||||
notify_on_like = db.Column(db.Boolean, default=True)
|
||||
notify_on_comment = db.Column(db.Boolean, default=True)
|
||||
notify_on_mention = db.Column(db.Boolean, default=True)
|
||||
notify_on_friend_request = db.Column(db.Boolean, default=True)
|
||||
|
||||
# Interface-Einstellungen
|
||||
dark_mode = db.Column(db.Boolean, default=False)
|
||||
language = db.Column(db.String(10), default='de')
|
||||
|
||||
# Beziehung
|
||||
user = db.relationship('User', backref=db.backref('settings', uselist=False))
|
||||
|
||||
def __repr__(self):
|
||||
return f'<UserSettings for {self.user.username}>'
|
||||
|
||||
class Activity(db.Model):
|
||||
"""Aktivitätsprotokoll für Benutzer"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
action = db.Column(db.String(100), nullable=False) # login, logout, post_created, thought_shared, etc.
|
||||
description = db.Column(db.String(500), nullable=True)
|
||||
ip_address = db.Column(db.String(45), nullable=True) # IPv4/IPv6
|
||||
user_agent = db.Column(db.String(500), nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Verknüpfungen zu anderen Entitäten
|
||||
related_post_id = db.Column(db.Integer, db.ForeignKey('social_post.id'), nullable=True)
|
||||
related_thought_id = db.Column(db.Integer, db.ForeignKey('thought.id'), nullable=True)
|
||||
related_mindmap_id = db.Column(db.Integer, db.ForeignKey('user_mindmap.id'), nullable=True)
|
||||
|
||||
# Beziehungen
|
||||
user = db.relationship('User', backref='activities')
|
||||
related_post = db.relationship('SocialPost')
|
||||
related_thought = db.relationship('Thought')
|
||||
related_mindmap = db.relationship('UserMindmap')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Activity {self.action} by {self.user.username}>'
|
||||
Reference in New Issue
Block a user