forked from logseq/logseq-plugin-samples
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
162 lines (140 loc) · 4.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import "@logseq/libs";
import { delay } from "./helpers";
import { loadPtnData, markItemSynced } from "./ptn";
import { format, sub } from "date-fns";
import { FeedItem, itemToNode, organizeFeedItems, PtnNode } from "ptn-helpers";
import { LSPluginBaseInfo, IBatchBlock } from "@logseq/libs/dist/LSPlugin.user";
import { settingSchema } from "./setting-schema";
/**
* main entry
* @param baseInfo
*/
function main(baseInfo: LSPluginBaseInfo) {
let loading = false;
const findOrCreateParentUid = async (
date: Date,
parentBlock: string,
dateFormat: string
): Promise<string> => {
let datePageUid;
const possibleExistingDatePage = await logseq.DB.datascriptQuery(`
[
:find (pull ?p [*])
:where
[?b :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(== ?d ${format(date, "yyyyMMdd")})]
]
`);
if (possibleExistingDatePage.length > 0) {
datePageUid = possibleExistingDatePage[0][0]["uuid"];
} else {
const datePageTitle = format(date, dateFormat);
const newDatePage = await window.logseq.Editor.createPage(
datePageTitle,
{},
{
journal: true,
redirect: false,
}
);
datePageUid = newDatePage!.uuid;
}
if (!parentBlock || parentBlock.length === 0) {
return datePageUid;
}
const existingBlockQuery: any[][] = (await logseq.DB.datascriptQuery(`
[
:find (pull ?b [*])
:where
[?b :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(== ?d ${format(date, "yyyyMMdd")})]
]
`)) ?? [[]];
const existingBlocks = existingBlockQuery.map((x) => x[0]);
const possibleExistingParentBlock = existingBlocks.filter(
(x) => x["content"].trim() === parentBlock.trim()
);
if (possibleExistingParentBlock.length > 0) {
const existingParentUuid = possibleExistingParentBlock[0]["uuid"];
return existingParentUuid;
}
const newParentBlock = await window.logseq.Editor.appendBlockInPage(datePageUid, parentBlock);
return newParentBlock!.uuid;
};
const getMessages = async (showNotifcation: boolean) => {
const info = await logseq.App.getUserConfigs();
if (loading) return;
loading = true;
try {
const dateFormat = info?.["preferredDateFormat"] || "MMM do, yyyy";
const ptnKey = baseInfo?.settings?.["ptn_key"];
const parentBlock: string = baseInfo?.settings?.["parent_block"] ?? "";
if (!ptnKey) {
logseq.UI.showMsg("ptn key not found. edit in plugin settings", "warning", {
timeout: 2500,
});
} else {
const items: FeedItem[] = await loadPtnData(ptnKey);
if (items.length === 0 && showNotifcation) {
logseq.UI.showMsg("no new items found", "success", { timeout: 1500 });
} else {
const organizedItems = organizeFeedItems(items, dateFormat);
for (const pageName of Object.keys(organizedItems)) {
for (const senderType of Object.keys(organizedItems[pageName])) {
const feedItems: FeedItem[] = organizedItems[pageName][senderType];
const date = new Date(feedItems[0].date_published),
parentUid = await findOrCreateParentUid(date, parentBlock, dateFormat),
batch = organizedItems[pageName][senderType]
.map((feedItem: FeedItem) => {
return itemToNode(feedItem, baseInfo?.settings?.["ptn_hashtag"] || "ptn");
})
.map((node: PtnNode): IBatchBlock => {
return {
content: node.text,
children: node.children.map((child) => ({
content: child.text,
})),
};
});
await logseq.Editor.insertBatchBlock(parentUid, batch, {
sibling: false,
});
}
}
}
items.forEach((item) => {
markItemSynced(item, ptnKey);
});
}
} catch (e) {
logseq.UI.showMsg(e.message, "warning");
console.error(e);
} finally {
loading = false;
}
};
logseq.provideModel({
async loadPtnAndNotify() {
getMessages(true);
},
});
logseq.App.registerUIItem("toolbar", {
key: "phonetonote-logseq",
template: `
<a data-on-click="loadPtnAndNotify"
class="button">
<i class="ti ti-device-mobile-message"></i>
</a>
`,
});
logseq.useSettingsSchema(settingSchema);
if (baseInfo?.settings?.["auto_sync"]) {
getMessages(false);
window.setInterval(() => getMessages(false), 1000 * 90);
}
}
logseq.ready(main).catch(console.error);