@Override public OmemoCachedDeviceList loadCachedDeviceList(OmemoDevice userDevice, BareJid contact) { OmemoCachedDeviceList cachedDeviceList = new OmemoCachedDeviceList(); if (contact == null) { throw new IllegalArgumentException("Contact can not be null."); } // active File activeDevicesPath = hierarchy.getContactsActiveDevicesPath(userDevice, contact); Set<Integer> active = readIntegers(activeDevicesPath); if (active != null) { cachedDeviceList.getActiveDevices().addAll(active); } // inactive File inactiveDevicesPath = hierarchy.getContactsInactiveDevicesPath(userDevice, contact); Set<Integer> inactive = readIntegers(inactiveDevicesPath); if (inactive != null) { cachedDeviceList.getInactiveDevices().addAll(inactive); } return cachedDeviceList; }
/** * Check, if our freshly generated deviceId is available (unique) in our deviceList. * * @param userDevice our current device. * @param id deviceId to check for. * @return true if list did not contain our id, else false */ boolean isAvailableDeviceId(OmemoDevice userDevice, int id) { LOGGER.log(Level.INFO, "Check if id " + id + " is available..."); // Lookup local cached device list BareJid ownJid = userDevice.getJid(); OmemoCachedDeviceList cachedDeviceList; cachedDeviceList = loadCachedDeviceList(userDevice, ownJid); if (cachedDeviceList == null) { cachedDeviceList = new OmemoCachedDeviceList(); } // Does the list already contain that id? return !cachedDeviceList.contains(id); }
public OmemoCachedDeviceList(OmemoCachedDeviceList original) { this(original.getActiveDevices(), original.getInactiveDevices()); }
OmemoCachedDeviceList cached = new OmemoCachedDeviceList(); assertNotNull(cached.getActiveDevices()); assertNotNull(cached.getInactiveDevices()); cached.getInactiveDevices().add(1); cached.getInactiveDevices().add(2); cached.getActiveDevices().add(3); cached.merge(update); assertTrue(cached.getActiveDevices().contains(1) && !cached.getActiveDevices().contains(2) && !cached.getActiveDevices().contains(3) && cached.getActiveDevices().contains(4)); assertTrue(!cached.getInactiveDevices().contains(1) && cached.getInactiveDevices().contains(2) && cached.getInactiveDevices().contains(3) && !cached.getInactiveDevices().contains(4)); assertTrue(cached.getAllDevices().size() == 4); assertFalse(cached.contains(17)); cached.addDevice(17); assertTrue(cached.getActiveDevices().contains(17)); assertNotNull(cached.toString());
@Test public void loadStoreCachedDeviceList() throws IOException { Integer[] active = new Integer[] {1,5,999,10}; Integer[] inactive = new Integer[] {6,7,8}; OmemoCachedDeviceList before = new OmemoCachedDeviceList( new HashSet<>(Arrays.asList(active)), new HashSet<>(Arrays.asList(inactive))); assertNotNull("Loading a non-existent cached deviceList must return an empty list.", store.loadCachedDeviceList(alice, bob.getJid())); store.storeCachedDeviceList(alice, bob.getJid(), before); OmemoCachedDeviceList after = store.loadCachedDeviceList(alice, bob.getJid()); assertTrue("Loaded deviceList must not be empty", after.getAllDevices().size() != 0); assertEquals("Number of entries in active devices must match.", active.length, after.getActiveDevices().size()); assertEquals("Number of entries in inactive devices must match.", inactive.length, after.getInactiveDevices().size()); assertEquals("Number of total entries must match.", active.length + inactive.length, after.getAllDevices().size()); for (Integer a : active) { assertTrue(after.getActiveDevices().contains(a)); assertTrue(after.getAllDevices().contains(a)); } for (Integer i : inactive) { assertTrue(after.getInactiveDevices().contains(i)); assertTrue(after.getAllDevices().contains(i)); } store.storeCachedDeviceList(alice, bob.getJid(), new OmemoCachedDeviceList()); assertEquals("DeviceList must be empty after overwriting it with empty list.", 0, store.loadCachedDeviceList(alice, bob.getJid()).getAllDevices().size()); }
/** * Merge the received OmemoDeviceListElement with the one we already have. If we had none, the received one is saved. * * @param userDevice our OmemoDevice. * @param contact Contact we received the list from. * @param list List we received. */ OmemoCachedDeviceList mergeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoDeviceListElement list) { OmemoCachedDeviceList cached = loadCachedDeviceList(userDevice, contact); if (cached == null) { cached = new OmemoCachedDeviceList(); } if (list == null) { return cached; } for (int devId : list.getDeviceIds()) { if (!cached.contains(devId)) { setDateOfLastDeviceIdPublication(userDevice, new OmemoDevice(contact, devId), new Date()); } } cached.merge(list.getDeviceIds()); storeCachedDeviceList(userDevice, contact, cached); return cached; }
OmemoCachedDeviceList contactsDeviceList, int maxAgeHours) { OmemoCachedDeviceList deviceList = new OmemoCachedDeviceList(contactsDeviceList); // Don't work on original list. for (int deviceId : contactsDeviceList.getActiveDevices()) { OmemoDevice device = new OmemoDevice(contact, deviceId); deviceList.addInactiveDevice(deviceId);
public boolean isActive(int deviceId) { return getActiveDevices().contains(deviceId); }
/** * Add our load the deviceList of the user from cache, delete stale devices if needed, add the users device * back if necessary, store the refurbished list in cache and return it. * * @param userDevice * @return */ OmemoCachedDeviceList cleanUpDeviceList(OmemoDevice userDevice) { OmemoCachedDeviceList cachedDeviceList; // Delete stale devices if allowed and necessary if (OmemoConfiguration.getDeleteStaleDevices()) { cachedDeviceList = deleteStaleDevices(userDevice); } else { cachedDeviceList = getOmemoStoreBackend().loadCachedDeviceList(userDevice); } // Add back our device if necessary if (!cachedDeviceList.getActiveDevices().contains(userDevice.getDeviceId())) { cachedDeviceList.addDevice(userDevice.getDeviceId()); } getOmemoStoreBackend().storeCachedDeviceList(userDevice, userDevice.getJid(), cachedDeviceList); return cachedDeviceList; }
@Override public OmemoCachedDeviceList loadCachedDeviceList(OmemoDevice userDevice, BareJid contact) { OmemoCachedDeviceList list = getCache(userDevice).deviceLists.get(contact); if (list == null && persistent != null) { list = persistent.loadCachedDeviceList(userDevice, contact); if (list != null) { getCache(userDevice).deviceLists.put(contact, list); } } return list == null ? new OmemoCachedDeviceList() : new OmemoCachedDeviceList(list); }
.loadCachedDeviceList(omemoManager.getOwnDevice(), omemoManager.getOwnJid()); for (int id : deviceList.getAllDevices()) { try { pm.getLeafNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id)).deleteAllItems();
@Override public void storeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoCachedDeviceList contactsDeviceList) { if (contact == null) { return; } File activeDevices = hierarchy.getContactsActiveDevicesPath(userDevice, contact); writeIntegers(activeDevices, contactsDeviceList.getActiveDevices()); File inactiveDevices = hierarchy.getContactsInactiveDevicesPath(userDevice, contact); writeIntegers(inactiveDevices, contactsDeviceList.getInactiveDevices()); }
public OmemoDeviceListElement(OmemoCachedDeviceList cachedList) { this.deviceIds = Collections.unmodifiableSet(cachedList.getActiveDevices()); }
@Override public void storeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoCachedDeviceList deviceList) { getCache(userDevice).deviceLists.put(contact, new OmemoCachedDeviceList(deviceList)); if (persistent != null) { persistent.storeCachedDeviceList(userDevice, contact, deviceList); } }
/** * Return a set of all OMEMO capable devices of a contact. * Note, that this method does not explicitly refresh the device list of the contact, so it might be outdated. * @see #requestDeviceListUpdateFor(BareJid) * @param contact contact we want to get a set of device of. * @return set of known devices of that contact. */ public Set<OmemoDevice> getDevicesOf(BareJid contact) { OmemoCachedDeviceList list = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact); HashSet<OmemoDevice> devices = new HashSet<>(); for (int deviceId : list.getActiveDevices()) { devices.add(new OmemoDevice(contact, deviceId)); } return devices; }
/** * Build OMEMO sessions with all devices of the contact, we haven't had sessions with before. * This method returns a set of OmemoDevices. This set contains all devices, with which we either had sessions * before, plus those devices with which we just built sessions. * * @param connection authenticated XMPP connection. * @param userDevice our OmemoDevice * @param contact the BareJid of the contact with whom we want to build sessions with. * @return set of devices with a session. * @throws SmackException.NotConnectedException * @throws InterruptedException * @throws SmackException.NoResponseException */ private Set<OmemoDevice> buildMissingSessionsWithContact(XMPPConnection connection, OmemoDevice userDevice, BareJid contact) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { OmemoCachedDeviceList contactsDeviceIds = getOmemoStoreBackend().loadCachedDeviceList(userDevice, contact); Set<OmemoDevice> contactsDevices = new HashSet<>(); for (int deviceId : contactsDeviceIds.getActiveDevices()) { contactsDevices.add(new OmemoDevice(contact, deviceId)); } return buildMissingSessionsWithDevices(connection, userDevice, contactsDevices); }
/** * Returns true, if the contact has any active devices published in a deviceList. * * @param contact contact * @return true if contact has at least one OMEMO capable device. * @throws SmackException.NotConnectedException * @throws InterruptedException * @throws SmackException.NoResponseException * @throws PubSubException.NotALeafNodeException * @throws XMPPException.XMPPErrorException */ public boolean contactSupportsOmemo(BareJid contact) throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { synchronized (LOCK) { OmemoCachedDeviceList deviceList = getOmemoService().refreshDeviceList(connection(), getOwnDevice(), contact); return !deviceList.getActiveDevices().isEmpty(); } }
.loadCachedDeviceList(getOwnDevice(), contact); for (int id : deviceList.getActiveDevices()) { OmemoDevice device = new OmemoDevice(contact, id); OmemoFingerprint fingerprint = getFingerprint(device);
if (!publishedList.getDeviceIds().equals(cachedList.getActiveDevices())) { publishDeviceList(connection, new OmemoDeviceListElement_VAxolotl(cachedList));