2021-08-20 10:26:56 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-08-20 10:26:56 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
package bundb
|
2021-08-20 10:26:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
"codeberg.org/gruf/go-cache/v2"
|
2021-08-20 10:26:56 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-08-20 10:26:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type notificationDB struct {
|
2021-12-07 12:31:39 +00:00
|
|
|
conn *DBConn
|
2022-07-10 15:18:21 +00:00
|
|
|
cache cache.Cache[string, *gtsmodel.Notification]
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) {
|
2022-07-10 15:18:21 +00:00
|
|
|
if notification, ok := n.cache.Get(id); ok {
|
2021-08-20 10:26:56 +00:00
|
|
|
return notification, nil
|
|
|
|
}
|
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
dst := gtsmodel.Notification{ID: id}
|
|
|
|
|
|
|
|
q := n.conn.NewSelect().
|
|
|
|
Model(&dst).
|
|
|
|
Relation("OriginAccount").
|
|
|
|
Relation("TargetAccount").
|
|
|
|
Relation("Status").
|
|
|
|
WherePK()
|
|
|
|
|
|
|
|
if err := q.Scan(ctx); err != nil {
|
|
|
|
return nil, n.conn.ProcessError(err)
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
2022-07-10 15:18:21 +00:00
|
|
|
|
|
|
|
copy := dst
|
|
|
|
n.cache.Set(id, ©)
|
|
|
|
|
|
|
|
return &dst, nil
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) {
|
2021-08-29 14:41:41 +00:00
|
|
|
// Ensure reasonable
|
|
|
|
if limit < 0 {
|
|
|
|
limit = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a guess for slice size
|
2022-07-10 15:18:21 +00:00
|
|
|
notifIDs := make([]string, 0, limit)
|
2021-08-29 14:41:41 +00:00
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
q := n.conn.
|
2021-08-25 13:34:33 +00:00
|
|
|
NewSelect().
|
2022-07-10 15:18:21 +00:00
|
|
|
Table("notifications").
|
2022-03-07 10:33:18 +00:00
|
|
|
Column("id")
|
2021-08-20 10:26:56 +00:00
|
|
|
|
|
|
|
if maxID != "" {
|
|
|
|
q = q.Where("id < ?", maxID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinceID != "" {
|
|
|
|
q = q.Where("id > ?", sinceID)
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:33:18 +00:00
|
|
|
q = q.
|
|
|
|
Where("target_account_id = ?", accountID).
|
|
|
|
Order("id DESC")
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
if limit != 0 {
|
|
|
|
q = q.Limit(limit)
|
|
|
|
}
|
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
if err := q.Scan(ctx, ¬ifIDs); err != nil {
|
2021-08-29 14:41:41 +00:00
|
|
|
return nil, n.conn.ProcessError(err)
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
notifs := make([]*gtsmodel.Notification, 0, limit)
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
// now we have the IDs, select the notifs one by one
|
|
|
|
// reason for this is that for each notif, we can instead get it from our cache if it's cached
|
2022-07-10 15:18:21 +00:00
|
|
|
for _, id := range notifIDs {
|
|
|
|
// Attempt fetch from DB
|
|
|
|
notif, err := n.GetNotification(ctx, id)
|
2021-08-29 14:41:41 +00:00
|
|
|
if err != nil {
|
2022-07-19 08:47:55 +00:00
|
|
|
log.Errorf("GetNotifications: error getting notification %q: %v", id, err)
|
2022-07-10 15:18:21 +00:00
|
|
|
continue
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
2022-04-02 13:40:09 +00:00
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
// Append notification
|
|
|
|
notifs = append(notifs, notif)
|
2021-08-29 14:41:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 15:18:21 +00:00
|
|
|
return notifs, nil
|
2021-08-29 14:41:41 +00:00
|
|
|
}
|