mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
![mlctrez](/assets/img/avatar_default.png)
Makes the avatar url point to salt shaker icon in application when no avatar found on lookup. Co-authored-by: mlctrez <mlctrez@gmail.com> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/111 Reviewed-by: James Mills <james@mills.io> Co-authored-by: mlctrez <mlctrez@noreply@mills.io> Co-committed-by: mlctrez <mlctrez@noreply@mills.io>
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package components
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/maxence-charriere/go-app/v9/pkg/app"
|
|
"go.yarn.social/lextwt"
|
|
)
|
|
|
|
const (
|
|
chatMessageTimeAction = "chatMessage.time"
|
|
chatMessageTimeKey = "time"
|
|
)
|
|
|
|
type ChatMessage struct {
|
|
app.Compo
|
|
Self bool
|
|
time string
|
|
Message *lextwt.SaltyText
|
|
}
|
|
|
|
func (c *ChatMessage) Render() app.UI {
|
|
message := c.Message.LiteralText()
|
|
messageBody := []app.UI{
|
|
app.Span().Class("msg-avatar").Body(app.Img().Src(c.lookupAvatar())).Title(c.Message.User.String()),
|
|
app.Div().Class("cm-msg-text").Body(app.Text(message),
|
|
app.If(c.time != "",
|
|
app.Br(),
|
|
app.Div().Class("cm-msg-time").Text(c.time),
|
|
),
|
|
),
|
|
}
|
|
|
|
return app.Div().OnClick(c.showTime).Class(c.messageClass()).Body(messageBody...)
|
|
}
|
|
|
|
func (c *ChatMessage) lookupAvatar() string {
|
|
var avatar string
|
|
if lookup, err := client.Lookup(c.Message.User.String()); err == nil {
|
|
avatar = lookup.Avatar()
|
|
}
|
|
if avatar == "" {
|
|
avatar = "web/favicon-ap.png"
|
|
}
|
|
return avatar
|
|
}
|
|
|
|
func (c *ChatMessage) OnMount(ctx app.Context) {
|
|
ctx.Handle(chatMessageTimeAction, func(context app.Context, action app.Action) {
|
|
if action.Value == c {
|
|
c.time = action.Tags.Get(chatMessageTimeKey)
|
|
c.Update()
|
|
}
|
|
})
|
|
}
|
|
|
|
func (c *ChatMessage) messageClass() string {
|
|
|
|
if c.Self {
|
|
return "chat-msg self"
|
|
}
|
|
|
|
return "chat-msg user"
|
|
}
|
|
|
|
func (c *ChatMessage) showTime(ctx app.Context, event app.Event) {
|
|
|
|
localTime := c.Message.Timestamp.DateTime().Local().Format(time.Stamp)
|
|
|
|
ctx.NewActionWithValue(chatMessageTimeAction, c, app.T(chatMessageTimeKey, localTime))
|
|
}
|