update mecount and chatcount in hydration

This commit is contained in:
Ted Unangst 2022-02-05 16:53:19 -05:00
parent 28a061a03f
commit 1af5998b20
6 changed files with 79 additions and 15 deletions

View File

@ -595,26 +595,25 @@ func savechonk(ch *Chonk) error {
break
}
}
chatplusone(tx, ch.UserID)
err = tx.Commit()
chatplusone(ch.UserID)
} else {
tx.Rollback()
}
return err
}
func chatplusone(userid int64) {
func chatplusone(tx *sql.Tx, userid int64) {
var user *WhatAbout
ok := somenumberedusers.Get(userid, &user)
if !ok {
return
}
options := user.Options
options.Chats += 1
options.ChatCount += 1
j, err := jsonify(options)
if err == nil {
db := opendatabase()
_, err = db.Exec("update users set options = ? where username = ?", j, user.Name)
_, err = tx.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error plussing chat: %s", err)
@ -626,11 +625,11 @@ func chatplusone(userid int64) {
func chatnewnone(userid int64) {
var user *WhatAbout
ok := somenumberedusers.Get(userid, &user)
if !ok || user.Options.Chats == 0 {
if !ok || user.Options.ChatCount == 0 {
return
}
options := user.Options
options.Chats = 0
options.ChatCount = 0
j, err := jsonify(options)
if err == nil {
db := opendatabase()
@ -643,6 +642,45 @@ func chatnewnone(userid int64) {
somenumberedusers.Clear(user.ID)
}
func meplusone(tx *sql.Tx, userid int64) {
var user *WhatAbout
ok := somenumberedusers.Get(userid, &user)
if !ok {
return
}
options := user.Options
options.MeCount += 1
j, err := jsonify(options)
if err == nil {
_, err = tx.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error plussing me: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
}
func menewnone(userid int64) {
var user *WhatAbout
ok := somenumberedusers.Get(userid, &user)
if !ok || user.Options.MeCount == 0 {
return
}
options := user.Options
options.MeCount = 0
j, err := jsonify(options)
if err == nil {
db := opendatabase()
_, err = db.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error noneing me: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
}
func loadchatter(userid int64) []*Chatter {
duedt := time.Now().Add(-3 * 24 * time.Hour).UTC().Format(dbtimeformat)
rows, err := stmtLoadChonks.Query(userid, duedt)
@ -724,6 +762,9 @@ func savehonk(h *Honk) error {
err = saveextras(tx, h)
}
if err == nil {
if h.Whofore == 1 {
meplusone(tx, h.UserID)
}
err = tx.Commit()
} else {
tx.Rollback()

View File

@ -2,9 +2,9 @@ changelog
=== next
+ Images in the hooter.
+ Low key unread counters.
+ Unread count for chatter.
+ Images in the hooter.
+ More flexible hashtag characters.

View File

@ -56,7 +56,8 @@ type UserOptions struct {
Avatar string `json:",omitempty"`
MapLink string `json:",omitempty"`
Reaction string `json:",omitempty"`
Chats int
MeCount int64
ChatCount int64
}
type KeyInfo struct {

View File

@ -20,7 +20,7 @@
<summary>menu<span> {{ .UserInfo.Name }}</span></summary>
<ul>
<li><a id="homelink" href="/">home</a>
<li><a id="atmelink" href="/atme">@me</a>
<li><a id="atmelink" href="/atme">@me<span id=mecount>{{ if .UserInfo.Options.MeCount }}({{ .UserInfo.Options.MeCount }}){{ end }}</span></a>
<li><a id="firstlink" href="/first">first</a>
<li style="list-style-type:none; margin-left:-1em">
<details>
@ -31,7 +31,7 @@
{{ end }}
</ul>
</details>
<li><a href="/chatter">chatter{{ if .UserInfo.Options.Chats }} ({{ .UserInfo.Options.Chats }}){{ end }}</a>
<li><a href="/chatter">chatter<span id=chatcount>{{ if .UserInfo.Options.ChatCount }}({{ .UserInfo.Options.ChatCount }}){{ end }}</span></a>
<li><a href="/o">tags</a>
<li><a href="/events">events</a>
<li><a id="longagolink" href="/longago">long ago</a>

View File

@ -92,6 +92,19 @@ function fillinhonks(xhr, glowit) {
doc.innerHTML = resp.Honks
var honks = doc.children
var mecount = document.getElementById("mecount")
if (resp.MeCount) {
mecount.innerHTML = "(" + resp.MeCount + ")"
} else {
mecount.innerHTML = ""
}
var chatcount = document.getElementById("chatcount")
if (resp.ChatCount) {
chatcount.innerHTML = "(" + resp.ChatCount + ")"
} else {
chatcount.innerHTML = ""
}
var srvel = document.getElementById("srvmsg")
while (srvel.children[0]) {
srvel.children[0].remove()

15
web.go
View File

@ -116,6 +116,7 @@ func homepage(w http.ResponseWriter, r *http.Request) {
templinfo["PageName"] = "atme"
honks = gethonksforme(userid, 0)
honks = osmosis(honks, userid, false)
menewnone(userid)
case "/longago":
templinfo["ServerMessage"] = "long ago and far away!"
templinfo["PageName"] = "longago"
@ -2195,9 +2196,11 @@ func nomoroboto(w http.ResponseWriter, r *http.Request) {
}
type Hydration struct {
Tophid int64
Srvmsg template.HTML
Honks string
Tophid int64
Srvmsg template.HTML
Honks string
MeCount int64
ChatCount int64
}
func webhydra(w http.ResponseWriter, r *http.Request) {
@ -2216,6 +2219,7 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
case "atme":
honks = gethonksforme(userid, wanted)
honks = osmosis(honks, userid, false)
menewnone(userid)
hydra.Srvmsg = "at me!"
case "longago":
honks = gethonksfromlongago(userid, wanted)
@ -2264,6 +2268,8 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
}
reverbolate(userid, honks)
user, _ := butwhatabout(u.Username)
var buf strings.Builder
templinfo["Honks"] = honks
templinfo["MapLink"] = getmaplink(u)
@ -2274,6 +2280,8 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
return
}
hydra.Honks = buf.String()
hydra.MeCount = user.Options.MeCount
hydra.ChatCount = user.Options.ChatCount
w.Header().Set("Content-Type", "application/json")
j, _ := jsonify(&hydra)
io.WriteString(w, j)
@ -2328,6 +2336,7 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
case "atme":
honks = gethonksforme(userid, wanted)
honks = osmosis(honks, userid, false)
menewnone(userid)
case "longago":
honks = gethonksfromlongago(userid, wanted)
honks = osmosis(honks, userid, false)