mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-30 00:06:14 -06:00
Improve titles, trim body to reasonable length
This commit is contained in:
parent
13eda35985
commit
93aeadbd9f
30 changed files with 16492 additions and 9 deletions
|
|
@ -56,7 +56,7 @@ const (
|
|||
NotificationPendingReply NotificationType = 10 // NotificationPendingReply -- Someone has replied to a status of yours, which requires approval by you.
|
||||
NotificationPendingReblog NotificationType = 11 // NotificationPendingReblog -- Someone has boosted a status of yours, which requires approval by you.
|
||||
NotificationAdminReport NotificationType = 12 // NotificationAdminReport -- someone has submitted a new report to the instance.
|
||||
NotificationUpdate NotificationType = 13
|
||||
NotificationUpdate NotificationType = 13 // NotificationUpdate -- someone has edited their status.
|
||||
NotificationTypeNumValues NotificationType = 14 // NotificationTypeNumValues -- 1 + number of max notification type
|
||||
)
|
||||
|
||||
|
|
|
|||
45
internal/text/substring.go
Normal file
45
internal/text/substring.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// GoToSocial
|
||||
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package text
|
||||
|
||||
import (
|
||||
"github.com/rivo/uniseg"
|
||||
)
|
||||
|
||||
// FirstNBytesByWords produces a prefix substring of up to n bytes from a given string, respecting Unicode grapheme and
|
||||
// word boundaries. The substring may be empty, and may include leading or trailing whitespace.
|
||||
func FirstNBytesByWords(s string, n int) string {
|
||||
substringEnd := 0
|
||||
|
||||
graphemes := uniseg.NewGraphemes(s)
|
||||
for graphemes.Next() {
|
||||
|
||||
if !graphemes.IsWordBoundary() {
|
||||
continue
|
||||
}
|
||||
|
||||
_, end := graphemes.Positions()
|
||||
if end > n {
|
||||
break
|
||||
}
|
||||
|
||||
substringEnd = end
|
||||
}
|
||||
|
||||
return s[0:substringEnd]
|
||||
}
|
||||
47
internal/text/substring_test.go
Normal file
47
internal/text/substring_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// GoToSocial
|
||||
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package text_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/text"
|
||||
)
|
||||
|
||||
type SubstringTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *SubstringTestSuite) TestText() {
|
||||
suite.Equal(
|
||||
"Sphinx of black quartz, ",
|
||||
text.FirstNBytesByWords("Sphinx of black quartz, judge my vow!", 25),
|
||||
)
|
||||
}
|
||||
|
||||
func (suite *SubstringTestSuite) TestEmoji() {
|
||||
suite.Equal(
|
||||
"🏳️⚧️ ",
|
||||
text.FirstNBytesByWords("🏳️⚧️ 🙈", 20),
|
||||
)
|
||||
}
|
||||
|
||||
func TestSubstringTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(SubstringTestSuite))
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
webpushgo "github.com/SherClockHolmes/webpush-go"
|
||||
|
|
@ -59,9 +60,13 @@ func NewRealSender(httpClient *http.Client, state *state.State) Sender {
|
|||
// while waiting for the client to retrieve them.
|
||||
const TTL = 48 * time.Hour
|
||||
|
||||
// responseBodyMaxLen limits how much of the Web Push server response we use for error messages.
|
||||
// responseBodyMaxLen limits how much of the Web Push server response we read for error messages.
|
||||
const responseBodyMaxLen = 1024
|
||||
|
||||
// bodyMaxLen is a polite maximum length for a Web Push notification's body text, in bytes.
|
||||
// Note that this isn't limited per se, but Web Push servers may reject anything with a total request body size over 4k.
|
||||
const bodyMaxLen = 3000
|
||||
|
||||
func (r *realSender) Send(
|
||||
ctx context.Context,
|
||||
notification *gtsmodel.Notification,
|
||||
|
|
@ -126,7 +131,7 @@ func (r *realSender) Send(
|
|||
vapidKeyPair,
|
||||
vapidSubjectEmail,
|
||||
subscription,
|
||||
notification.TargetAccount,
|
||||
notification,
|
||||
apiNotification,
|
||||
); err != nil {
|
||||
log.Errorf(
|
||||
|
|
@ -148,7 +153,7 @@ func (r *realSender) sendToSubscription(
|
|||
vapidKeyPair *gtsmodel.VAPIDKeyPair,
|
||||
vapidSubjectEmail string,
|
||||
subscription *gtsmodel.WebPushSubscription,
|
||||
targetAccount *gtsmodel.Account,
|
||||
notification *gtsmodel.Notification,
|
||||
apiNotification *apimodel.Notification,
|
||||
) error {
|
||||
// Get the associated access token.
|
||||
|
|
@ -162,7 +167,7 @@ func (r *realSender) sendToSubscription(
|
|||
NotificationID: apiNotification.ID,
|
||||
NotificationType: apiNotification.Type,
|
||||
Icon: apiNotification.Account.Avatar,
|
||||
PreferredLocale: targetAccount.Settings.Language,
|
||||
PreferredLocale: notification.TargetAccount.Settings.Language,
|
||||
AccessToken: token.Access,
|
||||
}
|
||||
|
||||
|
|
@ -171,8 +176,45 @@ func (r *realSender) sendToSubscription(
|
|||
if displayNameOrAcct == "" {
|
||||
displayNameOrAcct = apiNotification.Account.Acct
|
||||
}
|
||||
// TODO: (Vyr) improve copy
|
||||
pushNotification.Title = fmt.Sprintf("%s from %s", apiNotification.Type, displayNameOrAcct)
|
||||
switch notification.NotificationType {
|
||||
case gtsmodel.NotificationFollow:
|
||||
pushNotification.Title = fmt.Sprintf("%s followed you", displayNameOrAcct)
|
||||
case gtsmodel.NotificationFollowRequest:
|
||||
pushNotification.Title = fmt.Sprintf("%s requested to follow you", displayNameOrAcct)
|
||||
case gtsmodel.NotificationMention:
|
||||
pushNotification.Title = fmt.Sprintf("%s mentioned you", displayNameOrAcct)
|
||||
case gtsmodel.NotificationReblog:
|
||||
pushNotification.Title = fmt.Sprintf("%s boosted your post", displayNameOrAcct)
|
||||
case gtsmodel.NotificationFavourite:
|
||||
pushNotification.Title = fmt.Sprintf("%s faved your post", displayNameOrAcct)
|
||||
case gtsmodel.NotificationPoll:
|
||||
if subscription.AccountID == notification.TargetAccountID {
|
||||
pushNotification.Title = fmt.Sprintf("Your poll has ended")
|
||||
} else {
|
||||
pushNotification.Title = fmt.Sprintf("%s's poll has ended", displayNameOrAcct)
|
||||
}
|
||||
case gtsmodel.NotificationStatus:
|
||||
pushNotification.Title = fmt.Sprintf("%s posted", displayNameOrAcct)
|
||||
case gtsmodel.NotificationAdminSignup:
|
||||
pushNotification.Title = fmt.Sprintf("%s requested to sign up", displayNameOrAcct)
|
||||
case gtsmodel.NotificationPendingFave:
|
||||
pushNotification.Title = fmt.Sprintf("%s faved your post, which requires your approval", displayNameOrAcct)
|
||||
case gtsmodel.NotificationPendingReply:
|
||||
pushNotification.Title = fmt.Sprintf("%s mentioned you, which requires your approval", displayNameOrAcct)
|
||||
case gtsmodel.NotificationPendingReblog:
|
||||
pushNotification.Title = fmt.Sprintf("%s boosted your post, which requires your approval", displayNameOrAcct)
|
||||
case gtsmodel.NotificationAdminReport:
|
||||
pushNotification.Title = fmt.Sprintf("%s submitted a report", displayNameOrAcct)
|
||||
case gtsmodel.NotificationUpdate:
|
||||
pushNotification.Title = fmt.Sprintf("%s updated their post", displayNameOrAcct)
|
||||
default:
|
||||
log.Warnf(ctx, "Unknown notification type: %d", notification.NotificationType)
|
||||
pushNotification.Title = fmt.Sprintf(
|
||||
"%s did something (unknown notification type %d)",
|
||||
displayNameOrAcct,
|
||||
notification.NotificationType,
|
||||
)
|
||||
}
|
||||
|
||||
// Set the notification body.
|
||||
if apiNotification.Status != nil {
|
||||
|
|
@ -184,7 +226,7 @@ func (r *realSender) sendToSubscription(
|
|||
} else {
|
||||
pushNotification.Body = text.SanitizeToPlaintext(apiNotification.Account.Note)
|
||||
}
|
||||
// TODO: (Vyr) trim this
|
||||
pushNotification.Body = firstNBytesTrimSpace(pushNotification.Body, bodyMaxLen)
|
||||
|
||||
// Encode the push notification as JSON.
|
||||
pushNotificationBytes, err := json.Marshal(pushNotification)
|
||||
|
|
@ -221,6 +263,7 @@ func (r *realSender) sendToSubscription(
|
|||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 499 &&
|
||||
resp.StatusCode != http.StatusRequestTimeout &&
|
||||
resp.StatusCode != http.StatusRequestEntityTooLarge &&
|
||||
resp.StatusCode != http.StatusTooManyRequests {
|
||||
// We should not send any more notifications to this subscription. Try to delete it.
|
||||
if err := r.state.DB.DeleteWebPushSubscriptionByTokenID(ctx, subscription.TokenID); err != nil {
|
||||
|
|
@ -256,6 +299,11 @@ func (r *realSender) sendToSubscription(
|
|||
return nil
|
||||
}
|
||||
|
||||
// firstNBytesTrimSpace returns the first N bytes of a string, trimming leading and trailing whitespace.
|
||||
func firstNBytesTrimSpace(s string, n int) string {
|
||||
return strings.TrimSpace(text.FirstNBytesByWords(strings.TrimSpace(s), n))
|
||||
}
|
||||
|
||||
// gtsHTTPClientRoundTripper helps wrap a GtS HTTP client back into a regular HTTP client,
|
||||
// so that webpush-go can use our IP filters, bad hosts list, and retries.
|
||||
type gtsHTTPClientRoundTripper struct {
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ type notifyingReadCloser struct {
|
|||
bodyClosed chan struct{}
|
||||
}
|
||||
|
||||
func (rc *notifyingReadCloser) Read(p []byte) (n int, err error) {
|
||||
func (rc *notifyingReadCloser) Read(_ []byte) (n int, err error) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue