]> Sergey Matveev's repositories - mmc.git/commitdiff
DRY GetUser
authorSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 08:42:35 +0000 (11:42 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 12:42:11 +0000 (15:42 +0300)
cmd/mmc/main.go
cmd/mmc/post.go
cmd/scrollback/main.go
common.go

index c74d9f8f70b8320e6c429f53a4d5012ac8a6577d..20c8da45cc6426f3216834888d1ab655f049686d 100644 (file)
@@ -39,10 +39,7 @@ import (
        "go.stargrave.org/mmc"
 )
 
        "go.stargrave.org/mmc"
 )
 
-const (
-       PerPage = 100
-       CmdFile = "/FILE "
-)
+const CmdFile = "/FILE "
 
 var (
        Newwin    = flag.String("newwin", "cmd/newwin", "Path to newwin command")
 
 var (
        Newwin    = flag.String("newwin", "cmd/newwin", "Path to newwin command")
@@ -112,29 +109,13 @@ func main() {
        }
        Team := teams[0]
 
        }
        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()
        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)
        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
                                }
                                        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)
                                        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)
                                        }
                                        for _, u := range users {
                                                fmt.Fprintf(fd, "%s\n", u.Username)
                                        }
-                                       if len(users) < PerPage {
+                                       if len(users) < mmc.PerPage {
                                                break
                                        }
                                }
                                                break
                                        }
                                }
index 4e70932962a82a3b0f338b9e36f835bca6e47a2a..0adcd2227f24fbcea718b859bf9379e74cabedce 100644 (file)
@@ -97,13 +97,13 @@ func updatePosts(c *model.Client4, users map[string]*model.User, where, chId str
        if lastPostId == "" {
                return nil
        }
        if lastPostId == "" {
                return nil
        }
-       for pageNum := 0; ; pageNum++ {
+       for n := 0; ; n++ {
                time.Sleep(mmc.SleepTime)
                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 {
                )
                if DebugFd != nil {
-                       spew.Fdump(DebugFd, postList, resp)
+                       spew.Fdump(DebugFd, list, resp)
                }
                if resp.StatusCode == http.StatusNotFound {
                        break
                }
                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
                        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
                }
                        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
                }
        }
                        break
                }
        }
index 638f38b34cecfa40088d076b45b5bc2229011752..95f735e1676057518d1f7f27c03d02d637543008 100644 (file)
@@ -20,7 +20,6 @@ import (
        "flag"
        "log"
        "os"
        "flag"
        "log"
        "os"
-       "time"
 
        "github.com/mattermost/mattermost-server/v6/model"
        "go.cypherpunks.ru/recfile"
 
        "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)
        }
        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)
        }
 
        list, _, err := c.GetPostsBefore(chId, postId, 0, 200, "", false)
index 014d8f9c438849336fa7f042035d6d21b60947c7..04126017888120498d5dabc236bb75cdd8236e56 100644 (file)
--- a/common.go
+++ b/common.go
 package mmc
 
 import (
 package mmc
 
 import (
+       "os"
        "strings"
        "time"
 
        "strings"
        "time"
 
+       "github.com/davecgh/go-spew/spew"
        "github.com/mattermost/mattermost-server/v6/model"
        "go.cypherpunks.ru/recfile"
 )
 
 const (
        "github.com/mattermost/mattermost-server/v6/model"
        "go.cypherpunks.ru/recfile"
 )
 
 const (
+       PerPage    = 100
        OutRec     = "out.rec"
        OutRecLock = "out.rec.lock"
        Last       = "last"
        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
 }
        _, 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
+}