1// Copyright 2022 Frédéric Guillot. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5package googlereader // import "miniflux.app/googlereader"
6
7import (
8	"fmt"
9	"net/http"
10
11	"miniflux.app/http/response"
12	"miniflux.app/logger"
13)
14
15type login struct {
16	SID  string `json:"SID,omitempty"`
17	LSID string `json:"LSID,omitempty"`
18	Auth string `json:"Auth,omitempty"`
19}
20
21func (l login) String() string {
22	return fmt.Sprintf("SID=%s\nLSID=%s\nAuth=%s\n", l.SID, l.LSID, l.Auth)
23}
24
25type userInfo struct {
26	UserID        string `json:"userId"`
27	UserName      string `json:"userName"`
28	UserProfileID string `json:"userProfileId"`
29	UserEmail     string `json:"userEmail"`
30}
31
32type subscription struct {
33	ID         string                 `json:"id"`
34	Title      string                 `json:"title"`
35	Categories []subscriptionCategory `json:"categories"`
36	URL        string                 `json:"url"`
37	HTMLURL    string                 `json:"htmlUrl"`
38	IconURL    string                 `json:"iconUrl"`
39}
40
41type quickAddResponse struct {
42	NumResults int64  `json:"numResults"`
43	Query      string `json:"query,omitempty"`
44	StreamID   string `json:"streamId,omitempty"`
45	StreamName string `json:"streamName,omitempty"`
46}
47
48type subscriptionCategory struct {
49	ID    string `json:"id"`
50	Label string `json:"label,omitempty"`
51	Type  string `json:"type,omitempty"`
52}
53type subscriptionsResponse struct {
54	Subscriptions []subscription `json:"subscriptions"`
55}
56
57type itemRef struct {
58	ID              string `json:"id"`
59	DirectStreamIDs string `json:"directStreamIds,omitempty"`
60	TimestampUsec   string `json:"timestampUsec,omitempty"`
61}
62
63type streamIDResponse struct {
64	ItemRefs []itemRef `json:"itemRefs"`
65}
66
67type tagsResponse struct {
68	Tags []subscriptionCategory `json:"tags"`
69}
70
71type streamContentItems struct {
72	Direction string            `json:"direction"`
73	ID        string            `json:"id"`
74	Title     string            `json:"title"`
75	Self      []contentHREF     `json:"self"`
76	Alternate []contentHREFType `json:"alternate"`
77	Updated   int64             `json:"updated"`
78	Items     []contentItem     `json:"items"`
79	Author    string            `json:"author"`
80}
81
82type contentItem struct {
83	ID            string                 `json:"id"`
84	Categories    []string               `json:"categories"`
85	Title         string                 `json:"title"`
86	CrawlTimeMsec string                 `json:"crawlTimeMsec"`
87	TimestampUsec string                 `json:"timestampUsec"`
88	Published     int64                  `json:"published"`
89	Updated       int64                  `json:"updated"`
90	Author        string                 `json:"author"`
91	Alternate     []contentHREFType      `json:"alternate"`
92	Summary       contentItemContent     `json:"summary"`
93	Content       contentItemContent     `json:"content"`
94	Origin        contentItemOrigin      `json:"origin"`
95	Enclosure     []contentItemEnclosure `json:"enclosure"`
96	Canonical     []contentHREF          `json:"canonical"`
97}
98
99type contentHREFType struct {
100	HREF string `json:"href"`
101	Type string `json:"type"`
102}
103
104type contentHREF struct {
105	HREF string `json:"href"`
106}
107
108type contentItemEnclosure struct {
109	URL  string `json:"url"`
110	Type string `json:"type"`
111}
112type contentItemContent struct {
113	Direction string `json:"direction"`
114	Content   string `json:"content"`
115}
116
117type contentItemOrigin struct {
118	StreamID string `json:"streamId"`
119	Title    string `json:"title"`
120	HTMLUrl  string `json:"htmlUrl"`
121}
122
123// Unauthorized sends a not authorized error to the client.
124func Unauthorized(w http.ResponseWriter, r *http.Request) {
125	logger.Error("[HTTP:Unauthorized] %s", r.URL)
126
127	builder := response.New(w, r)
128	builder.WithStatus(http.StatusUnauthorized)
129	builder.WithHeader("Content-Type", "text/plain")
130	builder.WithHeader("X-Reader-Google-Bad-Token", "true")
131	builder.WithBody("Unauthorized")
132	builder.Write()
133}
134
135// OK sends a ok response to the client.
136func OK(w http.ResponseWriter, r *http.Request) {
137	logger.Info("[HTTP:OK] %s", r.URL)
138
139	builder := response.New(w, r)
140	builder.WithStatus(http.StatusOK)
141	builder.WithHeader("Content-Type", "text/plain")
142	builder.WithBody("OK")
143	builder.Write()
144}
145