-
Notifications
You must be signed in to change notification settings - Fork 12
/
user_responses.go
122 lines (91 loc) · 3.71 KB
/
user_responses.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package activitypub_user
import (
"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"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func GetResponseCollection(serverFactory *server.Factory, responseType string) echo.HandlerFunc {
const location = "handler.activitypub.ActivityPub_GetResponseCollection"
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.ActivityPubLikedURL())
return ctx.JSON(http.StatusOK, result)
}
// Fall through means that we're looking for a specific page of the collection
publishedDate := convert.Int64(publishDateString)
responseService := factory.Response()
pageSize := 60
// Retrieve a page of responses from the database
responses, err := responseService.QueryByUserAndDate(user.UserID, responseType, publishedDate, pageSize)
if err != nil {
return derp.Wrap(err, location, "Error loading responses")
}
// Return results as an OrderedCollectionPage
ctx.Response().Header().Set("Content-Type", "application/activity+json")
result := activitypub.CollectionPage(user.ActivityPubLikedURL(), pageSize, responses)
return ctx.JSON(http.StatusOK, result)
}
}
func GetResponse(serverFactory *server.Factory, responseType string) echo.HandlerFunc {
const location = "handler.activitypub.ActivityPub_GetUserResponse"
return func(ctx echo.Context) error {
// Collect ResponseID from URL
responseID, err := primitive.ObjectIDFromHex(ctx.Param("response"))
if err != nil {
return derp.NewNotFoundError(location, "Invalid Response ID", err)
}
// Validate the domain name
factory, err := serverFactory.ByContext(ctx)
if err != nil {
return derp.Wrap(err, location, "Unrecognized domain name")
}
// 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")
}
// Try to load the Response from the database
responseService := factory.Response()
response := model.NewResponse()
if err := responseService.LoadByID(responseID, &response); err != nil {
return derp.Wrap(err, location, "Error loading response")
}
if response.Actor != user.ProfileURL {
return derp.NewNotFoundError(location, "Response not found", "ActorID does not match")
}
if response.Type != responseType {
return derp.NewNotFoundError(location, "Response not found", "Type does not match")
}
// Return the response as JSON-LD
ctx.Response().Header().Set("Content-Type", "application/activity+json")
return ctx.JSON(http.StatusOK, response.GetJSONLD())
}
}