feat(organization): implement searchable organizations feature

- Added a 'searchable' flag to organizations, allowing them to be discoverable and joinable by ID without an invite code.
- Introduced API endpoints for searching and joining searchable organizations.
- Updated UI components to support searching for organizations and displaying results.
- Enhanced localization for new search-related features in both English and Chinese.

This update improves user experience by enabling easier discovery and joining of organizations.
This commit is contained in:
wizardchen
2026-01-30 19:20:52 +08:00
committed by lyingbug
parent 760bbffafe
commit 2818a4fe8f
13 changed files with 1065 additions and 312 deletions
@@ -77,10 +77,28 @@ func (r *organizationRepository) ListByUserID(ctx context.Context, userID string
return orgs, nil
}
// ListSearchable lists organizations that are searchable (open for discovery), optionally filtered by name/description
func (r *organizationRepository) ListSearchable(ctx context.Context, query string, limit int) ([]*types.Organization, error) {
if limit <= 0 {
limit = 20
}
var orgs []*types.Organization
q := r.db.WithContext(ctx).Where("searchable = ?", true)
if query != "" {
pattern := "%" + query + "%"
q = q.Where("name ILIKE ? OR description ILIKE ?", pattern, pattern)
}
err := q.Order("created_at DESC").Limit(limit).Find(&orgs).Error
if err != nil {
return nil, err
}
return orgs, nil
}
// Update updates an organization (Select ensures zero values like invite_code_validity_days=0 are persisted)
func (r *organizationRepository) Update(ctx context.Context, org *types.Organization) error {
return r.db.WithContext(ctx).Model(&types.Organization{}).Where("id = ?", org.ID).
Select("name", "description", "require_approval", "invite_code_validity_days", "updated_at").
Select("name", "description", "require_approval", "searchable", "invite_code_validity_days", "updated_at").
Updates(org).Error
}
@@ -170,6 +170,9 @@ func (s *organizationService) UpdateOrganization(ctx context.Context, id string,
if req.RequireApproval != nil {
org.RequireApproval = *req.RequireApproval
}
if req.Searchable != nil {
org.Searchable = *req.Searchable
}
if req.InviteCodeValidityDays != nil {
if !ValidInviteCodeValidityDays[*req.InviteCodeValidityDays] {
return nil, ErrInvalidValidityDays
@@ -185,6 +188,85 @@ func (s *organizationService) UpdateOrganization(ctx context.Context, id string,
return org, nil
}
// SearchSearchableOrganizations returns searchable (discoverable) organizations for the current user
func (s *organizationService) SearchSearchableOrganizations(ctx context.Context, userID string, query string, limit int) (*types.ListSearchableOrganizationsResponse, error) {
if limit <= 0 {
limit = 20
}
orgs, err := s.orgRepo.ListSearchable(ctx, query, limit)
if err != nil {
return nil, err
}
memberCounts := make(map[string]int64)
shareCounts := make(map[string]int64)
memberOrgIDs := make(map[string]bool)
for _, org := range orgs {
if mc, err := s.orgRepo.CountMembers(ctx, org.ID); err == nil {
memberCounts[org.ID] = mc
}
shares, _ := s.shareRepo.ListByOrganization(ctx, org.ID)
shareCounts[org.ID] = int64(len(shares))
_, err := s.orgRepo.GetMember(ctx, org.ID, userID)
memberOrgIDs[org.ID] = (err == nil)
}
items := make([]types.SearchableOrganizationItem, 0, len(orgs))
for _, org := range orgs {
items = append(items, types.SearchableOrganizationItem{
ID: org.ID,
Name: org.Name,
Description: org.Description,
Avatar: org.Avatar,
MemberCount: int(memberCounts[org.ID]),
ShareCount: int(shareCounts[org.ID]),
IsAlreadyMember: memberOrgIDs[org.ID],
RequireApproval: org.RequireApproval,
})
}
return &types.ListSearchableOrganizationsResponse{
Organizations: items,
Total: int64(len(items)),
}, nil
}
// JoinByOrganizationID joins a searchable organization by ID (no invite code required)
func (s *organizationService) JoinByOrganizationID(ctx context.Context, orgID string, userID string, tenantID uint64, message string, requestedRole types.OrgMemberRole) (*types.Organization, error) {
org, err := s.orgRepo.GetByID(ctx, orgID)
if err != nil {
if errors.Is(err, repository.ErrOrganizationNotFound) {
return nil, ErrOrgNotFound
}
return nil, err
}
if !org.Searchable {
return nil, ErrOrgPermissionDenied // or a dedicated "org not discoverable" error
}
_, err = s.orgRepo.GetMember(ctx, orgID, userID)
if err == nil {
return org, nil // already member
}
// Validate requested role if provided
if requestedRole != "" && !requestedRole.IsValid() {
return nil, ErrInvalidRole
}
// Default to viewer if not specified
if requestedRole == "" {
requestedRole = types.OrgRoleViewer
}
if org.RequireApproval {
_, err = s.SubmitJoinRequest(ctx, orgID, userID, tenantID, message, requestedRole)
if err != nil {
return nil, err
}
return org, nil
}
// Direct join using invite code flow logic (add member)
_, err = s.JoinByInviteCode(ctx, org.InviteCode, userID, tenantID)
if err != nil {
return nil, err
}
return org, nil
}
// DeleteOrganization deletes an organization
func (s *organizationService) DeleteOrganization(ctx context.Context, id string, userID string) error {
org, err := s.orgRepo.GetByID(ctx, id)