Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi
I was playing around with databag and I loved the concept. I have some suggestions that could probably improve its use. (There is a suggestion I had made several months back, but that may be an esoteric suggestion) I think it is an excellent system which should see wider usage.

But before that, I wanted to share a useful piece of code that I wrote in Javascript that I think can help others. When a new user joins, he is facing a largely blank screen with no idea what to do next. So I wrote this utility to allow to join a list of topics previously made by another user.

The Javascript I used is the one used on the server side of Pocketbase (another excellent system) but I think it is
quite easily adaptable for other types of Javascript (Nodejs, etc) This code will find out the list of topics made by a particular user (in my case 'adminchat' and then adds the new user to one of the topics of 'adminchat'.

Only those topics which has a "Subject" can be used here. The handle (i.e. username) of the user who wants to use should at least send a connection request to 'adminchat'.

I did not have much access to the API. I tried importing the .oa3 file (there is a small bug in the format which I had to remove) into Postman and I got a rough idea. But I could figure out what was going on based on the inspection of the browser console when playing around with Databag.

I don't think there is any security risks in the way Databag carries out its tasks but the API calls are quite unusual (or maybe I have misunderstood some and by some luck all this seems to work)

Hope this is useful to others. I will make my suggestions in another section of these discussions. Hope Databag is used widely! All the best.

//userhandle and topicname comes from query string variables to this code
//utils.send(...) function is for sending HTTP requests available in a separate utils.js module. It is like the JS 'fetch(...)' function but synchronous
//'adminchat' is the name of the user who has created the topics, so we first login as that user as the first step
//bcode is the variable that has the Base64 of username:password (needed to do a login) 

//The user who wants to join a topic should at least have sent a connection request to the 'adminchat' user

const postLoginUrl = "https://<REDACTEDDOMAIN>/account/apps?appName=Databag"
const getListingUrl  = "https://<REDACTEDDOMAIN>/account/listing"
const getTopicsUrl = "https://<REDACTEDDOMAIN>/content/channels?agent=<APPTOKEN>"
const getCardsUrl = "https://<REDACTEDDOMAIN>/contact/cards?agent=<APPTOKEN>"
const getTokenUrl = "https://<REDACTEDDOMAIN>/contact/cards/<USERID>/detail?agent=<APPTOKEN>"
const putStatusConnectingUrl = "https://<REDACTEDDOMAIN>/contact/cards/<USERID>/status?agent=<APPTOKEN>" //with body as "\"connecting\""
const getUserJsonUrl = "https://<REDACTEDDOMAIN>/contact/cards/<USERID>/openMessage?agent=<APPTOKEN>"
const getUserDetailUrl = "https://<REDACTEDDOMAIN>/contact/cards/<USERID>/detail?agent=<APPTOKEN>"
const putUserJsonUrl = "https://<REDACTEDDOMAIN>/contact/openMessage" //with body from above 
const putUserConnectedUrl = "https://<REDACTEDDOMAIN>/contact/cards/<USERID>/status?agent=<APPTOKEN>&token=<TOKEN>&viewRevision=1&articleRevision=1&channelRevision=8&profileRevision=1"
const putUserIntoTopicUrl = "https://<REDACTEDDOMAIN>/content/channels/<TOPICID>/cards/<USERID>?agent=<APPTOKEN>"


let ans = utils.send(postLoginUrl,"POST",{"Authorization":`Basic ${bcode}`});
const appToken = ans.json.appToken
console.log("AppToken after login "+JSON.stringify(appToken));

let userguid
ans = utils.send(getListingUrl,"GET");
const listing = ans.json
listing.forEach((element) => {
    if(element.handle == userhandle){ 
        userguid = element.guid
      }
  });


let Msg = "Getting ..."; //for debug

let msg = "";

ans = utils.send(getTopicsUrl.replace('<APPTOKEN>',appToken),"GET");
const topics = ans.json
let topicid
topics.forEach(element => {
      if(element.data != null){
        let d = JSON.parse(element.data.channelDetail.data)
        if(d.subject == topicname){
            Msg += "Found topic "+d.subject
            topicid = element.id
            if(element.data.channelDetail.members.indexOf(userguid) != -1){
                msg = "User has already joined the topic"
                
            }
         }
       }
});

if (msg != ''){
    return c.json(200,{success: false,"Msg": msg })
}

if(!topicid){
     return c.json(200,{success: false,"Msg": "Topic not available" })
}

ans = utils.send(getCardsUrl.replace('<APPTOKEN>',appToken),"GET");
const cards = ans.json
let userid,connecteduserid 
cards.forEach(element => {
    if(element.data != null){
        if(element.data.cardProfile.handle == userhandle)
             {
                Msg += " ... user found ... status was: "+element.data.cardDetail.status

                if(element.data.cardDetail.status == "connected")
                     {
                        Msg += "... user is connected ... ID: "+element.id
                        connecteduserid = element.id
                     }
             }    
        if(element.data.cardDetail.status == "pending")
             {
                Msg += "... user is pending ... ID: "+element.id
                userid = element.id
             }
    }    
})

if(userid){
        ans = utils.send(getTokenUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid),"GET");
        Msg += "["+ans.raw +"]"

        ans = utils.send(putStatusConnectingUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid),"PUT",{"Content-Type": "application/json"},"\"connecting\""); 
        Msg += "[" +ans.raw +"]" 

        ans = utils.send(getUserJsonUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid),"GET");
        Msg += "["+ ans.raw+"]" 

        Msg += "Got the JSON returned by openMessage... "
        let interimJson = ans.raw 

        ans = utils.send(getUserDetailUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid),"GET");
        Msg += "Got the user detail...["+ans.raw+"]"

        ans = utils.send(putUserJsonUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid),"PUT",
                {"Content-Type": "text/plain","referer": "https://<REDACTEDDOMAIN>"},interimJson); 
        Msg += "hope this came...["
        Msg += ans.raw
        Msg += "] ... kinda done..."

        let theToken = ans.json.token

        ans = utils.send(putUserConnectedUrl.replace('<APPTOKEN>',appToken).replace('<USERID>',userid).replace('<TOKEN>',theToken),"PUT",
                    {"Content-Type": "text/plain","referer": "https://<REDACTEDDOMAIN>"},"\"connected\"");

        connecteduserid = userid; //Now the userid is indeed connected! (or better be!!)
        msg += "Connected user. "
}

if(connecteduserid){
    ans = utils.send(putUserIntoTopicUrl.replace('<APPTOKEN>',appToken).replace('<TOPICID>',topicid).replace('<USERID>',connecteduserid),"PUT",
                {"Content-Type": "text/plain","referer": "https://<REDACTEDDOMAIN>"});
    Msg += "Added user into topic... ["+ans.raw+"]"
    msg += "Added user to selected topic."
} else {
    return c.json(200,{success: false,"Msg": 'Could not get a proper connected user' })

}
return c.json(200,{success: true,"Msg": msg }) //change msg to Msg for debug messages
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.