Skip to content

Commit

Permalink
Fix Note / Question type
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jun 27, 2019
1 parent b7dfc71 commit 739d31a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/remote/activitypub/models/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s
const apEmojis = emojis.map(emoji => emoji.name);

const questionUri = note._misskey_question;
const poll = await extractPollFromQuestion(note._misskey_question || note).catch(() => undefined);
const poll = await extractPollFromQuestion(note._misskey_question || note, resolver).catch(() => undefined);

// ユーザーの情報が古かったらついでに更新しておく
if (actor.lastFetchedAt == null || Date.now() - actor.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
Expand Down
13 changes: 10 additions & 3 deletions src/remote/activitypub/models/question.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import config from '../../../config';
import Note, { IChoice, IPoll } from '../../../models/note';
import Resolver from '../resolver';
import { IQuestion } from '../type';
import { IObject, IQuestion, isQuestion, } from '../type';
import { apLogger } from '../logger';

export async function extractPollFromQuestion(source: string | IQuestion): Promise<IPoll> {
const question = typeof source === 'string' ? await new Resolver().resolve(source) as IQuestion : source;
export async function extractPollFromQuestion(source: string | IObject, resolver?: Resolver): Promise<IPoll> {
if (resolver == null) resolver = new Resolver();

const question = await resolver.resolve(source);

if (!isQuestion(question)) {
throw new Error('invalid type');
}

const multiple = !question.oneOf;
const expiresAt = question.endTime ? new Date(question.endTime) : null;

Expand Down
5 changes: 4 additions & 1 deletion src/remote/activitypub/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export interface IOrderedCollectionPage extends IObject {
export const validPost = ['Note', 'Question', 'Article', 'Audio', 'Document', 'Image', 'Page', 'Video'];

export interface INote extends IObject {
type: 'Note' | 'Question';
type: 'Note' | 'Question' | 'Article' | 'Audio' | 'Document' | 'Image' | 'Page' | 'Video';
_misskey_content: string;
_misskey_quote: string;
_misskey_question: string;
Expand All @@ -117,6 +117,9 @@ export interface IQuestion extends IObject {
endTime?: Date;
}

export const isQuestion = (object: IObject): object is IQuestion =>
object.type === 'Note' || object.type === 'Question';

interface IQuestionChoice {
name?: string;
replies?: ICollection;
Expand Down

0 comments on commit 739d31a

Please sign in to comment.