Archived
1
0

add option to fetch referenced uri

This commit is contained in:
Jeff Becker 2019-03-02 11:10:05 -05:00
parent 8ef4322fba
commit 0252dfa512
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
4 changed files with 51 additions and 23 deletions

View File

@ -1062,7 +1062,6 @@ func (self httpFrontend) handle_authed_api(wr http.ResponseWriter, r *http.Reque
func (self *httpFrontend) handle_api_find(wr http.ResponseWriter, r *http.Request) { func (self *httpFrontend) handle_api_find(wr http.ResponseWriter, r *http.Request) {
q := r.URL.Query() q := r.URL.Query()
h := q.Get("hash") h := q.Get("hash")
if len(h) > 0 {
msgid := q.Get("id") msgid := q.Get("id")
if len(h) > 0 { if len(h) > 0 {
e, err := self.daemon.database.GetMessageIDByHash(h) e, err := self.daemon.database.GetMessageIDByHash(h)
@ -1070,20 +1069,24 @@ func (self *httpFrontend) handle_api_find(wr http.ResponseWriter, r *http.Reques
msgid = e.MessageID() msgid = e.MessageID()
} }
} }
if !ValidMessageID(msgid) {
msgid = ""
}
if len(msgid) > 0 { if len(msgid) > 0 {
// found it (probaly) self.daemon.store.GetMessage(msgid, func(nntp NNTPMessage) {
model := self.daemon.database.GetPostModel(self.prefix, msgid) if nntp == nil {
if model == nil {
// no model
wr.WriteHeader(404) wr.WriteHeader(404)
} else { return
}
model := PostModelFromMessage(self.prefix, nntp)
// we found it // we found it
wr.Header().Add("Content-Type", "text/json; encoding=UTF-8") wr.Header().Add("Content-Type", "text/json; encoding=UTF-8")
json.NewEncoder(wr).Encode([]PostModel{model}) json.NewEncoder(wr).Encode([]PostModel{model})
} })
return return
} }
}
s := q.Get("text") s := q.Get("text")
g := q.Get("group") g := q.Get("group")

View File

@ -295,6 +295,7 @@ type post struct {
Type string Type string
nntp_id int nntp_id int
FrontendPublicKey string FrontendPublicKey string
ReferencedURI string
} }
func (p *post) IsCtl() bool { func (p *post) IsCtl() bool {
@ -411,7 +412,7 @@ func (self *attachment) Filename() string {
return self.Name return self.Name
} }
func PostModelFromMessage(parent, prefix string, nntp NNTPMessage) PostModel { func PostModelFromMessage(prefix string, nntp NNTPMessage) PostModel {
p := new(post) p := new(post)
p.PostName = nntp.Name() p.PostName = nntp.Name()
p.PostSubject = nntp.Subject() p.PostSubject = nntp.Subject()
@ -422,10 +423,11 @@ func PostModelFromMessage(parent, prefix string, nntp NNTPMessage) PostModel {
p.Posted = nntp.Posted() p.Posted = nntp.Posted()
p.op = nntp.OP() p.op = nntp.OP()
p.prefix = prefix p.prefix = prefix
p.Parent = parent p.Parent = nntp.Reference()
p.addr = nntp.Addr() p.addr = nntp.Addr()
p.sage = nntp.Sage() p.sage = nntp.Sage()
p.Key = nntp.Pubkey() p.Key = nntp.Pubkey()
p.ReferencedURI = nntp.Headers().Get("X-Referenced-Uri", "")
p.FrontendPublicKey = nntp.FrontendPubkey() p.FrontendPublicKey = nntp.FrontendPubkey()
for _, att := range nntp.Attachments() { for _, att := range nntp.Attachments() {
p.Files = append(p.Files, att.ToModel(prefix)) p.Files = append(p.Files, att.ToModel(prefix))
@ -627,6 +629,7 @@ func (self *post) Truncate() PostModel {
// TODO: copy? // TODO: copy?
Files: self.Files, Files: self.Files,
FrontendPublicKey: self.FrontendPublicKey, FrontendPublicKey: self.FrontendPublicKey,
ReferencedURI: self.ReferencedURI,
} }
} }

View File

@ -42,5 +42,27 @@
</span> </span>
<br /><br /> <br /><br />
<span id="post_body_{{post.PostHash}}" class="message_span">{{{post.RenderBody}}}</span> <span id="post_body_{{post.PostHash}}" class="message_span">{{{post.RenderBody}}}</span>
{{#post.OP}}
<script type="text/javascript">
var j = fetch("/api/find?id={{post.MessageID}}").then(function(resp) {
return resp.json();
}).catch(function(resp) {
return [];
});
if (j.legnth) {
var e = document.getElementById("post_body_{{post.PostHash}}");
if(e) {
var link = document.createElement("a");
var u = new URL(j[0].ReferencedURI);
if(u.protocol === "https" || u.protocol === "http") {
link.href = u.toString();
link.innerText = "[Link]";
e.appendChild(txt);
}
}
}
</script>
{{/post.OP}}
<br style="clear: both;" /> <br style="clear: both;" />
</div> </div>