Skip to content

Commit

Permalink
Fix broken prev/next buttons on Inbox Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Jul 4, 2023
1 parent c38a02c commit f2046e3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
8 changes: 4 additions & 4 deletions _embed/templates/user-message/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ <h1 role="link" class="clickable" script="">{{.Label}}</h1>
<div class="text-sm">

<span class="button-group">
<button id="previous" hx-get="/@me/message-newspaper?rank=LT:{{.Rank}}&folderId={{.FolderID}}&origin.followingId={{.QueryParam `origin.followingId`}}">&laquo; Prev</button>
<button id="next" hx-get="/@me/message-newspaper?rank=GT:{{.Rank}}&folderId={{.FolderID}}&origin.followingId={{.QueryParam `origin.followingId`}}">Next &raquo;</button>
<button id="previous" hx-get="/@me/messages/{{.MessageID}}/view?sibling=prev&followingId={{.QueryParam `origin.followingId`}}">&laquo; Prev</button>
<button id="next" hx-get="/@me/messages/{{.MessageID}}/view?sibling=next&followingId={{.QueryParam `origin.followingId`}}">Next &raquo;</button>
</span>

<span class="button-group">
<button hx-get="/@me/following-edit?followingId={{.Origin.FollowingID.Hex}}">Edit Source</button>
<button hx-get="/@me/message-delete?messageId={{.MessageID}}">Delete</button>
<button hx-get="/@me/block-new?messageId={{.MessageID}}">Block</button>
<button hx-get="/@me/messages/{{.MessageID}}/delete">Delete</button>
<button hx-get="/@me/inbox/block-new?messageId={{.MessageID}}">Block</button>
</span>

</div>
Expand Down
14 changes: 12 additions & 2 deletions handler/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@ func renderMessage(serverFactory *server.Factory, actionMethod render.ActionMeth
return derp.Wrap(err, location, "Error loading message", context.Param("message"))
}

// Try to load the User's Outbox
actionID := first.String(context.Param("action"), "view")
// Move to previous/next sibling if requested
if siblingType := context.QueryParam("sibling"); siblingType != "" {
followingID := context.QueryParam("followingId")
if sibling, err := inboxService.LoadSibling(message.FolderID, message.Rank, followingID, siblingType); err == nil {
message = sibling
}
}

// Render in JSON-LD (if requested)
// TODO: Templates should probably use the new "view-json" action instead.
if ok, err := handleJSONLD(context, &user); ok {
return derp.Wrap(err, location, "Error rendering JSON-LD")
}

// Try to load the User's Outbox
actionID := first.String(context.Param("action"), "view")

// Create the new Renderer
renderer, err := render.NewMessage(factory, sterankoContext, inboxService, &message, actionID)

Expand Down
42 changes: 41 additions & 1 deletion service/inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,47 @@ func (service *Inbox) LoadOrCreate(userID primitive.ObjectID, url string, result
return nil
}

return derp.Wrap(err, "service.Inbox", "Error loading Inbox", userID, url)
return derp.Wrap(err, "service.Inbox.LoadOrCreate", "Error loading Inbox", userID, url)
}

// LoadSibling searches for the previous/next sibling to the provided message criteria.
func (service *Inbox) LoadSibling(folderID primitive.ObjectID, rank int64, following string, direction string) (model.Message, error) {

// Initialize query parameters
var criteria exp.Expression = exp.Equal("folderId", folderID)
var sort option.Option

// Specific criteria for previous/next
if direction == "prev" {
criteria = criteria.AndLessThan("rank", rank)
sort = option.SortDesc("rank")
} else {
criteria = criteria.AndGreaterThan("rank", rank)
sort = option.SortAsc("rank")
}

// Limit further if a "followingId" is present
if followingID, err := primitive.ObjectIDFromHex(following); err == nil {
criteria = criteria.AndEqual("origin.followingId", followingID)
}

// Query the database.
it, err := service.List(criteria, option.FirstRow(), sort)

if err != nil {
return model.Message{}, derp.Wrap(err, "service.Inbox.LoadSibling", "Error retrieving siblings")
}

// Try to read the results
result := model.NewMessage()

// This *should* read the prev/next message into the pointer and be done.
if it.Next(&result) {
return result, nil
}

// No results. Shame! Shame!
return model.Message{}, derp.NewNotFoundError("service.Inbox.LoadSibling", "No record found")
}

/******************************************
Expand Down

0 comments on commit f2046e3

Please sign in to comment.