-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
82 lines (73 loc) · 1.92 KB
/
handlers.go
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
82
package main
import (
"encoding/json"
"go.mau.fi/whatsmeow/types"
"html/template"
"net/http"
"time"
)
func selectContactsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
http.Error(w, "Error parsing form", http.StatusBadRequest)
return
}
selectedJIDs := r.Form["contacts"]
for _, jid := range selectedJIDs {
parsedJID, _ := types.ParseJID(jid)
client.SubscribePresence(parsedJID)
}
time.Sleep(1 * time.Second)
http.Redirect(w, r, "/status", http.StatusSeeOther)
return
}
contacts, _ := client.Store.Contacts.GetAllContacts()
for jid, contact := range contacts {
if len(contact.FullName) > 0 {
userNames[jid.String()] = contact.FullName
} else {
userNames[jid.String()] = contact.PushName
}
}
tmpl := template.Must(template.ParseFiles("select_contacts.html"))
tmpl.Execute(w, contacts)
}
func statusUpdateHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
defer mu.RUnlock()
updates := []StatusUpdate{}
for jid, logs := range userStatusLog {
onlineRanges := calculateOnlineRanges(logs)
isOnline := false
if len(logs) > 0 {
lastLog := logs[len(logs)-1]
isOnline = lastLog.Status == "Online"
}
username := userNames[jid]
updates = append(updates, StatusUpdate{
JID: jid,
Username: username,
OnlineRanges: onlineRanges,
IsOnline: isOnline,
})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(updates)
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
statuses := make(map[string]ContactData)
for jid, status := range userStatus {
statuses[jid] = ContactData{
CurrentStatus: status,
Log: userStatusLog[jid],
}
}
mu.RUnlock()
tmpl := template.Must(template.ParseFiles("status.html"))
err := tmpl.Execute(w, statuses)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}