1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/models/conversation.dart

116 lines
3.9 KiB
Dart
Raw Permalink Normal View History

import 'package:collection/collection.dart' show IterableExtension;
2021-03-10 16:54:41 +00:00
import 'package:comunic/helpers/serialization/base_serialization_helper.dart';
import 'package:comunic/models/conversation_member.dart';
2019-05-01 07:24:50 +00:00
import 'package:comunic/utils/account_utils.dart';
2021-03-10 17:04:29 +00:00
import 'package:flutter/material.dart';
2019-04-23 12:35:41 +00:00
2021-04-06 15:04:55 +00:00
import 'group.dart';
2019-04-23 12:35:41 +00:00
/// Conversation model
///
/// @author Pierre HUBERT
2020-04-20 08:37:59 +00:00
enum CallCapabilities { NONE, AUDIO, VIDEO }
2021-03-10 16:54:41 +00:00
class Conversation extends SerializableElement<Conversation> {
final int? id;
final int? lastActivity;
final String? name;
final Color? color;
final String? logoURL;
final int? groupID;
final GroupMembershipLevel? groupMinMembershipLevel;
final List<ConversationMember>? members;
final bool? canEveryoneAddMembers;
2020-04-20 08:37:59 +00:00
final CallCapabilities callCapabilities;
final bool isHavingCall;
2019-04-23 12:35:41 +00:00
2021-03-10 16:54:41 +00:00
Conversation({
/*required*/ required int this.id,
/*required*/ required int this.lastActivity,
required this.name,
required this.color,
required this.logoURL,
required this.groupID,
required this.groupMinMembershipLevel,
/*required*/ required List<ConversationMember> this.members,
/*required*/ required bool this.canEveryoneAddMembers,
2020-04-20 08:37:59 +00:00
this.callCapabilities = CallCapabilities.NONE,
this.isHavingCall = false,
2022-03-10 19:36:55 +00:00
}) : assert((groupID == null) == (groupMinMembershipLevel == null));
/// Check out whether a conversation has a fixed name or not
2019-04-25 09:14:05 +00:00
bool get hasName => this.name != null;
2021-03-10 16:54:41 +00:00
/// Get current user membership
ConversationMember get membership =>
members!.firstWhere((m) => m.userID == userID());
2019-05-01 07:24:50 +00:00
2021-03-10 16:54:41 +00:00
/// Check out whether current user of the application is an admin
bool get isAdmin => membership.isAdmin;
/// Check if current user is the last admin of the conversation
bool get isLastAdmin => isAdmin && adminsID.length == 1;
2021-03-10 16:54:41 +00:00
/// Check it current user is following the conversation or not
bool get following => membership.following;
/// Get the list of members in the conversation
Set<int?> get membersID => members!.map((e) => e.userID).toSet();
2021-03-10 16:54:41 +00:00
2021-03-13 09:32:11 +00:00
/// Get the list of admins in the conversation
Set<int?> get adminsID =>
members!.where((e) => e.isAdmin).map((e) => e.userID).toSet();
2021-03-13 09:32:11 +00:00
2021-03-11 17:00:06 +00:00
/// Get the list of the OTHER members of the conversation (all except current user)
Set<int?> get otherMembersID => membersID..remove(userID());
2021-03-11 17:00:06 +00:00
2021-03-10 16:54:41 +00:00
/// Check if the last message has been seen or not
bool get sawLastMessage => lastActivity! <= membership.lastAccessTime;
2021-03-10 16:54:41 +00:00
2021-03-13 10:02:44 +00:00
/// Check out whether a conversation is managed or not
2021-04-06 16:04:16 +00:00
bool get isManaged => isGroupConversation;
bool get isGroupConversation => groupID != null;
2021-03-13 10:02:44 +00:00
2021-04-06 15:40:13 +00:00
bool get hasLogo => logoURL != null;
2021-03-10 16:54:41 +00:00
Conversation.fromJson(Map<String, dynamic> map)
: id = map["id"],
name = map["name"],
2021-03-10 17:04:29 +00:00
color = map["color"] == null ? null : Color(map["color"]),
2021-03-10 16:54:41 +00:00
logoURL = map["logoURL"],
groupID = map["groupID"],
groupMinMembershipLevel = GroupMembershipLevel.values.firstWhereOrNull(
(element) => element.toString() == map["groupMinMembershipLevel"]),
2021-03-10 16:54:41 +00:00
lastActivity = map["lastActivity"],
members = map["members"]
.map((el) => ConversationMember.fromJSON(el))
2021-03-10 23:02:41 +00:00
.toList()
.cast<ConversationMember>(),
2021-03-10 16:54:41 +00:00
canEveryoneAddMembers = map["canEveryoneAddMembers"],
2020-04-20 08:37:59 +00:00
// By default, we can not do any call
callCapabilities = CallCapabilities.NONE,
2021-03-10 16:54:41 +00:00
isHavingCall = false;
2021-03-10 16:54:41 +00:00
Map<String, dynamic> toJson() {
return {
2021-03-10 16:54:41 +00:00
"id": id,
"name": name,
2021-03-10 17:04:29 +00:00
"color": color?.value,
2021-03-10 16:54:41 +00:00
"logoURL": logoURL,
"groupID": groupID,
2021-04-06 15:04:55 +00:00
"groupMinMembershipLevel": groupMinMembershipLevel?.toString(),
2021-03-10 16:54:41 +00:00
"lastActivity": lastActivity,
"members": members!.map((e) => e.toJson()).toList(),
2021-03-10 16:54:41 +00:00
"canEveryoneAddMembers": canEveryoneAddMembers,
};
}
@override
2021-03-10 16:54:41 +00:00
int compareTo(Conversation other) {
return other.lastActivity!.compareTo(this.lastActivity!);
}
2019-04-23 12:35:41 +00:00
}