summaryrefslogtreecommitdiff
path: root/internal/processing/timeline
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/timeline')
-rw-r--r--internal/processing/timeline/common.go71
-rw-r--r--internal/processing/timeline/faved.go73
-rw-r--r--internal/processing/timeline/home.go133
-rw-r--r--internal/processing/timeline/list.go157
-rw-r--r--internal/processing/timeline/notification.go144
-rw-r--r--internal/processing/timeline/public.go88
-rw-r--r--internal/processing/timeline/timeline.go38
7 files changed, 704 insertions, 0 deletions
diff --git a/internal/processing/timeline/common.go b/internal/processing/timeline/common.go
new file mode 100644
index 000000000..6d29d81d6
--- /dev/null
+++ b/internal/processing/timeline/common.go
@@ -0,0 +1,71 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+
+ "github.com/superseriousbusiness/gotosocial/internal/timeline"
+)
+
+// SkipInsert returns a function that satisifes SkipInsertFunction.
+func SkipInsert() timeline.SkipInsertFunction {
+ // Gap to allow between a status or boost of status,
+ // and reinsertion of a new boost of that status.
+ // This is useful to avoid a heavily boosted status
+ // showing up way too often in a user's timeline.
+ const boostReinsertionDepth = 50
+
+ return func(
+ ctx context.Context,
+ newItemID string,
+ newItemAccountID string,
+ newItemBoostOfID string,
+ newItemBoostOfAccountID string,
+ nextItemID string,
+ nextItemAccountID string,
+ nextItemBoostOfID string,
+ nextItemBoostOfAccountID string,
+ depth int,
+ ) (bool, error) {
+ if newItemID == nextItemID {
+ // Don't insert duplicates.
+ return true, nil
+ }
+
+ if newItemBoostOfID != "" {
+ if newItemBoostOfID == nextItemBoostOfID &&
+ depth < boostReinsertionDepth {
+ // Don't insert boosts of items
+ // we've seen boosted recently.
+ return true, nil
+ }
+
+ if newItemBoostOfID == nextItemID &&
+ depth < boostReinsertionDepth {
+ // Don't insert boosts of items when
+ // we've seen the original recently.
+ return true, nil
+ }
+ }
+
+ // Proceed with insertion
+ // (that's what she said!).
+ return false, nil
+ }
+}
diff --git a/internal/processing/timeline/faved.go b/internal/processing/timeline/faved.go
new file mode 100644
index 000000000..0fc92d8fa
--- /dev/null
+++ b/internal/processing/timeline/faved.go
@@ -0,0 +1,73 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+)
+
+func (p *Processor) FavedTimelineGet(ctx context.Context, authed *oauth.Auth, maxID string, minID string, limit int) (*apimodel.PageableResponse, gtserror.WithCode) {
+ statuses, nextMaxID, prevMinID, err := p.state.DB.GetFavedTimeline(ctx, authed.Account.ID, maxID, minID, limit)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ err = fmt.Errorf("FavedTimelineGet: db error getting statuses: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ count := len(statuses)
+ if count == 0 {
+ return util.EmptyPageableResponse(), nil
+ }
+
+ items := make([]interface{}, 0, count)
+ for _, s := range statuses {
+ visible, err := p.filter.StatusVisible(ctx, authed.Account, s)
+ if err != nil {
+ log.Debugf(ctx, "skipping status %s because of an error checking status visibility: %s", s.ID, err)
+ continue
+ }
+
+ if !visible {
+ continue
+ }
+
+ apiStatus, err := p.tc.StatusToAPIStatus(ctx, s, authed.Account)
+ if err != nil {
+ log.Debugf(ctx, "skipping status %s because it couldn't be converted to its api representation: %s", s.ID, err)
+ continue
+ }
+
+ items = append(items, apiStatus)
+ }
+
+ return util.PackagePageableResponse(util.PageableResponseParams{
+ Items: items,
+ Path: "api/v1/favourites",
+ NextMaxIDValue: nextMaxID,
+ PrevMinIDValue: prevMinID,
+ Limit: limit,
+ })
+}
diff --git a/internal/processing/timeline/home.go b/internal/processing/timeline/home.go
new file mode 100644
index 000000000..e65f12e17
--- /dev/null
+++ b/internal/processing/timeline/home.go
@@ -0,0 +1,133 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/state"
+ "github.com/superseriousbusiness/gotosocial/internal/timeline"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+ "github.com/superseriousbusiness/gotosocial/internal/visibility"
+)
+
+// HomeTimelineGrab returns a function that satisfies GrabFunction for home timelines.
+func HomeTimelineGrab(state *state.State) timeline.GrabFunction {
+ return func(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int) ([]timeline.Timelineable, bool, error) {
+ statuses, err := state.DB.GetHomeTimeline(ctx, accountID, maxID, sinceID, minID, limit, false)
+ if err != nil {
+ if errors.Is(err, db.ErrNoEntries) {
+ return nil, true, nil // we just don't have enough statuses left in the db so return stop = true
+ }
+ return nil, false, fmt.Errorf("HomeTimelineGrab: error getting statuses from db: %w", err)
+ }
+
+ items := make([]timeline.Timelineable, len(statuses))
+ for i, s := range statuses {
+ items[i] = s
+ }
+
+ return items, false, nil
+ }
+}
+
+// HomeTimelineFilter returns a function that satisfies FilterFunction for home timelines.
+func HomeTimelineFilter(state *state.State, filter *visibility.Filter) timeline.FilterFunction {
+ return func(ctx context.Context, accountID string, item timeline.Timelineable) (shouldIndex bool, err error) {
+ status, ok := item.(*gtsmodel.Status)
+ if !ok {
+ return false, errors.New("HomeTimelineFilter: could not convert item to *gtsmodel.Status")
+ }
+
+ requestingAccount, err := state.DB.GetAccountByID(ctx, accountID)
+ if err != nil {
+ return false, fmt.Errorf("HomeTimelineFilter: error getting account with id %s: %w", accountID, err)
+ }
+
+ timelineable, err := filter.StatusHomeTimelineable(ctx, requestingAccount, status)
+ if err != nil {
+ return false, fmt.Errorf("HomeTimelineFilter: error checking hometimelineability of status %s for account %s: %w", status.ID, accountID, err)
+ }
+
+ return timelineable, nil
+ }
+}
+
+// HomeTimelineStatusPrepare returns a function that satisfies PrepareFunction for home timelines.
+func HomeTimelineStatusPrepare(state *state.State, tc typeutils.TypeConverter) timeline.PrepareFunction {
+ return func(ctx context.Context, accountID string, itemID string) (timeline.Preparable, error) {
+ status, err := state.DB.GetStatusByID(ctx, itemID)
+ if err != nil {
+ return nil, fmt.Errorf("StatusPrepare: error getting status with id %s: %w", itemID, err)
+ }
+
+ requestingAccount, err := state.DB.GetAccountByID(ctx, accountID)
+ if err != nil {
+ return nil, fmt.Errorf("StatusPrepare: error getting account with id %s: %w", accountID, err)
+ }
+
+ return tc.StatusToAPIStatus(ctx, status, requestingAccount)
+ }
+}
+
+func (p *Processor) HomeTimelineGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, minID string, limit int, local bool) (*apimodel.PageableResponse, gtserror.WithCode) {
+ statuses, err := p.state.Timelines.Home.GetTimeline(ctx, authed.Account.ID, maxID, sinceID, minID, limit, local)
+ if err != nil {
+ err = fmt.Errorf("HomeTimelineGet: error getting statuses: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ count := len(statuses)
+ if count == 0 {
+ return util.EmptyPageableResponse(), nil
+ }
+
+ var (
+ items = make([]interface{}, count)
+ nextMaxIDValue string
+ prevMinIDValue string
+ )
+
+ for i, item := range statuses {
+ if i == count-1 {
+ nextMaxIDValue = item.GetID()
+ }
+
+ if i == 0 {
+ prevMinIDValue = item.GetID()
+ }
+
+ items[i] = item
+ }
+
+ return util.PackagePageableResponse(util.PageableResponseParams{
+ Items: items,
+ Path: "api/v1/timelines/home",
+ NextMaxIDValue: nextMaxIDValue,
+ PrevMinIDValue: prevMinIDValue,
+ Limit: limit,
+ })
+}
diff --git a/internal/processing/timeline/list.go b/internal/processing/timeline/list.go
new file mode 100644
index 000000000..adad35197
--- /dev/null
+++ b/internal/processing/timeline/list.go
@@ -0,0 +1,157 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/state"
+ "github.com/superseriousbusiness/gotosocial/internal/timeline"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+ "github.com/superseriousbusiness/gotosocial/internal/visibility"
+)
+
+// ListTimelineGrab returns a function that satisfies GrabFunction for list timelines.
+func ListTimelineGrab(state *state.State) timeline.GrabFunction {
+ return func(ctx context.Context, listID string, maxID string, sinceID string, minID string, limit int) ([]timeline.Timelineable, bool, error) {
+ statuses, err := state.DB.GetListTimeline(ctx, listID, maxID, sinceID, minID, limit)
+ if err != nil {
+ if errors.Is(err, db.ErrNoEntries) {
+ return nil, true, nil // we just don't have enough statuses left in the db so return stop = true
+ }
+ return nil, false, fmt.Errorf("ListTimelineGrab: error getting statuses from db: %w", err)
+ }
+
+ items := make([]timeline.Timelineable, len(statuses))
+ for i, s := range statuses {
+ items[i] = s
+ }
+
+ return items, false, nil
+ }
+}
+
+// HomeTimelineFilter returns a function that satisfies FilterFunction for list timelines.
+func ListTimelineFilter(state *state.State, filter *visibility.Filter) timeline.FilterFunction {
+ return func(ctx context.Context, listID string, item timeline.Timelineable) (shouldIndex bool, err error) {
+ status, ok := item.(*gtsmodel.Status)
+ if !ok {
+ return false, errors.New("ListTimelineFilter: could not convert item to *gtsmodel.Status")
+ }
+
+ list, err := state.DB.GetListByID(ctx, listID)
+ if err != nil {
+ return false, fmt.Errorf("ListTimelineFilter: error getting list with id %s: %w", listID, err)
+ }
+
+ requestingAccount, err := state.DB.GetAccountByID(ctx, list.AccountID)
+ if err != nil {
+ return false, fmt.Errorf("ListTimelineFilter: error getting account with id %s: %w", list.AccountID, err)
+ }
+
+ timelineable, err := filter.StatusHomeTimelineable(ctx, requestingAccount, status)
+ if err != nil {
+ return false, fmt.Errorf("ListTimelineFilter: error checking hometimelineability of status %s for account %s: %w", status.ID, list.AccountID, err)
+ }
+
+ return timelineable, nil
+ }
+}
+
+// ListTimelineStatusPrepare returns a function that satisfies PrepareFunction for list timelines.
+func ListTimelineStatusPrepare(state *state.State, tc typeutils.TypeConverter) timeline.PrepareFunction {
+ return func(ctx context.Context, listID string, itemID string) (timeline.Preparable, error) {
+ status, err := state.DB.GetStatusByID(ctx, itemID)
+ if err != nil {
+ return nil, fmt.Errorf("ListTimelineStatusPrepare: error getting status with id %s: %w", itemID, err)
+ }
+
+ list, err := state.DB.GetListByID(ctx, listID)
+ if err != nil {
+ return nil, fmt.Errorf("ListTimelineStatusPrepare: error getting list with id %s: %w", listID, err)
+ }
+
+ requestingAccount, err := state.DB.GetAccountByID(ctx, list.AccountID)
+ if err != nil {
+ return nil, fmt.Errorf("ListTimelineStatusPrepare: error getting account with id %s: %w", list.AccountID, err)
+ }
+
+ return tc.StatusToAPIStatus(ctx, status, requestingAccount)
+ }
+}
+
+func (p *Processor) ListTimelineGet(ctx context.Context, authed *oauth.Auth, listID string, maxID string, sinceID string, minID string, limit int) (*apimodel.PageableResponse, gtserror.WithCode) {
+ // Ensure list exists + is owned by this account.
+ list, err := p.state.DB.GetListByID(ctx, listID)
+ if err != nil {
+ if errors.Is(err, db.ErrNoEntries) {
+ return nil, gtserror.NewErrorNotFound(err)
+ }
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ if list.AccountID != authed.Account.ID {
+ err = fmt.Errorf("list with id %s does not belong to account %s", list.ID, authed.Account.ID)
+ return nil, gtserror.NewErrorNotFound(err)
+ }
+
+ statuses, err := p.state.Timelines.List.GetTimeline(ctx, listID, maxID, sinceID, minID, limit, false)
+ if err != nil {
+ err = fmt.Errorf("ListTimelineGet: error getting statuses: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ count := len(statuses)
+ if count == 0 {
+ return util.EmptyPageableResponse(), nil
+ }
+
+ var (
+ items = make([]interface{}, count)
+ nextMaxIDValue string
+ prevMinIDValue string
+ )
+
+ for i, item := range statuses {
+ if i == count-1 {
+ nextMaxIDValue = item.GetID()
+ }
+
+ if i == 0 {
+ prevMinIDValue = item.GetID()
+ }
+
+ items[i] = item
+ }
+
+ return util.PackagePageableResponse(util.PageableResponseParams{
+ Items: items,
+ Path: "api/v1/timelines/list/" + listID,
+ NextMaxIDValue: nextMaxIDValue,
+ PrevMinIDValue: prevMinIDValue,
+ Limit: limit,
+ })
+}
diff --git a/internal/processing/timeline/notification.go b/internal/processing/timeline/notification.go
new file mode 100644
index 000000000..4a79fb82a
--- /dev/null
+++ b/internal/processing/timeline/notification.go
@@ -0,0 +1,144 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+)
+
+func (p *Processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, minID string, limit int, excludeTypes []string) (*apimodel.PageableResponse, gtserror.WithCode) {
+ notifs, err := p.state.DB.GetAccountNotifications(ctx, authed.Account.ID, maxID, sinceID, minID, limit, excludeTypes)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ err = fmt.Errorf("NotificationsGet: db error getting notifications: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ count := len(notifs)
+ if count == 0 {
+ return util.EmptyPageableResponse(), nil
+ }
+
+ var (
+ items = make([]interface{}, 0, count)
+ nextMaxIDValue string
+ prevMinIDValue string
+ )
+
+ for i, n := range notifs {
+ // Set next + prev values before filtering and API
+ // converting, so caller can still page properly.
+ if i == count-1 {
+ nextMaxIDValue = n.ID
+ }
+
+ if i == 0 {
+ prevMinIDValue = n.ID
+ }
+
+ // Ensure this notification should be shown to requester.
+ if n.OriginAccount != nil {
+ // Account is set, ensure it's visible to notif target.
+ visible, err := p.filter.AccountVisible(ctx, authed.Account, n.OriginAccount)
+ if err != nil {
+ log.Debugf(ctx, "skipping notification %s because of an error checking notification visibility: %s", n.ID, err)
+ continue
+ }
+
+ if !visible {
+ continue
+ }
+ }
+
+ if n.Status != nil {
+ // Status is set, ensure it's visible to notif target.
+ visible, err := p.filter.StatusVisible(ctx, authed.Account, n.Status)
+ if err != nil {
+ log.Debugf(ctx, "skipping notification %s because of an error checking notification visibility: %s", n.ID, err)
+ continue
+ }
+
+ if !visible {
+ continue
+ }
+ }
+
+ item, err := p.tc.NotificationToAPINotification(ctx, n)
+ if err != nil {
+ log.Debugf(ctx, "skipping notification %s because it couldn't be converted to its api representation: %s", n.ID, err)
+ continue
+ }
+
+ items = append(items, item)
+ }
+
+ return util.PackagePageableResponse(util.PageableResponseParams{
+ Items: items,
+ Path: "api/v1/notifications",
+ NextMaxIDValue: nextMaxIDValue,
+ PrevMinIDValue: prevMinIDValue,
+ Limit: limit,
+ })
+}
+
+func (p *Processor) NotificationGet(ctx context.Context, account *gtsmodel.Account, targetNotifID string) (*apimodel.Notification, gtserror.WithCode) {
+ notif, err := p.state.DB.GetNotificationByID(ctx, targetNotifID)
+ if err != nil {
+ if errors.Is(err, db.ErrNoEntries) {
+ return nil, gtserror.NewErrorNotFound(err)
+ }
+
+ // Real error.
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ if notifTargetAccountID := notif.TargetAccountID; notifTargetAccountID != account.ID {
+ err = fmt.Errorf("account %s does not have permission to view notification belong to account %s", account.ID, notifTargetAccountID)
+ return nil, gtserror.NewErrorNotFound(err)
+ }
+
+ apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
+ if err != nil {
+ if errors.Is(err, db.ErrNoEntries) {
+ return nil, gtserror.NewErrorNotFound(err)
+ }
+
+ // Real error.
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ return apiNotif, nil
+}
+
+func (p *Processor) NotificationsClear(ctx context.Context, authed *oauth.Auth) gtserror.WithCode {
+ // Delete all notifications of all types that target the authorized account.
+ if err := p.state.DB.DeleteNotifications(ctx, nil, authed.Account.ID, ""); err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return gtserror.NewErrorInternalError(err)
+ }
+
+ return nil
+}
diff --git a/internal/processing/timeline/public.go b/internal/processing/timeline/public.go
new file mode 100644
index 000000000..67893ecfa
--- /dev/null
+++ b/internal/processing/timeline/public.go
@@ -0,0 +1,88 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+)
+
+func (p *Processor) PublicTimelineGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, minID string, limit int, local bool) (*apimodel.PageableResponse, gtserror.WithCode) {
+ statuses, err := p.state.DB.GetPublicTimeline(ctx, maxID, sinceID, minID, limit, local)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ err = fmt.Errorf("PublicTimelineGet: db error getting statuses: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ count := len(statuses)
+ if count == 0 {
+ return util.EmptyPageableResponse(), nil
+ }
+
+ var (
+ items = make([]interface{}, 0, count)
+ nextMaxIDValue string
+ prevMinIDValue string
+ )
+
+ for i, s := range statuses {
+ // Set next + prev values before filtering and API
+ // converting, so caller can still page properly.
+ if i == count-1 {
+ nextMaxIDValue = s.ID
+ }
+
+ if i == 0 {
+ prevMinIDValue = s.ID
+ }
+
+ timelineable, err := p.filter.StatusPublicTimelineable(ctx, authed.Account, s)
+ if err != nil {
+ log.Debugf(ctx, "skipping status %s because of an error checking StatusPublicTimelineable: %s", s.ID, err)
+ continue
+ }
+
+ if !timelineable {
+ continue
+ }
+
+ apiStatus, err := p.tc.StatusToAPIStatus(ctx, s, authed.Account)
+ if err != nil {
+ log.Debugf(ctx, "skipping status %s because it couldn't be converted to its api representation: %s", s.ID, err)
+ continue
+ }
+
+ items = append(items, apiStatus)
+ }
+
+ return util.PackagePageableResponse(util.PageableResponseParams{
+ Items: items,
+ Path: "api/v1/timelines/public",
+ NextMaxIDValue: nextMaxIDValue,
+ PrevMinIDValue: prevMinIDValue,
+ Limit: limit,
+ })
+}
diff --git a/internal/processing/timeline/timeline.go b/internal/processing/timeline/timeline.go
new file mode 100644
index 000000000..7a95f9a11
--- /dev/null
+++ b/internal/processing/timeline/timeline.go
@@ -0,0 +1,38 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// 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 timeline
+
+import (
+ "github.com/superseriousbusiness/gotosocial/internal/state"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/internal/visibility"
+)
+
+type Processor struct {
+ state *state.State
+ tc typeutils.TypeConverter
+ filter *visibility.Filter
+}
+
+func New(state *state.State, tc typeutils.TypeConverter, filter *visibility.Filter) Processor {
+ return Processor{
+ state: state,
+ tc: tc,
+ filter: filter,
+ }
+}