Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add Comment and Reaction models with data provider

ashilkn497b4d76d2e6
diff --git a/mobile/apps/photos/lib/models/social/comment.dart b/mobile/apps/photos/lib/models/social/comment.dart
new file mode 100644
index 0000000000..fc46b7b2fe
--- /dev/null
+++ b/mobile/apps/photos/lib/models/social/comment.dart
@@ -0,0 +1,87 @@
+class Comment {
+ final String id;
+ final int collectionID;
+ final int? fileID;
+ final String data;
+ final String? parentCommentID;
+ final bool isDeleted;
+ final int userID;
+ final String? anonUserID;
+ final int createdAt;
+ final int updatedAt;
+
+ Comment({
+ required this.id,
+ required this.collectionID,
+ this.fileID,
+ required this.data,
+ this.parentCommentID,
+ this.isDeleted = false,
+ required this.userID,
+ this.anonUserID,
+ required this.createdAt,
+ required this.updatedAt,
+ });
+
+ bool get isAnonymous => userID <= 0;
+
+ bool get isReply => parentCommentID != null;
+
+ bool get isOnFile => fileID != null;
+
+ Comment copyWith({
+ String? id,
+ int? collectionID,
+ int? fileID,
+ String? data,
+ String? parentCommentID,
+ bool? isDeleted,
+ int? userID,
+ String? anonUserID,
+ int? createdAt,
+ int? updatedAt,
+ }) {
+ return Comment(
+ id: id ?? this.id,
+ collectionID: collectionID ?? this.collectionID,
+ fileID: fileID ?? this.fileID,
+ data: data ?? this.data,
+ parentCommentID: parentCommentID ?? this.parentCommentID,
+ isDeleted: isDeleted ?? this.isDeleted,
+ userID: userID ?? this.userID,
+ anonUserID: anonUserID ?? this.anonUserID,
+ createdAt: createdAt ?? this.createdAt,
+ updatedAt: updatedAt ?? this.updatedAt,
+ );
+ }
+
+ Map<String, dynamic> toMap() {
+ return {
+ 'id': id,
+ 'collectionID': collectionID,
+ 'fileID': fileID,
+ 'data': data,
+ 'parentCommentID': parentCommentID,
+ 'isDeleted': isDeleted,
+ 'userID': userID,
+ 'anonUserID': anonUserID,
+ 'createdAt': createdAt,
+ 'updatedAt': updatedAt,
+ };
+ }
+
+ factory Comment.fromMap(Map<String, dynamic> map) {
+ return Comment(
+ id: map['id'] as String,
+ collectionID: map['collectionID'] as int,
+ fileID: map['fileID'] as int?,
+ data: map['data'] as String,
+ parentCommentID: map['parentCommentID'] as String?,
+ isDeleted: map['isDeleted'] as bool? ?? false,
+ userID: map['userID'] as int,
+ anonUserID: map['anonUserID'] as String?,
+ createdAt: map['createdAt'] as int,
+ updatedAt: map['updatedAt'] as int,
+ );
+ }
+}
diff --git a/mobile/apps/photos/lib/models/social/reaction.dart b/mobile/apps/photos/lib/models/social/reaction.dart
new file mode 100644
index 0000000000..9555803fab
--- /dev/null
+++ b/mobile/apps/photos/lib/models/social/reaction.dart
@@ -0,0 +1,89 @@
+class Reaction {
+ final String id;
+ final int collectionID;
+ final int? fileID;
+ final String? commentID;
+ final String data;
+ final bool isDeleted;
+ final int userID;
+ final String? anonUserID;
+ final int createdAt;
+ final int updatedAt;
+
+ Reaction({
+ required this.id,
+ required this.collectionID,
+ this.fileID,
+ this.commentID,
+ required this.data,
+ this.isDeleted = false,
+ required this.userID,
+ this.anonUserID,
+ required this.createdAt,
+ required this.updatedAt,
+ });
+
+ bool get isAnonymous => userID <= 0;
+
+ bool get isOnFile => fileID != null && commentID == null;
+
+ bool get isOnComment => commentID != null;
+
+ bool get isOnCollection => fileID == null && commentID == null;
+
+ Reaction copyWith({
+ String? id,
+ int? collectionID,
+ int? fileID,
+ String? commentID,
+ String? data,
+ bool? isDeleted,
+ int? userID,
+ String? anonUserID,
+ int? createdAt,
+ int? updatedAt,
+ }) {
+ return Reaction(
+ id: id ?? this.id,
+ collectionID: collectionID ?? this.collectionID,
+ fileID: fileID ?? this.fileID,
+ commentID: commentID ?? this.commentID,
+ data: data ?? this.data,
+ isDeleted: isDeleted ?? this.isDeleted,
+ userID: userID ?? this.userID,
+ anonUserID: anonUserID ?? this.anonUserID,
+ createdAt: createdAt ?? this.createdAt,
+ updatedAt: updatedAt ?? this.updatedAt,
+ );
+ }
+
+ Map<String, dynamic> toMap() {
+ return {
+ 'id': id,
+ 'collectionID': collectionID,
+ 'fileID': fileID,
+ 'commentID': commentID,
+ 'data': data,
+ 'isDeleted': isDeleted,
+ 'userID': userID,
+ 'anonUserID': anonUserID,
+ 'createdAt': createdAt,
+ 'updatedAt': updatedAt,
+ };
+ }
+
+ factory Reaction.fromMap(Map<String, dynamic> map) {
+ return Reaction(
+ id: map['id'] as String,
+ collectionID: map['collectionID'] as int,
+ fileID: map['fileID'] as int?,
+ commentID: map['commentID'] as String?,
+ data: map['data'] as String,
+ isDeleted: map['isDeleted'] as bool? ?? false,
+ userID: map['userID'] as int,
+ anonUserID: map['anonUserID'] as String?,
+ createdAt: map['createdAt'] as int,
+ updatedAt: map['updatedAt'] as int,
+ );
+ }
+}
diff --git a/mobile/apps/photos/lib/models/social/social_data_provider.dart b/mobile/apps/photos/lib/models/social/social_data_provider.dart
new file mode 100644
index 0000000000..ef07452df3
--- /dev/null
+++ b/mobile/apps/photos/lib/models/social/social_data_provider.dart
@@ -0,0 +1,187 @@
+import "package:flutter/foundation.dart";
+import "package:photos/models/social/comment.dart";
+import "package:photos/models/social/reaction.dart";
+
+class SocialDataProvider {
+ SocialDataProvider._();
+ static final instance = SocialDataProvider._();
+
+ // Dummy data maps - add your test data here
+ // Key: comment/reaction ID
+ final Map<String, Comment> _comments = {
+ // Example:
+ // 'comment-1': Comment(
+ // id: 'comment-1',
+ // collectionID: 1,
+ // fileID: 100,
+ // data: 'Nice photo!',
+ // userID: 1,
+ // createdAt: 1700000000,
+ // updatedAt: 1700000000,
+ // ),
+ };
+
+ final Map<String, Reaction> _reactions = {
+ // Example:
+ // 'reaction-1': Reaction(
+ // id: 'reaction-1',
+ // collectionID: 1,
+ // fileID: 100,
+ // data: '',
+ // userID: 1,
+ // createdAt: 1700000000,
+ // updatedAt: 1700000000,
+ // ),
+ };
+
+ // Query methods
+
+ List<Comment> getCommentsForFile(int fileID) {
+ return _comments.values
+ .where((c) => c.fileID == fileID && !c.isDeleted)
+ .toList();
+ }
+
+ List<Reaction> getReactionsForFile(int fileID) {
+ return _reactions.values
+ .where((r) => r.fileID == fileID && r.commentID == null && !r.isDeleted)
+ .toList();
+ }
+
+ List<Comment> getRepliesForComment(String commentID) {
+ return _comments.values
+ .where((c) => c.parentCommentID == commentID && !c.isDeleted)
+ .toList();
+ }
+
+ List<Reaction> getReactionsForComment(String commentID) {
+ return _reactions.values
+ .where((r) => r.commentID == commentID && !r.isDeleted)
+ .toList();
+ }
+
+ // Collection-level queries
+
+ List<Comment> getCommentsForCollection(int collectionID) {
+ return _comments.values
+ .where(
+ (c) =>
+ c.collectionID == collectionID &&
+ c.fileID == null &&
+ !c.isDeleted,
+ )
+ .toList();
+ }
+
+ List<Reaction> getReactionsForCollection(int collectionID) {
+ return _reactions.values
+ .where(
+ (r) =>
+ r.collectionID == collectionID &&
+ r.fileID == null &&
+ r.commentID == null &&
+ !r.isDeleted,
+ )
+ .toList();
+ }
+
+ // Comment mutation methods
+
+ Comment? addComment(Comment comment) {
+ if (comment.data.trim().isEmpty) {
+ debugPrint('addComment: Cannot add comment with empty data');
+ return null;
+ }
+ if (comment.parentCommentID != null &&
+ !_comments.containsKey(comment.parentCommentID)) {
+ debugPrint(
+ 'addComment: Parent comment ${comment.parentCommentID} does not exist',
+ );
+ return null;
+ }
+ _comments[comment.id] = comment;
+ return comment;
+ }
+
+ Comment? updateComment(String id, String newData) {
+ if (newData.trim().isEmpty) {
+ debugPrint('updateComment: Cannot update comment with empty data');
+ return null;
+ }
+ final existing = _comments[id];
+ if (existing == null) {
+ debugPrint('updateComment: Comment $id does not exist');
+ return null;
+ }
+ final updated = existing.copyWith(
+ data: newData,
+ updatedAt: DateTime.now().millisecondsSinceEpoch,
+ );
+ _comments[id] = updated;
+ return updated;
+ }
+
+ Comment? deleteComment(String id) {
+ final existing = _comments[id];
+ if (existing == null) {
+ debugPrint('deleteComment: Comment $id does not exist');
+ return null;
+ }
+ final updated = existing.copyWith(
+ isDeleted: true,
+ updatedAt: DateTime.now().millisecondsSinceEpoch,
+ );
+ _comments[id] = updated;
+ return updated;
+ }
+
+ // Reaction mutation methods
+
+ Reaction? addReaction(Reaction reaction) {
+ // Validate comment exists if reacting to a comment
+ if (reaction.commentID != null &&
+ !_comments.containsKey(reaction.commentID)) {
+ debugPrint(
+ 'addReaction: Comment ${reaction.commentID} does not exist',
+ );
+ return null;
+ }
+
+ // Check if reaction already exists for same user + target
+ final existing = _reactions.values.where((r) {
+ if (r.userID != reaction.userID) return false;
+ if (reaction.commentID != null) {
+ return r.commentID == reaction.commentID;
+ }
+ if (reaction.fileID != null) {
+ return r.fileID == reaction.fileID && r.commentID == null;
+ }
+ return r.collectionID == reaction.collectionID &&
+ r.fileID == null &&
+ r.commentID == null;
+ }).firstOrNull;
+
+ if (existing != null) {
+ debugPrint(
+ 'addReaction: Reaction already exists for user ${reaction.userID} on target',
+ );
+ return null;
+ }
+ _reactions[reaction.id] = reaction;
+ return reaction;
+ }
+
+ Reaction? toggleReaction(String id) {
+ final existing = _reactions[id];
+ if (existing == null) {
+ debugPrint('toggleReaction: Reaction $id does not exist');
+ return null;
+ }
+ final updated = existing.copyWith(
+ isDeleted: !existing.isDeleted,
+ updatedAt: DateTime.now().millisecondsSinceEpoch,
+ );
+ _reactions[id] = updated;
+ return updated;
+ }
+}