chat.dim.CommonFacebook Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Common Show documentation
Show all versions of Common Show documentation
Decentralized Instant Messaging (Java SDK)
/* license: https://mit-license.org
*
* DIM-SDK : Decentralized Instant Messaging Software Development Kit
*
* Written in 2022 by Moky
*
* ==============================================================================
* The MIT License (MIT)
*
* Copyright (c) 2022 Albert Moky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ==============================================================================
*/
package chat.dim;
import java.util.ArrayList;
import java.util.List;
import chat.dim.crypto.DecryptKey;
import chat.dim.crypto.SignKey;
import chat.dim.mkm.DocumentHelper;
import chat.dim.mkm.User;
import chat.dim.protocol.Document;
import chat.dim.protocol.ID;
/**
* Common Facebook with Database
*/
public abstract class CommonFacebook extends Facebook {
private User current;
public CommonFacebook() {
super();
current = null;
}
@Override
public abstract CommonArchivist getArchivist();
@Override
public List getLocalUsers() {
List localUsers = new ArrayList<>();
User user;
CommonArchivist archivist = getArchivist();
List array = archivist.getLocalUsers();
if (array == null || array.isEmpty()) {
user = current;
if (user != null) {
localUsers.add(user);
}
} else {
for (ID item : array) {
assert getPrivateKeyForSignature(item) != null : "private key not found: " + item;
user = getUser(item);
if (user != null) {
localUsers.add(user);
} else {
assert false : "failed to create user: " + item;
}
}
}
return localUsers;
}
public User getCurrentUser() {
// Get current user (for signing and sending message)
User user = current;
if (user == null) {
//current = super.getCurrentUser();
List localUsers = getLocalUsers();
if (/*localUsers != null && */!localUsers.isEmpty()) {
user = localUsers.get(0);
current = user;
}
}
return user;
}
public void setCurrentUser(User user) {
if (user.getDataSource() == null) {
user.setDataSource(this);
}
current = user;
}
public Document getDocument(ID identifier, String type) {
List documents = getDocuments(identifier);
Document doc = DocumentHelper.lastDocument(documents, type);
// compatible for document type
if (doc == null && Document.VISA.equals(type)) {
doc = DocumentHelper.lastDocument(documents, "profile");
}
return doc;
}
public String getName(ID identifier) {
String type;
if (identifier.isUser()) {
type = Document.VISA;
} else if (identifier.isGroup()) {
type = Document.BULLETIN;
} else {
type = "*";
}
// get name from document
Document doc = getDocument(identifier, type);
if (doc != null) {
String name = doc.getName();
if (name != null && name.length() > 0) {
return name;
}
}
// get name from ID
return Anonymous.getName(identifier);
}
//
// UserDataSource
//
@Override
public List getContacts(ID user) {
CommonArchivist archivist = getArchivist();
return archivist.getContacts(user);
}
@Override
public List getPrivateKeysForDecryption(ID user) {
CommonArchivist archivist = getArchivist();
return archivist.getPrivateKeysForDecryption(user);
}
@Override
public SignKey getPrivateKeyForSignature(ID user) {
CommonArchivist archivist = getArchivist();
return archivist.getPrivateKeyForSignature(user);
}
@Override
public SignKey getPrivateKeyForVisaSignature(ID user) {
CommonArchivist archivist = getArchivist();
return archivist.getPrivateKeyForVisaSignature(user);
}
}