From: Sergey Matveev Date: Mon, 13 Mar 2023 10:48:11 +0000 (+0300) Subject: Deal with channels first X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;ds=sidebyside;h=75c872effcb2075ef77864dca13b90c934c5fd2e;p=mmc.git Deal with channels first Because dealing with users opens direct channels first. --- diff --git a/cmd/mmc/main.go b/cmd/mmc/main.go index f3faa86..c383cff 100644 --- a/cmd/mmc/main.go +++ b/cmd/mmc/main.go @@ -114,6 +114,105 @@ func main() { var updateQueue []string LastSent := time.Now() + Chans := make(map[string]*model.Channel) + time.Sleep(mmc.SleepTime) + page, resp, err := c.GetChannelsForTeamForUser(Team.Id, me.Id, false, "") + if err != nil { + if DebugFd != nil { + spew.Fdump(DebugFd, resp) + } + log.Fatalln(err) + } + if DebugFd != nil { + spew.Fdump(DebugFd, page) + } + for _, ch := range page { + if ch.Type == "D" { + continue + } + Chans[ch.Name] = ch + pth := path.Join("chans", strings.ReplaceAll(ch.Name, ".", "_")) + updateQueue = append(updateQueue, pth, ch.Id) + os.MkdirAll(pth, 0777) + rewriteIfChanged(path.Join(pth, "id"), ch.Id+"\n") + rewriteIfChanged(path.Join(pth, "info"), fmt.Sprintf( + "%s\n%s\n%s\n", + ch.DisplayName, + ch.Header, + ch.Purpose, + )) + if _, err := os.Stat(path.Join(pth, mmc.OutRec)); err != nil && + errors.Is(err, fs.ErrNotExist) { + if _, err = os.OpenFile( + path.Join(pth, mmc.OutRec), os.O_WRONLY|os.O_CREATE, 0o666, + ); err != nil { + log.Fatalln(err) + } + } + + usersPth := path.Join(pth, "users") + os.Remove(usersPth) + if err := syscall.Mkfifo(usersPth, 0666); err != nil { + log.Fatalln(err) + } + go func(ch *model.Channel) { + for { + time.Sleep(mmc.SleepTime) + fd, err := os.OpenFile( + usersPth, os.O_WRONLY|os.O_APPEND, os.FileMode(0666), + ) + if err != nil { + log.Println("OpenFile:", usersPth, err) + continue + } + for n := 0; ; n++ { + users, resp, err := c.GetUsersInChannel(ch.Id, n, mmc.PerPage, "") + if err != nil { + if DebugFd != nil { + spew.Fdump(DebugFd, resp) + } + log.Println("GetUsersInChannel:", err) + fd.Close() + continue + } + if DebugFd != nil { + spew.Fdump(DebugFd, users) + } + for _, u := range users { + fmt.Fprintf(fd, "%s\n", u.Username) + } + if len(users) < mmc.PerPage { + break + } + } + fd.Close() + } + }(ch) + + pth = path.Join(pth, "in") + os.Remove(pth) + if err := syscall.Mkfifo(pth, 0666); err != nil { + log.Fatalln(err) + } + go func(ch *model.Channel) { + for { + fd, err := os.OpenFile(pth, os.O_RDONLY, os.FileMode(0666)) + if err != nil { + continue + } + data, err := io.ReadAll(fd) + fd.Close() + if err != nil { + continue + } + if _, err = makePost(c, ch.Id, string(data)); err != nil { + log.Println("makePost:", err) + } + LastSent = time.Now() + } + }(ch) + } + Users, err := mmc.GetUsers(c, DebugFd) if err != nil { log.Fatalln(err) @@ -273,105 +372,6 @@ func main() { }() } - Chans := make(map[string]*model.Channel) - time.Sleep(mmc.SleepTime) - page, resp, err := c.GetChannelsForTeamForUser(Team.Id, me.Id, false, "") - if err != nil { - if DebugFd != nil { - spew.Fdump(DebugFd, resp) - } - log.Fatalln(err) - } - if DebugFd != nil { - spew.Fdump(DebugFd, page) - } - for _, ch := range page { - if ch.Type == "D" { - continue - } - Chans[ch.Name] = ch - pth := path.Join("chans", strings.ReplaceAll(ch.Name, ".", "_")) - updateQueue = append(updateQueue, pth, ch.Id) - os.MkdirAll(pth, 0777) - rewriteIfChanged(path.Join(pth, "id"), ch.Id+"\n") - rewriteIfChanged(path.Join(pth, "info"), fmt.Sprintf( - "%s\n%s\n%s\n", - ch.DisplayName, - ch.Header, - ch.Purpose, - )) - if _, err := os.Stat(path.Join(pth, mmc.OutRec)); err != nil && - errors.Is(err, fs.ErrNotExist) { - if _, err = os.OpenFile( - path.Join(pth, mmc.OutRec), os.O_WRONLY|os.O_CREATE, 0o666, - ); err != nil { - log.Fatalln(err) - } - } - - usersPth := path.Join(pth, "users") - os.Remove(usersPth) - if err := syscall.Mkfifo(usersPth, 0666); err != nil { - log.Fatalln(err) - } - go func(ch *model.Channel) { - for { - time.Sleep(mmc.SleepTime) - fd, err := os.OpenFile( - usersPth, os.O_WRONLY|os.O_APPEND, os.FileMode(0666), - ) - if err != nil { - log.Println("OpenFile:", usersPth, err) - continue - } - for n := 0; ; n++ { - users, resp, err := c.GetUsersInChannel(ch.Id, n, mmc.PerPage, "") - if err != nil { - if DebugFd != nil { - spew.Fdump(DebugFd, resp) - } - log.Println("GetUsersInChannel:", err) - fd.Close() - continue - } - if DebugFd != nil { - spew.Fdump(DebugFd, users) - } - for _, u := range users { - fmt.Fprintf(fd, "%s\n", u.Username) - } - if len(users) < mmc.PerPage { - break - } - } - fd.Close() - } - }(ch) - - pth = path.Join(pth, "in") - os.Remove(pth) - if err := syscall.Mkfifo(pth, 0666); err != nil { - log.Fatalln(err) - } - go func(ch *model.Channel) { - for { - fd, err := os.OpenFile(pth, os.O_RDONLY, os.FileMode(0666)) - if err != nil { - continue - } - data, err := io.ReadAll(fd) - fd.Close() - if err != nil { - continue - } - if _, err = makePost(c, ch.Id, string(data)); err != nil { - log.Println("makePost:", err) - } - LastSent = time.Now() - } - }(ch) - } - log.Println("syncing", len(updateQueue)/2, "rooms") for len(updateQueue) > 0 { err := updatePosts(c, Users, updateQueue[0], updateQueue[1])