···
+
"github.com/bluesky-social/indigo/atproto/syntax"
+
"tangled.sh/tangled.sh/core/api/tangled"
+
func (s *String) StringAt() syntax.ATURI {
+
return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", s.Did, tangled.StringNSID, s.Rkey))
+
type StringStats struct {
+
func (s String) Stats() StringStats {
+
lineCount, err := countLines(strings.NewReader(s.Contents))
+
LineCount: uint64(lineCount),
+
ByteCount: uint64(len(s.Contents)),
+
func (s String) Validate() error {
+
if !strings.Contains(s.Filename, ".") {
+
err = errors.Join(err, fmt.Errorf("missing filename extension"))
+
if strings.HasSuffix(s.Filename, ".") {
+
err = errors.Join(err, fmt.Errorf("filename ends with `.`"))
+
if utf8.RuneCountInString(s.Filename) > 140 {
+
err = errors.Join(err, fmt.Errorf("filename too long"))
+
if utf8.RuneCountInString(s.Description) > 280 {
+
err = errors.Join(err, fmt.Errorf("description too long"))
+
if len(s.Contents) == 0 {
+
err = errors.Join(err, fmt.Errorf("contents is empty"))
+
func (s *String) AsRecord() tangled.String {
+
Description: s.Description,
+
CreatedAt: s.Created.Format(time.RFC3339),
+
func StringFromRecord(did, rkey string, record tangled.String) String {
+
created, err := time.Parse(record.CreatedAt, time.RFC3339)
+
Filename: record.Filename,
+
Description: record.Description,
+
Contents: record.Contents,
+
func AddString(e Execer, s String) error {
+
values (?, ?, ?, ?, ?, ?, null)
+
on conflict(did, rkey) do update set
+
filename = excluded.filename,
+
description = excluded.description,
+
content = excluded.content,
+
strings.content != excluded.content
+
or strings.filename != excluded.filename
+
or strings.description != excluded.description then ?
+
s.Created.Format(time.RFC3339),
+
time.Now().Format(time.RFC3339),
+
func GetStrings(e Execer, filters ...filter) ([]String, error) {
+
var conditions []string
+
for _, filter := range filters {
+
conditions = append(conditions, filter.Condition())
+
args = append(args, filter.Arg()...)
+
whereClause = " where " + strings.Join(conditions, " and ")
+
query := fmt.Sprintf(`select
+
rows, err := e.Query(query, args...)
+
var editedAt sql.NullString
+
s.Created, err = time.Parse(time.RFC3339, createdAt)
+
e, err := time.Parse(time.RFC3339, editedAt.String)
+
if err := rows.Err(); err != nil {
+
func DeleteString(e Execer, filters ...filter) error {
+
var conditions []string
+
for _, filter := range filters {
+
conditions = append(conditions, filter.Condition())
+
args = append(args, filter.Arg()...)
+
whereClause = " where " + strings.Join(conditions, " and ")
+
query := fmt.Sprintf(`delete from strings %s`, whereClause)
+
_, err := e.Exec(query, args...)
+
func countLines(r io.Reader) (int, error) {
+
buf := make([]byte, 32*1024)
+
count += bytes.Count(buf[:c], nl)
+
/* handle last line not having a newline at the end */
+
if bufLen >= 1 && buf[(bufLen-1)%(32*1024)] != '\n' {