|
| 1 | +class SubscriptionsStorage { |
| 2 | + async #getSubsArray() { |
| 3 | + const key = "subscriptions_list"; |
| 4 | + |
| 5 | + const r = await chrome.storage.local.get(key).then((r) => r?.[key]); |
| 6 | + |
| 7 | + return r === undefined ? [] : r; |
| 8 | + } |
| 9 | + |
| 10 | + async #insertToSubsArray(page_id) { |
| 11 | + const key = "subscriptions_list"; |
| 12 | + |
| 13 | + let r = await this.#getSubsArray(); |
| 14 | + |
| 15 | + r.push(page_id); |
| 16 | + |
| 17 | + chrome.storage.local.set({[key]: r}); |
| 18 | + } |
| 19 | + |
| 20 | + async #removeListFromSubsArray(page_ids) { |
| 21 | + let r = await this.#getSubsArray(); |
| 22 | + |
| 23 | + if(r === undefined) |
| 24 | + return; |
| 25 | + |
| 26 | + r = r.filter((e) => e.includes(page_ids) == false); |
| 27 | + |
| 28 | + const key = "subscriptions_list"; |
| 29 | + |
| 30 | + chrome.storage.local.set({[key]: r}); |
| 31 | + } |
| 32 | + |
| 33 | + #getKey(page_id) { return "subscription:" + page_id; } |
| 34 | + |
| 35 | + async #getByPageId(page_id) { |
| 36 | + const key = this.#getKey(page_id); |
| 37 | + |
| 38 | + const j = await chrome.storage.local.get(key); |
| 39 | + |
| 40 | + return j?.[key]; |
| 41 | + } |
| 42 | + |
| 43 | + #setByPageId(page_id, v) { |
| 44 | + const key = this.#getKey(page_id); |
| 45 | + |
| 46 | + return chrome.storage.local.set({ [key]: v }); |
| 47 | + } |
| 48 | + |
| 49 | + async bySub() { |
| 50 | + const r = await this.#getSubsArray(); |
| 51 | + |
| 52 | + return await Promise.all( |
| 53 | + r.map(async(id) => await this.#getByPageId(id)) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + async upsertSubscription(page_id, url, title, latest_activity_counter) { |
| 58 | + let r = await this.#getByPageId(page_id); |
| 59 | + |
| 60 | + if(r === undefined) |
| 61 | + { |
| 62 | + r = { |
| 63 | + "page_id": page_id, |
| 64 | + "url": url, |
| 65 | + "title": title, |
| 66 | + "added": Date.now(), |
| 67 | + "curr_activity_counter": latest_activity_counter, |
| 68 | + "latest_activity_counter": latest_activity_counter, |
| 69 | + "isRemoved": false |
| 70 | + }; |
| 71 | + |
| 72 | + this.#insertToSubsArray(page_id); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + r["url"] = url; |
| 77 | + r["title"] = title; |
| 78 | + r["latest_activity_counter"] = latest_activity_counter; |
| 79 | + } |
| 80 | + |
| 81 | + this.#setByPageId(page_id, r); |
| 82 | + } |
| 83 | + |
| 84 | + async isSubscribed(page_id) { |
| 85 | + const v = await this.#getByPageId(page_id); |
| 86 | + |
| 87 | + return v !== undefined; |
| 88 | + } |
| 89 | + |
| 90 | + async markAsReadIfSubscribed(page_id, latest_activity_counter) { |
| 91 | + const r = await this.#getByPageId(page_id); |
| 92 | + |
| 93 | + if(r !== undefined) |
| 94 | + { |
| 95 | + r.curr_activity_counter = latest_activity_counter; |
| 96 | + r.latest_activity_counter = latest_activity_counter; |
| 97 | + } |
| 98 | + |
| 99 | + await this.#setByPageId(page_id, r); |
| 100 | + } |
| 101 | + |
| 102 | + async setActivityCounterIfSubscribed(page_id, latest_activity_counter) { |
| 103 | + const r = await this.#getByPageId(page_id); |
| 104 | + |
| 105 | + if(r !== undefined) |
| 106 | + r.latest_activity_counter = latest_activity_counter; |
| 107 | + |
| 108 | + await this.#setByPageId(page_id, r); |
| 109 | + } |
| 110 | + |
| 111 | + async setRemoved(page_id) { |
| 112 | + const r = await this.#getByPageId(page_id); |
| 113 | + |
| 114 | + if(r !== undefined) |
| 115 | + r.isRemoved = true; |
| 116 | + |
| 117 | + await this.#setByPageId(page_id, r); |
| 118 | + } |
| 119 | + |
| 120 | + async getSortedSubscriptions() { |
| 121 | + const key = "subscriptions_list"; |
| 122 | + |
| 123 | + const r = await this.bySub(); |
| 124 | + |
| 125 | + const sorted = r |
| 126 | + .sort((a, b) => a.added - b.added); |
| 127 | + |
| 128 | + return sorted; |
| 129 | + } |
| 130 | + |
| 131 | + async deleteSubscription(page_id) { |
| 132 | + const e = [page_id]; |
| 133 | + await this.#removeListFromSubsArray(e); |
| 134 | + |
| 135 | + const key = this.#getKey(page_id); |
| 136 | + await chrome.storage.local.remove(key); |
| 137 | + } |
| 138 | + |
| 139 | + async isSubsStored() { |
| 140 | + const r = await this.#getSubsArray(); |
| 141 | + |
| 142 | + return r.length > 0; |
| 143 | + } |
| 144 | + |
| 145 | + async getUnreadCount() { |
| 146 | + const unreadSubsFlags = (await this.bySub()) |
| 147 | + .map((e) => e.curr_activity_counter < e.latest_activity_counter); |
| 148 | + |
| 149 | + const unreadNum = unreadSubsFlags |
| 150 | + .map((isUnread) => isUnread ? 1 : 0) |
| 151 | + .reduce((ret, i) => ret + i, 0); |
| 152 | + |
| 153 | + return unreadNum; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +const subscriptions = new SubscriptionsStorage(); |
| 158 | + |
| 159 | +const own_key = "pages"; |
| 160 | + |
| 161 | +const own_comments = { |
| 162 | + upsertComment: async(page_id, msg_id, secret) => { |
| 163 | + await chrome.storage.local.get(own_key).then((aa) => { |
| 164 | + let r = aa[own_key]; |
| 165 | + |
| 166 | + if(r === undefined) |
| 167 | + r = {}; |
| 168 | + |
| 169 | + if(r[page_id] === undefined) |
| 170 | + r[page_id] = { |
| 171 | + "page_id": page_id, |
| 172 | + "own_messages": {} |
| 173 | + }; |
| 174 | + |
| 175 | + r[page_id].own_messages[msg_id] = { |
| 176 | + "msg_id": msg_id, |
| 177 | + "secret": secret, |
| 178 | + "added": Date.now(), |
| 179 | + }; |
| 180 | + |
| 181 | + return r; |
| 182 | + }).then( |
| 183 | + async (r) => await chrome.storage.local.set({[own_key]: r}) |
| 184 | + ); |
| 185 | + }, |
| 186 | + |
| 187 | + getCommentBelongings: (page_id, msg_id) => { |
| 188 | + return chrome.storage.local.get(own_key).then( |
| 189 | + (r) => r?.[own_key]?.[page_id]?.own_messages?.[msg_id] |
| 190 | + ); |
| 191 | + }, |
| 192 | + |
| 193 | + isCommentModifiable: async(page_id, msg_id) => { |
| 194 | + const timeout = 1000 * 60 * 60 * 24 * 2; // 2 days |
| 195 | + |
| 196 | + return await own_comments.getCommentBelongings(page_id, msg_id). |
| 197 | + then((r) => { |
| 198 | + if(r?.added === undefined) |
| 199 | + return false; |
| 200 | + else |
| 201 | + return (Date.now() - r.added) < timeout; |
| 202 | + }); |
| 203 | + } |
| 204 | +} |
0 commit comments