- Item 1: Extract GetUserByID/GetChannelByID lookup methods, use from relation methods - Item 2: Initialize slices with literals so JSON gets [] not null - Item 3: Populate CreatedAt/UpdatedAt with time.Now() on all Create methods - Item 4: Wrap each migration's SQL + recording in a transaction - Item 5: Check error from res.LastInsertId() in QueueMessage - Item 6: Add DequeueMessages and AckMessages methods - Item 8: Add GetUserByNick, GetUserByToken, DeleteAuthToken, UpdateUserLastSeen - Item 9: Run PRAGMA foreign_keys = ON on every new connection - Item 10: Builds clean, all tests pass
37 lines
919 B
Go
37 lines
919 B
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// ChannelMember represents a user's membership in a channel.
|
|
type ChannelMember struct {
|
|
Base
|
|
|
|
ChannelID string `json:"channelId"`
|
|
UserID string `json:"userId"`
|
|
Modes string `json:"modes"`
|
|
JoinedAt time.Time `json:"joinedAt"`
|
|
Nick string `json:"nick"` // denormalized from users table
|
|
}
|
|
|
|
// User returns the full User for this membership.
|
|
func (cm *ChannelMember) User(ctx context.Context) (*User, error) {
|
|
if ul := cm.GetUserLookup(); ul != nil {
|
|
return ul.GetUserByID(ctx, cm.UserID)
|
|
}
|
|
|
|
return nil, fmt.Errorf("user lookup not available")
|
|
}
|
|
|
|
// Channel returns the full Channel for this membership.
|
|
func (cm *ChannelMember) Channel(ctx context.Context) (*Channel, error) {
|
|
if cl := cm.GetChannelLookup(); cl != nil {
|
|
return cl.GetChannelByID(ctx, cm.ChannelID)
|
|
}
|
|
|
|
return nil, fmt.Errorf("channel lookup not available")
|
|
}
|