-
Notifications
You must be signed in to change notification settings - Fork 2
feat(parity): padDiff port, compactPad, anonymizeAuthor, preAuthorize hooks #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,178 @@ | ||
| package pad | ||
|
|
||
| import ( | ||
| "github.com/ether/etherpad-go/lib" | ||
| errors2 "github.com/ether/etherpad-go/lib/api/errors" | ||
| utils2 "github.com/ether/etherpad-go/lib/api/utils" | ||
| "github.com/ether/etherpad-go/lib/apool" | ||
| "github.com/ether/etherpad-go/lib/author" | ||
| io2 "github.com/ether/etherpad-go/lib/io" | ||
| "github.com/ether/etherpad-go/lib/paddiff" | ||
| "github.com/ether/etherpad-go/lib/utils" | ||
| "github.com/ether/etherpad-go/lib/ws" | ||
| "github.com/gofiber/fiber/v3" | ||
| ) | ||
|
|
||
| // CompactPadRequest represents the request to compact a pad's revision history | ||
| type CompactPadRequest struct { | ||
| KeepRevisions int `json:"keepRevisions"` | ||
| } | ||
|
|
||
| // CompactPadResponse represents the response after compacting a pad | ||
| type CompactPadResponse struct { | ||
| Ok bool `json:"ok"` | ||
| KeepRevisions int `json:"keepRevisions"` | ||
| } | ||
|
|
||
| // CompactPad godoc | ||
| // @Summary Compact a pad's revision history | ||
| // @Description Collapses the pad's revision history so that only the last keepRevisions revisions are kept (original API: compactPad). The revisions below the cut are composed into a single base revision; pad text is preserved. Destructive — consider exporting the pad first. | ||
| // @Tags Pads | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param padId path string true "Pad ID" | ||
| // @Param request body CompactPadRequest true "Number of recent revisions to keep (must be >= 1 and lower than the pad's head revision)" | ||
| // @Success 200 {object} CompactPadResponse | ||
| // @Failure 400 {object} errors.Error | ||
| // @Failure 404 {object} errors.Error | ||
| // @Failure 500 {object} errors.Error | ||
| // @Security BearerAuth | ||
| // @Router /admin/api/pads/{padId}/compact [post] | ||
| func CompactPad(initStore *lib.InitStore) fiber.Handler { | ||
| return func(c fiber.Ctx) error { | ||
| padId := c.Params("padId") | ||
| var request CompactPadRequest | ||
| if err := c.Bind().Body(&request); err != nil { | ||
| return c.Status(400).JSON(errors2.InvalidRequestError) | ||
| } | ||
|
|
||
| foundPad, err := utils2.GetPadSafe(padId, true, nil, nil, initStore.PadManager) | ||
| if err != nil { | ||
| return c.Status(404).JSON(errors2.PadNotFoundError) | ||
| } | ||
|
|
||
| if request.KeepRevisions < 1 { | ||
| return c.Status(400).JSON(errors2.NewInvalidParamError("keepRevisions must be at least 1")) | ||
| } | ||
| if request.KeepRevisions >= foundPad.Head { | ||
| return c.Status(400).JSON(errors2.NewInvalidParamError("keepRevisions must be lower than the pad's head revision")) | ||
| } | ||
|
|
||
| // Reuse the revision compaction the admin UI uses | ||
| // (AdminMessageHandler.DeleteRevisions). DeleteRevisions only needs the | ||
| // store, pad manager, pad message handler and logger, all of which are | ||
| // available from the InitStore, so a handler is wired up on the fly | ||
| // (hub is not used by DeleteRevisions). | ||
| adminHandler := ws.NewAdminMessageHandler(initStore.Store, initStore.Hooks, initStore.PadManager, initStore.Handler, initStore.Logger, nil, initStore.C) | ||
| if err := adminHandler.DeleteRevisions(padId, request.KeepRevisions); err != nil { | ||
| initStore.Logger.Errorf("Error compacting pad %s: %v", padId, err) | ||
| return c.Status(500).JSON(errors2.InternalServerError) | ||
| } | ||
|
|
||
| return c.JSON(CompactPadResponse{ | ||
| Ok: true, | ||
| KeepRevisions: request.KeepRevisions, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // CreateDiffHTML godoc | ||
| // @Summary Create an HTML diff between two revisions | ||
| // @Description Returns the changes between startRev and endRev as HTML (original API: createDiffHTML). Insertions keep their author attribution (rendered with the author's color), deletions are re-inserted with a 'removed' attribute (rendered struck through). Also returns the list of authors involved in the changes. | ||
| // @Tags Pads | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param padId path string true "Pad ID" | ||
| // @Param startRev query int true "Start revision number" | ||
| // @Param endRev query int false "End revision number (defaults to the head revision)" | ||
| // @Success 200 {object} DiffHTMLResponse | ||
| // @Failure 400 {object} errors.Error | ||
| // @Failure 404 {object} errors.Error | ||
| // @Failure 500 {object} errors.Error | ||
| // @Security BearerAuth | ||
| // @Router /admin/api/pads/{padId}/diffHTML [get] | ||
| func CreateDiffHTML(initStore *lib.InitStore) fiber.Handler { | ||
| return func(c fiber.Ctx) error { | ||
| padId := c.Params("padId") | ||
|
|
||
| foundPad, err := utils2.GetPadSafe(padId, true, nil, nil, initStore.PadManager) | ||
| if err != nil { | ||
| return c.Status(404).JSON(errors2.PadNotFoundError) | ||
| } | ||
|
|
||
| startRevStr := c.Query("startRev") | ||
| if startRevStr == "" { | ||
| return c.Status(400).JSON(errors2.NewMissingParamError("startRev")) | ||
| } | ||
| startRev, err := utils.CheckValidRev(startRevStr) | ||
| if err != nil { | ||
| return c.Status(400).JSON(errors2.InvalidRevisionError) | ||
| } | ||
|
|
||
| var endRev *int | ||
| if endRevStr := c.Query("endRev"); endRevStr != "" { | ||
| endRevNum, err := utils.CheckValidRev(endRevStr) | ||
| if err != nil { | ||
| return c.Status(400).JSON(errors2.InvalidRevisionError) | ||
| } | ||
| endRev = endRevNum | ||
| } | ||
|
|
||
| // The original API clamps startRev to the head revision before | ||
| // validating the range; endRev is clamped inside GetValidRevisionRange. | ||
| head := foundPad.Head | ||
| start := *startRev | ||
| if start > head { | ||
| start = head | ||
| } | ||
|
|
||
| from, to, ok := paddiff.GetValidRevisionRange(start, endRev, head) | ||
| if !ok { | ||
| return c.Status(400).JSON(errors2.NewInvalidParamError("invalid revision range")) | ||
| } | ||
|
|
||
| diffAText, authors, err := paddiff.CreateDiffAText(foundPad, &foundPad.Pool, from, to) | ||
| if err != nil { | ||
| initStore.Logger.Errorf("Error creating diff atext for pad %s: %v", padId, err) | ||
| return c.Status(500).JSON(errors2.InternalServerError) | ||
| } | ||
|
|
||
| // Render the diff atext with the regular export-HTML pipeline (it | ||
| // understands the 'removed' attribute). GetPadHTML reads pad.AText when | ||
| // no revision is requested, so a shallow copy of the pad carrying the | ||
| // diff atext is passed. | ||
| padWithDiff := *foundPad | ||
| padWithDiff.AText = *diffAText | ||
|
|
||
| authorColors := buildAuthorColors(&foundPad.Pool, initStore.AuthorManager) | ||
| exporter := io2.NewExportHtml(initStore.PadManager, initStore.AuthorManager, initStore.Hooks) | ||
| html, err := exporter.GetPadHTML(&padWithDiff, nil, authorColors) | ||
| if err != nil { | ||
| initStore.Logger.Errorf("Error rendering diff HTML for pad %s: %v", padId, err) | ||
| return c.Status(500).JSON(errors2.InternalServerError) | ||
| } | ||
|
|
||
| return c.JSON(DiffHTMLResponse{ | ||
| HTML: html, | ||
| Authors: authors, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // buildAuthorColors maps the author IDs found in the pad's attribute pool to | ||
| // their colors (equivalent of the original pad.getAllAuthorColors; mirrors the | ||
| // unexported buildAuthorColorCache in lib/io/exportHtml.go). | ||
| func buildAuthorColors(padPool *apool.APool, authorManager *author.Manager) map[string]string { | ||
| authorColors := make(map[string]string) | ||
| for _, attr := range padPool.NumToAttrib { | ||
| if attr.Key == "author" && attr.Value != "" { | ||
| if _, exists := authorColors[attr.Value]; exists { | ||
| continue | ||
| } | ||
| if authorData, err := authorManager.GetAuthor(attr.Value); err == nil { | ||
| authorColors[attr.Value] = authorData.ColorId | ||
| } | ||
| } | ||
| } | ||
| return authorColors | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -169,6 +169,47 @@ func (m *Manager) GetAuthor(authorId string) (*Author, error) { | |
| return &mappedDbAuthor, nil | ||
| } | ||
|
|
||
| /** | ||
| * AnonymizeAuthor performs GDPR Art. 17 erasure for an author, mirroring the | ||
| * original Etherpad's AuthorManager.anonymizeAuthor (API 1.3.1): | ||
| * - the token binding that links a person to this author id is severed | ||
| * first, so a concurrent token lookup can no longer resolve the author | ||
| * mid-erasure, | ||
| * - the display identity on the author record is zeroed (name -> null, | ||
| * colorId -> 0) while the record itself is kept, | ||
| * - authorship on chat messages the author posted is nulled; the message | ||
| * text itself survives, | ||
| * - pad content, revisions and attribute pools are left intact: changeset | ||
| * references are opaque without the identity record. | ||
| * The operation is idempotent: re-running it leaves the same erased state. | ||
| * Returns db.AuthorNotFoundError if the author does not exist. | ||
| * @param {String} authorId The id of the author | ||
| */ | ||
| func (m *Manager) AnonymizeAuthor(authorId string) error { | ||
| if _, err := m.Db.GetAuthor(authorId); err != nil { | ||
| return errors.New(db.AuthorNotFoundError) | ||
| } | ||
|
Comment on lines
+188
to
+191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Db errors returned as 404 author.Manager.AnonymizeAuthor converts any error from Db.GetAuthor into db.AuthorNotFoundError, so DB/query failures are misreported as “not found” and the API returns 404 instead of 500. This hides real backend failures and breaks correct error semantics. Agent Prompt
|
||
|
|
||
| // Sever the token binding first, before touching anything else. | ||
| if err := m.Db.RemoveTokenOfAuthor(authorId); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Zero the display identity. The token was already removed above, so | ||
| // SaveAuthor's token-preservation has nothing left to preserve. | ||
| if err := m.saveAuthor(Author{ | ||
| Id: authorId, | ||
| Name: nil, | ||
| ColorId: "0", | ||
| Timestamp: time.Now().Unix(), | ||
| }); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Null authorship on chat messages the author posted. | ||
| return m.Db.ClearChatAuthorship(authorId) | ||
| } | ||
|
|
||
| func (m *Manager) GetPadsOfAuthor(authorId string) (*[]string, error) { | ||
| padIds, err := m.Db.GetPadIdsOfAuthor(authorId) | ||
| if err != nil { | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Diffhtml mutates cached pool
🐞 Bug☼ ReliabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools