commit 2a4edef3d088dbb3d4653ffa1edf6920955c7f79 from: Oliver Lowe date: Sun Feb 04 11:29:45 2024 UTC lemmy: decode person, counts from API responses We want these so we can render the info easily in clients commit - 4c497c575cd1d39337adc22dfb9ee2b2d23042cf commit + 2a4edef3d088dbb3d4653ffa1edf6920955c7f79 blob - c8087b3a191946d99eda401cd408e29da0810beb blob + ee83ee254f6554490a600bccba8a6f1de979529b --- client.go +++ client.go @@ -61,8 +61,8 @@ func (c *Client) Communities(mode ListMode) ([]Communi params := map[string]string{ "type_": string(mode), - "limit": "30", // TODO go through pages - "sort": "New", // Required - Lemmy bug - even though we don't care about sorting. + "limit": "30", // TODO go through pages + "sort": "New", } if mode == ListSubscribed { if c.authToken == "" { @@ -94,35 +94,36 @@ func (c *Client) Communities(mode ListMode) ([]Communi return communities, nil } -func (c *Client) LookupCommunity(name string) (Community, error) { +func (c *Client) LookupCommunity(name string) (Community, Counts, error) { if !c.ready { if err := c.init(); err != nil { - return Community{}, err + return Community{}, Counts{}, err } } params := map[string]string{"name": name} resp, err := c.get("community", params) if err != nil { - return Community{}, err + return Community{}, Counts{}, err } defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { - return Community{}, ErrNotFound + return Community{}, Counts{}, ErrNotFound } else if resp.StatusCode != http.StatusOK { - return Community{}, fmt.Errorf("remote status %s: %w", resp.Status, decodeError(resp.Body)) + return Community{}, Counts{}, fmt.Errorf("remote status %s: %w", resp.Status, decodeError(resp.Body)) } type response struct { View struct { Community Community + Counts Counts } `json:"community_view"` } var cres response if err := json.NewDecoder(resp.Body).Decode(&cres); err != nil { - return Community{}, fmt.Errorf("decode community response: %w", err) + return Community{}, Counts{}, fmt.Errorf("decode community response: %w", err) } - return cres.View.Community, nil + return cres.View.Community, cres.View.Counts, nil } func (c *Client) Posts(community string, mode ListMode) ([]Post, error) { @@ -136,7 +137,7 @@ func (c *Client) Posts(community string, mode ListMode "community_name": community, "limit": "30", "type_": string(mode), - "sort": "New", // Required - Lemmy bug - even though we don't care about sorting. + "sort": "New", } resp, err := c.get("post/list", params) if err != nil { @@ -149,7 +150,8 @@ func (c *Client) Posts(community string, mode ListMode var jresponse struct { Posts []struct { - Post Post + Post Post + Creator Person } } if err := json.NewDecoder(resp.Body).Decode(&jresponse); err != nil { @@ -157,6 +159,7 @@ func (c *Client) Posts(community string, mode ListMode } var posts []Post for _, post := range jresponse.Posts { + post.Post.Creator = post.Creator posts = append(posts, post.Post) } return posts, nil @@ -194,7 +197,8 @@ func (c *Client) Comments(post int, mode ListMode) ([] params := map[string]string{ "post_id": strconv.Itoa(post), "type_": string(mode), - "sort": "New", // Required - Lemmy bug - even though we don't care about sorting. + "limit": "30", + "sort": "New", } resp, err := c.get("comment/list", params) if err != nil { @@ -208,6 +212,7 @@ func (c *Client) Comments(post int, mode ListMode) ([] var jresponse struct { Comments []struct { Comment Comment + Creator Person } } if err := json.NewDecoder(resp.Body).Decode(&jresponse); err != nil { @@ -215,6 +220,7 @@ func (c *Client) Comments(post int, mode ListMode) ([] } var comments []Comment for _, comment := range jresponse.Comments { + comment.Comment.Creator = comment.Creator comments = append(comments, comment.Comment) } return comments, nil @@ -327,3 +333,10 @@ func decodeError(r io.Reader) error { } return jerr } + +type Counts struct { + Posts int + Comments int + CommunityID int `json:"community_id"` + PostID int `json:"post_id"` +} blob - 023adbc6b2985e9e542b3452e9af34072dc71850 blob + 59d0d76dadbcfaa8839516f898f9138b05834559 --- decode.go +++ decode.go @@ -34,5 +34,6 @@ func decodePostResponse(r io.Reader) (Post, Person, Co if err := json.NewDecoder(r).Decode(&jresp); err != nil { return Post{}, Person{}, Community{}, fmt.Errorf("decode post: %w", err) } + jresp.PostView.Post.Creator = jresp.PostView.Creator return jresp.PostView.Post, jresp.PostView.Creator, jresp.PostView.Community, nil } blob - 3a1bbd61539a774aec3760301b0e3a5fbbc12703 blob + 0e77af598acb419e017ac16d27b15bf86ec8c861 --- fs/file.go +++ fs/file.go @@ -102,7 +102,7 @@ func (f *comFile) Close() error { } func (f *comFile) Stat() (fs.FileInfo, error) { - community, err := f.client.LookupCommunity(f.name) + community, _, err := f.client.LookupCommunity(f.name) if err != nil { return nil, &fs.PathError{"stat", f.name, err} } blob - 635e8a6a94b392f31739c7fe63046b5eb64392f0 blob + 798eb37e257ab1e4e90922d31f86ab832ec5688f --- fs/fs.go +++ fs/fs.go @@ -70,7 +70,7 @@ func (fsys *FS) Open(name string) (fs.File, error) { community, ok := fsys.Communities[elems[0]] if !ok { var err error - community, err = fsys.Client.LookupCommunity(elems[0]) + community, _, err = fsys.Client.LookupCommunity(elems[0]) if errors.Is(err, lemmy.ErrNotFound) { return nil, &fs.PathError{"open", name, fs.ErrNotExist} } else if err != nil {