-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServices.js
78 lines (67 loc) · 2.72 KB
/
ChatServices.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
function calculateDuration(durationInMinutes) {
var now = new Date()
var fetchedTime = convertLocalTime(durationInMinutes)
var diffMs = fetchedTime - now
var diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24))
var diffHours = Math.floor(
(diffMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
var diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60))
// Build the result string conditionally
var result = "Expires in: "
if (diffDays > 0)
result += diffDays + " days "
if (diffHours > 0)
result += diffHours + " hours "
if (diffMinutes > 0 || result === "Expires in: ")
result += diffMinutes + " minutes"
return result.trim() + " left"
}
// fn caculate time display
function formatTimeDifference(createdAt, latestMsContent, latestMsTime) {
var fetchedTime
var timeString
if (latestMsContent === "") {
latestMsContent = "Group just created"
fetchedTime = convertLocalTime(createdAt)
} else {
fetchedTime = convertLocalTime(latestMsTime)
}
var currentTime = new Date()
var timeDifference = Math.floor((currentTime - fetchedTime) / 1000)
if (timeDifference < 60) {
timeString = timeDifference + " seconds ago"
} else if (timeDifference < 3600) {
timeString = Math.floor(timeDifference / 60) + " minutes ago"
} else if (timeDifference < 86400) {
timeString = Math.floor(timeDifference / 3600) + " hours ago"
} else {
timeString = Math.floor(timeDifference / 86400) + " days ago"
}
return {
"latestMsContent": latestMsContent,
"timeString": timeString
}
}
// fn convert into short time HH:MM
function formatTime(originalTimeString) {
let fetchedTime = convertLocalTime(originalTimeString)
// Format the time to "HH:mm"
let formattedTime = fetchedTime.getHours().toString().padStart(
2, '0') + ":" + fetchedTime.getMinutes().toString().padStart(2, '0')
return formattedTime
}
//function convert time to GMT+7
function convertLocalTime(time_utc) {
let originalTime = new Date(time_utc)
return originalTime
}
//fn generate uuid
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function (c) {
var r = Math.random(
) * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(
16)
})
}