fix misskey author fetch, fmt
This commit is contained in:
parent
44bdad2e50
commit
fead16168a
6 changed files with 162 additions and 151 deletions
|
@ -25,7 +25,6 @@ func (self *MastoAdapter) Name() string {
|
||||||
return self.nickname
|
return self.nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (self *MastoAdapter) Init(settings Settings, data chan SocketData) error {
|
func (self *MastoAdapter) Init(settings Settings, data chan SocketData) error {
|
||||||
self.nickname = settings.Nickname
|
self.nickname = settings.Nickname
|
||||||
self.server = *settings.Server
|
self.server = *settings.Server
|
||||||
|
|
|
@ -7,9 +7,10 @@ import (
|
||||||
mkcore "github.com/yitsushi/go-misskey/core"
|
mkcore "github.com/yitsushi/go-misskey/core"
|
||||||
mkm "github.com/yitsushi/go-misskey/models"
|
mkm "github.com/yitsushi/go-misskey/models"
|
||||||
n "github.com/yitsushi/go-misskey/services/notes"
|
n "github.com/yitsushi/go-misskey/services/notes"
|
||||||
users "github.com/yitsushi/go-misskey/services/users"
|
|
||||||
tl "github.com/yitsushi/go-misskey/services/notes/timeline"
|
tl "github.com/yitsushi/go-misskey/services/notes/timeline"
|
||||||
|
users "github.com/yitsushi/go-misskey/services/users"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,6 +26,7 @@ type MisskeyAdapter struct {
|
||||||
// from different API calls instead of streaming them in a single channel
|
// from different API calls instead of streaming them in a single channel
|
||||||
|
|
||||||
cache map[string]time.Time
|
cache map[string]time.Time
|
||||||
|
mtx sync.RWMutex
|
||||||
|
|
||||||
stop chan bool
|
stop chan bool
|
||||||
}
|
}
|
||||||
|
@ -99,8 +101,8 @@ func (self *MisskeyAdapter) poll() {
|
||||||
default:
|
default:
|
||||||
|
|
||||||
// TODO: we have to actually decode and pass our filter criteria
|
// TODO: we have to actually decode and pass our filter criteria
|
||||||
// probe for new notes
|
|
||||||
|
|
||||||
|
// probe for new notes
|
||||||
probenote, err := timelineService.Get(tl.GetRequest{
|
probenote, err := timelineService.Get(tl.GetRequest{
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
})
|
})
|
||||||
|
@ -166,22 +168,24 @@ func (self *MisskeyAdapter) poll() {
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MisskeyAdapter) isNew(n mkm.Note) bool {
|
func (self *MisskeyAdapter) isNew(n mkm.Note) bool {
|
||||||
|
self.mtx.RLock()
|
||||||
timestamp, exists := self.cache[n.ID]
|
timestamp, exists := self.cache[n.ID]
|
||||||
|
self.mtx.RUnlock()
|
||||||
return !exists || timestamp.Before(n.CreatedAt)
|
return !exists || timestamp.Before(n.CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MisskeyAdapter) toMessageIfNew(n mkm.Note) *Message {
|
func (self *MisskeyAdapter) toMessageIfNew(n mkm.Note) *Message {
|
||||||
return self.toMessage(n, false);
|
return self.toMessage(n, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MisskeyAdapter) toMessage(n mkm.Note, bustCache bool) *Message {
|
func (self *MisskeyAdapter) toMessage(n mkm.Note, bustCache bool) *Message {
|
||||||
|
self.mtx.RLock()
|
||||||
timestamp, exists := self.cache[n.ID]
|
timestamp, exists := self.cache[n.ID]
|
||||||
|
self.mtx.RUnlock()
|
||||||
if bustCache || !exists || timestamp.Before(n.CreatedAt) {
|
if bustCache || !exists || timestamp.Before(n.CreatedAt) {
|
||||||
host := mkcore.StringValue(n.User.Host)
|
host := mkcore.StringValue(n.User.Host)
|
||||||
authorId := ""
|
authorId := ""
|
||||||
|
@ -191,7 +195,9 @@ func (self *MisskeyAdapter) toMessage(n mkm.Note, bustCache bool) *Message {
|
||||||
authorId = fmt.Sprintf("@%s", n.User.Username)
|
authorId = fmt.Sprintf("@%s", n.User.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.mtx.Lock()
|
||||||
self.cache[n.ID] = n.CreatedAt
|
self.cache[n.ID] = n.CreatedAt
|
||||||
|
self.mtx.Unlock()
|
||||||
msg := Message{
|
msg := Message{
|
||||||
Datagram: Datagram{
|
Datagram: Datagram{
|
||||||
Id: n.ID,
|
Id: n.ID,
|
||||||
|
@ -237,7 +243,7 @@ func (self *MisskeyAdapter) toAuthor(usr mkm.User) *Author {
|
||||||
}
|
}
|
||||||
|
|
||||||
author := Author{
|
author := Author{
|
||||||
Datagram: Datagram {
|
Datagram: Datagram{
|
||||||
Id: authorId,
|
Id: authorId,
|
||||||
Uri: mkcore.StringValue(usr.URL),
|
Uri: mkcore.StringValue(usr.URL),
|
||||||
Protocol: "misskey",
|
Protocol: "misskey",
|
||||||
|
@ -253,7 +259,7 @@ func (self *MisskeyAdapter) toAuthor(usr mkm.User) *Author {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MisskeyAdapter) Fetch(etype, id string) error {
|
func (self *MisskeyAdapter) Fetch(etype, id string) error {
|
||||||
switch (etype) {
|
switch etype {
|
||||||
case "message":
|
case "message":
|
||||||
data, err := self.mk.Notes().Show(id)
|
data, err := self.mk.Notes().Show(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -289,7 +295,7 @@ func (self *MisskeyAdapter) Fetch(etype, id string) error {
|
||||||
} else {
|
} else {
|
||||||
for _, n := range data {
|
for _, n := range data {
|
||||||
msg := self.toMessage(n, true)
|
msg := self.toMessage(n, true)
|
||||||
if msg != nil{
|
if msg != nil {
|
||||||
self.data <- msg
|
self.data <- msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,10 +303,11 @@ func (self *MisskeyAdapter) Fetch(etype, id string) error {
|
||||||
case "author":
|
case "author":
|
||||||
user := ""
|
user := ""
|
||||||
host := ""
|
host := ""
|
||||||
|
fmt.Printf("fetch author: %s\n", id)
|
||||||
idParts := strings.Split(id, "@")
|
idParts := strings.Split(id, "@")
|
||||||
user = idParts[0]
|
user = idParts[1]
|
||||||
if len(idParts) == 2 {
|
if len(idParts) == 3 {
|
||||||
host = idParts[1]
|
host = idParts[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
var hostPtr *string = nil
|
var hostPtr *string = nil
|
||||||
|
@ -308,6 +315,12 @@ func (self *MisskeyAdapter) Fetch(etype, id string) error {
|
||||||
hostPtr = &host
|
hostPtr = &host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if hostPtr == nil {
|
||||||
|
fmt.Printf("looking up user: @%s\n", user)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("looking up remote user: @%s@%s\n", user, host)
|
||||||
|
}
|
||||||
|
|
||||||
// fmt.Printf("attempting user resolution: @%s@%s\n", user, host)
|
// fmt.Printf("attempting user resolution: @%s@%s\n", user, host)
|
||||||
data, err := self.mk.Users().Show(users.ShowRequest{
|
data, err := self.mk.Users().Show(users.ShowRequest{
|
||||||
Username: &user,
|
Username: &user,
|
||||||
|
|
4
build.sh
4
build.sh
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
client)
|
front)
|
||||||
if [ ! -e ./frontend/.js ]; then
|
if [ ! -e ./frontend/.js ]; then
|
||||||
mkdir ./frontend/.js
|
mkdir ./frontend/.js
|
||||||
fi
|
fi
|
||||||
|
@ -13,10 +13,10 @@ case "$1" in
|
||||||
fi
|
fi
|
||||||
if [ -s ${buildlog} ]; then
|
if [ -s ${buildlog} ]; then
|
||||||
cat ${buildlog} | head
|
cat ${buildlog} | head
|
||||||
rm ${buildlog}
|
|
||||||
else
|
else
|
||||||
npx webpack --config webpack.config.js
|
npx webpack --config webpack.config.js
|
||||||
fi
|
fi
|
||||||
|
rm ${buildlog}
|
||||||
;;
|
;;
|
||||||
server)
|
server)
|
||||||
go mod tidy
|
go mod tidy
|
||||||
|
|
|
@ -148,7 +148,7 @@ func ProtectWithSubscriberKey(next http.Handler, subscribers map[*Subscriber][]a
|
||||||
if strings.HasPrefix(authHeader, "Bearer ") {
|
if strings.HasPrefix(authHeader, "Bearer ") {
|
||||||
subscriberKey := strings.Split(authHeader, "Bearer ")[1]
|
subscriberKey := strings.Split(authHeader, "Bearer ")[1]
|
||||||
if getSubscriberByKey(subscriberKey, subscribers) != nil {
|
if getSubscriberByKey(subscriberKey, subscribers) != nil {
|
||||||
next.ServeHTTP(w, req);
|
next.ServeHTTP(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,6 @@ func apiAdapterFetch(next http.Handler, subscribers map[*Subscriber][]adapter.Ad
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (self *BBSServer) apiMux() http.Handler {
|
func (self *BBSServer) apiMux() http.Handler {
|
||||||
errTemplate, err := template.New("err").Parse("{{ $params := (.Context).Value \"params\" }}<html><body><h1>ERROR {{ $params.ErrorCode }}</h1><p class='error'>{{ $params.ErrorMessage }}</p></body></html>")
|
errTemplate, err := template.New("err").Parse("{{ $params := (.Context).Value \"params\" }}<html><body><h1>ERROR {{ $params.ErrorCode }}</h1><p class='error'>{{ $params.ErrorMessage }}</p></body></html>")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue