summaryrefslogtreecommitdiff
path: root/internal/client/ui/core/profile/profile.go
blob: 3ba539d63fc1a6964c4f878f9bf0b93849e04514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Eko: A terminal based social media platform
// Copyright (C) 2025 Kyren223
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package profile

import (
	"crypto/ed25519"
	"strconv"
	"strings"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/kyren223/eko/internal/client/ui/colors"
	"github.com/kyren223/eko/internal/client/ui/core/state"
	"github.com/kyren223/eko/pkg/assert"
	"github.com/kyren223/eko/pkg/snowflake"
	"golang.org/x/crypto/ssh"
)

var width = 70

type Model struct {
	id snowflake.ID
}

func New(userId snowflake.ID) Model {
	return Model{
		id: userId,
	}
}

func (m Model) Init() tea.Cmd {
	return nil
}

func (m Model) View() string {
	user := state.State.Users[m.id]

	var builder strings.Builder

	headerStyle := lipgloss.NewStyle().
		PaddingLeft(2).
		Width(width).
		Background(colors.Background).
		Foreground(colors.Focus)

	name := user.Name
	name = lipgloss.NewStyle().
		MarginBackground(colors.Background).
		Background(colors.Gold).
		Foreground(colors.Background).
		Bold(true).
		Render(name)
	paddingLeft := lipgloss.NewStyle().
		MarginLeft(2).
		Background(colors.Background).
		Foreground(colors.Gold).
		Render("")
	paddingRight := lipgloss.NewStyle().
		Background(colors.Background).
		Foreground(colors.Gold).
		Render("")
	builder.WriteString(paddingLeft)
	builder.WriteString(name)
	builder.WriteString(paddingRight)
	builder.WriteByte('\n')
	builder.WriteByte('\n')

	publicDMs := "Private DMs"
	if user.IsPublicDM {
		publicDMs = "Public DMs"
	}

	id := strconv.FormatInt(int64(user.ID), 10)
	info := lipgloss.NewStyle().
		PaddingLeft(2).
		Width(width).
		Background(colors.Background).
		Foreground(colors.LightGray).
		Render("ID: " + id + " | " + publicDMs)
	builder.WriteString(info)
	builder.WriteByte('\n')
	builder.WriteByte('\n')

	publicKeyHeader := headerStyle.Render("Public Key (ssh-ed25519)")
	publicKey := lipgloss.NewStyle().
		PaddingLeft(2).
		Width(width).
		Render(publicKeyToSshString(user.PublicKey))
	builder.WriteString(publicKeyHeader)
	builder.WriteByte('\n')
	builder.WriteByte('\n')
	builder.WriteString(publicKey)
	builder.WriteByte('\n')

	aboutMeHeader := headerStyle.Render("About me")
	builder.WriteString(aboutMeHeader)
	builder.WriteByte('\n')
	builder.WriteByte('\n')

	textBg := colors.BackgroundHighlight
	textFg := colors.White
	bg := colors.Background

	description := user.Description
	if description == "" {
		description = lipgloss.NewStyle().
			Foreground(colors.Purple).
			Render("No description was provided")
	}

	excessWidth := 4 - 2
	bgStyle := lipgloss.NewStyle().Background(textBg).Foreground(bg)
	topMiddle := strings.Repeat(" ", width-excessWidth)
	top := bgStyle.Render("🭠🭘" + topMiddle + "🭣🭕")
	middle := lipgloss.NewStyle().Width(width+2).Padding(0, 2).
		Background(textBg).Foreground(textFg).Render(description)
	bgStyle2 := lipgloss.NewStyle().Foreground(textBg)
	bottomMiddle := strings.Repeat("█", width-excessWidth)
	bottom := bgStyle2.Render("🭥🭓" + bottomMiddle + "🭞🭚")
	aboutMe := lipgloss.JoinVertical(lipgloss.Left, top, middle, bottom)

	builder.WriteString(aboutMe)

	content := builder.String()

	return lipgloss.NewStyle().
		Border(lipgloss.ThickBorder()).
		Padding(1, 2).
		Align(lipgloss.Left, lipgloss.Center).
		BorderBackground(colors.Background).
		BorderForeground(colors.White).
		Background(colors.Background).
		Foreground(colors.White).
		Render(content)
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
	return m, nil
}

func publicKeyToSshString(pub ed25519.PublicKey) string {
	sshPubKey, err := ssh.NewPublicKey(pub)
	assert.NoError(err, "converting to ssh key should never fail")
	sshPubKeyBytes := ssh.MarshalAuthorizedKey(sshPubKey)
	return strings.Split(string(sshPubKeyBytes), " ")[1]
}