This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_support_reddit.go
194 lines (158 loc) · 5.71 KB
/
proof_support_reddit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package externals
import (
"net/url"
"regexp"
"strings"
libkb "github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
//=============================================================================
// Reddit
//
type RedditChecker struct {
proof libkb.RemoteProofChainLink
}
var _ libkb.ProofChecker = (*RedditChecker)(nil)
const (
RedditPrefix = "https://www.reddit.com"
RedditSub = RedditPrefix + "/r/keybaseproofs"
)
func NewRedditChecker(p libkb.RemoteProofChainLink) (*RedditChecker, libkb.ProofError) {
return &RedditChecker{p}, nil
}
func (rc *RedditChecker) GetTorError() libkb.ProofError { return nil }
func (rc *RedditChecker) CheckStatus(ctx libkb.ProofContext, h libkb.SigHint, _ libkb.ProofCheckerMode, pvlU libkb.PvlUnparsed) libkb.ProofError {
return CheckProofPvl(ctx, keybase1.ProofType_REDDIT, rc.proof, h, pvlU)
}
//
//=============================================================================
func urlReencode(s string) string {
// Reddit interprets plusses in the query string differently depending
// on whether the user is using the old or new (2018) design, and
// on whether it's the title or body of the post.
// old, *, '+' -> ' '
// old, *, '%2B' -> '+'
// new, title, '+' -> ' '
// new, title, '%2B' -> ' '
// new, body, '+' -> '+'
// new, body, '%2B' -> '+'
// Examples:
// https://www.reddit.com/r/test/submit?text=content+fmt%0Aplus+x%2By%20z&title=title+fmt%0Aplus+x%2By%20z
// old: https://www.reddit.com/r/test/comments/8eee0e/title_fmt_plus_xy_z/
// new: https://www.reddit.com/r/test/comments/8eelwf/title_fmt_plus_x_y_z/
// Replace '(', ")" and "'" so that URL-detection works in Linux
// Padding is not needed now, but might be in the future depending on
// changes we make
rxx := regexp.MustCompile(`[()'+]`)
s = rxx.ReplaceAllStringFunc(s, func(r string) string {
switch r {
case `(`:
return `%28`
case `)`:
return `%29`
case `'`:
return `%27`
case `+`:
// HTTPArgs.EncodeToString has encoded ' ' -> '+'
// we recode '+' -> '%20'.
return `%20`
default:
return r
}
})
return s
}
type RedditServiceType struct{ libkb.BaseServiceType }
func (t RedditServiceType) AllStringKeys() []string { return t.BaseAllStringKeys(t) }
var redditUsernameRegexp = regexp.MustCompile(`^(?i:[a-z0-9_-]{3,20})$`)
func (t RedditServiceType) NormalizeUsername(s string) (string, error) {
if !redditUsernameRegexp.MatchString(s) {
return "", libkb.NewBadUsernameError(s)
}
return strings.ToLower(s), nil
}
func (t RedditServiceType) NormalizeRemoteName(ctx libkb.ProofContext, s string) (ret string, err error) {
return t.NormalizeUsername(s)
}
func (t RedditServiceType) GetTypeName() string { return "reddit" }
func (t RedditServiceType) GetPrompt() string { return "Your username on Reddit" }
func (t RedditServiceType) ToServiceJSON(un string) *jsonw.Wrapper {
return t.BaseToServiceJSON(t, un)
}
func (t RedditServiceType) PostInstructions(un string) *libkb.Markup {
return libkb.FmtMarkup(`Please click on the following link to post to Reddit:`)
}
func (t RedditServiceType) FormatProofText(ctx libkb.ProofContext, ppr *libkb.PostProofRes) (res string, err error) {
var title string
if title, err = ppr.Metadata.AtKey("title").GetString(); err != nil {
return
}
urlPre := libkb.HTTPArgs{"title": libkb.S{Val: title}, "text": libkb.S{Val: ppr.Text}}.EncodeToString()
q := urlReencode(urlPre)
chooseHost := func(untrustedHint, trustedDefault string) string {
allowedHosts := []string{
"reddit.com",
"www.reddit.com",
// 2017-04-18: The new reddit mobile site doesn't respect the post-pre-populate query parameters.
// The i.reddit.com site may be better.
"i.reddit.com",
"old.reddit.com",
}
if untrustedHint == "" {
return trustedDefault
}
for _, h := range allowedHosts {
if untrustedHint == h {
return h
}
}
return trustedDefault
}
var host string
if ctx.GetAppType() == libkb.MobileAppType {
hostHint, err := ppr.Metadata.AtKey("mobile_host").GetString()
if err != nil {
hostHint = ""
}
host = chooseHost(hostHint, "old.reddit.com")
} else {
// Note that GetAppType() often returns libkb.NoAppType. Don't assume that we get
// libkb.DesktopAppType in the non-mobile case.
// Use the old reddit design until this bug is fixed:
// https://www.reddit.com/r/redesign/comments/8evfap/bug_post_contents_gets_dropped_when_using/
hostHint, err := ppr.Metadata.AtKey("other_host").GetString()
if err != nil {
hostHint = ""
}
host = chooseHost(hostHint, "old.reddit.com")
}
u := url.URL{
Scheme: "https",
Host: host,
Path: "/r/KeybaseProofs/submit",
RawQuery: q,
}
res = u.String()
return
}
func (t RedditServiceType) DisplayName(un string) string { return "Reddit" }
func (t RedditServiceType) RecheckProofPosting(tryNumber int, status keybase1.ProofStatus, _ string) (warning *libkb.Markup, err error) {
warning, err = t.BaseRecheckProofPosting(tryNumber, status)
return
}
func (t RedditServiceType) GetProofType() string { return t.BaseGetProofType(t) }
func (t RedditServiceType) CheckProofText(text string, id keybase1.SigID, sig string) (err error) {
// Anything is fine. We might get rid of the body later.
return nil
}
func (t RedditServiceType) MakeProofChecker(l libkb.RemoteProofChainLink) libkb.ProofChecker {
return &RedditChecker{l}
}
//=============================================================================
func init() {
externalServices.Register(RedditServiceType{})
}
//=============================================================================