| 1159 | * @param {function} [callback] function callback |
| 1160 | */ |
| 1161 | const refreshMails = (callback) => { |
| 1162 | fetch(`https://${GetParentResourceName()}/appAction`, |
| 1163 | { |
| 1164 | method: "POST", |
| 1165 | body: JSON.stringify({ |
| 1166 | app: "mail", |
| 1167 | type: "refresh" |
| 1168 | }) |
| 1169 | }).then(response => response.json()).then(data => { |
| 1170 | mailsList = data; |
| 1171 | let container = document.getElementById("mail-container"); |
| 1172 | container.innerHTML = ""; |
| 1173 | const dateFormat = GetLocale("date_format"); |
| 1174 | |
| 1175 | let newElements = []; |
| 1176 | for (const [key, mails] of Object.entries(mailsList)) { |
| 1177 | let date = new Date(mails[0].timestamp*1000); |
| 1178 | let dateString = date.toLocaleTimeString(dateFormat) + " " + date.toLocaleDateString(dateFormat) |
| 1179 | let newElement = document.createElement("div"); |
| 1180 | newElement.classList = "mail" + (!mails[0].read && mails[0].to == mailAccount ? " new" : ""); |
| 1181 | newElement.innerHTML = `${!mails[0].read && mails[0].to == mailAccount ? `<div>${GetLocale("mail_new")}</div><div class="separator"></div>` : ""} |
| 1182 | <div class="object">${mails[mails.length-1].object}</div> |
| 1183 | <div class="separator"></div> |
| 1184 | <div>${(mails[0].from == mailAccount ? mails[0].to : mails[0].from) + mailDomain}</div> |
| 1185 | <div class="separator"></div> |
| 1186 | <div>${dateString}</div>`; |
| 1187 | |
| 1188 | newElement.onclick = () => { |
| 1189 | document.getElementById("mail-refresh").style.display = "none"; |
| 1190 | document.getElementById("mail-signout").style.display = "none"; |
| 1191 | document.getElementById("mail-create").innerText = GetLocale("os_cancel"); |
| 1192 | mailCreation = !mailCreation; |
| 1193 | |
| 1194 | // we edit the state only if the last mail isn't ours |
| 1195 | if (mailAccount == mails[0].to && !mails[0].read) { |
| 1196 | fetch(`https://${GetParentResourceName()}/appAction`, |
| 1197 | { |
| 1198 | method: "POST", |
| 1199 | body: JSON.stringify({ |
| 1200 | app: "mail", |
| 1201 | type: "readed", |
| 1202 | id: mails[0].answer_to ? mails[0].answer_to : mails[0].id |
| 1203 | }) |
| 1204 | }) |
| 1205 | } |
| 1206 | |
| 1207 | // reversed order so we go from oldest to newest |
| 1208 | document.getElementById("mail-reader-object").innerText = GetLocale("mail_object")+": " + mails[mails.length - 1].object; |
| 1209 | let readerContainer = document.getElementById("mail-reader-container"); |
| 1210 | document.getElementById("mail-container").style.display = "none"; |
| 1211 | readerContainer.innerHTML = ""; |
| 1212 | |
| 1213 | for (i = mails.length - 1; i >= 0; i--) { |
| 1214 | let newElement = document.createElement("div"); |
| 1215 | newElement.innerHTML = `<div class="mail-reader-info">${mails[i].from + mailDomain}${!mails[i].read && mails[0].to == mailAccount ? `<div class="mail-new">${GetLocale("mail_new")}</div>` : ""}</div> |
| 1216 | <div class="mail-reader-text">${mails[i].text}<div class="mail-reader-date">${dateString}</div></div>` |
| 1217 | readerContainer.appendChild(newElement); |
| 1218 | } |