Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add community_templates with associated default topics #518

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/graphql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export function makeAuthenticatedQueries (userId, fetchOne, fetchMany) {
groupTopic: (root, { topicName, groupSlug }) =>
GroupTag.findByTagAndGroup(topicName, groupSlug),
groupTopics: (root, args) => fetchMany('GroupTopic', args),
groupTemplates: (root, args) => GroupTemplate.fetchAll(),
groups: (root, args) => fetchMany('Group', args),
joinRequests: (root, args) => fetchMany('JoinRequest', args),
me: () => fetchOne('Me', userId),
Expand Down
13 changes: 13 additions & 0 deletions api/graphql/makeModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,19 @@ export default async function makeModels (userId, isAdmin) {
})
},

GroupTemplate: {
model: GroupTemplate,
attributes: [
'name',
'display_name'
],
relations: [
'groups',
'defaultTopics'
],
fetchMany: () => GroupTemplate
},

GroupJoinQuestion: {
model: GroupJoinQuestion,
attributes: [
Expand Down
15 changes: 15 additions & 0 deletions api/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ type UserSettings {
streamPostType: String
}

type GroupTemplate {
id: ID
name: String
displayName: String
groups: GroupQuerySet
groupsTotal: Int
defaultTopics: [Topic]
defaultTopicsTotal: Int
newPostCount: Int
updatedAt: String
createdAt: String
}

type GroupTopicQuerySet {
total: Int
hasMore: Boolean
Expand Down Expand Up @@ -608,6 +621,7 @@ type Query {
sortBy: String,
visibility: Int
): GroupQuerySet
groupTemplates: [GroupTemplate]
joinRequests(groupId: ID, status: Int): JoinRequestQuerySet
messageThread(id: ID): MessageThread
post(id: ID): Post
Expand Down Expand Up @@ -804,6 +818,7 @@ input GroupInput {
avatarUrl: String
bannerUrl: String
description: String
groupTemplateId: ID
groupToGroupJoinQuestions: [QuestionInput]
joinQuestions: [QuestionInput]
location: String
Expand Down
14 changes: 13 additions & 1 deletion api/models/Community.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ module.exports = bookshelf.Model.extend(merge({
async create (userId, data) {
var attrs = defaults(
pick(data,
'name', 'description', 'slug', 'category',
'name', 'description', 'slug', 'category', 'community_template_id',
'beta_access_code', 'banner_url', 'avatar_url', 'location_id', 'location', 'network_id'),
{'banner_url': DEFAULT_BANNER, 'avatar_url': DEFAULT_AVATAR})

Expand All @@ -322,6 +322,18 @@ module.exports = bookshelf.Model.extend(merge({
const memberships = await bookshelf.transaction(async trx => {
await community.save(null, {transacting: trx})
await community.createStarterPosts(trx)
if (data.default_topics) {
data.default_topics.forEach(async name => {
const topic = await Tag.findOrCreate(name, {transacting: trx})
await Tag.addToCommunity({
community_id: community.id,
tag_id: topic.id,
user_id: userId,
is_default: true,
isSubscribing: true
}, {transacting: trx})
})
}
return community.addGroupMembers([userId],
{role: GroupMembership.Role.MODERATOR}, {transacting: trx})
})
Expand Down
18 changes: 18 additions & 0 deletions api/models/CommunityTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { includes } from 'lodash'

var knex = bookshelf.knex

module.exports = bookshelf.Model.extend({
tableName: 'community_templates',

communities: function () {
return this.hasMany(Community).query({where: {'communities.active': true}})
},

defaultTopics: function () {
return this.belongsToMany(Tag, 'community_template_default_topics', 'community_template_id', 'tag_id')
},

}, {

})
4 changes: 4 additions & 0 deletions api/models/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ module.exports = bookshelf.Model.extend({
return this.hasMany(GroupTag)
},

groupTemplates: function () {
return this.belongsToMany(GroupTemplate, 'group_templates_default_tags', 'tag_id', 'group_template_id')
},

posts: function () {
return this.belongsToMany(Post).through(PostTag).withPivot('selected')
},
Expand Down
36 changes: 36 additions & 0 deletions migrations/20200722214505_add_community_templates_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
exports.up = async function(knex, Promise) {
await knex.schema.createTable('community_templates', table => {
table.increments().primary()

table.string('name')
table.string('display_name')
table.index(['name'])

table.timestamp('created_at')
table.timestamp('updated_at')
})

await knex.schema.createTable('community_template_default_topics', table => {
table.increments().primary()

table.bigInteger('community_template_id').references('id').inTable('community_templates')
table.bigInteger('tag_id').references('id').inTable('tags')
table.index(['community_template_id'])

table.timestamp('created_at')
table.timestamp('updated_at')
})

return knex.schema.table('communities', table => {
table.bigInteger('community_template_id').references('id').inTable('community_templates')
})
};

exports.down = async function(knex, Promise) {
await knex.schema.dropTable('community_templates')
await knex.schema.dropTable('community_template_default_topics')

return knex.schema.table('communities', table => {
table.dropColumn('community_template_id')
})
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
"GroupRelationship",
"GroupRelationshipInvite",
"GroupTag",
"GroupTemplate",
"Invitation",
"JoinRequest",
"LinkedAccount",
Expand Down