]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/contacts/JsonContactsStore.java
d2859f3f837a38e17aa8488498295e00bbbc61c8
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / contacts / JsonContactsStore.java
1 package org.asamk.signal.manager.storage.contacts;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4
5 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 public class JsonContactsStore {
11
12 @JsonProperty("contacts")
13 private List<ContactInfo> contacts = new ArrayList<>();
14
15 public void updateContact(ContactInfo contact) {
16 final SignalServiceAddress contactAddress = contact.getAddress();
17 for (int i = 0; i < contacts.size(); i++) {
18 if (contacts.get(i).getAddress().matches(contactAddress)) {
19 contacts.set(i, contact);
20 return;
21 }
22 }
23
24 contacts.add(contact);
25 }
26
27 public ContactInfo getContact(SignalServiceAddress address) {
28 for (ContactInfo contact : contacts) {
29 if (contact.getAddress().matches(address)) {
30 if (contact.uuid == null) {
31 contact.uuid = address.getUuid().orNull();
32 } else if (contact.number == null) {
33 contact.number = address.getNumber().orNull();
34 }
35
36 return contact;
37 }
38 }
39 return null;
40 }
41
42 public List<ContactInfo> getContacts() {
43 return new ArrayList<>(contacts);
44 }
45
46 /**
47 * Remove all contacts from the store
48 */
49 public void clear() {
50 contacts.clear();
51 }
52 }