60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
import { MastodonStream } from "../channel.js";
|
|
import { NoteConverter } from "../../converters/note.js";
|
|
import { NoteHelpers } from "../../helpers/note.js";
|
|
export class MastodonStreamDirect extends MastodonStream {
|
|
static shouldShare = true;
|
|
static requireCredential = true;
|
|
static requiredScopes = [
|
|
'read:statuses'
|
|
];
|
|
constructor(connection, name){
|
|
super(connection, name);
|
|
this.onNote = this.onNote.bind(this);
|
|
this.onNoteEvent = this.onNoteEvent.bind(this);
|
|
}
|
|
get user() {
|
|
return this.connection.user;
|
|
}
|
|
async init() {
|
|
this.subscriber.on("notesStream", this.onNote);
|
|
this.subscriber.on("noteUpdatesStream", this.onNoteEvent);
|
|
}
|
|
async onNote(note) {
|
|
if (!this.shouldProcessNote(note)) return;
|
|
NoteConverter.encodeEvent(note, this.user).then((encoded)=>{
|
|
this.connection.send(this.chName, "update", encoded);
|
|
});
|
|
NoteHelpers.getConversationFromEvent(note.id, this.user).then((conversation)=>{
|
|
this.connection.send(this.chName, "conversation", conversation);
|
|
});
|
|
}
|
|
async onNoteEvent(data) {
|
|
const note = data.body;
|
|
if (!this.shouldProcessNote(note)) return;
|
|
NoteHelpers.getConversationFromEvent(note.id, this.user).then((conversation)=>{
|
|
this.connection.send(this.chName, "conversation", conversation);
|
|
});
|
|
switch(data.type){
|
|
case "updated":
|
|
NoteConverter.encodeEvent(note, this.user).then((encoded)=>{
|
|
this.connection.send(this.chName, "status.update", encoded);
|
|
});
|
|
break;
|
|
case "deleted":
|
|
this.connection.send(this.chName, "delete", note.id);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
shouldProcessNote(note) {
|
|
if (note.visibility !== "specified") return false;
|
|
if (note.userId !== this.user.id && !note.visibleUserIds?.includes(this.user.id)) return false;
|
|
return true;
|
|
}
|
|
dispose() {
|
|
this.subscriber.off("notesStream", this.onNote);
|
|
this.subscriber.off("noteUpdatesStream", this.onNoteEvent);
|
|
}
|
|
}
|