From 0e6c14eb5c3296ae7d36a51eac1c93b0e87146b4 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 26 Nov 2018 12:19:30 +1100 Subject: [PATCH] Added profile model. --- js/models/profile.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 js/models/profile.js diff --git a/js/models/profile.js b/js/models/profile.js new file mode 100644 index 000000000..be551ca73 --- /dev/null +++ b/js/models/profile.js @@ -0,0 +1,43 @@ +/* global storage */ +/* global storage: false */ + +/* eslint-disable more/no-then */ + +// eslint-disable-next-line func-names +(function() { + 'use strict'; + + window.Whisper = window.Whisper || {}; + + const PROFILE_ID = 'profiles'; + + storage.getProfile = number => { + const profiles = storage.get(PROFILE_ID, {}); + return profiles[number] || null; + } + + storage.saveProfile = async (number, profile) => { + const profiles = storage.get(PROFILE_ID, {}); + if (profiles[number]) { + return; + } + + window.log.info('adding profile ', profile, 'for ', number); + await storage.put(PROFILE_ID, { + ...profiles, + number: profile, + }); + } + + storage.removeProfile = async number => { + const profiles = storage.get(PROFILE_ID, {}); + if (!profiles[number]) { + return; + } + + delete profiles[number]; + + window.log.info('removing profile for ', number); + await storage.put(PROFILE_ID, profiles); + } +})();