-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2024 Dima Krasner | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package front | ||
|
||
import ( | ||
"crypto/sha256" | ||
"github.com/dimkr/tootik/front/text" | ||
"github.com/dimkr/tootik/front/text/plain" | ||
"net/url" | ||
"time" | ||
"unicode/utf8" | ||
) | ||
|
||
const ( | ||
minBioEditInterval = time.Minute * 30 | ||
maxBioLength = 500 | ||
) | ||
|
||
func bio(w text.Writer, r *request) { | ||
if r.User == nil { | ||
w.Redirect("/users") | ||
return | ||
} | ||
|
||
now := time.Now() | ||
|
||
if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < minBioEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < minBioEditInterval) { | ||
r.Log.Warn("Throttled request to set summary") | ||
w.Status(40, "Please try again later") | ||
return | ||
} | ||
|
||
if r.URL.RawQuery == "" { | ||
w.Status(10, "Summary") | ||
return | ||
} | ||
|
||
summary, err := url.QueryUnescape(r.URL.RawQuery) | ||
if err != nil { | ||
w.Status(40, "Bad input") | ||
return | ||
} | ||
|
||
if utf8.RuneCountInString(summary) > maxBioLength { | ||
w.Status(40, "Summary is too long") | ||
return | ||
} | ||
|
||
if _, err := r.Exec( | ||
"update persons set actor = json_set(actor, '$.summary', $1, '$.updated', $2) where id = $3", | ||
plain.ToHTML(summary, nil), | ||
now.Format(time.RFC3339Nano), | ||
r.User.ID, | ||
); err != nil { | ||
r.Log.Error("Failed to update summary", "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
w.Redirectf("/users/outbox/%x", sha256.Sum256([]byte(r.User.ID))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2024 Dima Krasner | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBio_Throttled(t *testing.T) { | ||
server := newTestServer() | ||
defer server.Shutdown() | ||
|
||
assert := assert.New(t) | ||
|
||
summary := server.Handle("/users/bio?Hello%20world", server.Alice) | ||
assert.Equal("40 Please try again later\r\n", summary) | ||
} | ||
|
||
func TestBio_HappyFlow(t *testing.T) { | ||
server := newTestServer() | ||
defer server.Shutdown() | ||
|
||
assert := assert.New(t) | ||
|
||
server.Alice.Published.Time = server.Alice.Published.Time.Add(-time.Hour) | ||
|
||
summary := server.Handle("/users/bio?Hello%20world", server.Alice) | ||
assert.Equal(fmt.Sprintf("30 /users/outbox/%x\r\n", sha256.Sum256([]byte(server.Alice.ID))), summary) | ||
|
||
outbox := server.Handle(fmt.Sprintf("/users/outbox/%x", sha256.Sum256([]byte(server.Alice.ID))), server.Bob) | ||
assert.Contains(strings.Split(outbox, "\n"), "> Hello world") | ||
} | ||
|
||
func TestBio_TooLong(t *testing.T) { | ||
server := newTestServer() | ||
defer server.Shutdown() | ||
|
||
assert := assert.New(t) | ||
|
||
server.Alice.Published.Time = server.Alice.Published.Time.Add(-time.Hour) | ||
|
||
summary := server.Handle("/users/bio?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", server.Alice) | ||
assert.Equal("40 Summary is too long\r\n", summary) | ||
} | ||
|
||
func TestBio_MultiLine(t *testing.T) { | ||
server := newTestServer() | ||
defer server.Shutdown() | ||
|
||
assert := assert.New(t) | ||
|
||
server.Alice.Published.Time = server.Alice.Published.Time.Add(-time.Hour) | ||
|
||
summary := server.Handle("/users/bio?Hello%0Aworld", server.Alice) | ||
assert.Equal(fmt.Sprintf("30 /users/outbox/%x\r\n", sha256.Sum256([]byte(server.Alice.ID))), summary) | ||
|
||
outbox := strings.Split(server.Handle(fmt.Sprintf("/users/outbox/%x", sha256.Sum256([]byte(server.Alice.ID))), server.Bob), "\n") | ||
assert.Contains(outbox, "> Hello") | ||
assert.Contains(outbox, "> world") | ||
} | ||
|
||
func TestBio_MultiLineWithLink(t *testing.T) { | ||
server := newTestServer() | ||
defer server.Shutdown() | ||
|
||
assert := assert.New(t) | ||
|
||
server.Alice.Published.Time = server.Alice.Published.Time.Add(-time.Hour) | ||
|
||
summary := server.Handle("/users/bio?Hi%21%0A%0AI%27m%20a%20friend%20of%20https%3a%2f%2flocalhost.localdomain%3a8443%2fuser%2fbob", server.Alice) | ||
assert.Equal(fmt.Sprintf("30 /users/outbox/%x\r\n", sha256.Sum256([]byte(server.Alice.ID))), summary) | ||
|
||
outbox := strings.Split(server.Handle(fmt.Sprintf("/users/outbox/%x", sha256.Sum256([]byte(server.Alice.ID))), server.Bob), "\n") | ||
assert.Contains(outbox, "> Hi!") | ||
assert.Contains(outbox, "> I'm a friend of https://localhost.localdomain:8443/user/bob") | ||
assert.Contains(outbox, "=> https://localhost.localdomain:8443/user/bob https://localhost.localdomain:8443/user/bob") | ||
} |
Oops, something went wrong.