posting on bluesky
This commit is contained in:
parent
a035d4cf9a
commit
2ded7743b9
3 changed files with 43 additions and 24 deletions
|
@ -2,13 +2,47 @@ import * as Cookie from "./Cookies.js";
|
|||
|
||||
export async function GetBlueskyDID(PDS, Handle) {
|
||||
let DPoP = await ClientDPoPPDS("GET", PDS + "/xrpc/com.atproto.identity.resolveHandle?handle=" + Handle);
|
||||
return fetch(PDS + "/xrpc/com.atproto.identity.resolveHandle?handle=" + Handle, { method: "GET", headers: {"Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}});
|
||||
let request = fetch(PDS + "/xrpc/com.atproto.identity.resolveHandle?handle=" + Handle, { method: "GET", headers: {"Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}});
|
||||
let body = await request.then((response) => response.json());
|
||||
let status = await request.then((response) => response.status);
|
||||
let header = await request.then((response) => response.headers.get("dpop-nonce"));
|
||||
if (status == 401) {
|
||||
await FixNonceMismatch(header);
|
||||
request = fetch(PDS + "/xrpc/com.atproto.identity.resolveHandle?handle=" + Handle, { method: "GET", headers: {"Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}});
|
||||
body = await request.then((response) => response.json());
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
export async function CreatePost(PDS, DID, Text) {
|
||||
let Json = {
|
||||
"$type": "app.bsky.feed.post",
|
||||
"text": Text,
|
||||
"createdAt": new Date(Date.now()).toISOString()
|
||||
}
|
||||
let RequestBody = {
|
||||
"repo": DID,
|
||||
"collection": "app.bsky.feed.post",
|
||||
"record": Json
|
||||
}
|
||||
console.log(DID);
|
||||
console.log(RequestBody.repo);
|
||||
let DPoP = await ClientDPoPPDS("POST", PDS + "/xrpc/com.atproto.repo.createRecord");
|
||||
let request = fetch(PDS + "/xrpc/com.atproto.repo.createRecord", { body: JSON.stringify(RequestBody), method: "POST", headers: {"Content-Type": "application/json", "Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}});
|
||||
let body = await request.then((response) => response.json());
|
||||
let status = await request.then((response) => response.status);
|
||||
let header = await request.then((response) => response.headers.get("dpop-nonce"));
|
||||
if (status == 401) {
|
||||
await FixNonceMismatch(header);
|
||||
let request = fetch(PDS + "/xrpc/com.atproto.repo.createRecord", { body: JSON.stringify(RequestBody), method: "POST", headers: {"Content-Type": "application/json", "Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}});
|
||||
body = await request.then((response) => response.json());
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
// Added after all the components: in case of nonce mismatch...
|
||||
export async function FixNonceMismatch(head) {
|
||||
Cookie.InputCookie(Cookie.BlueskyNonceName, head);
|
||||
return true;
|
||||
return Cookie.InputCookie(Cookie.BlueskyNonceName, head);
|
||||
}
|
||||
|
||||
// Component 1/4
|
||||
|
|
|
@ -20,7 +20,8 @@ async function Post() {
|
|||
}
|
||||
// Bluesky posting.
|
||||
if (Cookie.IsCookieReal(Cookie.BlueskyAccessTokenCookie)) {
|
||||
|
||||
let DID = await BlueskyAPI.GetBlueskyDID("https://woodear.us-west.host.bsky.network", "crowdedgames.group");
|
||||
BlueskyAPI.CreatePost("https://woodear.us-west.host.bsky.network", DID.did, Text);
|
||||
}
|
||||
InputArea.value = "";
|
||||
}
|
||||
|
|
|
@ -20,8 +20,8 @@ let Origin = location.origin + "/HTML/setting.html"
|
|||
// Change weather the timelines are public or remote
|
||||
LocalButton.onclick = (event) => {
|
||||
// Toggle the cookie
|
||||
if (Cookie.IsCookieReal(GetCookie("Local"))) {
|
||||
Cookie.ExpireCookie(GetCookie("Local"));
|
||||
if (Cookie.IsCookieReal(Cookie.GetCookie("Local"))) {
|
||||
Cookie.ExpireCookie("Local");
|
||||
} else {
|
||||
Cookie.InputCookie("Local", "true");
|
||||
}
|
||||
|
@ -29,8 +29,8 @@ LocalButton.onclick = (event) => {
|
|||
|
||||
RemoteButton.onclick = (event) => {
|
||||
// Toggle the cookie
|
||||
if (Cookie.IsCookieReal(GetCookie("Remote"))) {
|
||||
Cookie.ExpireCookie(GetCookie("Remote"));
|
||||
if (Cookie.IsCookieReal(Cookie.GetCookie("Remote"))) {
|
||||
Cookie.ExpireCookie("Remote");
|
||||
} else {
|
||||
Cookie.InputCookie("Remote", "true");
|
||||
}
|
||||
|
@ -101,19 +101,3 @@ async function CheckLogin() {
|
|||
// Runs on website start.
|
||||
// Remove traces of "login".
|
||||
CheckLogin();
|
||||
|
||||
// TESTING!
|
||||
async function TESTING() {
|
||||
let response = BlueskyAPI.GetBlueskyDID("https://woodear.us-west.host.bsky.network", "crowdedgames.group");
|
||||
let body = await response.then((res) => res.json());
|
||||
let status = await response.then((res) => res.status);
|
||||
let header = await response.then((res) => res.headers.get("dpop-nonce"));
|
||||
if (status == 401) {
|
||||
await BlueskyAPI.FixNonceMismatch(header);
|
||||
response = BlueskyAPI.GetBlueskyDID("https://woodear.us-west.host.bsky.network", "crowdedgames.group");
|
||||
body = await response.then((res) => res.json());
|
||||
}
|
||||
console.log(body);
|
||||
}
|
||||
|
||||
TESTING();
|
||||
|
|
Loading…
Add table
Reference in a new issue