Skip to content

Commit

Permalink
bump to Go 1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Feb 7, 2024
1 parent f8ca4fa commit 7eedcee
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '>=1.21.0'
go-version: '>=1.22.0'
- run: |
go generate ./migrations
go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.21-alpine AS build
FROM golang:1.22-alpine AS build
RUN apk add --no-cache gcc musl-dev openssl
COPY go.mod /src/
COPY go.sum /src/
Expand Down
28 changes: 0 additions & 28 deletions ap/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,3 @@ func (a *Actor) Value() (driver.Value, error) {
buf, err := json.Marshal(a)
return string(buf), err
}

type NullActor struct {
Valid bool
Actor
}

func (a *NullActor) Scan(src any) error {
if src == nil {
return nil
}
s, ok := src.(string)
if !ok {
return fmt.Errorf("unsupported conversion from %T to %T", src, a)
}
err := json.Unmarshal([]byte(s), &a.Actor)
if err == nil {
a.Valid = true
}
return err
}

func (a *NullActor) Value() (driver.Value, error) {
if !a.Valid {
return nil, nil
}
buf, err := json.Marshal(a.Actor)
return string(buf), err
}
23 changes: 8 additions & 15 deletions cmd/tootik/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,10 @@ func main() {
},
},
} {
l := svc.Listener
name := svc.Name
wg.Add(1)
go func() {
if err := l.ListenAndServe(ctx); err != nil {
log.Error("Listener has failed", "name", name, "error", err)
if err := svc.Listener.ListenAndServe(ctx); err != nil {
log.Error("Listener has failed", "name", svc.Name, "error", err)
}
cancel()
wg.Done()
Expand Down Expand Up @@ -291,12 +289,10 @@ func main() {
},
},
} {
q := queue.Queue
name := queue.Name
wg.Add(1)
go func() {
if err := q.Process(ctx); err != nil {
log.Error("Failed to process queue", "name", name, "error", err)
if err := queue.Queue.Process(ctx); err != nil {
log.Error("Failed to process queue", "name", queue.Name, "error", err)
}
cancel()
wg.Done()
Expand Down Expand Up @@ -340,21 +336,18 @@ func main() {
},
},
} {
name := job.Name
interval := job.Interval
runner := job.Runner
wg.Add(1)
go func() {
defer wg.Done()
defer cancel()

t := time.NewTicker(interval)
t := time.NewTicker(job.Interval)
defer t.Stop()

for {
log.Info("Running periodic job", "name", name)
if err := runner.Run(ctx); err != nil {
log.Error("Periodic job has failed", "name", name, "error", err)
log.Info("Running periodic job", "name", job.Name)
if err := job.Runner.Run(ctx); err != nil {
log.Error("Periodic job has failed", "name", job.Name, "error", err)
break
}

Expand Down
4 changes: 2 additions & 2 deletions front/guppy/guppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"io"
"log/slog"
"math"
"math/rand"
"math/rand/v2"
"net"
"net/url"
"sync"
Expand Down Expand Up @@ -80,7 +80,7 @@ func (gl *Listener) handle(ctx context.Context, wg *sync.WaitGroup, from net.Add
return
}

seq := 6 + rand.Intn(math.MaxInt16/2)
seq := 6 + rand.IntN(math.MaxInt16/2)

var buf bytes.Buffer
w := guppy.Wrap(&buf, seq)
Expand Down
22 changes: 11 additions & 11 deletions front/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (

type noteMetadata struct {
Note ap.Object
Author ap.NullActor
Sharer ap.NullActor
Author sql.Null[ap.Actor]
Sharer sql.Null[ap.Actor]
}

var verifiedRegex = regexp.MustCompile(`(\s*:[a-zA-Z0-9_]+:\s*)+`)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (r *request) PrintNote(w text.Writer, note *ap.Object, author *ap.Actor, sh
title += " ┃ edited"
}

var parentAuthor ap.NullActor
var parentAuthor sql.Null[ap.Actor]
if note.InReplyTo != "" {
if err := r.QueryRow(`select persons.actor from notes join persons on persons.id = notes.author where notes.id = ?`, note.InReplyTo).Scan(&parentAuthor); err != nil && errors.Is(err, sql.ErrNoRows) {
r.Log.Info("Parent post or author is missing", "id", note.InReplyTo)
Expand All @@ -249,11 +249,11 @@ func (r *request) PrintNote(w text.Writer, note *ap.Object, author *ap.Actor, sh
meta += fmt.Sprintf(" %d#️", len(hashtags))
}

if len(mentionedUsers.OrderedMap) == 1 && (!parentAuthor.Valid || !mentionedUsers.Contains(parentAuthor.ID)) {
if len(mentionedUsers.OrderedMap) == 1 && (!parentAuthor.Valid || !mentionedUsers.Contains(parentAuthor.V.ID)) {
meta += " 1👤"
} else if len(mentionedUsers.OrderedMap) > 1 && (!parentAuthor.Valid || !mentionedUsers.Contains(parentAuthor.ID)) {
} else if len(mentionedUsers.OrderedMap) > 1 && (!parentAuthor.Valid || !mentionedUsers.Contains(parentAuthor.V.ID)) {
meta += fmt.Sprintf(" %d👤", len(mentionedUsers.OrderedMap))
} else if len(mentionedUsers.OrderedMap) > 1 && parentAuthor.Valid && mentionedUsers.Contains(parentAuthor.ID) {
} else if len(mentionedUsers.OrderedMap) > 1 && parentAuthor.Valid && mentionedUsers.Contains(parentAuthor.V.ID) {
meta += fmt.Sprintf(" %d👤", len(mentionedUsers.OrderedMap)-1)
}

Expand All @@ -266,9 +266,9 @@ func (r *request) PrintNote(w text.Writer, note *ap.Object, author *ap.Actor, sh
}
}

if printParentAuthor && parentAuthor.Valid && parentAuthor.PreferredUsername != "" {
title += fmt.Sprintf(" ┃ RE: %s", parentAuthor.PreferredUsername)
} else if printParentAuthor && note.InReplyTo != "" && (!parentAuthor.Valid || parentAuthor.PreferredUsername == "") {
if printParentAuthor && parentAuthor.Valid && parentAuthor.V.PreferredUsername != "" {
title += fmt.Sprintf(" ┃ RE: %s", parentAuthor.V.PreferredUsername)
} else if printParentAuthor && note.InReplyTo != "" && (!parentAuthor.Valid || parentAuthor.V.PreferredUsername == "") {
title += " ┃ RE: ?"
}

Expand Down Expand Up @@ -464,9 +464,9 @@ func (r *request) PrintNotes(w text.Writer, rows data.OrderedMap[string, noteMet
}

if meta.Sharer.Valid {
r.PrintNote(w, &meta.Note, &meta.Author.Actor, &meta.Sharer.Actor, true, true, printParentAuthor, true)
r.PrintNote(w, &meta.Note, &meta.Author.V, &meta.Sharer.V, true, true, printParentAuthor, true)
} else {
r.PrintNote(w, &meta.Note, &meta.Author.Actor, nil, true, true, printParentAuthor, true)
r.PrintNote(w, &meta.Note, &meta.Author.V, nil, true, true, printParentAuthor, true)
}

lastDay = currentDay
Expand Down
4 changes: 2 additions & 2 deletions front/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *Handler) view(w text.Writer, r *request, args ...string) {

var note ap.Object
var author ap.Actor
var group ap.NullActor
var group sql.Null[ap.Actor]

if r.User == nil {
err = r.QueryRow(`select notes.object, persons.actor, groups.actor from notes join persons on persons.id = notes.author left join (select id, actor from persons where actor->>'type' = 'Group') groups on groups.id = notes.object->>'audience' where notes.id = ? and notes.public = 1`, postID).Scan(&note, &author, &group)
Expand Down Expand Up @@ -121,7 +121,7 @@ func (h *Handler) view(w text.Writer, r *request, args ...string) {
}

if group.Valid {
r.PrintNote(w, &note, &author, &group.Actor, false, false, true, false)
r.PrintNote(w, &note, &author, &group.V, false, false, true, false)
} else {
r.PrintNote(w, &note, &author, nil, false, false, true, false)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dimkr/tootik

go 1.21
go 1.22

require (
github.com/fsnotify/fsnotify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions inbox/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (q *Queue) ProcessBatch(ctx context.Context) (int, error) {

var id int64
var activityString string
var sender ap.NullActor
var sender sql.Null[ap.Actor]
if err := rows.Scan(&id, &sender, &activityString); err != nil {
q.Log.Error("Failed to scan activity", "error", err)
continue
Expand All @@ -494,7 +494,7 @@ func (q *Queue) ProcessBatch(ctx context.Context) (int, error) {
continue
}

activities.Store(activityString, &sender.Actor)
activities.Store(activityString, &sender.V)
}
rows.Close()

Expand Down

0 comments on commit 7eedcee

Please sign in to comment.