mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update common utils (#1908)
This commit is contained in:
185
server/rss.go
185
server/rss.go
@@ -1,185 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
"github.com/labstack/echo/v4"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
"github.com/usememos/memos/common"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/yuin/goldmark"
|
||||
)
|
||||
|
||||
func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||
g.GET("/explore/rss.xml", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
||||
}
|
||||
|
||||
normalStatus := store.Normal
|
||||
memoFind := store.FindMemo{
|
||||
RowStatus: &normalStatus,
|
||||
VisibilityList: []store.Visibility{store.Public},
|
||||
}
|
||||
memoList, err := s.Store.ListMemos(ctx, &memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
||||
}
|
||||
|
||||
baseURL := c.Scheme() + "://" + c.Request().Host
|
||||
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
||||
}
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
||||
return c.String(http.StatusOK, rss)
|
||||
})
|
||||
|
||||
g.GET("/u/:id/rss.xml", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err)
|
||||
}
|
||||
|
||||
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
||||
}
|
||||
|
||||
normalStatus := store.Normal
|
||||
memoFind := store.FindMemo{
|
||||
CreatorID: &id,
|
||||
RowStatus: &normalStatus,
|
||||
VisibilityList: []store.Visibility{store.Public},
|
||||
}
|
||||
memoList, err := s.Store.ListMemos(ctx, &memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
||||
}
|
||||
|
||||
baseURL := c.Scheme() + "://" + c.Request().Host
|
||||
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
||||
}
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
||||
return c.String(http.StatusOK, rss)
|
||||
})
|
||||
}
|
||||
|
||||
const MaxRSSItemCount = 100
|
||||
const MaxRSSItemTitleLength = 100
|
||||
|
||||
func (s *Server) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string, profile *apiv1.CustomizedProfile) (string, error) {
|
||||
feed := &feeds.Feed{
|
||||
Title: profile.Name,
|
||||
Link: &feeds.Link{Href: baseURL},
|
||||
Description: profile.Description,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
var itemCountLimit = common.Min(len(memoList), MaxRSSItemCount)
|
||||
feed.Items = make([]*feeds.Item, itemCountLimit)
|
||||
for i := 0; i < itemCountLimit; i++ {
|
||||
memo := memoList[i]
|
||||
feed.Items[i] = &feeds.Item{
|
||||
Title: getRSSItemTitle(memo.Content),
|
||||
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
|
||||
Description: getRSSItemDescription(memo.Content),
|
||||
Created: time.Unix(memo.CreatedTs, 0),
|
||||
Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + strconv.Itoa(memo.ID) + "/image"},
|
||||
}
|
||||
if len(memo.ResourceIDList) > 0 {
|
||||
resourceID := memo.ResourceIDList[0]
|
||||
resource, err := s.Store.GetResource(ctx, &store.FindResource{
|
||||
ID: &resourceID,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
enclosure := feeds.Enclosure{}
|
||||
if resource.ExternalLink != "" {
|
||||
enclosure.Url = resource.ExternalLink
|
||||
} else {
|
||||
enclosure.Url = baseURL + "/o/r/" + strconv.Itoa(resource.ID) + "/" + resource.PublicID + "/" + resource.Filename
|
||||
}
|
||||
enclosure.Length = strconv.Itoa(int(resource.Size))
|
||||
enclosure.Type = resource.Type
|
||||
feed.Items[i].Enclosure = &enclosure
|
||||
}
|
||||
}
|
||||
|
||||
rss, err := feed.ToRss()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return rss, nil
|
||||
}
|
||||
|
||||
func (s *Server) getSystemCustomizedProfile(ctx context.Context) (*apiv1.CustomizedProfile, error) {
|
||||
systemSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
|
||||
Name: apiv1.SystemSettingCustomizedProfileName.String(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
customizedProfile := &apiv1.CustomizedProfile{
|
||||
Name: "memos",
|
||||
LogoURL: "",
|
||||
Description: "",
|
||||
Locale: "en",
|
||||
Appearance: "system",
|
||||
ExternalURL: "",
|
||||
}
|
||||
if systemSetting != nil {
|
||||
if err := json.Unmarshal([]byte(systemSetting.Value), customizedProfile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return customizedProfile, nil
|
||||
}
|
||||
|
||||
func getRSSItemTitle(content string) string {
|
||||
var title string
|
||||
if isTitleDefined(content) {
|
||||
title = strings.Split(content, "\n")[0][2:]
|
||||
} else {
|
||||
title = strings.Split(content, "\n")[0]
|
||||
var titleLengthLimit = common.Min(len(title), MaxRSSItemTitleLength)
|
||||
if titleLengthLimit < len(title) {
|
||||
title = title[:titleLengthLimit] + "..."
|
||||
}
|
||||
}
|
||||
return title
|
||||
}
|
||||
|
||||
func getRSSItemDescription(content string) string {
|
||||
var description string
|
||||
if isTitleDefined(content) {
|
||||
var firstLineEnd = strings.Index(content, "\n")
|
||||
description = strings.Trim(content[firstLineEnd+1:], " ")
|
||||
} else {
|
||||
description = content
|
||||
}
|
||||
|
||||
// TODO: use our `./plugin/gomark` parser to handle markdown-like content.
|
||||
var buf bytes.Buffer
|
||||
if err := goldmark.Convert([]byte(description), &buf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func isTitleDefined(content string) bool {
|
||||
return strings.HasPrefix(content, "# ")
|
||||
}
|
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
"github.com/usememos/memos/common"
|
||||
"github.com/usememos/memos/common/util"
|
||||
"github.com/usememos/memos/plugin/telegram"
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
@@ -86,8 +86,6 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
|
||||
s.Secret = secret
|
||||
|
||||
rootGroup := e.Group("")
|
||||
s.registerRSSRoutes(rootGroup)
|
||||
|
||||
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store)
|
||||
apiV1Service.Register(rootGroup)
|
||||
|
||||
@@ -129,7 +127,7 @@ func (s *Server) getSystemServerID(ctx context.Context) (string, error) {
|
||||
serverIDSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
|
||||
Name: apiv1.SystemSettingServerIDName.String(),
|
||||
})
|
||||
if err != nil && common.ErrorCode(err) != common.NotFound {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if serverIDSetting == nil || serverIDSetting.Value == "" {
|
||||
@@ -148,7 +146,7 @@ func (s *Server) getSystemSecretSessionName(ctx context.Context) (string, error)
|
||||
secretSessionNameValue, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
|
||||
Name: apiv1.SystemSettingSecretSessionName.String(),
|
||||
})
|
||||
if err != nil && common.ErrorCode(err) != common.NotFound {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if secretSessionNameValue == nil || secretSessionNameValue.Value == "" {
|
||||
@@ -190,5 +188,5 @@ func defaultGetRequestSkipper(c echo.Context) bool {
|
||||
|
||||
func defaultAPIRequestSkipper(c echo.Context) bool {
|
||||
path := c.Path()
|
||||
return common.HasPrefixes(path, "/api", "/api/v1")
|
||||
return util.HasPrefixes(path, "/api", "/api/v1")
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
"github.com/usememos/memos/common"
|
||||
"github.com/usememos/memos/common/util"
|
||||
"github.com/usememos/memos/plugin/telegram"
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
@@ -94,7 +94,7 @@ func (t *telegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
|
||||
Type: mime,
|
||||
Size: int64(len(blob)),
|
||||
Blob: blob,
|
||||
PublicID: common.GenUUID(),
|
||||
PublicID: util.GenUUID(),
|
||||
})
|
||||
if err != nil {
|
||||
_, err := bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("failed to CreateResource: %s", err), nil)
|
||||
|
Reference in New Issue
Block a user