/*
    Agora Forum Software
    Copyright (C) 2016 Gregory Sartucci
    License: AGPL-3.0, Check file LICENSE
*/

import simplSchema from 'simpl-schema';

if (!this.Schema)
    this.Schema = {}; //If this.Schema hasn't been defined, make it an empty object.

this.Schema.Post = new SimpleSchema({ //The schema for posts.
    attributedTo: { //id to the Actor object that this post is attrubuted to.
        type: simplSchema.oneOf(String, Object, Array),
        regEx: SimpleSchema.RegEx.Url,
        blackbox: true,
        optional: true
    },
    summary: { //A short description of this post.
        type: String,
        optional: true,
        max: 100
    },
    content: { //This posts main body of content
        type: String,
        min: 1,
        max: 100000
    },
    inReplyTo: { //The id for the post this post is in reply to.
        type: String,
        regEx: SimpleSchema.RegEx.Id,
        optional: true,
        index: 1
    },
    tag: { //The tags for this post. Not currently used.
        type: [String],
        regEx: /(^|\s)([a-z\d][\w-]*)/i,
        optional: true
    },
    replies: { //The ids for the posts that respond to this one.
        type: [String],
        regEx: SimpleSchema.RegEx.Id,
        defaultValue: [],
        optional: true
    },
    published: { //The date this post was published.
        type: Date,
        optional: true,
        index: 1
    },
    updated: { //The date this post was last updated.
        type: Date,
        optional: true
    }
});

this.Posts = new Mongo.Collection('posts');

this.Posts.attachSchema(this.Schema.Post);

this.Posts.before.insert(function(userId, post) {
    if (!post.attributedTo && userId) post.attributedTo = Meteor.users.findOne({_id: userId}).actor;
    if (!post.published) post.published = Date.now();
});

@@ -537,24 +537,227 @@
 true%0A    %7D,%0A
+    id: %7B%0A        type: String,%0A        regEx: SimpleSchema.RegEx.Url,%0A        optional: true%0A    %7D,%0A    url: %7B%0A        type: String,%0A        regEx: SimpleSchema.RegEx.Url,%0A        optional: true%0A    %7D,%0A
     summary:
@@ -1960,60 +1960,248 @@
 or;%0A
-    if (!post.published) post.published = Date.now()
+%0A    let actor = Actors.findOne(%7Bid: post.attributedTo%7D);%0A%0A    if (!post.published) post.published = Date.now();%0A    if (!post.id) post.id = process.env.ROOT_URL + %22post/%22 + post._id;%0A    if (!post.url) post.url = actor.url + %22/%22 + post._id
 ;%0A%7D)

add id and url to posts