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

96 lines
2.9 KiB
Dart

import 'package:comunic/helpers/serialization/base_serialization_helper.dart';
import 'package:comunic/models/conversation_member.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
/// Conversation model
///
/// @author Pierre HUBERT
enum CallCapabilities { NONE, AUDIO, VIDEO }
class Conversation extends SerializableElement<Conversation> {
final int id;
final int lastActivity;
final String name;
final Color color;
final String logoURL;
final int groupID;
final List<ConversationMember> members;
final bool canEveryoneAddMembers;
final CallCapabilities callCapabilities;
final bool isHavingCall;
Conversation({
@required this.id,
@required this.lastActivity,
@required this.name,
@required this.color,
@required this.logoURL,
@required this.groupID,
@required this.members,
@required this.canEveryoneAddMembers,
this.callCapabilities = CallCapabilities.NONE,
this.isHavingCall = false,
}) : assert(id != null),
assert(lastActivity != null),
assert(members != null),
assert(canEveryoneAddMembers != null),
assert(callCapabilities != null),
assert(isHavingCall != null);
/// Check out whether a conversation has a fixed name or not
bool get hasName => this.name != null;
/// Get current user membership
ConversationMember get membership =>
members.firstWhere((m) => m.userID == userID());
/// Check out whether current user of the application is an admin
bool get isAdmin => membership.isAdmin;
/// 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();
/// Check if the last message has been seen or not
bool get sawLastMessage => lastActivity <= membership.lastAccessTime;
Conversation.fromJson(Map<String, dynamic> map)
: id = map["id"],
name = map["name"],
color = map["color"] == null ? null : Color(map["color"]),
logoURL = map["logoURL"],
groupID = map["groupID"],
lastActivity = map["lastActivity"],
members = map["members"]
.map((el) => ConversationMember.fromJSON(el))
.toList(),
canEveryoneAddMembers = map["canEveryoneAddMembers"],
// By default, we can not do any call
callCapabilities = CallCapabilities.NONE,
isHavingCall = false;
Map<String, dynamic> toJson() {
return {
"id": id,
"name": name,
"color": color?.value,
"logoURL": logoURL,
"groupID": groupID,
"lastActivity": lastActivity,
"members": members.map((e) => e.toJson()).toList(),
"canEveryoneAddMembers": canEveryoneAddMembers,
};
}
@override
int compareTo(Conversation other) {
return other.lastActivity.compareTo(this.lastActivity);
}
}