diff --git a/model/contact/contacts.go b/model/contact/contacts.go index 9c27a92d358..b076912da10 100644 --- a/model/contact/contacts.go +++ b/model/contact/contacts.go @@ -6,6 +6,7 @@ import ( "encoding/json" "strings" + "github.com/cozy/cozy-stack/model/instance" "github.com/cozy/cozy-stack/pkg/consts" "github.com/cozy/cozy-stack/pkg/couchdb" "github.com/cozy/cozy-stack/pkg/couchdb/mango" @@ -256,7 +257,7 @@ func FindByEmail(db prefixer.Prefixer, email string) (*Contact, error) { } // CreateMyself creates the myself contact document from the instance settings. -func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, error) { +func CreateMyself(inst *instance.Instance, settings *couchdb.JSONDoc) (*Contact, error) { doc := New() doc.JSONDoc.M["me"] = true if name, ok := settings.M["public_name"]; ok { @@ -267,7 +268,10 @@ func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, er {"address": email, "primary": true}, } } - if err := couchdb.CreateDoc(db, doc); err != nil { + doc.JSONDoc.M["cozy"] = []map[string]interface{}{ + {"url": inst.PageURL("", nil), "primary": true}, + } + if err := couchdb.CreateDoc(inst, doc); err != nil { return nil, err } return doc, nil diff --git a/model/rag/chat.go b/model/rag/chat.go index b45ebaed9f2..9980c9f98f4 100644 --- a/model/rag/chat.go +++ b/model/rag/chat.go @@ -128,10 +128,12 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er return err } myself, _ := contact.GetMyself(inst) + relatives, _ := getRelatives(inst, myself) payload := map[string]interface{}{ - "messages": chat.Messages, - "myself": myself, - "stream": true, + "messages": chat.Messages, + "myself": myself, + "relatives": relatives, + "stream": true, } res, err := callRAGQuery(inst, payload) @@ -178,6 +180,40 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er return couchdb.UpdateDoc(inst, &chat) } +func getRelatives(inst *instance.Instance, myself *contact.Contact) ([]*contact.Contact, error) { + if myself == nil { + return nil, errors.New("no myself contact") + } + var ids []string + rels, _ := myself.Get("relationships").(map[string]interface{}) + if len(rels) == 0 { + return nil, nil + } + related, _ := rels["related"].(map[string]interface{}) + if len(related) == 0 { + return nil, nil + } + data, _ := related["data"].([]interface{}) + for _, item := range data { + item, _ := item.(map[string]interface{}) + if len(item) > 0 { + id, _ := item["_id"].(string) + if len(id) > 0 { + ids = append(ids, id) + } + } + } + if len(ids) == 0 { + return nil, nil + } + var relatives []*contact.Contact + req := &couchdb.AllDocsRequest{Keys: ids} + if err := couchdb.GetAllDocs(inst, consts.Contacts, req, &relatives); err != nil { + return nil, err + } + return relatives, nil +} + func callRAGQuery(inst *instance.Instance, payload map[string]interface{}) (*http.Response, error) { ragServer := inst.RAGServer() if ragServer.URL == "" {