CREATE OR REPLACE VIEW view__posts as
SELECT
    post.postID AS postID,
    post.userID AS userID,
    socialSite.siteID AS socialSiteID,
    socialSite.socialType AS socialType,
    CONCAT_WS('\n\n', post.headline, post.subHeadline, post.mainCopy, post.signature) AS message,
    GROUP_CONCAT(CASE WHEN phm.mediaType = 'file' THEN phm.mediaID ELSE NULL END) AS fileIDs,
    CASE WHEN phm.mediaType = 'carousel' THEN phm.mediaID END AS carouselID,
    post.activeFrom AS activeFrom,
    postHasSocialSites.serverTime AS serverTime,
    post.type AS postType,
    post.status AS postStatus,
    post.expiryDate AS postExpiryDate,
    post.expiryPeriod AS postExpiryPeriod
FROM
    smd__post AS post
INNER JOIN
    smd__post_has_social_sites AS postHasSocialSites
ON
    post.postID = postHasSocialSites.postID
LEFT JOIN
    smd__social_site AS socialSite
ON
    socialSite.siteID = postHasSocialSites.socialSiteID
LEFT JOIN
    smd__post_has_media AS phm
ON
    phm.postID = post.postID
WHERE
    serverTime <= CURRENT_TIMESTAMP() AND postHasSocialSites.postStatus = 'waiting'
GROUP BY
    postHasSocialSites.postID, postHasSocialSites.socialSiteID, postHasSocialSites.serverTime
HAVING
    (postStatus = 'approved' OR postStatus = 'underPost') AND postType = 'customFeed'