list.go
1 package subscriptions 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/google/uuid" 8 "github.com/mrusme/journalist/ent" 9 "github.com/mrusme/journalist/ent/item" 10 "github.com/mrusme/journalist/ent/subscription" 11 "github.com/mrusme/journalist/ent/user" 12 "github.com/mrusme/journalist/journalistd" 13 14 "context" 15 16 "github.com/gofiber/fiber/v2" 17 ) 18 19 func (h *handler) List(ctx *fiber.Ctx) error { 20 qat := ctx.Query("qat") 21 group := ctx.Query("group") 22 sessionUsername := ctx.Locals("username").(string) 23 sessionUserId := ctx.Locals("user_id").(string) 24 myId, err := uuid.Parse(sessionUserId) 25 if err != nil { 26 ctx.SendStatus(fiber.StatusInternalServerError) 27 return err 28 } 29 30 dbItemsTmp := h.entClient.Subscription. 31 Query() 32 33 if group == "" { 34 dbItemsTmp = dbItemsTmp.Where( 35 subscription.UserID(myId), 36 ) 37 } else { 38 dbItemsTmp = dbItemsTmp.Where( 39 subscription.UserID(myId), 40 subscription.Group(group), 41 ) 42 } 43 44 dbItems, err := dbItemsTmp. 45 QueryFeed(). 46 QueryItems(). 47 Where( 48 item.Not( 49 item.HasReadByUsersWith( 50 user.ID(myId), 51 ), 52 ), 53 ). 54 Order( 55 ent.Desc(item.FieldItemPublished), 56 ). 57 All(context.Background()) 58 if err != nil { 59 ctx.SendStatus(fiber.StatusInternalServerError) 60 return err 61 } 62 63 err = ctx.Render("views/subscriptions.list", fiber.Map{ 64 "Config": h.config, 65 "Token": fiber.Map{ 66 "Type": "qat", 67 "Token": qat, 68 }, 69 "Group": group, 70 71 "Title": h.tmplTitle(group), 72 "Link": h.tmplLink("qat", qat, group), 73 "Description": h.tmplDescription(sessionUsername, group), 74 "Generator": h.tmplGenerator(), 75 "Language": "en-us", 76 "LastBuildDate": time.Now(), 77 78 "Items": dbItems, 79 }) 80 ctx.Set("Content-type", "text/xml; charset=utf-8") 81 return err 82 } 83 84 func (h *handler) tmplTitle(group string) string { 85 var title string = "Subscriptions" 86 if group != "" { 87 title = group 88 } 89 90 return title 91 } 92 93 func (h *handler) tmplDescription( 94 username string, 95 group string, 96 ) string { 97 var description string = "" 98 99 if username[len(username)-1] == 's' { 100 description = fmt.Sprintf( 101 "%s' subscriptions", 102 username, 103 ) 104 } else { 105 description = fmt.Sprintf( 106 "%s's subscriptions", 107 username, 108 ) 109 } 110 111 if group != "" { 112 description = fmt.Sprintf( 113 "%s in %s", 114 description, 115 group, 116 ) 117 } 118 119 return description 120 } 121 122 func (h *handler) tmplLink( 123 tokenType string, 124 token string, 125 group string, 126 ) string { 127 return fmt.Sprintf( 128 "%s/subscriptions?group=%s&%s=%s", 129 h.config.Server.Endpoint.Web, 130 group, 131 tokenType, 132 token, 133 ) 134 } 135 136 func (h *handler) tmplGenerator() string { 137 return fmt.Sprintf( 138 "Journalist %s", 139 journalistd.Version(), 140 ) 141 }