-
Notifications
You must be signed in to change notification settings - Fork 24
/
view.js
81 lines (66 loc) · 3 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function show_mail (txid) {
(async () => {
switch_to_page('view_page')
var mail_id = document.getElementById('view_title')
var view_contents = document.getElementById('view_contents')
var view_subject = document.getElementById('view_subject')
var reply = document.getElementById('reply')
var previousSender = document.getElementById('previousSender')
var previousMsg = document.getElementById('previousMsg')
var previousDateMsg = document.getElementById('previousDateMsg')
var previousDateTag = document.getElementById('previousDateTag')
var previousSubject = document.getElementById('previousSubject')
var previous_page = document.getElementById('previous_page')
var unixTime = '0'
var tx = await arweave.transactions.get(txid)
tx.get('tags').forEach(tag => {
let key = tag.get('name', { decode: true, string: true })
let value = tag.get('value', { decode: true, string: true })
if (key === 'Unix-Time') unixTime = value
})
var key = await wallet_to_key(wallet)
reply.onclick = async function () {
var recipient = document.getElementById('compose_address')
recipient.value = await arweave.wallets.ownerToAddress(tx.owner)
previousDateMsg.textContent = timeConverter(unixTime)
previousSender.textContent = recipient.value
previousMsg.textContent = mail.body
previousSubject.textContent = mail.subject
;(unixTime === '0') ? previousDateTag.style.display = 'none' : previousDateTag.style.display = 'block'
switch_to_page('compose_page')
previous_page.style.display = 'block'
}
var mail =
arweave.utils.bufferToString(
await decrypt_mail(arweave.utils.b64UrlToBuffer(tx.data), key))
try {
mail = JSON.parse(mail);
} catch (e) {}
// Upgrade old format.
if (typeof mail === 'string') {
mail = {
body: mail,
subject: txid,
}
}
// Validate
if (typeof mail !== 'object' || typeof mail.body !== 'string' || typeof mail.subject !== 'string') {
console.error(mail);
throw new Error(`Unexpected mail format: ${mail}`);
}
view_contents.textContent = mail.body
view_subject.textContent = mail.subject
function timeConverter (UNIX_timestamp) {
var a = new Date(UNIX_timestamp * 1000)
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
var year = a.getFullYear()
var month = months[a.getMonth()]
var date = a.getDate()
var hour = a.getHours()
var min = a.getMinutes()
var sec = a.getSeconds()
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec
return time
}
})()
}