Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 162x 162x 162x 162x 162x 162x 56x 33x 1x 26x 24x 24x 16x 8x 12x 7x 3x 3x 3x 3x 3x 3x 4x 4x 23x 23x 18x 10x 6x 12x 162x | import { filter, maxBy, uniq } from 'lodash-es';
import { DateTime } from 'luxon';
import { Plugin } from '../model/plugin';
import { Ref } from '../model/ref';
import { Mod } from '../model/tag';
import { Template } from '../model/template';
import { userAuthors } from '../util/format';
import {
access,
hasPrefix,
hasTag,
localTag,
prefix,
removeParentOrigin,
removePrefix,
setPublic,
subOrigin,
tagOrigin
} from '../util/tag';
export const dmTemplate: Template = {
tag: 'dm',
name: $localize`📨️ DM`,
config: {
mod: $localize`📮️ Mailbox`,
version: 1,
default: true,
genId: true,
internal: true,
reply: ['dm', 'internal', 'plugin/thread'],
generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
description: $localize`Adds dms tab to inbox. Adds buttons to create private direct messages with users.`,
aiInstructions: `# dm
The dm tag indicates the Ref represents a private direct message.`,
icons: [
{ tag: '!public', thumbnail: $localize`📨️`, title: $localize`Direct Message`, order: 1 },
{ tag: 'public', thumbnail: $localize`📝️`, title: $localize`Memo`, order: 1 },
{ tag: 'internal', thumbnail: $localize`📨️`, title: $localize`DM Thread`, order: 2 },
],
filters: [
{ query: 'dm', label: $localize`📨️ dm`, title: $localize`DMs`, group: $localize`Templates 🎨️` },
],
},
};
export const inboxPlugin: Plugin = {
tag: 'plugin/inbox',
name: $localize`✉️ Inbox`,
config: {
mod: $localize`📮️ Mailbox`,
version: 1,
default: true,
generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
description: $localize`'The inbox plugin allow sending notifications to another user
on the same server.'`,
readAccess: ['plugin/inbox'],
},
};
export const outboxPlugin: Plugin = {
tag: 'plugin/outbox',
name: $localize`📬️ Outbox`,
config: {
mod: $localize`📮️ Mailbox`,
version: 1,
default: true,
generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
description: $localize`The outbox plugin allow sending notifications to another user on a remote server.`,
icons: [{ label: $localize`📬️`, title: $localize`Notifications in outbox`, order: -2 }],
filters: [
{ query: 'plugin/outbox', label: $localize`📬️ outbox`, title: $localize`Messages from another server`, group: $localize`Plugins 🧰️` },
],
},
};
export function isMailbox(tag: string) {
return tag.startsWith('plugin/inbox') ||
tag.startsWith('plugin/outbox');
}
export function notifications(ref: Ref): string[] {
return filter(ref.tags || [], isMailbox);
}
export function addressedTo(ref: Ref): string[] {
return notifications(ref).map(mailbox => getUser(mailbox, ref.origin || '')).filter(u => u) as string[];
}
export function getUser(mailbox: string, localOrigin: string): string | undefined {
const tag = getMailboxTag(mailbox, localOrigin);
if (!tag) return tag;
if (mailbox.startsWith('_')) return '_' + tag;
if (tag == 'user') return '+user';
if (tag.startsWith('user/')) return '+' + tag;
return tag;
}
export function getMailboxTag(mailbox: string, localOrigin: string): string | undefined {
if (mailbox.startsWith('_plugin/inbox/')) return mailbox.substring('_plugin/inbox/'.length) + localOrigin;
if (mailbox.startsWith('plugin/inbox/')) return mailbox.substring('plugin/inbox/'.length) + localOrigin;
if (mailbox.startsWith('_plugin/outbox/')) return reverseOrigin(mailbox.substring('_plugin/outbox/'.length), localOrigin);
if (mailbox.startsWith('plugin/outbox/')) return reverseOrigin(mailbox.substring('plugin/outbox/'.length), localOrigin);
if (mailbox.startsWith('_plugin/from/')) return reverseOrigin(mailbox.substring('_plugin/from/'.length), localOrigin);
if (mailbox.startsWith('plugin/from/')) return reverseOrigin(mailbox.substring('plugin/from/'.length), localOrigin);
return undefined;
}
/**
* Convert from reverse origin syntax (origin/tag) to qualified tag syntax (tag@origin).
*/
export function reverseOrigin(tag: string, rootOrigin: string): string {
let prefix = access(tag);
if (prefix) tag = tag.substring(1);
const len = tag.indexOf('/');
return prefix + tag.substring(len + 1) + subOrigin(rootOrigin, tag.substring(0, len));
}
export function getMailbox(tag: string, local: string): string {
if (hasPrefix(tag, 'plugin/inbox') || hasPrefix(tag, 'plugin/outbox')) return localTag(tag);
const origin = tagOrigin(tag);
if (!origin || origin === local) {
return setPublic(prefix('plugin/inbox', localTag(tag)));
} else {
return setPublic(prefix(`plugin/outbox/${removeParentOrigin(origin, local).substring(1)}`, localTag(tag)));
}
}
export function getLocalMailbox(mailbox: string, local: string, origin: string, lookup?: Map<string, Map<string, string>>) {
if (!origin || origin === local) return localTag(mailbox);
if (hasPrefix(mailbox, 'plugin/outbox')) {
Iif (!lookup?.has(origin)) {
console.warn('Cannot lookup mailbox translation for', origin);
return undefined;
}
const remote = '@' + mailbox.split('/')[2];
Iif (!lookup.get(origin)!.has(remote)) {
console.warn('Cannot lookup mailbox translation for', origin, 'on remote', remote);
return undefined;
}
const mapped = lookup.get(origin)!.get(remote);
Iif (!mapped || mapped === local) {
return `plugin/inbox/${removePrefix(mailbox, 3)}`;
}
return `plugin/outbox/${mapped.substring(1)}/${removePrefix(mailbox, 3)}`;
}
Eif (hasPrefix(mailbox, 'plugin/inbox')) {
return `plugin/outbox/${origin.substring(1)}/${removePrefix(mailbox, 2)}`;
}
throw 'not a mailbox';
}
export function mailboxes(ref: Ref, myUserTag: string, lookup?: Map<string, Map<string, string>>): string[] {
const local = tagOrigin(myUserTag);
return uniq([
...userAuthors(ref).filter(tag => tag !== myUserTag).map(tag => getMailbox(tag, local)),
...hasTag('public', ref)
? []
: userAuthors(ref)
.filter(tag => hasPrefix(tag, 'user') && (tagOrigin(tag) === local || !tagOrigin(tag)))
.map(tag => tag.startsWith('+') ? localTag(tag).substring(1) : localTag(tag)),
...notifications(ref).map(m => getLocalMailbox(m, local, ref.origin || '', lookup)).filter(t => !!t) as string[],
]);
}
export function newest(refs: Ref[]) {
return maxBy(refs, r => r.modified!.valueOf());
}
export const mailboxMod: Mod = {
plugin: [
inboxPlugin,
outboxPlugin,
],
template: [
dmTemplate,
],
};
|