summaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-05-17 19:06:58 +0200
committerLibravatar GitHub <noreply@github.com>2021-05-17 19:06:58 +0200
commit6cd033449fd328410128bc3e840f4b8d3d74f052 (patch)
tree9868db8439533e078dea5bb34ae4949e9460f2cb /internal/api
parentupdate progress (diff)
downloadgotosocial-6cd033449fd328410128bc3e840f4b8d3d74f052.tar.xz
Refine statuses (#26)
Remote media is now dereferenced and attached properly to incoming federated statuses. Mentions are now dereferenced and attached properly to incoming federated statuses. Small fixes to status visibility. Allow URL params for filtering statuses: // ExcludeRepliesKey is for specifying whether to exclude replies in a list of returned statuses by an account. // PinnedKey is for specifying whether to include pinned statuses in a list of returned statuses by an account. // MaxIDKey is for specifying the maximum ID of the status to retrieve. // MediaOnlyKey is for specifying that only statuses with media should be returned in a list of returned statuses by an account. Add endpoint for fetching an account's statuses.
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/client/account/account.go17
-rw-r--r--internal/api/client/account/followers.go49
-rw-r--r--internal/api/client/account/statuses.go117
-rw-r--r--internal/api/model/status.go28
-rw-r--r--internal/api/s2s/user/userget.go2
5 files changed, 198 insertions, 15 deletions
diff --git a/internal/api/client/account/account.go b/internal/api/client/account/account.go
index dce810202..1e4b716f5 100644
--- a/internal/api/client/account/account.go
+++ b/internal/api/client/account/account.go
@@ -32,6 +32,17 @@ import (
)
const (
+ // LimitKey is for setting the return amount limit for eg., requesting an account's statuses
+ LimitKey = "limit"
+ // ExcludeRepliesKey is for specifying whether to exclude replies in a list of returned statuses by an account.
+ ExcludeRepliesKey = "exclude_replies"
+ // PinnedKey is for specifying whether to include pinned statuses in a list of returned statuses by an account.
+ PinnedKey = "pinned"
+ // MaxIDKey is for specifying the maximum ID of the status to retrieve.
+ MaxIDKey = "max_id"
+ // MediaOnlyKey is for specifying that only statuses with media should be returned in a list of returned statuses by an account.
+ MediaOnlyKey = "only_media"
+
// IDKey is the key to use for retrieving account ID in requests
IDKey = "id"
// BasePath is the base API path for this module
@@ -42,6 +53,10 @@ const (
VerifyPath = BasePath + "/verify_credentials"
// UpdateCredentialsPath is for updating account credentials
UpdateCredentialsPath = BasePath + "/update_credentials"
+ // GetStatusesPath is for showing an account's statuses
+ GetStatusesPath = BasePathWithID + "/statuses"
+ // GetFollowersPath is for showing an account's followers
+ GetFollowersPath = BasePathWithID + "/followers"
)
// Module implements the ClientAPIModule interface for account-related actions
@@ -65,6 +80,8 @@ func (m *Module) Route(r router.Router) error {
r.AttachHandler(http.MethodPost, BasePath, m.AccountCreatePOSTHandler)
r.AttachHandler(http.MethodGet, BasePathWithID, m.muxHandler)
r.AttachHandler(http.MethodPatch, BasePathWithID, m.muxHandler)
+ r.AttachHandler(http.MethodGet, GetStatusesPath, m.AccountStatusesGETHandler)
+ r.AttachHandler(http.MethodGet, GetFollowersPath, m.AccountFollowersGETHandler)
return nil
}
diff --git a/internal/api/client/account/followers.go b/internal/api/client/account/followers.go
new file mode 100644
index 000000000..3401df24c
--- /dev/null
+++ b/internal/api/client/account/followers.go
@@ -0,0 +1,49 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package account
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+)
+
+// AccountFollowersGETHandler serves the followers of the requested account, if they're visible to the requester.
+func (m *Module) AccountFollowersGETHandler(c *gin.Context) {
+ authed, err := oauth.Authed(c, true, true, true, true)
+ if err != nil {
+ c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
+ return
+ }
+
+ targetAcctID := c.Param(IDKey)
+ if targetAcctID == "" {
+ c.JSON(http.StatusBadRequest, gin.H{"error": "no account id specified"})
+ return
+ }
+
+ followers, errWithCode := m.processor.AccountFollowersGet(authed, targetAcctID)
+ if errWithCode != nil {
+ c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
+ return
+ }
+
+ c.JSON(http.StatusOK, followers)
+}
diff --git a/internal/api/client/account/statuses.go b/internal/api/client/account/statuses.go
new file mode 100644
index 000000000..f03a942f3
--- /dev/null
+++ b/internal/api/client/account/statuses.go
@@ -0,0 +1,117 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package account
+
+import (
+ "net/http"
+ "strconv"
+
+ "github.com/gin-gonic/gin"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+)
+
+// AccountStatusesGETHandler serves the statuses of the requested account, if they're visible to the requester.
+//
+// Several different filters might be passed into this function in the query:
+//
+// limit -- show only limit number of statuses
+// exclude_replies -- exclude statuses that are a reply to another status
+// max_id -- the maximum ID of the status to show
+// pinned -- show only pinned statuses
+// media_only -- show only statuses that have media attachments
+func (m *Module) AccountStatusesGETHandler(c *gin.Context) {
+ l := m.log.WithField("func", "AccountStatusesGETHandler")
+
+ authed, err := oauth.Authed(c, false, false, false, false)
+ if err != nil {
+ l.Debugf("error authing: %s", err)
+ c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
+ return
+ }
+
+ targetAcctID := c.Param(IDKey)
+ if targetAcctID == "" {
+ l.Debug("no account id specified in query")
+ c.JSON(http.StatusBadRequest, gin.H{"error": "no account id specified"})
+ return
+ }
+
+ limit := 30
+ limitString := c.Query(LimitKey)
+ if limitString != "" {
+ i, err := strconv.ParseInt(limitString, 10, 64)
+ if err != nil {
+ l.Debugf("error parsing limit string: %s", err)
+ c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse limit query param"})
+ return
+ }
+ limit = int(i)
+ }
+
+ excludeReplies := false
+ excludeRepliesString := c.Query(ExcludeRepliesKey)
+ if excludeRepliesString != "" {
+ i, err := strconv.ParseBool(excludeRepliesString)
+ if err != nil {
+ l.Debugf("error parsing replies string: %s", err)
+ c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse exclude replies query param"})
+ return
+ }
+ excludeReplies = i
+ }
+
+ maxID := ""
+ maxIDString := c.Query(MaxIDKey)
+ if maxIDString != "" {
+ maxID = maxIDString
+ }
+
+ pinned := false
+ pinnedString := c.Query(PinnedKey)
+ if pinnedString != "" {
+ i, err := strconv.ParseBool(pinnedString)
+ if err != nil {
+ l.Debugf("error parsing pinned string: %s", err)
+ c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse pinned query param"})
+ return
+ }
+ pinned = i
+ }
+
+ mediaOnly := false
+ mediaOnlyString := c.Query(MediaOnlyKey)
+ if mediaOnlyString != "" {
+ i, err := strconv.ParseBool(mediaOnlyString)
+ if err != nil {
+ l.Debugf("error parsing media only string: %s", err)
+ c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse media only query param"})
+ return
+ }
+ mediaOnly = i
+ }
+
+ statuses, errWithCode := m.processor.AccountStatusesGet(authed, targetAcctID, limit, excludeReplies, maxID, pinned, mediaOnly)
+ if errWithCode != nil {
+ l.Debugf("error from processor account statuses get: %s", errWithCode)
+ c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
+ return
+ }
+
+ c.JSON(http.StatusOK, statuses)
+}
diff --git a/internal/api/model/status.go b/internal/api/model/status.go
index 2cb22aa0d..2456d1a8f 100644
--- a/internal/api/model/status.go
+++ b/internal/api/model/status.go
@@ -55,7 +55,7 @@ type Status struct {
// Have you bookmarked this status?
Bookmarked bool `json:"bookmarked"`
// Have you pinned this status? Only appears if the status is pinnable.
- Pinned bool `json:"pinned"`
+ Pinned bool `json:"pinned,omitempty"`
// HTML-encoded status content.
Content string `json:"content"`
// The status being reblogged.
@@ -86,23 +86,23 @@ type Status struct {
// It should be used at the path https://mastodon.example/api/v1/statuses
type StatusCreateRequest struct {
// Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
- Status string `form:"status"`
+ Status string `form:"status" json:"status" xml:"status"`
// Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
MediaIDs []string `form:"media_ids" json:"media_ids" xml:"media_ids"`
// Poll to include with this status.
- Poll *PollRequest `form:"poll"`
+ Poll *PollRequest `form:"poll" json:"poll" xml:"poll"`
// ID of the status being replied to, if status is a reply
- InReplyToID string `form:"in_reply_to_id"`
+ InReplyToID string `form:"in_reply_to_id" json:"in_reply_to_id" xml:"in_reply_to_id"`
// Mark status and attached media as sensitive?
- Sensitive bool `form:"sensitive"`
+ Sensitive bool `form:"sensitive" json:"sensitive" xml:"sensitive"`
// Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
- SpoilerText string `form:"spoiler_text"`
+ SpoilerText string `form:"spoiler_text" json:"spoiler_text" xml:"spoiler_text"`
// Visibility of the posted status. Enumerable oneOf public, unlisted, private, direct.
- Visibility Visibility `form:"visibility"`
+ Visibility Visibility `form:"visibility" json:"visibility" xml:"visibility"`
// ISO 8601 Datetime at which to schedule a status. Providing this paramter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future.
- ScheduledAt string `form:"scheduled_at"`
+ ScheduledAt string `form:"scheduled_at" json:"scheduled_at" xml:"scheduled_at"`
// ISO 639 language code for this status.
- Language string `form:"language"`
+ Language string `form:"language" json:"language" xml:"language"`
}
// Visibility denotes the visibility of this status to other users
@@ -130,13 +130,13 @@ type AdvancedStatusCreateForm struct {
// to the standard mastodon-compatible ones.
type AdvancedVisibilityFlagsForm struct {
// The gotosocial visibility model
- VisibilityAdvanced *string `form:"visibility_advanced"`
+ VisibilityAdvanced *string `form:"visibility_advanced" json:"visibility_advanced" xml:"visibility_advanced"`
// This status will be federated beyond the local timeline(s)
- Federated *bool `form:"federated"`
+ Federated *bool `form:"federated" json:"federated" xml:"federated"`
// This status can be boosted/reblogged
- Boostable *bool `form:"boostable"`
+ Boostable *bool `form:"boostable" json:"boostable" xml:"boostable"`
// This status can be replied to
- Replyable *bool `form:"replyable"`
+ Replyable *bool `form:"replyable" json:"replyable" xml:"replyable"`
// This status can be liked/faved
- Likeable *bool `form:"likeable"`
+ Likeable *bool `form:"likeable" json:"likeable" xml:"likeable"`
}
diff --git a/internal/api/s2s/user/userget.go b/internal/api/s2s/user/userget.go
index 8df137f44..9d268e121 100644
--- a/internal/api/s2s/user/userget.go
+++ b/internal/api/s2s/user/userget.go
@@ -56,7 +56,7 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
// make a copy of the context to pass along so we don't break anything
cp := c.Copy()
- user, err := m.processor.GetFediUser(requestedUsername, cp.Request) // GetAPUser handles auth as well
+ user, err := m.processor.GetFediUser(requestedUsername, cp.Request) // GetFediUser handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})