From: Sergey Matveev Date: Sun, 12 Mar 2023 08:42:35 +0000 (+0300) Subject: DRY GetUser X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=9d1285f5b74f62399a4d561577b749b29ba7a821;hp=85f306f5083661f4522c461f3a654dd9fe4dde21;p=mmc.git DRY GetUser --- diff --git a/cmd/mmc/main.go b/cmd/mmc/main.go index c74d9f8..20c8da4 100644 --- a/cmd/mmc/main.go +++ b/cmd/mmc/main.go @@ -39,10 +39,7 @@ import ( "go.stargrave.org/mmc" ) -const ( - PerPage = 100 - CmdFile = "/FILE " -) +const CmdFile = "/FILE " var ( Newwin = flag.String("newwin", "cmd/newwin", "Path to newwin command") @@ -112,29 +109,13 @@ func main() { } Team := teams[0] - Users := make(map[string]*model.User) - for pageNum := 0; ; pageNum++ { - time.Sleep(mmc.SleepTime) - page, resp, err := c.GetUsers(pageNum, PerPage, "") - if err != nil { - if DebugFd != nil { - spew.Fdump(DebugFd, resp) - } - log.Fatalln(err) - } - if DebugFd != nil { - spew.Fdump(DebugFd, page) - } - for _, u := range page { - Users[u.Id] = u - } - if len(page) < PerPage { - break - } - } - var updateQueue []string LastSent := time.Now() + + Users, err := mmc.GetUsers(c, DebugFd) + if err != nil { + log.Fatalln(err) + } for _, u := range Users { pth := path.Join("users", strings.ReplaceAll(u.Username, ".", "_")) os.MkdirAll(pth, 0777) @@ -278,8 +259,8 @@ func main() { log.Println("OpenFile:", usersPth, err) continue } - for pageNum := 0; ; pageNum++ { - users, resp, err := c.GetUsersInChannel(ch.Id, pageNum, PerPage, "") + for n := 0; ; n++ { + users, resp, err := c.GetUsersInChannel(ch.Id, n, mmc.PerPage, "") if err != nil { if DebugFd != nil { spew.Fdump(DebugFd, resp) @@ -294,7 +275,7 @@ func main() { for _, u := range users { fmt.Fprintf(fd, "%s\n", u.Username) } - if len(users) < PerPage { + if len(users) < mmc.PerPage { break } } diff --git a/cmd/mmc/post.go b/cmd/mmc/post.go index 4e70932..0adcd22 100644 --- a/cmd/mmc/post.go +++ b/cmd/mmc/post.go @@ -97,13 +97,13 @@ func updatePosts(c *model.Client4, users map[string]*model.User, where, chId str if lastPostId == "" { return nil } - for pageNum := 0; ; pageNum++ { + for n := 0; ; n++ { time.Sleep(mmc.SleepTime) - postList, resp, err := c.GetPostsAfter( - chId, lastPostId, pageNum, PerPage, "", false, + list, resp, err := c.GetPostsAfter( + chId, lastPostId, n, mmc.PerPage, "", false, ) if DebugFd != nil { - spew.Fdump(DebugFd, postList, resp) + spew.Fdump(DebugFd, list, resp) } if resp.StatusCode == http.StatusNotFound { break @@ -112,13 +112,13 @@ func updatePosts(c *model.Client4, users map[string]*model.User, where, chId str return err } var posts []mmc.Post - for _, post := range postList.ToSlice() { + for _, post := range list.ToSlice() { posts = append(posts, mmc.Post{P: post, E: model.WebsocketEventPosted}) } if err = writePosts(where, users, posts); err != nil { return err } - if len(posts) < PerPage { + if len(posts) < mmc.PerPage { break } } diff --git a/cmd/scrollback/main.go b/cmd/scrollback/main.go index 638f38b..95f735e 100644 --- a/cmd/scrollback/main.go +++ b/cmd/scrollback/main.go @@ -20,7 +20,6 @@ import ( "flag" "log" "os" - "time" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" @@ -43,20 +42,9 @@ func main() { } c := model.NewAPIv4Client("https://" + *entrypoint) c.Login(login, password) - - users := make(map[string]*model.User) - for pageNum := 0; ; pageNum++ { - time.Sleep(mmc.SleepTime) - page, _, err := c.GetUsers(pageNum, PerPage, "") - if err != nil { - log.Fatalln(err) - } - for _, u := range page { - users[u.Id] = u - } - if len(page) < PerPage { - break - } + users, err := mmc.GetUsers(c, nil) + if err != nil { + log.Fatalln(err) } list, _, err := c.GetPostsBefore(chId, postId, 0, 200, "", false) diff --git a/common.go b/common.go index 014d8f9..0412601 100644 --- a/common.go +++ b/common.go @@ -17,14 +17,17 @@ package mmc import ( + "os" "strings" "time" + "github.com/davecgh/go-spew/spew" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" ) const ( + PerPage = 100 OutRec = "out.rec" OutRecLock = "out.rec.lock" Last = "last" @@ -74,3 +77,27 @@ func PostToRec(w *recfile.Writer, users map[string]*model.User, post Post) error _, err = w.WriteFieldMultiline("Text", strings.Split(post.P.Message, "\n")) return err } + +func GetUsers(c *model.Client4, debugFd *os.File) (map[string]*model.User, error) { + users := make(map[string]*model.User) + for n := 0; ; n++ { + time.Sleep(SleepTime) + page, resp, err := c.GetUsers(n, PerPage, "") + if err != nil { + if debugFd != nil { + spew.Fdump(debugFd, resp) + } + return nil, err + } + if debugFd != nil { + spew.Fdump(debugFd, page) + } + for _, u := range page { + users[u.Id] = u + } + if len(page) < PerPage { + break + } + } + return users, nil +}