-
Notifications
You must be signed in to change notification settings - Fork 14
/
outbox.go
67 lines (51 loc) · 2.08 KB
/
outbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package activitypub_user
import (
"math"
"net/http"
"github.com/EmissarySocial/emissary/handler/activitypub"
"github.com/EmissarySocial/emissary/model"
"github.com/EmissarySocial/emissary/server"
"github.com/benpate/derp"
"github.com/benpate/rosetta/convert"
"github.com/labstack/echo/v4"
)
func GetOutboxCollection(serverFactory *server.Factory) echo.HandlerFunc {
const location = "handler.activitypub.ActivityPub_GetOutboxCollection"
return func(ctx echo.Context) error {
// Validate the domain name
factory, err := serverFactory.ByContext(ctx)
if err != nil {
return derp.Wrap(err, location, "Unrecognized domain name")
}
// Try to load the User from the database
userService := factory.User()
user := model.NewUser()
if err := userService.LoadByToken(ctx.Param("userId"), &user); err != nil {
return derp.NewNotFoundError(location, "User not found", err)
}
// RULE: Only public users can be queried
if !user.IsPublic {
return derp.NewNotFoundError(location, "User not found")
}
// If the request is for the collection itself, then return a summary and the URL of the first page
publishDateString := ctx.QueryParam("publishDate")
if publishDateString == "" {
ctx.Response().Header().Set("Content-Type", "application/activity+json")
result := activitypub.Collection(user.ActivityPubOutboxURL())
return ctx.JSON(http.StatusOK, result)
}
// Fall through means that we're looking for a specific page of the collection
publishedDate := convert.Int64Default(publishDateString, math.MaxInt64)
outboxService := factory.Outbox()
pageSize := 60
// Retrieve a page of messages from the database
messages, err := outboxService.QueryByUserAndDate(model.FollowerTypeUser, user.UserID, publishedDate, pageSize)
if err != nil {
return derp.Wrap(err, location, "Error loading outbox messages")
}
// Return results as an OrderedCollectionPage
ctx.Response().Header().Set("Content-Type", "application/activity+json")
result := activitypub.CollectionPage(user.ActivityPubOutboxURL(), pageSize, messages)
return ctx.JSON(http.StatusOK, result)
}
}