Files
zeekayandHanzo Dev aa87442fdd Record upstream authorship centrally in NOTICE instead of in every file header
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the
Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in
NOTICE, alongside LICENSE. The header lint accepts any copyright line above the
SPDX line, so it no longer demands an upstream one.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:50:35 -07:00

90 lines
2.0 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package mention
import (
"context"
"github.com/hanzoai/git/models/organization"
user_model "github.com/hanzoai/git/models/user"
)
// Mention is the JSON structure returned by mention autocomplete endpoints.
type Mention struct {
Key string `json:"key"`
Value string `json:"value"`
Name string `json:"name"`
FullName string `json:"fullname"`
Avatar string `json:"avatar"`
}
// Collector builds a deduplicated list of Mention entries.
type Collector struct {
seen map[string]bool
Result []Mention
}
// NewCollector creates a new Collector.
func NewCollector() *Collector {
return &Collector{seen: make(map[string]bool)}
}
// AddUsers adds user mentions, skipping duplicates.
func (c *Collector) AddUsers(ctx context.Context, users []*user_model.User) {
for _, u := range users {
if !c.seen[u.Name] {
c.seen[u.Name] = true
c.Result = append(c.Result, Mention{
Key: u.Name + " " + u.FullName,
Value: u.Name,
Name: u.Name,
FullName: u.FullName,
Avatar: u.AvatarLink(ctx),
})
}
}
}
// AddMentionableTeams loads and adds team mentions for the given owner (if it's an org).
func (c *Collector) AddMentionableTeams(ctx context.Context, doer, owner *user_model.User) error {
if doer == nil || !owner.IsOrganization() {
return nil
}
org := organization.OrgFromUser(owner)
isAdmin := doer.IsAdmin
if !isAdmin {
var err error
isAdmin, err = org.IsOwnedBy(ctx, doer.ID)
if err != nil {
return err
}
}
var teams []*organization.Team
var err error
if isAdmin {
teams, err = org.LoadTeams(ctx)
} else {
teams, err = org.GetUserTeams(ctx, doer.ID)
}
if err != nil {
return err
}
for _, team := range teams {
key := owner.Name + "/" + team.Name
if !c.seen[key] {
c.seen[key] = true
c.Result = append(c.Result, Mention{
Key: key,
Value: key,
Name: key,
Avatar: owner.AvatarLink(ctx),
})
}
}
return nil
}