Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link shorten feedme links #10

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 0
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go", "\\.#"]
exclude_unchanged = true
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = true
stop_on_error = true

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/goirc
/main
/tmp/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include .env

start:
go run main.go
watch:
air

fmt:
go fmt main.go
Expand Down
1 change: 1 addition & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kill_timeout = "5s"
IRC_NICK = "annie"
IRC_SERVER = "irc.libera.chat:6697"
PORT = "8080"
ROOT_URL = "https://annie.fly.dev"
SQLITE_DB = "/data/annie.db"

[[mounts]]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/kkdai/youtube/v2 v2.8.1
github.com/kljensen/snowball v0.9.0
github.com/sqids/sqids-go v0.4.1
github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64
github.com/xhit/go-str2duration/v2 v2.1.0
golang.org/x/text v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/sqids/sqids-go v0.4.1 h1:eQKYzmAZbLlRwHeHYPF35QhgxwZHLnlmVj9AkIj/rrw=
github.com/sqids/sqids-go v0.4.1/go.mod h1:EMwHuPQgSNFS0A49jESTfIQS+066XQTVhukrzEPScl8=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64 h1:l/T7dYuJEQZOwVOpjIXr1180aM9PZL/d1MnMVIxefX4=
github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64/go.mod h1:Q1NAJOuRdQCqN/VIWdnaaEhV8LpeO2rtlBP7/iDJNII=
Expand Down
16 changes: 15 additions & 1 deletion handlers/feed_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package handlers

import (
"database/sql"
"fmt"
"goirc/bot"
"goirc/durfmt"
"goirc/internal/idstr"
"goirc/model"
"goirc/model/notes"
"goirc/util"
"os"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -89,7 +92,18 @@ func FeedMe(params bot.HandlerParams) error {
return err
}

params.Privmsgf(params.Target, "%s (%s ago) [pipe=%d+%d]", note.Text, util.Since(note.CreatedAt), ready, fermenting)
var text string
if note.Kind == "link" {
str, err := idstr.Encode(note.Id)
if err != nil {
return err
}
text = fmt.Sprintf("%s/%s", os.Getenv("ROOT_URL"), str)
} else {
text = note.Text
}

params.Privmsgf(params.Target, "%s (%s ago) [pipe=%d+%d]", text, util.Since(note.CreatedAt), ready, fermenting)

lastSentAt = time.Now()

Expand Down
31 changes: 31 additions & 0 deletions internal/idstr/idstr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package idstr

import (
"errors"

"github.com/sqids/sqids-go"
)

var s = must(sqids.New(sqids.Options{
Alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
}))

func Decode(str string) (int64, error) {
n := s.Decode(str)

if len(n) != 1 {
return 0, errors.New("could not decode")
}
return int64(n[0]), nil
}

func Encode(id int64) (string, error) {
return s.Encode([]uint64{uint64(id)})
}

func must(s *sqids.Sqids, err error) *sqids.Sqids {
if err != nil {
panic(err)
}
return s
}
58 changes: 58 additions & 0 deletions internal/idstr/idstr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package idstr

import (
"fmt"
"testing"
)

var tt = []struct {
num int64
str string
}{
{
num: 1,
str: "Uk",
},
{
num: 2,
str: "gb",
},
{
num: 69,
str: "ARb",
},
{
num: 1000,
str: "pnd",
},
{
num: 23443,
str: "AV7d",
},
{
num: 999999,
str: "UQ1Nd",
},
}

func TestEncode(t *testing.T) {
for _, tc := range tt {
t.Run(fmt.Sprint(tc.num), func(t *testing.T) {
got, _ := Encode(tc.num)
if got != tc.str {
t.Errorf("expected %s got %s", tc.str, got)
}
})
}
}

func TestDecode(t *testing.T) {
for _, tc := range tt {
t.Run(tc.str, func(t *testing.T) {
got, _ := Decode(tc.str)
if got != tc.num {
t.Errorf("expected %d got %d", tc.num, got)
}
})
}
}
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ in
{ nixpkgs ? import <nixpkgs> {} }:
with nixpkgs; mkShell {
buildInputs = [
air
unstable.go_1_21
unstable.golint
unstable.gopls
Expand Down
25 changes: 25 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package web

import (
"bytes"
"database/sql"
_ "embed"
"errors"
"goirc/internal/idstr"
"goirc/model/notes"
"goirc/util"
"html/template"
Expand Down Expand Up @@ -128,6 +131,28 @@ func Serve(db *sqlx.DB) {
w.Write(out.Bytes())
})

r.Get("/{sqid}", func(w http.ResponseWriter, r *http.Request) {
sqid := chi.URLParam(r, "sqid")
id, err := idstr.Decode(sqid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var text string
err = db.Get(&text, `select text from notes where id = ? and kind = 'link'`, id)
if errors.Is(err, sql.ErrNoRows) {
http.NotFound(w, r)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Redirect(w, r, text, http.StatusSeeOther)
})

http.ListenAndServe(":"+os.Getenv("PORT"), r)
}

Expand Down