@easy-cms/plugin-social 1.38.0 → 1.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin.js +36 -36
- package/dist/index.d.ts +13 -3
- package/dist/index.js +274 -46
- package/package.json +5 -4
package/dist/admin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var A="/social/channels",v="/social/post",$="/social/ai",k="/social/share";var O=[[0,4351],[8192,8205],[8208,8223],[8242,8247]];function S(o){let t=0;for(let e of o.split(/(https?:\/\/\S+)/)){if(/^https?:\/\//.test(e)){t+=23;continue}let n=typeof Intl.Segmenter=="function"?Array.from(new Intl.Segmenter(void 0,{granularity:"grapheme"}).segment(e),r=>r.segment):[...e];for(let r of n){if(/\p{Extended_Pictographic}/u.test(r)){t+=2;continue}for(let i of r){let l=i.codePointAt(0)??0;t+=O.some(([p,u])=>l>=p&&l<=u)?1:2}}}return t}var P=["value","path","doc","collection","id","uiLocale","readOnly","api"],E={en:{pick:"Choose a channel",gone:"{id} (not connected)",none:"No channel is connected yet: add one in Settings \u2192 Integrations.",chars:"{n} characters",charsMax:"{n} / {max} characters",over:"Too long for {channel}",saveFirst:"Save the post first, then post it.",savedOnly:"Posts what is saved: save your changes first.",postNow:"Post now",confirmNow:"Post to {n} channels now? Posts can\u2019t be taken back from here.",schedule:"Schedule",when:"Post at",scheduledFor:"Scheduled for {time}",cancel:"Cancel the schedule",retry:"Retry",waiting:"Not posted",scheduled:"Scheduled",sending:"Sending\u2026",retrying:"Trying again at {time}",posted:"Posted",view:"View post",failed:"Failed",noRight:"You may write this post; someone who may post sends it.",share:"Share on social media",shareSave:"Save first, then share.",sharing:"Creating a post\u2026",writeAi:"Write with AI",writingAi:"Writing\u2026",spentAi:"Today's AI requests are used up",replaceAi:"Replace this text with one written by AI?",writtenAi:"Written by AI: check it before posting.",undo:"Undo"},th:{pick:"\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07",gone:"{id} (\u0E44\u0E21\u0E48\u0E44\u0E14\u0E49\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D\u0E41\u0E25\u0E49\u0E27)",none:"\u0E22\u0E31\u0E07\u0E44\u0E21\u0E48\u0E21\u0E35\u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07\u0E17\u0E35\u0E48\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D: \u0E40\u0E1E\u0E34\u0E48\u0E21\u0E44\u0E14\u0E49\u0E43\u0E19 Settings \u2192 \u0E01\u0E32\u0E23\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D",chars:"{n} \u0E15\u0E31\u0E27\u0E2D\u0E31\u0E01\u0E29\u0E23",charsMax:"{n} / {max} \u0E15\u0E31\u0E27\u0E2D\u0E31\u0E01\u0E29\u0E23",over:"\u0E22\u0E32\u0E27\u0E40\u0E01\u0E34\u0E19\u0E2A\u0E33\u0E2B\u0E23\u0E31\u0E1A {channel}",saveFirst:"\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E01\u0E48\u0E2D\u0E19 \u0E41\u0E25\u0E49\u0E27\u0E04\u0E48\u0E2D\u0E22\u0E42\u0E1E\u0E2A\u0E15\u0E4C",savedOnly:"\u0E08\u0E30\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E32\u0E21\u0E17\u0E35\u0E48\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E44\u0E27\u0E49: \u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E01\u0E32\u0E23\u0E41\u0E01\u0E49\u0E44\u0E02\u0E01\u0E48\u0E2D\u0E19",postNow:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E25\u0E22",confirmNow:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E44\u0E1B {n} \u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07\u0E40\u0E25\u0E22\u0E44\u0E2B\u0E21? \u0E22\u0E01\u0E40\u0E25\u0E34\u0E01\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E08\u0E32\u0E01\u0E17\u0E35\u0E48\u0E19\u0E35\u0E48\u0E44\u0E21\u0E48\u0E44\u0E14\u0E49",schedule:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32",when:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E27\u0E25\u0E32",scheduledFor:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32\u0E44\u0E27\u0E49 {time}",cancel:"\u0E22\u0E01\u0E40\u0E25\u0E34\u0E01\u0E01\u0E32\u0E23\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32",retry:"\u0E25\u0E2D\u0E07\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07",waiting:"\u0E22\u0E31\u0E07\u0E44\u0E21\u0E48\u0E42\u0E1E\u0E2A\u0E15\u0E4C",scheduled:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32\u0E44\u0E27\u0E49",sending:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E48\u0E07\u2026",retrying:"\u0E08\u0E30\u0E25\u0E2D\u0E07\u0E43\u0E2B\u0E21\u0E48\u0E40\u0E27\u0E25\u0E32 {time}",posted:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E41\u0E25\u0E49\u0E27",view:"\u0E14\u0E39\u0E42\u0E1E\u0E2A\u0E15\u0E4C",failed:"\u0E44\u0E21\u0E48\u0E2A\u0E33\u0E40\u0E23\u0E47\u0E08",noRight:"\u0E04\u0E38\u0E13\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E19\u0E35\u0E49\u0E44\u0E14\u0E49 \u0E2A\u0E48\u0E27\u0E19\u0E01\u0E32\u0E23\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E49\u0E2D\u0E07\u0E43\u0E2B\u0E49\u0E1C\u0E39\u0E49\u0E17\u0E35\u0E48\u0E21\u0E35\u0E2A\u0E34\u0E17\u0E18\u0E34\u0E4C\u0E40\u0E1B\u0E47\u0E19\u0E04\u0E19\u0E01\u0E14",share:"\u0E41\u0E0A\u0E23\u0E4C\u0E44\u0E1B\u0E42\u0E0B\u0E40\u0E0A\u0E35\u0E22\u0E25",shareSave:"\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E01\u0E48\u0E2D\u0E19 \u0E41\u0E25\u0E49\u0E27\u0E04\u0E48\u0E2D\u0E22\u0E41\u0E0A\u0E23\u0E4C",sharing:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E23\u0E49\u0E32\u0E07\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u2026",writeAi:"\u0E40\u0E02\u0E35\u0E22\u0E19\u0E14\u0E49\u0E27\u0E22 AI",writingAi:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E40\u0E02\u0E35\u0E22\u0E19\u2026",spentAi:"\u0E43\u0E0A\u0E49\u0E04\u0E33\u0E02\u0E2D AI \u0E02\u0E2D\u0E07\u0E27\u0E31\u0E19\u0E19\u0E35\u0E49\u0E04\u0E23\u0E1A\u0E41\u0E25\u0E49\u0E27",replaceAi:"\u0E41\u0E17\u0E19\u0E17\u0E35\u0E48\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E19\u0E35\u0E49\u0E14\u0E49\u0E27\u0E22\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E17\u0E35\u0E48 AI \u0E40\u0E02\u0E35\u0E22\u0E19\u0E44\u0E2B\u0E21?",writtenAi:"\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E14\u0E22 AI: \u0E15\u0E23\u0E27\u0E08\u0E14\u0E39\u0E01\u0E48\u0E2D\u0E19\u0E42\u0E1E\u0E2A\u0E15\u0E4C",undo:"\u0E40\u0E25\u0E34\u0E01\u0E17\u0E33"}},s=o=>o.replace(/[&<>"']/g,t=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[t]);function I(o){if(typeof Intl.Segmenter!="function")return[...o].length;let t=0;for(let e of new Intl.Segmenter(void 0,{granularity:"grapheme"}).segment(o))t++;return t}var T=new WeakMap;function H(o){let t=T.get(o);if(t)return t;let e=o("GET",A).then(n=>n).catch(()=>({channels:[],canPost:!1}));return T.set(o,e),e}var C=new WeakMap;function L(o,t=!1){let e=C.get(o);if(e&&!t)return e;let n=o("GET","/admin/ai/status").then(r=>{let i=r;return{ready:i.connected&&i.features.includes("social"),spent:i.used>=i.limit}}).catch(()=>({ready:!1,spent:!1}));return C.set(o,n),n}var M=`
|
|
2
2
|
:host { display: block; font: inherit; color: var(--text); }
|
|
3
3
|
.row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
|
|
4
4
|
.hint { margin: 0; font-size: 0.8rem; color: var(--text-muted); }
|
|
@@ -18,24 +18,24 @@ var x="/social/channels",f="/social/post",w="/social/ai",A="/social/share";var C
|
|
|
18
18
|
border-radius: var(--radius-sm, 8px); background: var(--surface); color: var(--text); font: inherit;
|
|
19
19
|
}
|
|
20
20
|
a { color: var(--accent-ink); }
|
|
21
|
-
`,
|
|
22
|
-
<select data-action="pick" aria-label="${
|
|
23
|
-
${t?"":`<option value="">${
|
|
24
|
-
${!r&&t?`<option value="${
|
|
25
|
-
${this.channels.map(i=>`<option value="${
|
|
21
|
+
`,g=class o extends HTMLElement{ctx={value:void 0,path:void 0,doc:{},collection:void 0,id:null,uiLocale:"en",readOnly:!1,api:void 0};channels=[];canPost=!1;asked=!1;queued=!1;html="";constructor(){super();let t=this.attachShadow({mode:"open"});t.addEventListener("click",e=>{let n=e.target.closest("[data-action]"),r=n?.getAttribute("data-action");r&&n?.tagName!=="SELECT"&&this.onAction(r,n)}),t.addEventListener("change",e=>this.onChange(e.target))}static{for(let t of P)Object.defineProperty(o.prototype,t,{get(){return this.ctx[t]},set(e){this.ctx[t]=e,this.update()}})}connectedCallback(){this.update()}t(t,e={}){return(this.ctx.uiLocale==="th"?E.th:E.en)[t].replace(/\{(\w+)\}/g,(r,i)=>String(e[i]??""))}time(t){return new Date(t).toLocaleString(this.ctx.uiLocale==="th"?"th-TH":"en-GB",{dateStyle:"medium",timeStyle:"short"})}labelOf(t){return this.channels.find(e=>e.id===t)?.label??t}update(){!this.asked&&this.ctx.api&&(this.asked=!0,H(this.ctx.api).then(({channels:t,canPost:e})=>{this.channels=t,this.canPost=e,this.update()})),!this.queued&&(this.queued=!0,queueMicrotask(()=>{this.queued=!1;let t=this.shadowRoot;if(!t)return;let e=`<style>${M}${this.css()}</style>${this.render()}`;if(e===this.html)return;let n=t.activeElement?.getAttribute("data-action");this.html=e,t.innerHTML=e,n&&t.querySelector(`[data-action="${n}"]`)?.focus()}))}onAction(t,e){}onChange(t){}css(){return""}},y=class extends g{render(){let t=typeof this.ctx.value=="string"?this.ctx.value:"",e=this.ctx.doc.delivery??null,n=!!t&&e?.channels?.[t]?.state==="posted";if(this.channels.length===0&&!t)return`<p class="hint">${s(this.t("none"))}</p>`;let r=this.channels.some(i=>i.id===t);return`
|
|
22
|
+
<select data-action="pick" aria-label="${s(this.t("pick"))}" ${this.ctx.readOnly||n?"disabled":""}>
|
|
23
|
+
${t?"":`<option value="">${s(this.t("pick"))}</option>`}
|
|
24
|
+
${!r&&t?`<option value="${s(t)}" selected>${s(this.t("gone",{id:t}))}</option>`:""}
|
|
25
|
+
${this.channels.map(i=>`<option value="${s(i.id)}" ${i.id===t?"selected":""}>${s(i.label)}</option>`).join("")}
|
|
26
26
|
</select>
|
|
27
|
-
`}css(){return"select { width: 100%; }"}onChange(t){t.getAttribute("data-action")==="pick"&&this.dispatchEvent(new CustomEvent("change",{detail:t.value}))}},
|
|
27
|
+
`}css(){return"select { width: 100%; }"}onChange(t){t.getAttribute("data-action")==="pick"&&this.dispatchEvent(new CustomEvent("change",{detail:t.value}))}},b=class extends g{ai={ready:!1,spent:!1};aiAsked=!1;busy=!1;beforeAi=null;notice=null;css(){return`
|
|
28
28
|
.bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.25rem 0.75rem; }
|
|
29
29
|
.ai { color: var(--accent-ink); font-weight: 600; border-color: transparent; background: transparent; }
|
|
30
30
|
.written { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 0.25rem 0 0; font-size: 0.8rem; color: var(--accent-ink); }
|
|
31
|
-
`}render(){!this.aiAsked&&this.ctx.api&&(this.aiAsked=!0,
|
|
31
|
+
`}render(){!this.aiAsked&&this.ctx.api&&(this.aiAsked=!0,L(this.ctx.api).then(e=>{this.ai=e,this.update()}));let t=this.ai.ready&&!this.ctx.readOnly?`<button type="button" class="ai" data-action="ai" ${this.busy||this.ai.spent?"disabled":""}>
|
|
32
32
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z"/></svg>
|
|
33
|
-
${
|
|
33
|
+
${s(this.t(this.busy?"writingAi":this.ai.spent?"spentAi":"writeAi"))}
|
|
34
34
|
</button>`:"";return`
|
|
35
35
|
<div class="bar">${this.count()}${t}</div>
|
|
36
|
-
${this.beforeAi?`<p class="written" role="status">${
|
|
37
|
-
${this.notice?`<p class="error" role="alert">${
|
|
38
|
-
`}async onAction(t){let{api:e,path:
|
|
36
|
+
${this.beforeAi?`<p class="written" role="status">${s(this.t("writtenAi"))} <button type="button" data-action="undo-ai">${s(this.t("undo"))}</button></p>`:""}
|
|
37
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
38
|
+
`}async onAction(t){let{api:e,path:n,id:r,doc:i}=this.ctx;if(!e||!n)return;let l=a=>this.dispatchEvent(new CustomEvent("set-field",{detail:{path:n,value:a}}));if(t==="undo-ai"&&this.beforeAi){l(this.beforeAi.value),this.beforeAi=null,this.update();return}if(t!=="ai"||this.busy||(typeof this.ctx.value=="string"?this.ctx.value.trim():"")&&!window.confirm(this.t("replaceAi")))return;let u=/^channels\.(\d+)\.text$/.exec(n),c=Array.isArray(i.channels)?i.channels:[],h=u?c[Number(u[1])]?.connection:void 0;this.busy=!0,this.notice=null,this.update();try{let a=await e("POST",$,{post:r,doc:{title:i.title,text:i.text,source:i.source??null},...typeof h=="string"?{channel:h}:{}});this.beforeAi??={value:this.ctx.value??null},l(a.text)}catch(a){this.notice=a.message}finally{this.busy=!1,this.ai=await L(e,!0),this.update()}}count(){let t=typeof this.ctx.value=="string"?this.ctx.value.trim():"",e=I(t),n=d=>d.counting==="x"?S(t):e,r=Array.isArray(this.ctx.doc.channels)?this.ctx.doc.channels:[],i=/^channels\.(\d+)\.text$/.exec(this.ctx.path??""),p=(i?r.filter((d,f)=>f===Number(i[1])):r.filter(d=>!(typeof d.text=="string"&&d.text.trim()))).map(d=>this.channels.find(f=>f.id===d.connection)).filter(d=>!!d?.maxLength),u=d=>(d.maxLength??0)-n(d),c=p.sort((d,f)=>u(d)-u(f))[0];if(!c)return`<span class="hint">${s(this.t("chars",{n:e}))}</span>`;let h=n(c),a=u(c)<0;return`<span class="${a?"error":"hint"}">${s(this.t("charsMax",{n:h,max:c.maxLength??0}))}${a?` \xB7 ${s(this.t("over",{channel:c.label}))}`:""}</span>`}},x=class extends g{live=null;busy=!1;notice=null;problems=[];notes={};checkedFor=null;timer;disconnectedCallback(){clearTimeout(this.timer)}delivery(){return this.live??this.ctx.value??{channels:{}}}css(){return`
|
|
39
39
|
.panel { display: flex; flex-direction: column; gap: 0.75rem; }
|
|
40
40
|
ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
|
|
41
41
|
li { display: flex; flex-direction: column; gap: 0.2rem; padding: 0.55rem 0.65rem; border: 1px solid var(--border); border-radius: var(--radius-sm, 8px); background: var(--surface); }
|
|
@@ -47,33 +47,33 @@ var x="/social/channels",f="/social/post",w="/social/ai",A="/social/share";var C
|
|
|
47
47
|
.schedule { display: flex; gap: 0.4rem; flex-wrap: wrap; }
|
|
48
48
|
.schedule input { flex: 1 1 10rem; }
|
|
49
49
|
.warning { margin: 0; padding: 0.5rem 0.65rem; border-radius: var(--radius-sm, 8px); background: var(--warning-soft); color: var(--warning-text); font-size: 0.8rem; }
|
|
50
|
-
`}check(){let{api:t,id:e}=this.ctx,{image:
|
|
51
|
-
<li class="${
|
|
52
|
-
<span class="name">${
|
|
53
|
-
<span class="state">${
|
|
54
|
-
${
|
|
55
|
-
${
|
|
56
|
-
${
|
|
57
|
-
${
|
|
58
|
-
${
|
|
50
|
+
`}check(){let{api:t,id:e}=this.ctx,{image:n=null,source:r=null}=this.ctx.doc,i=JSON.stringify([e,n,r]);!t||e===null||i===this.checkedFor||(this.checkedFor=i,t("POST",v,{post:e,action:"check",image:n,source:r}).then(l=>{let p=l;this.problems=p.problems??[],this.notes=p.notes??{},this.update()}).catch(()=>{}))}render(){if(this.ctx.id===null)return`<p class="hint">${s(this.t("saveFirst"))}</p>`;this.check();let e=this.delivery(),n=(Array.isArray(this.ctx.doc.channels)?this.ctx.doc.channels:[]).filter(c=>typeof c.connection=="string"&&c.connection),r=c=>e.channels?.[c]??{state:"waiting",attempts:0},i=n.filter(c=>["waiting","scheduled"].includes(r(c.connection).state)),l=n.some(c=>r(c.connection).state==="scheduled"),p=n.map(c=>{let h=c.connection,a=r(h),d=a.state==="sending"&&a.attempts>0&&a.nextAttemptAt?this.t("retrying",{time:this.time(a.nextAttemptAt)}):this.t(a.state);return`
|
|
51
|
+
<li class="${a.state}" data-channel="${s(h)}">
|
|
52
|
+
<span class="name">${s(this.labelOf(h))}</span>
|
|
53
|
+
<span class="state">${s(d)}</span>
|
|
54
|
+
${a.state==="posted"&&a.url?`<a href="${s(a.url)}" target="_blank" rel="noopener">${s(this.t("view"))}</a>`:""}
|
|
55
|
+
${a.error&&a.state!=="posted"?`<p class="error">${s(a.error)}</p>`:""}
|
|
56
|
+
${a.state!=="posted"&&this.notes[h]?.info?`<p class="hint">${s(this.notes[h]?.info??"")}</p>`:""}
|
|
57
|
+
${a.state!=="posted"&&this.notes[h]?.warning?`<p class="warning">${s(this.notes[h]?.warning??"")}</p>`:""}
|
|
58
|
+
${a.state==="failed"&&this.canPost?`<div class="row"><button type="button" data-action="retry" data-channel="${s(h)}" ${this.busy?"disabled":""}>${s(this.t("retry"))}</button></div>`:""}
|
|
59
59
|
</li>`}).join(""),u=this.canPost?i.length===0?"":`
|
|
60
|
-
<div class="row"><button type="button" class="primary" data-action="now" ${this.busy?"disabled":""}>${
|
|
61
|
-
${
|
|
62
|
-
<div class="row"><button type="button" data-action="cancel" ${this.busy?"disabled":""}>${
|
|
63
|
-
<input type="datetime-local" data-field="when" aria-label="${
|
|
64
|
-
<button type="button" data-action="schedule" ${this.busy?"disabled":""}>${
|
|
60
|
+
<div class="row"><button type="button" class="primary" data-action="now" ${this.busy?"disabled":""}>${s(this.t("postNow"))}</button></div>
|
|
61
|
+
${l&&e.postAt?`<p class="hint">${s(this.t("scheduledFor",{time:this.time(e.postAt)}))}</p>
|
|
62
|
+
<div class="row"><button type="button" data-action="cancel" ${this.busy?"disabled":""}>${s(this.t("cancel"))}</button></div>`:`<div class="schedule">
|
|
63
|
+
<input type="datetime-local" data-field="when" aria-label="${s(this.t("when"))}" />
|
|
64
|
+
<button type="button" data-action="schedule" ${this.busy?"disabled":""}>${s(this.t("schedule"))}</button>
|
|
65
65
|
</div>`}
|
|
66
|
-
<p class="hint">${
|
|
66
|
+
<p class="hint">${s(this.t("savedOnly"))}</p>`:`<p class="hint">${s(this.t("noRight"))}</p>`;return`
|
|
67
67
|
<div class="panel">
|
|
68
|
-
${
|
|
69
|
-
${this.problems.map(
|
|
70
|
-
${
|
|
71
|
-
${this.notice?`<p class="error" role="alert">${
|
|
68
|
+
${n.length?`<ul>${p}</ul>`:`<p class="hint">${s(this.t("none"))}</p>`}
|
|
69
|
+
${this.problems.map(c=>`<p class="warning">${s(c)}</p>`).join("")}
|
|
70
|
+
${n.length?u:""}
|
|
71
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
72
72
|
</div>
|
|
73
|
-
`}async onAction(t,e){let{api:
|
|
73
|
+
`}async onAction(t,e){let{api:n,id:r}=this.ctx;if(!n||r===null||this.busy)return;let i={post:r,action:t};if(t==="retry"&&(i.channel=e.getAttribute("data-channel")),t==="schedule"){let l=this.shadowRoot?.querySelector('[data-field="when"]');if(!l?.value){l?.focus();return}i.postAt=new Date(l.value).toISOString()}if(t==="now"){let p=(this.ctx.doc.channels??[]).filter(u=>this.delivery().channels?.[String(u.connection)]?.state!=="posted").length;if(!window.confirm(this.t("confirmNow",{n:p})))return}this.busy=!0,this.notice=null,this.update();try{let l=await n("POST",v,i);this.live=l.delivery,this.watch()}catch(l){this.notice=l.message}finally{this.busy=!1,this.update()}}watch(){clearTimeout(this.timer),!(!Object.values(this.delivery().channels??{}).some(e=>e.state==="sending")||!this.ctx.api||!this.ctx.collection||this.ctx.id===null)&&(this.timer=setTimeout(async()=>{try{let e=await this.ctx.api?.("GET",`/${this.ctx.collection}/${encodeURIComponent(String(this.ctx.id))}?depth=0`);this.live=e.delivery??null,this.update()}catch{}this.watch()},5e3))}},w=class extends g{busy=!1;notice=null;render(){let t=this.ctx.id!==null;return`
|
|
74
74
|
<div class="row">
|
|
75
|
-
<button type="button" data-action="share" ${!t||this.busy?"disabled":""}>${
|
|
75
|
+
<button type="button" data-action="share" ${!t||this.busy?"disabled":""}>${s(this.t(this.busy?"sharing":"share"))}</button>
|
|
76
76
|
</div>
|
|
77
|
-
${t?"":`<p class="hint">${
|
|
78
|
-
${this.notice?`<p class="error" role="alert">${
|
|
79
|
-
`}async onAction(t){let{api:e,id:
|
|
77
|
+
${t?"":`<p class="hint">${s(this.t("shareSave"))}</p>`}
|
|
78
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
79
|
+
`}async onAction(t){let{api:e,id:n,collection:r}=this.ctx;if(!(t!=="share"||!e||n===null||this.busy)){this.busy=!0,this.notice=null,this.update();try{let i=await e("POST",k,{collection:r,id:n});this.dispatchEvent(new CustomEvent("navigate",{detail:i.path}))}catch(i){this.notice=i.message}finally{this.busy=!1,this.update()}}}},m=(o,t)=>{customElements.get(o)||customElements.define(o,t)};m("ecms-social-channel",y);m("ecms-social-count",b);m("ecms-social-panel",x);m("ecms-social-share",w);
|
package/dist/index.d.ts
CHANGED
|
@@ -22,17 +22,21 @@ interface SocialMessage {
|
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* A place social posts go (#312): an integration admins connect accounts to, and how it posts.
|
|
25
|
-
*
|
|
25
|
+
* Built in: the webhook, Facebook Pages and LINE (#313), and X (#380).
|
|
26
26
|
*/
|
|
27
27
|
interface SocialChannel {
|
|
28
28
|
readonly integration: IntegrationConfig;
|
|
29
29
|
/** Characters a post may have there, for the admin's counter. */
|
|
30
30
|
readonly maxLength?: number;
|
|
31
|
+
/** How `maxLength` counts: `x` is X's weighted length (a link 23, an emoji 2). */
|
|
32
|
+
readonly counting?: 'x';
|
|
31
33
|
/**
|
|
32
34
|
* What to say before posting, beside the channel in the panel, e.g. how many people a LINE
|
|
33
35
|
* broadcast reaches and the month's quota; `warning` when it may cost or fail.
|
|
34
36
|
*/
|
|
35
|
-
readonly preview?: (values: Readonly<Record<string, string
|
|
37
|
+
readonly preview?: (values: Readonly<Record<string, string>>, draft: {
|
|
38
|
+
readonly link: string | null;
|
|
39
|
+
}) => Promise<{
|
|
36
40
|
readonly info?: string;
|
|
37
41
|
readonly warning?: string;
|
|
38
42
|
}>;
|
|
@@ -47,6 +51,10 @@ declare class ChannelError extends Error {
|
|
|
47
51
|
constructor(message: string, permanent?: boolean);
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
declare const facebook: SocialChannel;
|
|
55
|
+
|
|
56
|
+
declare const line: SocialChannel;
|
|
57
|
+
|
|
50
58
|
type Doc = Record<string, unknown>;
|
|
51
59
|
/** What a social post starts with when shared from a document. */
|
|
52
60
|
interface ShareValues {
|
|
@@ -107,5 +115,7 @@ interface Delivery {
|
|
|
107
115
|
|
|
108
116
|
declare const webhook: SocialChannel;
|
|
109
117
|
|
|
110
|
-
|
|
118
|
+
declare const x: SocialChannel;
|
|
119
|
+
|
|
120
|
+
export { ChannelError, facebook, line, socialPlugin, webhook, x };
|
|
111
121
|
export type { ChannelDelivery, ChannelState, Delivery, ShareValues, SocialChannel, SocialMessage, SocialPluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -8,10 +8,28 @@ var ChannelError = class extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
// ../../node_modules/.pnpm/simple-icons@16.36.0/node_modules/simple-icons/index.mjs
|
|
12
|
+
var c = '<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>';
|
|
13
|
+
var t = '</title><path d="';
|
|
14
|
+
var a = '"/></svg>';
|
|
15
|
+
var siFacebook = { title: "Facebook", slug: "facebook", get svg() {
|
|
16
|
+
return c + "Facebook" + t + this.path + a;
|
|
17
|
+
}, path: "M9.101 23.691v-7.98H6.627v-3.667h2.474v-1.58c0-4.085 1.848-5.978 5.858-5.978.401 0 .955.042 1.468.103a8.68 8.68 0 0 1 1.141.195v3.325a8.623 8.623 0 0 0-.653-.036 26.805 26.805 0 0 0-.733-.009c-.707 0-1.259.096-1.675.309a1.686 1.686 0 0 0-.679.622c-.258.42-.374.995-.374 1.752v1.297h3.919l-.386 2.103-.287 1.564h-3.246v8.245C19.396 23.238 24 18.179 24 12.044c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.628 3.874 10.35 9.101 11.647Z", source: "https://about.meta.com/brand/resources/facebook/logo", hex: "0866FF", guidelines: "https://about.meta.com/brand/resources/facebook/logo" };
|
|
18
|
+
var siLine = { title: "LINE", slug: "line", get svg() {
|
|
19
|
+
return c + "LINE" + t + this.path + a;
|
|
20
|
+
}, path: "M19.365 9.863c.349 0 .63.285.63.631 0 .345-.281.63-.63.63H17.61v1.125h1.755c.349 0 .63.283.63.63 0 .344-.281.629-.63.629h-2.386c-.345 0-.627-.285-.627-.629V8.108c0-.345.282-.63.63-.63h2.386c.346 0 .627.285.627.63 0 .349-.281.63-.63.63H17.61v1.125h1.755zm-3.855 3.016c0 .27-.174.51-.432.596-.064.021-.133.031-.199.031-.211 0-.391-.09-.51-.25l-2.443-3.317v2.94c0 .344-.279.629-.631.629-.346 0-.626-.285-.626-.629V8.108c0-.27.173-.51.43-.595.06-.023.136-.033.194-.033.195 0 .375.104.495.254l2.462 3.33V8.108c0-.345.282-.63.63-.63.345 0 .63.285.63.63v4.771zm-5.741 0c0 .344-.282.629-.631.629-.345 0-.627-.285-.627-.629V8.108c0-.345.282-.63.63-.63.346 0 .628.285.628.63v4.771zm-2.466.629H4.917c-.345 0-.63-.285-.63-.629V8.108c0-.345.285-.63.63-.63.348 0 .63.285.63.63v4.141h1.756c.348 0 .629.283.629.63 0 .344-.282.629-.629.629M24 10.314C24 4.943 18.615.572 12 .572S0 4.943 0 10.314c0 4.811 4.27 8.842 10.035 9.608.391.082.923.258 1.058.59.12.301.079.766.038 1.08l-.164 1.02c-.045.301-.24 1.186 1.049.645 1.291-.539 6.916-4.078 9.436-6.975C23.176 14.393 24 12.458 24 10.314", source: "https://line.me/en/logo", hex: "00C300", guidelines: "https://line.me/en/logo" };
|
|
21
|
+
var siX = { title: "X", slug: "x", get svg() {
|
|
22
|
+
return c + "X" + t + this.path + a;
|
|
23
|
+
}, path: "M14.234 10.162 22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299-.929-1.329L3.076 1.56h3.182l5.965 8.532.929 1.329 7.754 11.09h-3.182z", source: "https://x.com", hex: "000000", guidelines: "https://about.x.com/en/who-we-are/brand-toolkit" };
|
|
24
|
+
|
|
25
|
+
// src/logos.ts
|
|
26
|
+
var logo = (icon) => ({
|
|
27
|
+
paths: [{ d: icon.path }],
|
|
28
|
+
color: `#${icon.hex}`
|
|
29
|
+
});
|
|
30
|
+
var FACEBOOK = logo(siFacebook);
|
|
31
|
+
var LINE = logo(siLine);
|
|
32
|
+
var X = logo(siX);
|
|
15
33
|
|
|
16
34
|
// src/facebook.ts
|
|
17
35
|
var version = () => process.env.FACEBOOK_GRAPH_VERSION ?? "v26.0";
|
|
@@ -50,6 +68,7 @@ var facebook = {
|
|
|
50
68
|
id: "facebook-page",
|
|
51
69
|
label: "Facebook Page",
|
|
52
70
|
category: "social",
|
|
71
|
+
icon: FACEBOOK,
|
|
53
72
|
description: {
|
|
54
73
|
en: "Posts to Facebook Pages you manage. Needs a Meta app: set META_APP_ID and META_APP_SECRET.",
|
|
55
74
|
th: "\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E44\u0E1B\u0E22\u0E31\u0E07\u0E40\u0E1E\u0E08 Facebook \u0E17\u0E35\u0E48\u0E04\u0E38\u0E13\u0E14\u0E39\u0E41\u0E25 \u0E15\u0E49\u0E2D\u0E07\u0E21\u0E35 Meta app: \u0E15\u0E31\u0E49\u0E07 META_APP_ID \u0E41\u0E25\u0E30 META_APP_SECRET"
|
|
@@ -145,14 +164,6 @@ ${message.link}` : message.text,
|
|
|
145
164
|
}
|
|
146
165
|
};
|
|
147
166
|
|
|
148
|
-
// src/info.ts
|
|
149
|
-
var INFO = {
|
|
150
|
-
name: "@easy-cms/plugin-social",
|
|
151
|
-
// The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
|
|
152
|
-
apiVersion: 1,
|
|
153
|
-
...true ? { version: "1.38.0" } : {}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
167
|
// src/line.ts
|
|
157
168
|
var api = () => (process.env.LINE_API_URL ?? "https://api.line.me").replace(/\/+$/, "");
|
|
158
169
|
var MAX_TEXT = 300;
|
|
@@ -237,6 +248,7 @@ var line = {
|
|
|
237
248
|
id: "line-oa",
|
|
238
249
|
label: "LINE OA",
|
|
239
250
|
category: "social",
|
|
251
|
+
icon: LINE,
|
|
240
252
|
description: {
|
|
241
253
|
en: "Broadcasts each post as a card to your LINE Official Account\u2019s followers",
|
|
242
254
|
th: "broadcast \u0E41\u0E15\u0E48\u0E25\u0E30\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E1B\u0E47\u0E19\u0E01\u0E32\u0E23\u0E4C\u0E14\u0E16\u0E36\u0E07\u0E1C\u0E39\u0E49\u0E15\u0E34\u0E14\u0E15\u0E32\u0E21 LINE Official Account \u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13"
|
|
@@ -304,11 +316,55 @@ var line = {
|
|
|
304
316
|
}
|
|
305
317
|
};
|
|
306
318
|
|
|
319
|
+
// src/plugin.ts
|
|
320
|
+
import {
|
|
321
|
+
definePlugin
|
|
322
|
+
} from "@easy-cms/core";
|
|
323
|
+
|
|
324
|
+
// src/info.ts
|
|
325
|
+
var INFO = {
|
|
326
|
+
name: "@easy-cms/plugin-social",
|
|
327
|
+
// The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
|
|
328
|
+
apiVersion: 1,
|
|
329
|
+
...true ? { version: "1.39.0" } : {}
|
|
330
|
+
};
|
|
331
|
+
|
|
307
332
|
// src/shared.ts
|
|
308
333
|
var CHANNELS_PATH = "/social/channels";
|
|
309
334
|
var ACTION_PATH = "/social/post";
|
|
310
335
|
var AI_PATH = "/social/ai";
|
|
311
336
|
var SHARE_PATH = "/social/share";
|
|
337
|
+
var X_MAX = 280;
|
|
338
|
+
var LIGHT = [
|
|
339
|
+
[0, 4351],
|
|
340
|
+
[8192, 8205],
|
|
341
|
+
[8208, 8223],
|
|
342
|
+
[8242, 8247]
|
|
343
|
+
];
|
|
344
|
+
function xLength(text) {
|
|
345
|
+
let total = 0;
|
|
346
|
+
for (const part of text.split(/(https?:\/\/\S+)/)) {
|
|
347
|
+
if (/^https?:\/\//.test(part)) {
|
|
348
|
+
total += 23;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const graphemes = typeof Intl.Segmenter === "function" ? Array.from(
|
|
352
|
+
new Intl.Segmenter(void 0, { granularity: "grapheme" }).segment(part),
|
|
353
|
+
(g) => g.segment
|
|
354
|
+
) : [...part];
|
|
355
|
+
for (const grapheme of graphemes) {
|
|
356
|
+
if (/\p{Extended_Pictographic}/u.test(grapheme)) {
|
|
357
|
+
total += 2;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
for (const char of grapheme) {
|
|
361
|
+
const code = char.codePointAt(0) ?? 0;
|
|
362
|
+
total += LIGHT.some(([from, to]) => code >= from && code <= to) ? 1 : 2;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return total;
|
|
367
|
+
}
|
|
312
368
|
|
|
313
369
|
// src/webhook.ts
|
|
314
370
|
import { createHmac, randomUUID } from "crypto";
|
|
@@ -356,8 +412,8 @@ var webhook = {
|
|
|
356
412
|
label: { en: "Webhook (social)", th: "Webhook (\u0E42\u0E0B\u0E40\u0E0A\u0E35\u0E22\u0E25)" },
|
|
357
413
|
category: "social",
|
|
358
414
|
description: {
|
|
359
|
-
en: "Posts go to a URL as signed JSON: a Make, n8n or Zapier scenario that posts to
|
|
360
|
-
th: "\u0E2A\u0E48\u0E07\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E1B\u0E47\u0E19 JSON \u0E17\u0E35\u0E48\u0E25\u0E07\u0E25\u0E32\u0E22\u0E40\u0E0B\u0E47\u0E19\u0E44\u0E1B\u0E22\u0E31\u0E07 URL \u0E40\u0E0A\u0E48\u0E19 scenario \u0E02\u0E2D\u0E07 Make, n8n \u0E2B\u0E23\u0E37\u0E2D Zapier \u0E17\u0E35\u0E48\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E48\u0E2D\u0E44\u0E1B\u0E22\u0E31\u0E07
|
|
415
|
+
en: "Posts go to a URL as signed JSON: a Make, n8n or Zapier scenario that posts to LinkedIn, Threads, Discord or anywhere else",
|
|
416
|
+
th: "\u0E2A\u0E48\u0E07\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E1B\u0E47\u0E19 JSON \u0E17\u0E35\u0E48\u0E25\u0E07\u0E25\u0E32\u0E22\u0E40\u0E0B\u0E47\u0E19\u0E44\u0E1B\u0E22\u0E31\u0E07 URL \u0E40\u0E0A\u0E48\u0E19 scenario \u0E02\u0E2D\u0E07 Make, n8n \u0E2B\u0E23\u0E37\u0E2D Zapier \u0E17\u0E35\u0E48\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E48\u0E2D\u0E44\u0E1B\u0E22\u0E31\u0E07 LinkedIn, Threads, Discord \u0E2B\u0E23\u0E37\u0E2D\u0E17\u0E35\u0E48\u0E2D\u0E37\u0E48\u0E19"
|
|
361
417
|
},
|
|
362
418
|
icon: "link",
|
|
363
419
|
docs: "https://easycms.dev/docs/social",
|
|
@@ -388,6 +444,168 @@ var webhook = {
|
|
|
388
444
|
send: (values, message) => post(values, { ...message })
|
|
389
445
|
};
|
|
390
446
|
|
|
447
|
+
// src/x.ts
|
|
448
|
+
import { createHmac as createHmac2, randomBytes } from "crypto";
|
|
449
|
+
var api2 = () => (process.env.X_API_URL ?? "https://api.x.com").replace(/\/+$/, "");
|
|
450
|
+
var COST = { post: "$0.015", withLink: "$0.20" };
|
|
451
|
+
var enc = (value) => encodeURIComponent(value).replace(
|
|
452
|
+
/[!'()*]/g,
|
|
453
|
+
(c2) => `%${c2.charCodeAt(0).toString(16).toUpperCase()}`
|
|
454
|
+
);
|
|
455
|
+
function oauthHeader(method, url, keys, now = Math.floor(Date.now() / 1e3), nonce = randomBytes(16).toString("hex")) {
|
|
456
|
+
const target = new URL(url);
|
|
457
|
+
const oauth = {
|
|
458
|
+
oauth_consumer_key: keys.apiKey ?? "",
|
|
459
|
+
oauth_nonce: nonce,
|
|
460
|
+
oauth_signature_method: "HMAC-SHA1",
|
|
461
|
+
oauth_timestamp: String(now),
|
|
462
|
+
oauth_token: keys.accessToken ?? "",
|
|
463
|
+
oauth_version: "1.0"
|
|
464
|
+
};
|
|
465
|
+
const params = [...Object.entries(oauth), ...target.searchParams.entries()].map(([k, v]) => [enc(k), enc(v)]).sort(([a2, x2], [b, y]) => a2 === b ? x2 < y ? -1 : 1 : a2 < b ? -1 : 1).map(([k, v]) => `${k}=${v}`).join("&");
|
|
466
|
+
const base = [method.toUpperCase(), enc(`${target.origin}${target.pathname}`), enc(params)].join(
|
|
467
|
+
"&"
|
|
468
|
+
);
|
|
469
|
+
const signature = createHmac2(
|
|
470
|
+
"sha1",
|
|
471
|
+
`${enc(keys.apiSecret ?? "")}&${enc(keys.accessTokenSecret ?? "")}`
|
|
472
|
+
).update(base).digest("base64");
|
|
473
|
+
return `OAuth ${Object.entries({ ...oauth, oauth_signature: signature }).map(([k, v]) => `${enc(k)}="${enc(v)}"`).join(", ")}`;
|
|
474
|
+
}
|
|
475
|
+
function hint(status, message) {
|
|
476
|
+
if (status === 401)
|
|
477
|
+
return " (check the four keys: making new ones in the X console replaces the old ones)";
|
|
478
|
+
if (status === 402) return " (add credits in console.x.com)";
|
|
479
|
+
if (status === 403 && !/duplicate/i.test(message))
|
|
480
|
+
return " (the app needs Read and Write permission: set it, then make the access token again)";
|
|
481
|
+
return "";
|
|
482
|
+
}
|
|
483
|
+
async function call3(path, keys, body) {
|
|
484
|
+
for (const key of ["apiKey", "apiSecret", "accessToken", "accessTokenSecret"])
|
|
485
|
+
if (!keys[key]) throw new ChannelError(`X: ${key} is missing`, true);
|
|
486
|
+
const url = `${api2()}${path}`;
|
|
487
|
+
const method = body === void 0 ? "GET" : "POST";
|
|
488
|
+
let response;
|
|
489
|
+
try {
|
|
490
|
+
response = await fetch(url, {
|
|
491
|
+
method,
|
|
492
|
+
headers: {
|
|
493
|
+
authorization: oauthHeader(method, url, keys),
|
|
494
|
+
...body === void 0 ? {} : { "content-type": "application/json" }
|
|
495
|
+
},
|
|
496
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) },
|
|
497
|
+
signal: AbortSignal.timeout(3e4)
|
|
498
|
+
});
|
|
499
|
+
} catch (error) {
|
|
500
|
+
throw new ChannelError(`X could not be reached: ${error.message}`);
|
|
501
|
+
}
|
|
502
|
+
const answer = await response.json().catch(() => ({}));
|
|
503
|
+
if (!response.ok || !answer.data && answer.errors?.length) {
|
|
504
|
+
const said = answer.detail ?? answer.errors?.[0]?.detail ?? answer.errors?.[0]?.message ?? answer.title ?? `HTTP ${response.status}`;
|
|
505
|
+
throw new ChannelError(
|
|
506
|
+
`X: ${said}${hint(response.status, said)}`,
|
|
507
|
+
response.status !== 429 && response.status < 500
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
return answer.data ?? {};
|
|
511
|
+
}
|
|
512
|
+
var usernames = /* @__PURE__ */ new Map();
|
|
513
|
+
async function username(keys) {
|
|
514
|
+
const known = usernames.get(keys.accessToken ?? "");
|
|
515
|
+
if (known) return known;
|
|
516
|
+
const me = await call3("/2/users/me", keys);
|
|
517
|
+
if (me.username) usernames.set(keys.accessToken ?? "", me.username);
|
|
518
|
+
return me.username ?? null;
|
|
519
|
+
}
|
|
520
|
+
function postText(message) {
|
|
521
|
+
const text = message.text.trim();
|
|
522
|
+
const full = [text, message.link].filter(Boolean).join("\n\n");
|
|
523
|
+
if (xLength(full) > X_MAX)
|
|
524
|
+
throw new ChannelError(
|
|
525
|
+
`X: the post is ${xLength(full)} of ${X_MAX} characters (a link counts 23): shorten X\u2019s text`,
|
|
526
|
+
true
|
|
527
|
+
);
|
|
528
|
+
return full;
|
|
529
|
+
}
|
|
530
|
+
var x = {
|
|
531
|
+
// The text beside a link: 280, less the link (23) and the blank line before it.
|
|
532
|
+
maxLength: X_MAX - 25,
|
|
533
|
+
counting: "x",
|
|
534
|
+
integration: {
|
|
535
|
+
id: "x",
|
|
536
|
+
label: "X",
|
|
537
|
+
category: "social",
|
|
538
|
+
icon: X,
|
|
539
|
+
description: {
|
|
540
|
+
en: "Posts to your X account through your own X app. X charges for each post.",
|
|
541
|
+
th: "\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E44\u0E1B\u0E22\u0E31\u0E07\u0E1A\u0E31\u0E0D\u0E0A\u0E35 X \u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13\u0E1C\u0E48\u0E32\u0E19\u0E41\u0E2D\u0E1B X \u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13\u0E40\u0E2D\u0E07 X \u0E04\u0E34\u0E14\u0E40\u0E07\u0E34\u0E19\u0E15\u0E48\u0E2D\u0E42\u0E1E\u0E2A\u0E15\u0E4C"
|
|
542
|
+
},
|
|
543
|
+
docs: "https://easycms.dev/docs/social#x",
|
|
544
|
+
plugin: "@easy-cms/plugin-social",
|
|
545
|
+
kind: "fields",
|
|
546
|
+
fields: [
|
|
547
|
+
{
|
|
548
|
+
name: "apiKey",
|
|
549
|
+
label: { en: "API key", th: "API key" },
|
|
550
|
+
description: {
|
|
551
|
+
en: "console.x.com \u2192 your app \u2192 Keys and tokens \u2192 OAuth 1.0 keys: Consumer key.",
|
|
552
|
+
th: "console.x.com \u2192 \u0E41\u0E2D\u0E1B\u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13 \u2192 Keys and tokens \u2192 OAuth 1.0 keys: Consumer key"
|
|
553
|
+
},
|
|
554
|
+
required: true
|
|
555
|
+
},
|
|
556
|
+
{
|
|
557
|
+
name: "apiSecret",
|
|
558
|
+
label: { en: "API secret", th: "API secret" },
|
|
559
|
+
secret: true,
|
|
560
|
+
required: true
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
name: "accessToken",
|
|
564
|
+
label: { en: "Access token", th: "Access token" },
|
|
565
|
+
description: {
|
|
566
|
+
en: "Same page \u2192 Access token: made after the app has Read and Write permission.",
|
|
567
|
+
th: "\u0E2B\u0E19\u0E49\u0E32\u0E40\u0E14\u0E35\u0E22\u0E27\u0E01\u0E31\u0E19 \u2192 Access token: \u0E2A\u0E23\u0E49\u0E32\u0E07\u0E2B\u0E25\u0E31\u0E07\u0E15\u0E31\u0E49\u0E07\u0E41\u0E2D\u0E1B\u0E40\u0E1B\u0E47\u0E19 Read and Write \u0E41\u0E25\u0E49\u0E27"
|
|
568
|
+
},
|
|
569
|
+
secret: true,
|
|
570
|
+
required: true
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
name: "accessTokenSecret",
|
|
574
|
+
label: { en: "Access token secret", th: "Access token secret" },
|
|
575
|
+
secret: true,
|
|
576
|
+
required: true
|
|
577
|
+
}
|
|
578
|
+
],
|
|
579
|
+
env: {
|
|
580
|
+
apiKey: "X_API_KEY",
|
|
581
|
+
apiSecret: "X_API_SECRET",
|
|
582
|
+
accessToken: "X_ACCESS_TOKEN",
|
|
583
|
+
accessTokenSecret: "X_ACCESS_TOKEN_SECRET"
|
|
584
|
+
},
|
|
585
|
+
async test(values) {
|
|
586
|
+
try {
|
|
587
|
+
const me = await call3("/2/users/me", values);
|
|
588
|
+
if (me.username) usernames.set(values.accessToken ?? "", me.username);
|
|
589
|
+
return { ok: true, account: me.username ? `@${me.username}` : me.name ?? "X" };
|
|
590
|
+
} catch (error) {
|
|
591
|
+
return { ok: false, message: error.message };
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
// X's API has no balance to read: what this post costs.
|
|
596
|
+
async preview(_values, draft) {
|
|
597
|
+
return {
|
|
598
|
+
info: draft?.link ? `X API: about ${COST.withLink} for a post with a link` : `X API: about ${COST.post} a post`
|
|
599
|
+
};
|
|
600
|
+
},
|
|
601
|
+
async send(values, message) {
|
|
602
|
+
const post2 = await call3("/2/tweets", values, { text: postText(message) });
|
|
603
|
+
if (!post2.id) return { url: null };
|
|
604
|
+
const name = await username(values).catch(() => null);
|
|
605
|
+
return { url: `https://x.com/${name ?? "i/web"}/status/${post2.id}` };
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
|
|
391
609
|
// src/plugin.ts
|
|
392
610
|
var RETRIES = [6e4, 5 * 6e4, 30 * 6e4];
|
|
393
611
|
var LEASE = 2 * 6e4;
|
|
@@ -396,16 +614,16 @@ var nobody = () => false;
|
|
|
396
614
|
var later = (from, ms) => new Date(from.getTime() + ms).toISOString();
|
|
397
615
|
var json = (status, message) => Response.json({ errors: [{ message }] }, { status });
|
|
398
616
|
function socialPlugin(options = {}) {
|
|
399
|
-
const channels = options.channels ?? [webhook, facebook, line];
|
|
617
|
+
const channels = options.channels ?? [webhook, facebook, line, x];
|
|
400
618
|
const slug = options.slug ?? "social-posts";
|
|
401
619
|
const sources = options.collections ?? [];
|
|
402
620
|
return definePlugin((config) => {
|
|
403
|
-
if (config.collections?.some((
|
|
621
|
+
if (config.collections?.some((c2) => c2.slug === slug))
|
|
404
622
|
throw new Error(`socialPlugin: there is already a collection "${slug}"; set \`slug\``);
|
|
405
|
-
const missing = sources.filter((s) => !config.collections?.some((
|
|
623
|
+
const missing = sources.filter((s) => !config.collections?.some((c2) => c2.slug === s));
|
|
406
624
|
if (missing.length > 0)
|
|
407
625
|
throw new Error(`socialPlugin: unknown collection ${missing.map((s) => `"${s}"`).join(", ")}`);
|
|
408
|
-
const channelOf = (connection) => channels.find((
|
|
626
|
+
const channelOf = (connection) => channels.find((c2) => connection.startsWith(`${c2.integration.id}:`));
|
|
409
627
|
const nameOf = (connection) => connection.slice(connection.indexOf(":") + 1);
|
|
410
628
|
const fields = [
|
|
411
629
|
{ name: "title", type: "text", required: true, label: { en: "Title", th: "\u0E0A\u0E37\u0E48\u0E2D" } },
|
|
@@ -546,9 +764,9 @@ function socialPlugin(options = {}) {
|
|
|
546
764
|
(r) => typeof r.connection === "string" && r.connection
|
|
547
765
|
);
|
|
548
766
|
const nextAtOf = (delivery) => {
|
|
549
|
-
const times = Object.values(delivery.channels).filter((
|
|
550
|
-
(
|
|
551
|
-
).filter((
|
|
767
|
+
const times = Object.values(delivery.channels).filter((c2) => c2.state === "sending" || c2.state === "scheduled").map(
|
|
768
|
+
(c2) => c2.state === "scheduled" ? delivery.postAt ?? null : c2.nextAttemptAt ?? null
|
|
769
|
+
).filter((t2) => !!t2).sort();
|
|
552
770
|
return times[0] ?? null;
|
|
553
771
|
};
|
|
554
772
|
const save = (cms, id, delivery, where) => {
|
|
@@ -572,7 +790,7 @@ function socialPlugin(options = {}) {
|
|
|
572
790
|
return null;
|
|
573
791
|
};
|
|
574
792
|
const isLive = async (cms, ref) => {
|
|
575
|
-
const config2 = cms.config.collections.find((
|
|
793
|
+
const config2 = cms.config.collections.find((c2) => c2.slug === ref.collection);
|
|
576
794
|
if (!config2?.drafts) return true;
|
|
577
795
|
const live = await cms.findById(ref.collection, ref.id, { depth: 0 });
|
|
578
796
|
return !!live && live.status === "published";
|
|
@@ -698,7 +916,8 @@ function socialPlugin(options = {}) {
|
|
|
698
916
|
id: `${id}:${conn.name}`,
|
|
699
917
|
label: `${name} \xB7 ${conn.name}`,
|
|
700
918
|
...icon ? { icon } : {},
|
|
701
|
-
...channel.maxLength ? { maxLength: channel.maxLength } : {}
|
|
919
|
+
...channel.maxLength ? { maxLength: channel.maxLength } : {},
|
|
920
|
+
...channel.counting ? { counting: channel.counting } : {}
|
|
702
921
|
});
|
|
703
922
|
}
|
|
704
923
|
}
|
|
@@ -739,7 +958,9 @@ function socialPlugin(options = {}) {
|
|
|
739
958
|
const conn = await cms.integrations.get(channel.integration.id, {
|
|
740
959
|
name: nameOf(connection)
|
|
741
960
|
});
|
|
742
|
-
|
|
961
|
+
const link = typeof doc2.link === "string" && doc2.link ? doc2.link : null;
|
|
962
|
+
if (conn)
|
|
963
|
+
notes[connection] = await channel.preview(conn.values, { link }).catch(() => ({}));
|
|
743
964
|
}
|
|
744
965
|
return { problems, notes };
|
|
745
966
|
}
|
|
@@ -750,20 +971,20 @@ function socialPlugin(options = {}) {
|
|
|
750
971
|
const delivery = deliveryOf(doc);
|
|
751
972
|
const now = /* @__PURE__ */ new Date();
|
|
752
973
|
const action = input.action;
|
|
753
|
-
const open = rows.map((r) => r.connection).filter((
|
|
974
|
+
const open = rows.map((r) => r.connection).filter((c2) => delivery.channels[c2]?.state !== "posted");
|
|
754
975
|
if (action === "now" || action === "schedule" || action === "retry") {
|
|
755
976
|
if (rows.length === 0) return json(400, "Add a channel first");
|
|
756
977
|
const image = await imageOf(cms, doc.image);
|
|
757
978
|
if ("error" in image) return json(400, image.error);
|
|
758
|
-
for (const
|
|
759
|
-
if (!channelOf(
|
|
979
|
+
for (const c2 of open)
|
|
980
|
+
if (!channelOf(c2)) return json(400, `"${c2}" is not a channel this site has`);
|
|
760
981
|
}
|
|
761
982
|
if (action === "now" || action === "retry") {
|
|
762
|
-
const targets = action === "retry" ? open.filter((
|
|
983
|
+
const targets = action === "retry" ? open.filter((c2) => c2 === input.channel && delivery.channels[c2]?.state === "failed") : open.filter((c2) => delivery.channels[c2]?.state !== "sending");
|
|
763
984
|
if (targets.length === 0)
|
|
764
985
|
return json(400, action === "retry" ? "Nothing to retry" : "Nothing left to post");
|
|
765
|
-
for (const
|
|
766
|
-
delivery.channels[
|
|
986
|
+
for (const c2 of targets)
|
|
987
|
+
delivery.channels[c2] = {
|
|
767
988
|
state: "sending",
|
|
768
989
|
attempts: 0,
|
|
769
990
|
error: null,
|
|
@@ -780,8 +1001,8 @@ function socialPlugin(options = {}) {
|
|
|
780
1001
|
if (!postAt || Number.isNaN(postAt.getTime()) || postAt <= now)
|
|
781
1002
|
return json(400, "postAt must be a time in the future");
|
|
782
1003
|
delivery.postAt = postAt.toISOString();
|
|
783
|
-
for (const
|
|
784
|
-
delivery.channels[
|
|
1004
|
+
for (const c2 of open)
|
|
1005
|
+
delivery.channels[c2] = {
|
|
785
1006
|
state: "scheduled",
|
|
786
1007
|
attempts: 0,
|
|
787
1008
|
error: null,
|
|
@@ -792,9 +1013,9 @@ function socialPlugin(options = {}) {
|
|
|
792
1013
|
}
|
|
793
1014
|
if (action === "cancel") {
|
|
794
1015
|
delivery.postAt = null;
|
|
795
|
-
for (const [
|
|
1016
|
+
for (const [c2, state] of Object.entries(delivery.channels))
|
|
796
1017
|
if (state.state === "scheduled")
|
|
797
|
-
delivery.channels[
|
|
1018
|
+
delivery.channels[c2] = { state: "waiting", attempts: 0 };
|
|
798
1019
|
await save(cms, id, delivery);
|
|
799
1020
|
return { delivery };
|
|
800
1021
|
}
|
|
@@ -816,7 +1037,7 @@ function socialPlugin(options = {}) {
|
|
|
816
1037
|
const parts = [];
|
|
817
1038
|
const ref = sourceOf(form);
|
|
818
1039
|
if (ref) {
|
|
819
|
-
const config2 = cms.config.collections.find((
|
|
1040
|
+
const config2 = cms.config.collections.find((c2) => c2.slug === ref.collection);
|
|
820
1041
|
const source2 = await cms.findById(ref.collection, ref.id, {
|
|
821
1042
|
depth: 0,
|
|
822
1043
|
draft: true,
|
|
@@ -835,6 +1056,7 @@ function socialPlugin(options = {}) {
|
|
|
835
1056
|
const channel = typeof input.channel === "string" ? input.channel : null;
|
|
836
1057
|
const adapter = channel ? channelOf(channel) : void 0;
|
|
837
1058
|
const max = adapter?.maxLength ?? 500;
|
|
1059
|
+
const measure = adapter?.counting === "x" ? xLength : (value) => [...value].length;
|
|
838
1060
|
const where = channel ? `a post on ${channel.slice(0, channel.indexOf(":")) || "social media"}` : "a social media post";
|
|
839
1061
|
try {
|
|
840
1062
|
const result = await ai.generate(
|
|
@@ -842,7 +1064,7 @@ function socialPlugin(options = {}) {
|
|
|
842
1064
|
feature: "social",
|
|
843
1065
|
system: [
|
|
844
1066
|
`You write ${where} that makes people want to read the page it shares.`,
|
|
845
|
-
`At most ${max} characters, plain text, no hashtags unless they fit, no link (it is added`,
|
|
1067
|
+
`At most ${max} characters${adapter?.counting === "x" ? " (an emoji counts as two)" : ""}, plain text, no hashtags unless they fit, no link (it is added`,
|
|
846
1068
|
`separately). Write it in ${ai.languageOf(null)}. Answer with the post only.`
|
|
847
1069
|
].join(" "),
|
|
848
1070
|
prompt: `The page:
|
|
@@ -853,8 +1075,11 @@ ${source.slice(0, 6e3)}`,
|
|
|
853
1075
|
{ user, ...id !== null ? { about: { collection: slug, id } } : {} }
|
|
854
1076
|
);
|
|
855
1077
|
let text = result.text.trim().replace(/^["“]+|["”]+$/g, "");
|
|
856
|
-
if (
|
|
857
|
-
|
|
1078
|
+
if (measure(text) > max) {
|
|
1079
|
+
const chars = [...text];
|
|
1080
|
+
while (chars.length && measure(`${chars.join("").trimEnd()}\u2026`) > max) chars.pop();
|
|
1081
|
+
text = `${chars.join("").trimEnd()}\u2026`;
|
|
1082
|
+
}
|
|
858
1083
|
return { text };
|
|
859
1084
|
} catch (e) {
|
|
860
1085
|
const status = e.status;
|
|
@@ -882,7 +1107,7 @@ ${source.slice(0, 6e3)}`,
|
|
|
882
1107
|
});
|
|
883
1108
|
if (!doc) return json(404, "No such document");
|
|
884
1109
|
const values = options.share ? await options.share({ doc, collection, cms }) : defaults(
|
|
885
|
-
cms.config.collections.find((
|
|
1110
|
+
cms.config.collections.find((c2) => c2.slug === collection),
|
|
886
1111
|
doc
|
|
887
1112
|
);
|
|
888
1113
|
const link = values.link ?? options.url?.({ doc, collection }) ?? null;
|
|
@@ -919,14 +1144,14 @@ ${source.slice(0, 6e3)}`,
|
|
|
919
1144
|
for (const doc of due.docs) await sendDue(cms, doc, now);
|
|
920
1145
|
}
|
|
921
1146
|
};
|
|
922
|
-
const withShare = (
|
|
923
|
-
...
|
|
1147
|
+
const withShare = (c2) => sources.includes(c2.slug) ? {
|
|
1148
|
+
...c2,
|
|
924
1149
|
admin: {
|
|
925
|
-
...
|
|
926
|
-
sidebar: [...
|
|
1150
|
+
...c2.admin,
|
|
1151
|
+
sidebar: [...c2.admin?.sidebar ?? [], { tag: "ecms-social-share" }]
|
|
927
1152
|
}
|
|
928
|
-
} :
|
|
929
|
-
const integrations = channels.map((
|
|
1153
|
+
} : c2;
|
|
1154
|
+
const integrations = channels.map((c2) => c2.integration);
|
|
930
1155
|
return {
|
|
931
1156
|
...config,
|
|
932
1157
|
collections: [...(config.collections ?? []).map(withShare), posts],
|
|
@@ -997,6 +1222,9 @@ function defaults(collection, doc) {
|
|
|
997
1222
|
}
|
|
998
1223
|
export {
|
|
999
1224
|
ChannelError,
|
|
1225
|
+
facebook,
|
|
1226
|
+
line,
|
|
1000
1227
|
socialPlugin,
|
|
1001
|
-
webhook
|
|
1228
|
+
webhook,
|
|
1229
|
+
x
|
|
1002
1230
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-social",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.0",
|
|
4
4
|
"description": "Social posts for Easy CMS: write a post once and send it to several channels (webhooks to Make, n8n or Zapier first), with schedules, retries and a status per channel",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -17,12 +17,13 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@easy-cms/core": "^1.
|
|
20
|
+
"@easy-cms/core": "^1.39.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"happy-dom": "^21.0.4",
|
|
24
|
-
"
|
|
25
|
-
"@easy-cms/
|
|
24
|
+
"simple-icons": "16.36.0",
|
|
25
|
+
"@easy-cms/core": "^1.39.0",
|
|
26
|
+
"@easy-cms/db-sqlite": "^1.39.0"
|
|
26
27
|
},
|
|
27
28
|
"engines": {
|
|
28
29
|
"node": ">=22.12"
|