111 lines
3.3 KiB
Java
111 lines
3.3 KiB
Java
|
package org.mercury_im.messenger.core;
|
||
|
|
||
|
import org.jivesoftware.smack.roster.packet.RosterPacket;
|
||
|
import org.jxmpp.jid.Jid;
|
||
|
import org.mercury_im.messenger.persistence.model.ContactAttributes;
|
||
|
import org.mercury_im.messenger.persistence.model.ContactModel;
|
||
|
import org.mercury_im.messenger.persistence.model.EntityModel;
|
||
|
import org.mercury_im.messenger.persistence.repository.ContactRepository;
|
||
|
|
||
|
import java.util.Collection;
|
||
|
import java.util.List;
|
||
|
|
||
|
import javax.inject.Inject;
|
||
|
|
||
|
import io.reactivex.MaybeObserver;
|
||
|
import io.reactivex.Observer;
|
||
|
import io.reactivex.Scheduler;
|
||
|
import io.reactivex.disposables.Disposable;
|
||
|
import io.reactivex.functions.Consumer;
|
||
|
import io.reactivex.schedulers.Schedulers;
|
||
|
|
||
|
public class RosterStore implements org.jivesoftware.smack.roster.rosterstore.RosterStore {
|
||
|
|
||
|
private final ContactRepository contactRepository;
|
||
|
|
||
|
private long accountId;
|
||
|
|
||
|
@Inject
|
||
|
public RosterStore(ContactRepository contactRepository) {
|
||
|
this.contactRepository = contactRepository;
|
||
|
|
||
|
contactRepository.getAllContactsOfAccount(accountId)
|
||
|
.subscribeOn(Schedulers.io())
|
||
|
.observeOn(Schedulers.computation())
|
||
|
.subscribe();
|
||
|
}
|
||
|
|
||
|
public void setAccountId(long accountId) {
|
||
|
this.accountId = accountId;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public List<RosterPacket.Item> getEntries() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public RosterPacket.Item getEntry(Jid bareJid) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getRosterVersion() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean addEntry(RosterPacket.Item item, String version) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean resetEntries(Collection<RosterPacket.Item> items, String version) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean removeEntry(Jid bareJid, String version) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void resetStore() {
|
||
|
|
||
|
}
|
||
|
|
||
|
public RosterPacket.Item fromModel(ContactModel contactModel) {
|
||
|
RosterPacket.Item item = new RosterPacket.Item(contactModel.getEntity().getJid(), contactModel.getContact().getRosterName());
|
||
|
item.setItemType(convert(contactModel.getContact().getDirection()));
|
||
|
item.setApproved(contactModel.getContact().isApproved());
|
||
|
item.setSubscriptionPending(contactModel.getContact().isSubscriptionPending());
|
||
|
return item;
|
||
|
}
|
||
|
|
||
|
public ContactModel toModel(RosterPacket.Item item) {
|
||
|
ContactModel contact = contactRepository.newContactModel();
|
||
|
|
||
|
ContactAttributes attributes = contactRepository.newAttributesModel();
|
||
|
attributes.setRosterName(item.getName());
|
||
|
attributes.setDirection(convert(item.getItemType()));
|
||
|
attributes.setApproved(item.isApproved());
|
||
|
attributes.setSubscriptionPending(item.isSubscriptionPending());
|
||
|
|
||
|
EntityModel entity = contactRepository.newEntityModel();
|
||
|
entity.setJid(item.getJid().asEntityBareJidOrThrow());
|
||
|
|
||
|
contact.setContact(attributes);
|
||
|
contact.setEntity(entity);
|
||
|
|
||
|
return contact;
|
||
|
}
|
||
|
|
||
|
public ContactAttributes.DIRECTION convert(RosterPacket.ItemType type) {
|
||
|
return ContactAttributes.DIRECTION.valueOf(type.toString());
|
||
|
}
|
||
|
|
||
|
public RosterPacket.ItemType convert(ContactAttributes.DIRECTION direction) {
|
||
|
return RosterPacket.ItemType.fromString(direction.toString());
|
||
|
}
|
||
|
}
|