added oembed support to pnut channel message

This commit is contained in:
Morgan McMillian 2021-07-14 21:34:43 -07:00
parent 2217d19353
commit 151572154b
3 changed files with 48 additions and 20 deletions

3
go.mod
View file

@ -3,7 +3,8 @@ module git.sr.ht/~thrrgilag/pnut-bridge
go 1.16 go 1.16
require ( require (
git.sr.ht/~thrrgilag/woodstock v0.0.0-20210613221914-3b9e0ef92799 git.sr.ht/~thrrgilag/woodstock v0.0.0-20210714032038-b22c4f10cc34
github.com/gabriel-vasile/mimetype v1.3.1 // indirect
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect
gopkg.in/ini.v1 v1.62.0 gopkg.in/ini.v1 v1.62.0

11
go.sum
View file

@ -1,5 +1,9 @@
git.sr.ht/~thrrgilag/woodstock v0.0.0-20210613221914-3b9e0ef92799 h1:iiUiZTW7yksLArreiNO1KcAkMmAvmMquz6etgaKzpCU= git.sr.ht/~thrrgilag/woodstock v0.0.0-20210613221914-3b9e0ef92799 h1:iiUiZTW7yksLArreiNO1KcAkMmAvmMquz6etgaKzpCU=
git.sr.ht/~thrrgilag/woodstock v0.0.0-20210613221914-3b9e0ef92799/go.mod h1:giZm2mNVkz9BeA31LtUC2gwIYfUSUPQINp+tI8EGFTc= git.sr.ht/~thrrgilag/woodstock v0.0.0-20210613221914-3b9e0ef92799/go.mod h1:giZm2mNVkz9BeA31LtUC2gwIYfUSUPQINp+tI8EGFTc=
git.sr.ht/~thrrgilag/woodstock v0.0.0-20210714032038-b22c4f10cc34 h1:nTH4UWrZTwEhkFvuR4Y4vV6KMSoZuF+3qFmgGs9Vm2I=
git.sr.ht/~thrrgilag/woodstock v0.0.0-20210714032038-b22c4f10cc34/go.mod h1:giZm2mNVkz9BeA31LtUC2gwIYfUSUPQINp+tI8EGFTc=
github.com/gabriel-vasile/mimetype v1.3.1 h1:qevA6c2MtE1RorlScnixeG0VA1H4xrXyhyX3oWBynNQ=
github.com/gabriel-vasile/mimetype v1.3.1/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
@ -12,8 +16,15 @@ github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIK
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 h1:Ugb8sMTWuWRC3+sz5WeN/4kejDx9BvIwnPUiJBjJE+8=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View file

@ -2,16 +2,23 @@ package main
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/json" "encoding/json"
"flag" "flag"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"time" "time"
"git.sr.ht/~thrrgilag/woodstock" "git.sr.ht/~thrrgilag/woodstock"
"github.com/gabriel-vasile/mimetype"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
@ -97,6 +104,8 @@ type PnutOembed struct {
Type string `json:"type"` Type string `json:"type"`
Title string `json:"title"` Title string `json:"title"`
Url string `json:"url"` Url string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
} }
func subscribe(connection_id string, access_token string, rooms []Room) { func subscribe(connection_id string, access_token string, rooms []Room) {
@ -207,28 +216,35 @@ func bridgeMsgHandler(conf *Config, msg MbMessage) {
client.SetAccessToken(conf.PnutAccessToken) client.SetAccessToken(conf.PnutAccessToken)
channel_id := getPnutChannel(conf.Rooms, msg.Gateway) channel_id := getPnutChannel(conf.Rooms, msg.Gateway)
text := msg.Username + " " + msg.Text text := msg.Username + " " + msg.Text
var raw []woodstock.Raw
for _, file := range msg.Extra.File { for _, file := range msg.Extra.File {
text = text + "\n" + file.URL fdata, err := base64.StdEncoding.DecodeString(file.Data)
// log.Println("name, ", file.Name) if err != nil {
// log.Println("url, ", file.URL) log.Println("!! error decoding file", err)
// fdata, err := base64.StdEncoding.DecodeString(file.Data) }
// if err != nil { mtype := mimetype.Detect(fdata)
// log.Println("!! error decoding file", err) ir := bytes.NewReader(fdata)
// } im, _, err := image.DecodeConfig(ir)
// mtype := mimetype.Detect(fdata) if err != nil {
// log.Println("type, ", mtype) log.Println("Couldn't decode image?", err)
// if strings.HasPrefix(mtype, "image") { }
// oembed := PnutOembed{ if strings.HasPrefix(mtype.String(), "image") {
// Version: "1.0", oembed := PnutOembed{
// Type: "photo", Version: "1.0",
// Title: file.Name, Type: "photo",
// Url: file.URL, Title: file.Name,
// } Url: file.URL,
// // TODO finish this when woodstock is fixed Width: im.Width,
// } Height: im.Height,
}
raw = append(raw, woodstock.Raw{Type: "io.pnut.core.oembed", Value: oembed})
} else {
text = text + "\n" + file.URL
}
} }
log.Printf(">> sending message from %s to channel %s ...", msg.Gateway, channel_id) log.Printf(">> sending message from %s to channel %s ...", msg.Gateway, channel_id)
_, err := client.CreateMessage(channel_id, text) newmessage := woodstock.NewMessage{Text: text, Raw: raw}
_, err := client.CreateMessage(channel_id, newmessage)
if err != nil { if err != nil {
log.Println("!! problem posting message to pnut.io", err) log.Println("!! problem posting message to pnut.io", err)
} }