tombstone.go (2407B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package bundb 19 20 import ( 21 "context" 22 23 "github.com/superseriousbusiness/gotosocial/internal/db" 24 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 25 "github.com/superseriousbusiness/gotosocial/internal/state" 26 "github.com/uptrace/bun" 27 ) 28 29 type tombstoneDB struct { 30 conn *DBConn 31 state *state.State 32 } 33 34 func (t *tombstoneDB) GetTombstoneByURI(ctx context.Context, uri string) (*gtsmodel.Tombstone, db.Error) { 35 return t.state.Caches.GTS.Tombstone().Load("URI", func() (*gtsmodel.Tombstone, error) { 36 var tomb gtsmodel.Tombstone 37 38 q := t.conn. 39 NewSelect(). 40 Model(&tomb). 41 Where("? = ?", bun.Ident("tombstone.uri"), uri) 42 43 if err := q.Scan(ctx); err != nil { 44 return nil, t.conn.ProcessError(err) 45 } 46 47 return &tomb, nil 48 }, uri) 49 } 50 51 func (t *tombstoneDB) TombstoneExistsWithURI(ctx context.Context, uri string) (bool, db.Error) { 52 tomb, err := t.GetTombstoneByURI(ctx, uri) 53 if err == db.ErrNoEntries { 54 err = nil 55 } 56 return (tomb != nil), err 57 } 58 59 func (t *tombstoneDB) PutTombstone(ctx context.Context, tombstone *gtsmodel.Tombstone) db.Error { 60 return t.state.Caches.GTS.Tombstone().Store(tombstone, func() error { 61 _, err := t.conn. 62 NewInsert(). 63 Model(tombstone). 64 Exec(ctx) 65 return t.conn.ProcessError(err) 66 }) 67 } 68 69 func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) db.Error { 70 defer t.state.Caches.GTS.Tombstone().Invalidate("ID", id) 71 72 // Delete tombstone from DB. 73 _, err := t.conn.NewDelete(). 74 TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")). 75 Where("? = ?", bun.Ident("tombstone.id"), id). 76 Exec(ctx) 77 return t.conn.ProcessError(err) 78 }