Mit Handles lassen sich Nutzer in Beiträgen ansprechen. Der Server sucht beim Anlegen und Bearbeiten @handle im Text und speichert, wem der Handle in dem Moment gehört (mention: pid, handle, uid). Die API liefert je Beitrag geschriebenen -> heutigen Handle; so zeigt ein alter Link nach einer Umbenennung weiter auf dieselbe Person statt auf den neuen Träger. Erwähnte bekommen eine Benachrichtigung (kind mention), außer dem Autor selbst und dem Elternautor, der schon die Antwort gemeldet bekommt. Der Abgleich läuft über Personen: wer nach dem Bearbeiten weiter erwähnt ist, behält seine alte Benachrichtigung. Höchstens 10 Erwähnungen pro Beitrag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
3.5 KiB
Go
113 lines
3.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseMentions(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
in string
|
|
want []string
|
|
}{
|
|
{"hallo @Bob, und @carol!", []string{"bob", "carol"}},
|
|
{"@bob @BOB @bob", []string{"bob"}},
|
|
{"mail@bob.org x@bob", nil},
|
|
{"@ab @" + "abcdefghijklmnopqrstu", nil}, // zu kurz, zu lang
|
|
{"(@bob) #tag @_x_", []string{"bob", "_x_"}},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := parseMentions(tt.in); !slices.Equal(got, tt.want) {
|
|
t.Errorf("parseMentions(%q) = %q, erwartet %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
many := ""
|
|
for i := range 15 {
|
|
many += fmt.Sprintf(" @user%02d", i)
|
|
}
|
|
if n := len(parseMentions(many)); n != maxMentions {
|
|
t.Errorf("%d Erwähnungen, erwartet höchstens %d", n, maxMentions)
|
|
}
|
|
}
|
|
|
|
// mentionsOf liest die Erwähnungen eines Beitrags aus dem Thread.
|
|
func mentionsOf(t *testing.T, c *http.Client, srv *testServer, pid int64) map[string]string {
|
|
t.Helper()
|
|
var th struct {
|
|
Entry struct {
|
|
Mentions map[string]string `json:"mentions"`
|
|
} `json:"entry"`
|
|
}
|
|
getAs(t, c, fmt.Sprintf("%s/api/entry/%d/thread", srv.URL, pid), &th)
|
|
return th.Entry.Mentions
|
|
}
|
|
|
|
func notificationsOf(t *testing.T, c *http.Client, srv *testServer) []notificationJSON {
|
|
t.Helper()
|
|
var list notificationList
|
|
getAs(t, c, srv.URL+"/api/notifications", &list)
|
|
return list.Notifications
|
|
}
|
|
|
|
func TestMentions(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
bob := registerAndLogin(t, srv, "bob")
|
|
carol := registerAndLogin(t, srv, "carol")
|
|
|
|
root := createEntry(t, alice, srv, post("hallo @Bob und @carol, @alice, mail@bob.org, @niemand"))
|
|
if got := mentionsOf(t, alice, srv, root); len(got) != 3 || got["bob"] != "bob" || got["alice"] != "alice" {
|
|
t.Errorf("mentions: %v", got)
|
|
}
|
|
for _, c := range []*http.Client{bob, carol} {
|
|
n := notificationsOf(t, c, srv)
|
|
if len(n) != 1 || n[0].Kind != "mention" || n[0].Message != "@alice hat dich erwähnt." ||
|
|
n[0].Link != fmt.Sprintf("/e/%d", root) {
|
|
t.Errorf("erwähnt: %+v", n)
|
|
}
|
|
}
|
|
if n := notificationsOf(t, alice, srv); len(n) != 0 {
|
|
t.Errorf("selbst erwähnt: %+v", n)
|
|
}
|
|
|
|
// Antwort samt Erwähnung des Elternautors: nur die Antwort-Benachrichtigung.
|
|
createEntry(t, bob, srv, reply(root, "danke @alice"))
|
|
if n := notificationsOf(t, alice, srv); len(n) != 1 || n[0].Kind != "reply" {
|
|
t.Errorf("antwort mit erwähnung: %+v", n)
|
|
}
|
|
|
|
// Umbenennen: der alte Link führt zur selben Person, nicht zum neuen
|
|
// Träger des Handles.
|
|
for _, r := range []struct {
|
|
c *http.Client
|
|
handle string
|
|
}{{bob, "robert"}, {carol, "bob"}} {
|
|
resp := postForm(t, r.c, srv.URL+"/api/user/handle", url.Values{"handle": {r.handle}})
|
|
resp.Body.Close()
|
|
}
|
|
if got := mentionsOf(t, alice, srv, root); got["bob"] != "robert" || got["carol"] != "bob" {
|
|
t.Errorf("nach umbenennen: %v", got)
|
|
}
|
|
|
|
// Bearbeiten: weggefallene Erwähnung verschwindet samt Benachrichtigung.
|
|
resp := postForm(t, alice, fmt.Sprintf("%s/api/entry/%d/edit", srv.URL, root),
|
|
url.Values{"content": {"hallo @robert"}})
|
|
resp.Body.Close()
|
|
if got := mentionsOf(t, alice, srv, root); len(got) != 1 || got["robert"] != "robert" {
|
|
t.Errorf("nach bearbeiten: %v", got)
|
|
}
|
|
if n := notificationsOf(t, carol, srv); len(n) != 0 {
|
|
t.Errorf("carol nach bearbeiten: %+v", n)
|
|
}
|
|
// robert war schon als @bob erwähnt: die Benachrichtigung bleibt die alte
|
|
// (schon gesehene), keine zweite.
|
|
if n := notificationsOf(t, bob, srv); len(n) != 1 || !n[0].Seen {
|
|
t.Errorf("robert nach bearbeiten: %+v", n)
|
|
}
|
|
}
|