|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "mokapi/providers/mail" |
| 6 | + "mokapi/runtime/search" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type mailSearchIndexData struct { |
| 12 | + Type string `json:"type"` |
| 13 | + Discriminator string `json:"discriminator"` |
| 14 | + Api string `json:"api"` |
| 15 | + Name string `json:"name"` |
| 16 | + Version string `json:"version"` |
| 17 | + Description string `json:"description"` |
| 18 | + Contact *mail.Contact `json:"contact"` |
| 19 | + Servers []mailSearchIndexServer `json:"servers"` |
| 20 | +} |
| 21 | + |
| 22 | +type mailSearchIndexServer struct { |
| 23 | + mail.Server |
| 24 | + Name string `json:"name"` |
| 25 | +} |
| 26 | + |
| 27 | +type mailSearchIndexMailbox struct { |
| 28 | + Type string `json:"type"` |
| 29 | + Discriminator string `json:"discriminator"` |
| 30 | + Api string `json:"api"` |
| 31 | + Name string `json:"name"` |
| 32 | + Username string `json:"username"` |
| 33 | + Password string `json:"password"` |
| 34 | + Description string `json:"description"` |
| 35 | + Folders []mailSearchIndexFolder `json:"folders"` |
| 36 | +} |
| 37 | + |
| 38 | +type mailSearchIndexFolder struct { |
| 39 | + Name string `json:"name"` |
| 40 | + Flags []string `json:"flags"` |
| 41 | +} |
| 42 | + |
| 43 | +func (s *MailStore) addToIndex(cfg *mail.Config) { |
| 44 | + if cfg == nil || cfg.Info.Name == "" { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + c := mailSearchIndexData{ |
| 49 | + Type: "mail", |
| 50 | + Discriminator: "mail", |
| 51 | + Api: cfg.Info.Name, |
| 52 | + Name: cfg.Info.Name, |
| 53 | + Version: cfg.Info.Version, |
| 54 | + Description: cfg.Info.Description, |
| 55 | + Contact: cfg.Info.Contact, |
| 56 | + } |
| 57 | + for name, server := range cfg.Servers { |
| 58 | + if server == nil { |
| 59 | + continue |
| 60 | + } |
| 61 | + c.Servers = append(c.Servers, mailSearchIndexServer{ |
| 62 | + Name: name, |
| 63 | + Server: *server, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + add(s.index, fmt.Sprintf("mail_%s", cfg.Info.Name), c) |
| 68 | + |
| 69 | + for name, mb := range cfg.Mailboxes { |
| 70 | + mbi := mailSearchIndexMailbox{ |
| 71 | + Type: "mail", |
| 72 | + Discriminator: "mail_mailbox", |
| 73 | + Api: cfg.Info.Name, |
| 74 | + Name: name, |
| 75 | + Username: mb.Username, |
| 76 | + Password: mb.Password, |
| 77 | + Description: mb.Description, |
| 78 | + } |
| 79 | + for n, f := range mb.Folders { |
| 80 | + mbi.Folders = append(mbi.Folders, getMailboxFolders(f, n)...) |
| 81 | + } |
| 82 | + add(s.index, fmt.Sprintf("mail_%s_%s", cfg.Info.Name, name), mbi) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func getMailSearchResult(fields map[string]string, discriminator []string) (search.ResultItem, error) { |
| 87 | + result := search.ResultItem{ |
| 88 | + Type: "MAIL", |
| 89 | + } |
| 90 | + |
| 91 | + if len(discriminator) == 1 { |
| 92 | + result.Title = fields["name"] |
| 93 | + result.Params = map[string]string{ |
| 94 | + "type": strings.ToLower(result.Type), |
| 95 | + "service": result.Title, |
| 96 | + } |
| 97 | + return result, nil |
| 98 | + } |
| 99 | + |
| 100 | + switch discriminator[1] { |
| 101 | + case "mailbox": |
| 102 | + result.Domain = fields["api"] |
| 103 | + result.Title = fields["name"] |
| 104 | + result.Params = map[string]string{ |
| 105 | + "type": strings.ToLower(result.Type), |
| 106 | + "service": result.Domain, |
| 107 | + "mailbox": fields["name"], |
| 108 | + } |
| 109 | + default: |
| 110 | + return result, fmt.Errorf("unsupported search result: %s", strings.Join(discriminator, "_")) |
| 111 | + } |
| 112 | + return result, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (s *MailStore) removeFromIndex(cfg *mail.Config) { |
| 116 | + _ = s.index.Delete(fmt.Sprintf("mail_%s", cfg.Info.Name)) |
| 117 | +} |
| 118 | + |
| 119 | +func getMailboxFolders(f *mail.FolderConfig, name string) []mailSearchIndexFolder { |
| 120 | + var result []mailSearchIndexFolder |
| 121 | + result = append(result, mailSearchIndexFolder{ |
| 122 | + Name: name, |
| 123 | + Flags: f.Flags, |
| 124 | + }) |
| 125 | + |
| 126 | + for childName, child := range f.Folders { |
| 127 | + children := getMailboxFolders(child, path.Join(name, childName)) |
| 128 | + result = append(result, children...) |
| 129 | + } |
| 130 | + |
| 131 | + return result |
| 132 | +} |
0 commit comments