remove.go (2385B)
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 timeline 19 20 import ( 21 "container/list" 22 "context" 23 24 "codeberg.org/gruf/go-kv" 25 "github.com/superseriousbusiness/gotosocial/internal/log" 26 ) 27 28 func (t *timeline) Remove(ctx context.Context, statusID string) (int, error) { 29 l := log.WithContext(ctx). 30 WithFields(kv.Fields{ 31 {"accountTimeline", t.timelineID}, 32 {"statusID", statusID}, 33 }...) 34 35 t.Lock() 36 defer t.Unlock() 37 38 if t.items == nil || t.items.data == nil { 39 // Nothing to do. 40 return 0, nil 41 } 42 43 var toRemove []*list.Element 44 for e := t.items.data.Front(); e != nil; e = e.Next() { 45 entry := e.Value.(*indexedItemsEntry) // nolint:forcetypeassert 46 47 if entry.itemID != statusID { 48 // Not relevant. 49 continue 50 } 51 52 l.Debug("removing item") 53 toRemove = append(toRemove, e) 54 } 55 56 for _, e := range toRemove { 57 t.items.data.Remove(e) 58 } 59 60 return len(toRemove), nil 61 } 62 63 func (t *timeline) RemoveAllByOrBoosting(ctx context.Context, accountID string) (int, error) { 64 l := log. 65 WithContext(ctx). 66 WithFields(kv.Fields{ 67 {"accountTimeline", t.timelineID}, 68 {"accountID", accountID}, 69 }...) 70 71 t.Lock() 72 defer t.Unlock() 73 74 if t.items == nil || t.items.data == nil { 75 // Nothing to do. 76 return 0, nil 77 } 78 79 var toRemove []*list.Element 80 for e := t.items.data.Front(); e != nil; e = e.Next() { 81 entry := e.Value.(*indexedItemsEntry) // nolint:forcetypeassert 82 83 if entry.accountID != accountID && entry.boostOfAccountID != accountID { 84 // Not relevant. 85 continue 86 } 87 88 l.Debug("removing item") 89 toRemove = append(toRemove, e) 90 } 91 92 for _, e := range toRemove { 93 t.items.data.Remove(e) 94 } 95 96 return len(toRemove), nil 97 }