-
Notifications
You must be signed in to change notification settings - Fork 5
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
6 changed files
with
218 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2023 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 fed | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dimkr/tootik/ap" | ||
"github.com/dimkr/tootik/cfg" | ||
"time" | ||
) | ||
|
||
func Edit(ctx context.Context, db *sql.DB, note *ap.Object, newContent string) error { | ||
now := time.Now() | ||
|
||
note.Content = newContent | ||
note.Updated = &now | ||
|
||
body, err := json.Marshal(note) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal note: %w", err) | ||
} | ||
|
||
updateID := fmt.Sprintf("https://%s/update/%x", cfg.Domain, sha256.Sum256([]byte(fmt.Sprintf("%s|%d", note.ID, now.Unix())))) | ||
|
||
update, err := json.Marshal(ap.Activity{ | ||
Context: "https://www.w3.org/ns/activitystreams", | ||
ID: updateID, | ||
Type: ap.UpdateActivity, | ||
Actor: note.AttributedTo, | ||
Object: note, | ||
To: note.To, | ||
CC: note.CC, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal update: %w", err) | ||
} | ||
|
||
tx, err := db.BeginTx(ctx, nil) | ||
if err != nil { | ||
return fmt.Errorf("Failed to begin transaction: %w", err) | ||
} | ||
defer tx.Rollback() | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`UPDATE notes SET object = ? WHERE id = ?`, | ||
string(body), | ||
note.ID, | ||
); err != nil { | ||
return fmt.Errorf("Failed to update note: %w", err) | ||
} | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`INSERT INTO outbox (activity) VALUES(?)`, | ||
string(update), | ||
); err != nil { | ||
return fmt.Errorf("Failed to insert update activity: %w", err) | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return fmt.Errorf("Failed to update note: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,107 @@ | ||
/* | ||
Copyright 2023 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 ( | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"github.com/dimkr/tootik/ap" | ||
"github.com/dimkr/tootik/cfg" | ||
"github.com/dimkr/tootik/fed" | ||
"github.com/dimkr/tootik/text" | ||
"github.com/dimkr/tootik/text/plain" | ||
"math" | ||
"net/url" | ||
"path/filepath" | ||
"regexp" | ||
"time" | ||
) | ||
|
||
func init() { | ||
handlers[regexp.MustCompile(`^/users/edit/[0-9a-f]{64}`)] = edit | ||
} | ||
|
||
func edit(w text.Writer, r *request) { | ||
if r.User == nil { | ||
w.Redirect("/users") | ||
return | ||
} | ||
|
||
if r.URL.RawQuery == "" { | ||
w.Status(10, "Post content") | ||
return | ||
} | ||
|
||
hash := filepath.Base(r.URL.Path) | ||
|
||
var noteString string | ||
if err := r.QueryRow(`select object from notes where hash = ? and author = ?`, hash, r.User.ID).Scan(¬eString); err != nil && errors.Is(err, sql.ErrNoRows) { | ||
r.Log.Warn("Attempted to edit non-existing post", "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} else if err != nil { | ||
r.Log.Warn("Failed to fetch post to edit", "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
var note ap.Object | ||
if err := json.Unmarshal([]byte(noteString), ¬e); err != nil { | ||
r.Log.Warn("Failed to unmarshal post to edit", "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
var edits int | ||
if err := r.QueryRow(`select count(*) from outbox where activity->>'object.id' = ? and (activity->>'type' = 'Update' or activity->>'type' = 'Create')`, note.ID).Scan(&edits); err != nil { | ||
r.Log.Warn("Failed to count post edits", "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
lastEditTime := note.Published | ||
if note.Updated != nil && *note.Updated != (time.Time{}) { | ||
lastEditTime = *note.Updated | ||
} | ||
|
||
canEdit := lastEditTime.Add(time.Minute * time.Duration(math.Pow(4, float64(edits)))) | ||
if time.Now().Before(canEdit) { | ||
r.Log.Warn("Throttled request to edit post", "note", note.ID, "can", canEdit) | ||
w.Status(40, "Please try again later") | ||
return | ||
} | ||
|
||
content, err := url.QueryUnescape(r.URL.RawQuery) | ||
if err != nil { | ||
w.Error() | ||
return | ||
} | ||
|
||
if len(content) > cfg.MaxPostsLength { | ||
w.Status(40, "Post is too long") | ||
return | ||
} | ||
|
||
if err := fed.Edit(r.Context, r.DB, ¬e, plain.ToHTML(content)); err != nil { | ||
r.Log.Error("Failed to update post", "note", note.ID, "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
w.Redirectf("/users/view/%s", hash) | ||
} |
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,14 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
func edits(ctx context.Context, tx *sql.Tx) error { | ||
if _, err := tx.ExecContext(ctx, `CREATE INDEX outboxobjectid ON outbox(activity->>'object.id')`); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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