| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // 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/>. | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | package bundb | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2022-12-08 17:35:14 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/state" | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 	"github.com/uptrace/bun" | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type notificationDB struct { | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	conn  *DBConn | 
					
						
							| 
									
										
										
										
											2022-12-08 17:35:14 +00:00
										 |  |  | 	state *state.State | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) { | 
					
						
							| 
									
										
										
										
											2022-12-08 17:35:14 +00:00
										 |  |  | 	return n.state.Caches.GTS.Notification().Load("ID", func() (*gtsmodel.Notification, error) { | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | 		var notif gtsmodel.Notification | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		q := n.conn.NewSelect(). | 
					
						
							|  |  |  | 			Model(¬if). | 
					
						
							|  |  |  | 			Where("? = ?", bun.Ident("notification.id"), id) | 
					
						
							|  |  |  | 		if err := q.Scan(ctx); err != nil { | 
					
						
							|  |  |  | 			return nil, n.conn.ProcessError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return ¬if, nil | 
					
						
							|  |  |  | 	}, id) | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 13:20:52 -04:00
										 |  |  | func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, excludeTypes []string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) { | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	// Ensure reasonable | 
					
						
							|  |  |  | 	if limit < 0 { | 
					
						
							|  |  |  | 		limit = 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make a guess for slice size | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 	notifIDs := make([]string, 0, limit) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	q := n.conn. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")). | 
					
						
							|  |  |  | 		Column("notification.id") | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if maxID != "" { | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? < ?", bun.Ident("notification.id"), maxID) | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if sinceID != "" { | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? > ?", bun.Ident("notification.id"), sinceID) | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 13:20:52 -04:00
										 |  |  | 	for _, excludeType := range excludeTypes { | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? != ?", bun.Ident("notification.notification_type"), excludeType) | 
					
						
							| 
									
										
										
										
											2022-08-31 13:20:52 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 11:33:18 +01:00
										 |  |  | 	q = q. | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		Where("? = ?", bun.Ident("notification.target_account_id"), accountID). | 
					
						
							|  |  |  | 		Order("notification.id DESC") | 
					
						
							| 
									
										
										
										
											2022-03-07 11:33:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	if limit != 0 { | 
					
						
							|  |  |  | 		q = q.Limit(limit) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 	if err := q.Scan(ctx, ¬ifIDs); err != nil { | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 		return nil, n.conn.ProcessError(err) | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 	notifs := make([]*gtsmodel.Notification, 0, limit) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02: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 16:18:21 +01:00
										 |  |  | 	for _, id := range notifIDs { | 
					
						
							|  |  |  | 		// Attempt fetch from DB | 
					
						
							|  |  |  | 		notif, err := n.GetNotification(ctx, id) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 			log.Errorf(ctx, "error getting notification %q: %v", id, err) | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-04-02 15:40:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 		// Append notification | 
					
						
							|  |  |  | 		notifs = append(notifs, notif) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 16:18:21 +01:00
										 |  |  | 	return notifs, nil | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-01 11:13:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | func (n *notificationDB) DeleteNotification(ctx context.Context, id string) db.Error { | 
					
						
							| 
									
										
										
										
											2022-08-01 11:13:49 +02:00
										 |  |  | 	if _, err := n.conn. | 
					
						
							|  |  |  | 		NewDelete(). | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")). | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		Where("? = ?", bun.Ident("notification.id"), id). | 
					
						
							| 
									
										
										
										
											2022-08-01 11:13:49 +02:00
										 |  |  | 		Exec(ctx); err != nil { | 
					
						
							|  |  |  | 		return n.conn.ProcessError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	n.state.Caches.GTS.Notification().Invalidate("ID", id) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (n *notificationDB) DeleteNotifications(ctx context.Context, targetAccountID string, originAccountID string) db.Error { | 
					
						
							|  |  |  | 	if targetAccountID == "" && originAccountID == "" { | 
					
						
							|  |  |  | 		return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Capture notification IDs in a RETURNING statement. | 
					
						
							|  |  |  | 	ids := []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	q := n.conn. | 
					
						
							|  |  |  | 		NewDelete(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")). | 
					
						
							|  |  |  | 		Returning("?", bun.Ident("id")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if targetAccountID != "" { | 
					
						
							|  |  |  | 		q = q.Where("? = ?", bun.Ident("notification.target_account_id"), targetAccountID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if originAccountID != "" { | 
					
						
							|  |  |  | 		q = q.Where("? = ?", bun.Ident("notification.origin_account_id"), originAccountID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := q.Exec(ctx, &ids); err != nil { | 
					
						
							|  |  |  | 		return n.conn.ProcessError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Invalidate each returned ID. | 
					
						
							|  |  |  | 	for _, id := range ids { | 
					
						
							|  |  |  | 		n.state.Caches.GTS.Notification().Invalidate("ID", id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (n *notificationDB) DeleteNotificationsForStatus(ctx context.Context, statusID string) db.Error { | 
					
						
							|  |  |  | 	// Capture notification IDs in a RETURNING statement. | 
					
						
							|  |  |  | 	ids := []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	q := n.conn. | 
					
						
							|  |  |  | 		NewDelete(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("notification.status_id"), statusID). | 
					
						
							|  |  |  | 		Returning("?", bun.Ident("id")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := q.Exec(ctx, &ids); err != nil { | 
					
						
							|  |  |  | 		return n.conn.ProcessError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Invalidate each returned ID. | 
					
						
							|  |  |  | 	for _, id := range ids { | 
					
						
							|  |  |  | 		n.state.Caches.GTS.Notification().Invalidate("ID", id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 11:13:49 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } |