Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/assets/css/screen.css
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,23 @@ body {
content: "— ";
}

/* Source channel (e.g. Slack channel) for links and quotes */
.item-channel {
margin-left: 0.3em;
opacity: 0.8;
}

.item-channel::before {
content: "in ";
}

.quote-channel {
font-size: var(--font-size-sm);
color: var(--text-secondary);
padding-left: var(--space-4);
margin-top: var(--space-1);
}

/* Quote permalink link wrapper */
.quote-link {
text-decoration: none;
Expand Down
10 changes: 10 additions & 0 deletions internal/assets/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@
"description": "Client channel identifier (e.g., IRC channel, Slack channel ID)",
"example": "C67890"
},
"client_channel_name": {
"type": "string",
"description": "Display name of the channel (e.g., Slack channel name, with or without a leading #). Stored as the current name for client_channel and shown on the web view; sending a new name updates it for all items from that channel.",
"example": "general"
},
"client_user_id": {
"type": "string",
"description": "Platform-specific user ID",
Expand Down Expand Up @@ -765,6 +770,11 @@
"description": "Client channel identifier (e.g., IRC channel, Slack channel ID)",
"example": "C67890"
},
"client_channel_name": {
"type": "string",
"description": "Display name of the channel (e.g., Slack channel name, with or without a leading #). Stored as the current name for client_channel and shown on the web view; sending a new name updates it for all items from that channel.",
"example": "general"
},
"client_user_id": {
"type": "string",
"description": "Platform-specific user ID",
Expand Down
17 changes: 17 additions & 0 deletions internal/assets/themes/irc/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,23 @@ body {
color: var(--irc-muted);
}

/* Show the channel IRC-log style: "#channel <nick>" */
.author:has(.item-channel) {
display: flex;
justify-content: flex-end;
}

.author .item-channel {
order: -1;
margin: 0 0.5em 0 0;
color: var(--irc-muted);
opacity: 1;
}

.author .item-channel::before {
content: none;
}

.author a {
font-weight: 700;
}
Expand Down
55 changes: 55 additions & 0 deletions internal/data/channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package data

import (
"context"
"testing"
"time"
)

func TestChannelNames_UpsertAndRename(t *testing.T) {
store := newTestStore(t)
ctx := context.Background()

slack, team, chanID := "slack", "T1", "C1"
if err := store.UpsertChannel(ctx, &Channel{ClientType: slack, ClientNetwork: team, ClientChannel: chanID, Name: "beer", UpdatedAt: time.Now()}); err != nil {
t.Fatalf("UpsertChannel: %v", err)
}
if err := store.UpsertChannel(ctx, &Channel{ClientType: slack, ClientNetwork: team, ClientChannel: chanID, Name: "craft-beer", UpdatedAt: time.Now()}); err != nil {
t.Fatalf("UpsertChannel rename: %v", err)
}

names, err := store.GetChannelNames(ctx)
if err != nil {
t.Fatalf("GetChannelNames: %v", err)
}
if len(names) != 1 {
t.Fatalf("expected 1 channel, got %d", len(names))
}
if got := names.Lookup(&slack, &team, &chanID); got != "craft-beer" {
t.Errorf("expected renamed channel 'craft-beer', got %q", got)
}
other := "C2"
if got := names.Lookup(&slack, &team, &other); got != "" {
t.Errorf("expected empty name for unknown channel, got %q", got)
}
if got := names.Lookup(nil, &team, &chanID); got != "" {
t.Errorf("expected empty name for nil client type, got %q", got)
}
}

func TestGetGlobalTimeline_IncludesClientChannel(t *testing.T) {
store := newTestStore(t)
ctx := context.Background()

slack, team, chanID := "slack", "T1", "C1"
if _, err := store.InsertIRCLink(ctx, &IRCLink{User: "alice", URL: "https://example.com", Title: "x", ClientType: &slack, ClientNetwork: &team, ClientChannel: &chanID}); err != nil {
t.Fatalf("InsertIRCLink: %v", err)
}
items, err := store.GetGlobalTimeline(ctx, 10, 0)
if err != nil {
t.Fatalf("GetGlobalTimeline: %v", err)
}
if len(items) != 1 || items[0].ClientChannel == nil || *items[0].ClientChannel != chanID {
t.Fatalf("expected client_channel %q in timeline item, got %+v", chanID, items)
}
}
32 changes: 28 additions & 4 deletions internal/data/gorm_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *GormStore) Close() error {
}

func (s *GormStore) Bootstrap(ctx context.Context) error {
if err := s.db.AutoMigrate(&IRCLink{}, &Image{}, &Quote{}, &LinkPreview{}, &Tag{}, &ArchiveLookup{}, &ActivityPubKey{}, &ActivityPubFollower{}, &ActivityPubDelivery{}); err != nil {
if err := s.db.AutoMigrate(&IRCLink{}, &Image{}, &Quote{}, &LinkPreview{}, &Tag{}, &ArchiveLookup{}, &ActivityPubKey{}, &ActivityPubFollower{}, &ActivityPubDelivery{}, &Channel{}); err != nil {
return err
}

Expand Down Expand Up @@ -462,15 +462,18 @@ func (s *GormStore) GetGlobalTimeline(ctx context.Context, limit int, offset int

query := `
SELECT
'link' as type, ircLinkID as id, timestamp, title, url, '' as content, user as author, '' as md5sum
'link' as type, ircLinkID as id, timestamp, title, url, '' as content, user as author, '' as md5sum,
client_type, client_network, client_channel
FROM ircLink
UNION ALL
SELECT
'quote' as type, quoteID as id, timestamp, '' as title, '' as url, quote as content, author as author, '' as md5sum
'quote' as type, quoteID as id, timestamp, '' as title, '' as url, quote as content, author as author, '' as md5sum,
client_type, client_network, client_channel
FROM quote
UNION ALL
SELECT
'image' as type, imageID as id, timestamp, title, url, '' as content, '' as author, md5sum
'image' as type, imageID as id, timestamp, title, url, '' as content, '' as author, md5sum,
client_type, client_network, client_channel
FROM image
ORDER BY timestamp DESC
LIMIT ? OFFSET ?
Expand Down Expand Up @@ -592,6 +595,27 @@ func (s *GormStore) UpsertArchiveLookup(ctx context.Context, lookup *ArchiveLook
}).Create(lookup).Error
}

func (s *GormStore) UpsertChannel(ctx context.Context, channel *Channel) error {
return s.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "client_type"}, {Name: "client_network"}, {Name: "client_channel"},
},
DoUpdates: clause.AssignmentColumns([]string{"name", "updated_at"}),
}).Create(channel).Error
}

func (s *GormStore) GetChannelNames(ctx context.Context) (ChannelNames, error) {
var channels []Channel
if err := s.db.WithContext(ctx).Find(&channels).Error; err != nil {
return nil, err
}
names := make(ChannelNames, len(channels))
for _, c := range channels {
names[[3]string{c.ClientType, c.ClientNetwork, c.ClientChannel}] = c.Name
}
return names, nil
}

func (s *GormStore) GetUncheckedDeadLinkURLs(ctx context.Context) ([]string, error) {
var urls []string
// Find URLs with cached errors in link_previews that have no row in archive_lookups
Expand Down
32 changes: 32 additions & 0 deletions internal/data/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ func (Quote) TableName() string {
return "quote"
}

// Channel maps a client channel ID to its current display name.
// Names are refreshed whenever a client reports one, so channel
// renames are reflected on every past item from that channel.
type Channel struct {
ClientType string `json:"client_type" gorm:"column:client_type;type:varchar(50);primaryKey"`
ClientNetwork string `json:"client_network" gorm:"column:client_network;type:varchar(255);primaryKey"`
ClientChannel string `json:"client_channel" gorm:"column:client_channel;type:varchar(255);primaryKey"`
Name string `json:"name" gorm:"column:name;type:varchar(255)"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}

// TableName overrides the table name to `channels`
func (Channel) TableName() string {
return "channels"
}

// ChannelNames is a lookup of channel display names keyed by
// client type, network and channel ID.
type ChannelNames map[[3]string]string

// Lookup returns the display name for a channel, or "" if unknown.
func (n ChannelNames) Lookup(clientType, clientNetwork, clientChannel *string) string {
if clientType == nil || clientNetwork == nil || clientChannel == nil {
return ""
}
return n[[3]string{*clientType, *clientNetwork, *clientChannel}]
}

type UserStat struct {
User string `json:"user"`
LinkCount int `json:"link_count"`
Expand Down Expand Up @@ -244,6 +272,10 @@ type Store interface {
GetUncheckedDeadLinkURLs(ctx context.Context) ([]string, error)
GetStaleArchiveLookups(ctx context.Context, status string, recheckAfter time.Duration) ([]string, error)

// Channel display names
UpsertChannel(ctx context.Context, channel *Channel) error
GetChannelNames(ctx context.Context) (ChannelNames, error)

// Tag operations
CreateTag(ctx context.Context, tag Tag) (*Tag, error)
GetTagsByResource(ctx context.Context, resourceType string, resourceID int) ([]Tag, error)
Expand Down
18 changes: 10 additions & 8 deletions internal/handler/api_v1_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ func (h *Handler) apiV1ListLinks(w http.ResponseWriter, r *http.Request) {

// APILinkCreateRequest is the request body for POST /api/v1/links.
type APILinkCreateRequest struct {
URL string `json:"url"`
User string `json:"user"`
Tags []string `json:"tags,omitempty"`
ClientType *string `json:"client_type,omitempty"`
ClientNetwork *string `json:"client_network,omitempty"`
ClientChannel *string `json:"client_channel,omitempty"`
ClientUserID *string `json:"client_user_id,omitempty"`
ClientUserName *string `json:"client_user_name,omitempty"`
URL string `json:"url"`
User string `json:"user"`
Tags []string `json:"tags,omitempty"`
ClientType *string `json:"client_type,omitempty"`
ClientNetwork *string `json:"client_network,omitempty"`
ClientChannel *string `json:"client_channel,omitempty"`
ClientChannelName *string `json:"client_channel_name,omitempty"`
ClientUserID *string `json:"client_user_id,omitempty"`
ClientUserName *string `json:"client_user_name,omitempty"`
}

// apiV1CreateLink handles POST /api/v1/links
Expand Down Expand Up @@ -248,6 +249,7 @@ func (h *Handler) apiV1CreateLink(w http.ResponseWriter, r *http.Request) {
writeAPIError(w, http.StatusInternalServerError, "internal_error", "Failed to create link")
return
}
h.recordChannelName(ctx, req.ClientType, req.ClientNetwork, req.ClientChannel, req.ClientChannelName)

// Build response
isDuplicate := len(existingLinks) > 0
Expand Down
20 changes: 11 additions & 9 deletions internal/handler/api_v1_quotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ func (h *Handler) apiV1ListQuotes(w http.ResponseWriter, r *http.Request) {

// APIQuoteCreateRequest is the request body for POST /api/v1/quotes.
type APIQuoteCreateRequest struct {
Quote string `json:"quote"`
Author string `json:"author"`
Poster string `json:"poster"`
Tags []string `json:"tags,omitempty"`
ClientType *string `json:"client_type,omitempty"`
ClientNetwork *string `json:"client_network,omitempty"`
ClientChannel *string `json:"client_channel,omitempty"`
ClientUserID *string `json:"client_user_id,omitempty"`
ClientUserName *string `json:"client_user_name,omitempty"`
Quote string `json:"quote"`
Author string `json:"author"`
Poster string `json:"poster"`
Tags []string `json:"tags,omitempty"`
ClientType *string `json:"client_type,omitempty"`
ClientNetwork *string `json:"client_network,omitempty"`
ClientChannel *string `json:"client_channel,omitempty"`
ClientChannelName *string `json:"client_channel_name,omitempty"`
ClientUserID *string `json:"client_user_id,omitempty"`
ClientUserName *string `json:"client_user_name,omitempty"`
}

// apiV1CreateQuote handles POST /api/v1/quotes
Expand Down Expand Up @@ -180,6 +181,7 @@ func (h *Handler) apiV1CreateQuote(w http.ResponseWriter, r *http.Request) {
writeAPIError(w, http.StatusInternalServerError, "internal_error", "Failed to create quote")
return
}
h.recordChannelName(ctx, req.ClientType, req.ClientNetwork, req.ClientChannel, req.ClientChannelName)
if h.ActivityPub.Enabled() {
newQuote.ID = quoteID
h.ActivityPub.PublishNote(ctx, h.ActivityPub.NoteForQuote(newQuote))
Expand Down
43 changes: 43 additions & 0 deletions internal/handler/channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handler

import (
"context"
"log/slog"
"strings"
"time"

"tumble/internal/data"
)

// recordChannelName stores the display name a client reported for a
// channel. Failures are logged but never fail the request.
func (h *Handler) recordChannelName(ctx context.Context, clientType, clientNetwork, clientChannel, name *string) {
if clientType == nil || clientNetwork == nil || clientChannel == nil || name == nil {
return
}
n := strings.TrimPrefix(strings.TrimSpace(*name), "#")
if *clientType == "" || *clientNetwork == "" || *clientChannel == "" || n == "" {
return
}
err := h.Store.UpsertChannel(ctx, &data.Channel{
ClientType: *clientType,
ClientNetwork: *clientNetwork,
ClientChannel: *clientChannel,
Name: n,
UpdatedAt: time.Now(),
})
if err != nil {
slog.Error("Failed to record channel name", "channel", *clientChannel, "error", err)
}
}

// channelNames loads the channel display name lookup for rendering.
// On error it returns an empty lookup so pages still render.
func (h *Handler) channelNames(ctx context.Context) data.ChannelNames {
names, err := h.Store.GetChannelNames(ctx)
if err != nil {
slog.Error("Failed to load channel names", "error", err)
return data.ChannelNames{}
}
return names
}
Loading