mention.go (3809B)
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 "errors" 23 "fmt" 24 25 "github.com/superseriousbusiness/gotosocial/internal/db" 26 "github.com/superseriousbusiness/gotosocial/internal/gtscontext" 27 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 28 "github.com/superseriousbusiness/gotosocial/internal/log" 29 "github.com/superseriousbusiness/gotosocial/internal/state" 30 "github.com/uptrace/bun" 31 ) 32 33 type mentionDB struct { 34 conn *DBConn 35 state *state.State 36 } 37 38 func (m *mentionDB) GetMention(ctx context.Context, id string) (*gtsmodel.Mention, db.Error) { 39 mention, err := m.state.Caches.GTS.Mention().Load("ID", func() (*gtsmodel.Mention, error) { 40 var mention gtsmodel.Mention 41 42 q := m.conn. 43 NewSelect(). 44 Model(&mention). 45 Where("? = ?", bun.Ident("mention.id"), id) 46 47 if err := q.Scan(ctx); err != nil { 48 return nil, m.conn.ProcessError(err) 49 } 50 51 return &mention, nil 52 }, id) 53 if err != nil { 54 return nil, err 55 } 56 57 // Set the mention originating status. 58 mention.Status, err = m.state.DB.GetStatusByID( 59 gtscontext.SetBarebones(ctx), 60 mention.StatusID, 61 ) 62 if err != nil { 63 return nil, fmt.Errorf("error populating mention status: %w", err) 64 } 65 66 // Set the mention origin account model. 67 mention.OriginAccount, err = m.state.DB.GetAccountByID( 68 gtscontext.SetBarebones(ctx), 69 mention.OriginAccountID, 70 ) 71 if err != nil { 72 return nil, fmt.Errorf("error populating mention origin account: %w", err) 73 } 74 75 // Set the mention target account model. 76 mention.TargetAccount, err = m.state.DB.GetAccountByID( 77 gtscontext.SetBarebones(ctx), 78 mention.TargetAccountID, 79 ) 80 if err != nil { 81 return nil, fmt.Errorf("error populating mention target account: %w", err) 82 } 83 84 return mention, nil 85 } 86 87 func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.Mention, db.Error) { 88 mentions := make([]*gtsmodel.Mention, 0, len(ids)) 89 90 for _, id := range ids { 91 // Attempt fetch from DB 92 mention, err := m.GetMention(ctx, id) 93 if err != nil { 94 log.Errorf(ctx, "error getting mention %q: %v", id, err) 95 continue 96 } 97 98 // Append mention 99 mentions = append(mentions, mention) 100 } 101 102 return mentions, nil 103 } 104 105 func (m *mentionDB) PutMention(ctx context.Context, mention *gtsmodel.Mention) error { 106 return m.state.Caches.GTS.Mention().Store(mention, func() error { 107 _, err := m.conn.NewInsert().Model(mention).Exec(ctx) 108 return m.conn.ProcessError(err) 109 }) 110 } 111 112 func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error { 113 defer m.state.Caches.GTS.Mention().Invalidate("ID", id) 114 115 // Load mention into cache before attempting a delete, 116 // as we need it cached in order to trigger the invalidate 117 // callback. This in turn invalidates others. 118 _, err := m.GetMention(gtscontext.SetBarebones(ctx), id) 119 if err != nil { 120 if errors.Is(err, db.ErrNoEntries) { 121 // not an issue. 122 err = nil 123 } 124 return err 125 } 126 127 // Finally delete mention from DB. 128 _, err = m.conn.NewDelete(). 129 Table("mentions"). 130 Where("? = ?", bun.Ident("id"), id). 131 Exec(ctx) 132 return m.conn.ProcessError(err) 133 }