<commit_before>///<reference path='refs.ts'/>
module TDev.RT {
    //? A cloud data session
    //@ ctx(general) stem("session") cap(cloudData) serializable
    export class CloudSession
        extends RTValue {

        // the unique identifier for the session.
        public _id: string;

        // immutable information about this session
        public _title: string;
        public _permissions: string;

        // additional "soft" information about this session

        public position: number;     // used for sorting list in displayed dialog

        // filled in by server queries
        public serverinfo: Revisions.ServerJson;

        // link to ClientSession
        public sessionimpl: Revisions.ClientSession;

        // filled in by storage queries
        public localname: string;    // name for cache entry
        public membernumber: number; // member number on server
        public enable_sync: boolean;    // sync is enabled

        // info that is derived from _id
        public ownerid: string;
        public tag: string; //(pr|pu|cr|cw|cp)
        public guidhash: string;

        public authtoken:string;

        public isJustMeSession(): boolean { return this.tag === "pr"; }
        public isEveryoneSession(): boolean { return this.tag === "pu"; }
        public isBroadcastSession(): boolean { return this.tag === "cr"; }
        public isShareableSession(): boolean { return this.tag === "cw"; }
        public isPrivateSession(): boolean { return this.tag === "cp"; }
        public isNodeSession(): boolean { return this.tag === "pn"; }

        // used for assigning icon to session
        public type(): string {
            if (this.isBroadcastSession()) return "broadcast";
            if (this.isEveryoneSession() || this.isShareableSession()) return "public";
            return "";
        }

        constructor() {
            super()
        }

        public toString(): string {
            return "cloud session: " + this._id + (this._title ? "(" + this._title + ")" : "");
        }

        public static fromDescriptor(desc: Revisions.ISessionParams) {
            if (!desc) return undefined;
            var s = new CloudSession();
            s._id = desc.servername;
            s._title = desc.title;
            s._permissions = desc.permissions;
            return s.validate() ? s : undefined;
        }

        // parse unique id (check validity and extract info)
        public validate(): boolean {
            var pos = this._id.indexOf("0");
            if (pos > 3 && pos < this._id.length - 4) {
                this.ownerid = this._id.substr(0, pos);
                this.tag = this._id.substr(pos + 1, 2);
                this.guidhash = this._id.substr(pos + 3);
                return (/^[a-z]{3,}$/.test(this.ownerid) && /^[a-z]+$/.test(this.guidhash) && /^(pr|pu|cr|cw|cp|pn)$/.test(this.tag));
            }
            else
                return false;
        }

        // these tags are used for crs/cws/crs sessions to narrow down the target script
        public static makeScriptTag(author: string, scriptname: string) {
            return (author + "xx" + Revisions.letterify(scriptname) + "xx");
        }

        public static makeScriptIdentifier(scriptid: string, authorid: string) {
            return scriptid + "@" + authorid;
        }

        //? Checks if this cloud session is the same as another one
        public equals(other: CloudSession): boolean { return this._id === other._id; }

        //? Gets a string that uniquely identifies this cloud session; other users can connect by using this string.
        public id(): string { return this._id; }

        //? Gets a string that describes this cloud session
        public title(): string { return this._title; }

        //? Gets information about the user that owns this session
        public owner(): User { return User.mk(this.ownerid); }

        //? Indicates if the current user owns this session
        //@ returns(boolean)
        public is_owned(s: IStackFrame) { //: boolean {
            return s.rt.sessions.getUserId() === this.ownerid;
            //Bazaar.userIdAsync(r.rt).done(userId => r.resumeVal(userId === this.ownerid));
        }

        //? Displays the session description on the wall.
        public post_to_wall(s: IStackFrame) {
            s.rt.postBoxedHtml(
                CloudData.formatCloudSessionInfo(this, false, false, true),
                s.pc,
                this
                );
        }

        //? Query server about current state of this session. You must be the authenticated owner.
        //@ async returns(JsonObject)
        //@ dbgOnly
        public server_info(r: ResumeCtx) {

            if (this.isNodeSession())
                Util.userError(lf("cannot get session info for deployed cloud libraries"), r.rt.current.pc);
            if (this.ownerid !== r.rt.sessions.getUserId())
                Util.userError(lf("not permitted: must be session owner"), r.rt.current.pc);
            if (this.isNodeSession())
                Util.userError(lf("cannot query node sessions server info"));

            var promise = Revisions.getServerInfoAsync(this._id);
            promise.then((result) => r.resumeVal(result));

           // unrelated: testing attribute code
           // Revisions.setLocalSessionAttributeAsync("test", "42", r.rt).then(() => {
           //     var x = Revisions.getLocalSessionAttributeAsync("test", r.rt);
           //});

            return promise.done();
        }
    }

    //? Cloud session management
    //@ cap(cloudData) skill(3)
    export module CloudData {



        // ---- static formatters

        //? Gets the currently active session. When the script starts, this is always the just-me session.
        export function current_session(i: IStackFrame): CloudSession {
            return i.rt.sessions.getCurrentSession().getCloudSession();
        }

        //? Clear the local cache of the current session (discarding unsynced changes) and get fresh data from server
        //@ dbgOnly<commit_msg>make clouddata available in coder mode<commit_after>///<reference path='refs.ts'/>
module TDev.RT {
    //? A cloud data session
    //@ ctx(general) stem("session") cap(cloudData) serializable
    export class CloudSession
        extends RTValue {

        // the unique identifier for the session.
        public _id: string;

        // immutable information about this session
        public _title: string;
        public _permissions: string;

        // additional "soft" information about this session

        public position: number;     // used for sorting list in displayed dialog

        // filled in by server queries
        public serverinfo: Revisions.ServerJson;

        // link to ClientSession
        public sessionimpl: Revisions.ClientSession;

        // filled in by storage queries
        public localname: string;    // name for cache entry
        public membernumber: number; // member number on server
        public enable_sync: boolean;    // sync is enabled

        // info that is derived from _id
        public ownerid: string;
        public tag: string; //(pr|pu|cr|cw|cp)
        public guidhash: string;

        public authtoken:string;

        public isJustMeSession(): boolean { return this.tag === "pr"; }
        public isEveryoneSession(): boolean { return this.tag === "pu"; }
        public isBroadcastSession(): boolean { return this.tag === "cr"; }
        public isShareableSession(): boolean { return this.tag === "cw"; }
        public isPrivateSession(): boolean { return this.tag === "cp"; }
        public isNodeSession(): boolean { return this.tag === "pn"; }

        // used for assigning icon to session
        public type(): string {
            if (this.isBroadcastSession()) return "broadcast";
            if (this.isEveryoneSession() || this.isShareableSession()) return "public";
            return "";
        }

        constructor() {
            super()
        }

        public toString(): string {
            return "cloud session: " + this._id + (this._title ? "(" + this._title + ")" : "");
        }

        public static fromDescriptor(desc: Revisions.ISessionParams) {
            if (!desc) return undefined;
            var s = new CloudSession();
            s._id = desc.servername;
            s._title = desc.title;
            s._permissions = desc.permissions;
            return s.validate() ? s : undefined;
        }

        // parse unique id (check validity and extract info)
        public validate(): boolean {
            var pos = this._id.indexOf("0");
            if (pos > 3 && pos < this._id.length - 4) {
                this.ownerid = this._id.substr(0, pos);
                this.tag = this._id.substr(pos + 1, 2);
                this.guidhash = this._id.substr(pos + 3);
                return (/^[a-z]{3,}$/.test(this.ownerid) && /^[a-z]+$/.test(this.guidhash) && /^(pr|pu|cr|cw|cp|pn)$/.test(this.tag));
            }
            else
                return false;
        }

        // these tags are used for crs/cws/crs sessions to narrow down the target script
        public static makeScriptTag(author: string, scriptname: string) {
            return (author + "xx" + Revisions.letterify(scriptname) + "xx");
        }

        public static makeScriptIdentifier(scriptid: string, authorid: string) {
            return scriptid + "@" + authorid;
        }

        //? Checks if this cloud session is the same as another one
        public equals(other: CloudSession): boolean { return this._id === other._id; }

        //? Gets a string that uniquely identifies this cloud session; other users can connect by using this string.
        public id(): string { return this._id; }

        //? Gets a string that describes this cloud session
        public title(): string { return this._title; }

        //? Gets information about the user that owns this session
        public owner(): User { return User.mk(this.ownerid); }

        //? Indicates if the current user owns this session
        //@ returns(boolean)
        public is_owned(s: IStackFrame) { //: boolean {
            return s.rt.sessions.getUserId() === this.ownerid;
            //Bazaar.userIdAsync(r.rt).done(userId => r.resumeVal(userId === this.ownerid));
        }

        //? Displays the session description on the wall.
        public post_to_wall(s: IStackFrame) {
            s.rt.postBoxedHtml(
                CloudData.formatCloudSessionInfo(this, false, false, true),
                s.pc,
                this
                );
        }

        //? Query server about current state of this session. You must be the authenticated owner.
        //@ async returns(JsonObject)
        //@ dbgOnly
        public server_info(r: ResumeCtx) {

            if (this.isNodeSession())
                Util.userError(lf("cannot get session info for deployed cloud libraries"), r.rt.current.pc);
            if (this.ownerid !== r.rt.sessions.getUserId())
                Util.userError(lf("not permitted: must be session owner"), r.rt.current.pc);
            if (this.isNodeSession())
                Util.userError(lf("cannot query node sessions server info"));

            var promise = Revisions.getServerInfoAsync(this._id);
            promise.then((result) => r.resumeVal(result));

           // unrelated: testing attribute code
           // Revisions.setLocalSessionAttributeAsync("test", "42", r.rt).then(() => {
           //     var x = Revisions.getLocalSessionAttributeAsync("test", r.rt);
           //});

            return promise.done();
        }
    }

    //? Cloud session management
    //@ cap(cloudData) skill(2)
    export module CloudData {



        // ---- static formatters

        //? Gets the currently active session. When the script starts, this is always the just-me session.
        export function current_session(i: IStackFrame): CloudSession {
            return i.rt.sessions.getCurrentSession().getCloudSession();
        }

        //? Clear the local cache of the current session (discarding unsynced changes) and get fresh data from server
        //@ dbgOnly
{'fraction_non_alphanumeric': 0.11430914309143092, 'fraction_numerical': 0.0018040180401804019, 'mean_word_length': 2.014335145823035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807660', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3131, 'n_words': 980}