AVATAR
This commit is contained in:
@@ -3,12 +3,16 @@ package main
|
||||
// This file contains the function that generates message widgets depending on message stanza
|
||||
|
||||
import (
|
||||
"os"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"mellium.im/xmpp/jid"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jasonlovesdoggo/gopen"
|
||||
"encoding/base64"
|
||||
"github.com/jacoblockett/sanitizefilename"
|
||||
)
|
||||
|
||||
func generateMessageWidget(p stanza.Packet) gtk.Widgetter {
|
||||
@@ -65,11 +69,16 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter {
|
||||
mainBox.Append(replyBox)
|
||||
}
|
||||
|
||||
ocu := OccupantID{}
|
||||
m.Get(&ocu)
|
||||
|
||||
|
||||
authorBox := gtk.NewBox(gtk.OrientationHorizontal, 10)
|
||||
contentBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
|
||||
im := newImageFromPath("debug.png")
|
||||
// im := newImageFromPath("debug.png")
|
||||
|
||||
// authorBox.Append(im)
|
||||
im := getAvatar(m.From, ocu.ID)
|
||||
im.SetPixelSize(40)
|
||||
authorBox.Append(im)
|
||||
al := gtk.NewLabel(jid.MustParse(m.From).Resourcepart())
|
||||
@@ -104,6 +113,8 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter {
|
||||
authorBox.Append(mbtn)
|
||||
}
|
||||
|
||||
|
||||
|
||||
return mainBox
|
||||
}
|
||||
|
||||
@@ -111,3 +122,56 @@ func getVAdjustment(scrolledWindow *gtk.ScrolledWindow) *gtk.Adjustment {
|
||||
val := scrolledWindow.ObjectProperty("vadjustment").(*gtk.Adjustment)
|
||||
return val
|
||||
}
|
||||
|
||||
func getAvatar(j, hash string) *gtk.Image {
|
||||
hash = sanitizefilename.Sanitize(hash)
|
||||
|
||||
_, err := os.ReadFile(hash)
|
||||
if err == nil {
|
||||
return newImageFromPath(hash)
|
||||
}
|
||||
|
||||
iqResp, err := stanza.NewIQ(stanza.Attrs{
|
||||
Type: "get",
|
||||
From: clientroot.Session.BindJid,
|
||||
To: j,
|
||||
Id: "vc2",
|
||||
Lang: "e",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
iqResp.Payload = &VCard{}
|
||||
|
||||
ctx := context.TODO()
|
||||
mychan, err := client.SendIQ(ctx, iqResp)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
result := <- mychan
|
||||
card, ok := result.Payload.(*VCard)
|
||||
if !ok {
|
||||
return newImageFromPath("debug.png")
|
||||
}
|
||||
|
||||
base64_data := card.Photo.Binval
|
||||
if card.Photo.Binval == "" {
|
||||
return newImageFromPath("debug.png")
|
||||
}
|
||||
|
||||
data, err := base64.StdEncoding.DecodeString(base64_data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
||||
err = os.WriteFile(hash, data, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// TODO: Implement caching!
|
||||
return newImageFromPath(hash)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user