diff options
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/bundb.go | 63 | ||||
-rw-r--r-- | internal/db/db.go | 8 |
2 files changed, 34 insertions, 37 deletions
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 0ab1d1b83..2f7a8a022 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -473,43 +473,40 @@ func sqlitePragmas(ctx context.Context, conn *DBConn) error { CONVERSION FUNCTIONS */ -func (dbService *DBService) TagStringsToTags(ctx context.Context, tags []string, originAccountID string) ([]*gtsmodel.Tag, error) { +func (dbService *DBService) TagStringToTag(ctx context.Context, t string, originAccountID string) (*gtsmodel.Tag, error) { protocol := config.GetProtocol() host := config.GetHost() + now := time.Now() - newTags := []*gtsmodel.Tag{} - for _, t := range tags { - tag := >smodel.Tag{} - // we can use selectorinsert here to create the new tag if it doesn't exist already - // inserted will be true if this is a new tag we just created - if err := dbService.conn.NewSelect().Model(tag).Where("LOWER(?) = LOWER(?)", bun.Ident("name"), t).Scan(ctx); err != nil { - if err == sql.ErrNoRows { - // tag doesn't exist yet so populate it - newID, err := id.NewRandomULID() - if err != nil { - return nil, err - } - tag.ID = newID - tag.URL = fmt.Sprintf("%s://%s/tags/%s", protocol, host, t) - tag.Name = t - tag.FirstSeenFromAccountID = originAccountID - tag.CreatedAt = time.Now() - tag.UpdatedAt = time.Now() - useable := true - tag.Useable = &useable - listable := true - tag.Listable = &listable - } else { - return nil, fmt.Errorf("error getting tag with name %s: %s", t, err) - } - } + tag := >smodel.Tag{} + // we can use selectorinsert here to create the new tag if it doesn't exist already + // inserted will be true if this is a new tag we just created + if err := dbService.conn.NewSelect().Model(tag).Where("LOWER(?) = LOWER(?)", bun.Ident("name"), t).Scan(ctx); err != nil && err != sql.ErrNoRows { + return nil, fmt.Errorf("error getting tag with name %s: %s", t, err) + } - // bail already if the tag isn't useable - if !*tag.Useable { - continue + if tag.ID == "" { + // tag doesn't exist yet so populate it + newID, err := id.NewRandomULID() + if err != nil { + return nil, err } - tag.LastStatusAt = time.Now() - newTags = append(newTags, tag) + tag.ID = newID + tag.URL = protocol + "://" + host + "/tags/" + t + tag.Name = t + tag.FirstSeenFromAccountID = originAccountID + tag.CreatedAt = now + tag.UpdatedAt = now + useable := true + tag.Useable = &useable + listable := true + tag.Listable = &listable + } + + // bail already if the tag isn't useable + if !*tag.Useable { + return nil, fmt.Errorf("tag %s is not useable", t) } - return newTags, nil + tag.LastStatusAt = now + return tag, nil } diff --git a/internal/db/db.go b/internal/db/db.go index aa1929da9..b66b21141 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -52,12 +52,12 @@ type DB interface { USEFUL CONVERSION FUNCTIONS */ - // TagStringsToTags takes a slice of deduplicated, lowercase tags in the form "somehashtag", which have been + // TagStringToTag takes a lowercase tag in the form "somehashtag", which has been // used in a status. It takes the id of the account that wrote the status, and the id of the status itself, and then - // returns a slice of *apimodel.Tag corresponding to the given tags. If the tag already exists in database, that tag + // returns an *apimodel.Tag corresponding to the given tags. If the tag already exists in database, that tag // will be returned. Otherwise a pointer to a new tag struct will be created and returned. // - // Note: this func doesn't/shouldn't do any manipulation of the tags in the DB, it's just for checking + // Note: this func doesn't/shouldn't do any manipulation of tags in the DB, it's just for checking // if they exist in the db already, and conveniently returning them, or creating new tag structs. - TagStringsToTags(ctx context.Context, tags []string, originAccountID string) ([]*gtsmodel.Tag, error) + TagStringToTag(ctx context.Context, tag string, originAccountID string) (*gtsmodel.Tag, error) } |