[refactor] s/fed_user/ap_user

This commit is contained in:
les 2019-12-04 01:20:31 +01:00
parent c43fe095a5
commit 5052ceca0a
17 changed files with 214 additions and 123 deletions

View file

@ -1,6 +1,9 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
### unreleased ### unreleased
- [refactor] s/fed_user/ap_user
- [fedi] admin moderation
- [refactor] s/comment/resource/
- [refactor] remove `username` field - [refactor] remove `username` field
- [doc] about and federation - [doc] about and federation

View file

@ -2,37 +2,40 @@
div div
el-form(label-width='200px') el-form(label-width='200px')
el-form-item(:label="$t('admin.enable_federation')") el-form-item(:label="$t('admin.enable_federation')")
el-popover(:content="$t('admin.enable_federation_help')" placement='right' trigger='hover')
span(slot='reference')
el-switch(v-model='enable_federation') el-switch(v-model='enable_federation')
el-popover(:content="$t('admin.enable_federation_help')" trigger='hover')
span.ml-1(slot='reference')
i.el-icon-info
el-form-item(v-show='enable_federation' :label="$t('admin.enable_comments')") el-form-item(v-show='enable_federation' :label="$t('admin.enable_resources')")
el-switch(v-model='enable_comments') el-popover(:content="$t('admin.enable_resources_help')" placement='right' trigger='hover')
el-popover(:content="$t('admin.enable_comments_help')" trigger='hover') span(slot='reference')
span.ml-1(slot='reference') el-switch(v-model='enable_resources')
i.el-icon-info
el-form-item(v-show='enable_federation' :label="$t('admin.hide_boost_bookmark')") el-form-item(v-show='enable_federation' :label="$t('admin.hide_boost_bookmark')")
el-switch(v-model='disable_gamification') el-popover(:content="$t('admin.hide_boost_bookmark_help')" placement='right' trigger='hover')
el-popover(:content="$t('admin.hide_boost_bookmark_help')" trigger='hover') span(slot='reference')
span.ml-1(slot='reference') el-switch(v-model='hide_boosts')
i.el-icon-info
el-form-item(v-show='enable_federation' :label="$t('admin.instance_name')")
el-popover(:content="$t('admin.instance_name_help')" placement='right' trigger='hover')
span(slot='reference')
el-input.w-25(v-model='instance_name' placeholder='Instance name')
p Follow this instance from <u>@{{instance_name}}@{{settings.baseurl|url2host}}</u>
el-row(v-if='enable_federation') el-row(v-if='enable_federation')
el-col(:span='12') el-col(:span='12')
el-divider {{$t('common.instances')}} el-divider {{$t('common.instances')}}
el-input(v-model='instancesFilter' :placeholder="$t('admin.filter_instances')")
el-table(:data='paginatedInstances' small @row-click='instanceSelected') el-table(:data='paginatedInstances' small @row-click='instanceSelected')
el-table-column(label='Domain' width='200') el-table-column(label='Domain' width='180')
template(slot-scope='data') template(slot-scope='data')
span(slot='reference') <img class='instance_thumb' :src="data.row.data.thumbnail"/> {{data.row.domain}} span(slot='reference') {{data.row.domain}}
el-table-column(label='Name' width='100') el-table-column(label='Name' width='100')
template(slot-scope='data') template(slot-scope='data')
span(slot='reference') {{data.row.name}} span(slot='reference') {{data.row.name}}
el-table-column(label='Users' width='70') el-table-column(label='Users' width='60')
template(slot-scope='data') template(slot-scope='data')
span(slot='reference') {{data.row.users}} span(slot='reference') {{data.row.users}}
el-table-column(:label="$t('common.actions')" width='100') el-table-column(:label="$t('common.actions')" width='120')
template(slot-scope='data') template(slot-scope='data')
el-button-group el-button-group
el-button(size='mini' el-button(size='mini'
@ -41,16 +44,20 @@
client-only client-only
el-pagination(v-if='enable_federation && instances.length>perPage' :page-size='perPage' :currentPage.sync='instancePage' :total='instances.length') el-pagination(v-if='enable_federation && instances.length>perPage' :page-size='perPage' :currentPage.sync='instancePage' :total='instances.length')
el-col(:span='12') el-col.float-right(:span='11' align='right')
el-divider {{$t('common.users')}} el-divider {{$t('common.users')}}
el-input(v-model='usersFilter' :placeholder="$t('admin.filter_users')")
client-only
el-pagination(v-if='enable_federation && users.length>perPage' :page-size='perPage' :currentPage.sync='instancePage' :total='users.length')
el-table(:data='paginatedSelectedUsers' small) el-table(:data='paginatedSelectedUsers' small)
el-table-column(label='User' width='200') el-table-column(label='User' width='150')
template(slot-scope='data') template(slot-scope='data')
span(slot='reference') <img v-if='data.row.object.icon' class='instance_thumb' :src="data.row.object.icon.url"/> span(slot='reference')
a(:href='data.row.object.id' target='_blank') {{data.row.object.name}} a(:href='data.row.object.id' target='_blank') {{data.row.object.name}}
el-table-column(:label="$t('admin.comments')" width='70') small ({{data.row.object.preferredUsername}})
el-table-column(:label="$t('common.resources')" width='90')
template(slot-scope='data') template(slot-scope='data')
span {{data.row.comments.length}} span {{data.row.resources.length}}
el-table-column(:label="$t('common.actions')" width='200') el-table-column(:label="$t('common.actions')" width='200')
template(slot-scope='data') template(slot-scope='data')
el-button-group el-button-group
@ -58,28 +65,46 @@
:type='data.row.blocked?"danger":"warning"' :type='data.row.blocked?"danger":"warning"'
@click='toggleUserBlock(data.row)') {{data.row.blocked?$t('admin.unblock'):$t('admin.block')}} @click='toggleUserBlock(data.row)') {{data.row.blocked?$t('admin.unblock'):$t('admin.block')}}
client-only div(v-show='enable_federation')
el-pagination(v-if='enable_federation && instances.length>perPage' :page-size='perPage' :currentPage.sync='instancePage' :total='instances.length') el-divider {{$t('common.resources')}}
el-table(:data='paginatedResources' small)
el-table-column(:label="$t('common.resources')")
template(slot-scope='data')
span(:class='{disabled: data.row.hidden}' v-html='data.row.data.content')
el-table-column(:label="$t('common.actions')" width="150")
template(slot-scope='data')
el-button-group
el-button(size='mini'
:type='data.row.hidden?"danger":"warning"'
@click='toggleHideResource(data.row)') {{data.row.hidden?$t('admin.show_resource'):$t('admin.hide_resource')}}
</template> </template>
<script> <script>
import { mapState, mapActions } from 'vuex' import { mapState, mapActions, mapGetters } from 'vuex'
export default { export default {
name: 'Federation', name: 'Federation',
props: ['instances'],
data () { data () {
return { return {
perPage: 10, perPage: 10,
instancePage: 1, instancePage: 1,
userPage: 1, userPage: 1,
selectedInstance: null, resourcePage: 1,
usersFilter: '',
instancesFilter: '',
users: [], users: [],
resources: [],
instances: [],
} }
}, },
async mounted () {
console.error('mounted ')
this.instances = await this.$axios.$get('/instances')
this.resources = await this.$axios.$get('/resources')
// this.users = await this.$axios.$get('/users')
},
methods: { methods: {
...mapActions(['setSetting']), ...mapActions(['setSetting']),
async instanceSelected (instance) { async instanceSelected (instance) {
@ -92,29 +117,52 @@ export default {
async toggleUserBlock (user) { async toggleUserBlock (user) {
await this.$axios.post('/instances/toggle_user_block', { user_id: user.ap_id }) await this.$axios.post('/instances/toggle_user_block', { user_id: user.ap_id })
user.blocked = !user.blocked user.blocked = !user.blocked
},
async toggleHideResource (resource) {
await this.$axios.put(`/resources/${resource.id}`, { hidden: !resource.hidden})
resource.hidden = !resource.hidden
} }
}, },
computed: { computed: {
...mapState(['settings']), ...mapState(['settings']),
paginatedResources () {
return this.resources.slice((this.resourcePage -1) * this.perPage,
this.resourcePage * this.perPage)
},
paginatedInstances () { paginatedInstances () {
return this.instances.slice((this.instancePage - 1) * this.perPage, return this.filteredInstances.slice((this.instancePage - 1) * this.perPage,
this.instancePage * this.perPage) this.instancePage * this.perPage)
}, },
filteredUsers () {
if (!this.usersFilter) return this.users
this.usersFilter = this.usersFilter.toLowerCase()
console.error(this.users)
return this.users.filter(user => user.name.includes(this.usersFilter) || user.preferredName.includes(this.usersFilter))
},
filteredInstances () {
if (!this.instancesFilter) return this.instances
this.instancesFilter = this.instancesFilter.toLowerCase()
return this.instances.filter(instance => instance.name.includes(this.instancesFilter) || instance.domain.includes(this.instancesFilter))
},
paginatedSelectedUsers () { paginatedSelectedUsers () {
return this.users.slice((this.userPage - 1) * this.perPage, return this.filteredUsers.slice((this.userPage - 1) * this.perPage,
this.userPage * this.perPage) this.userPage * this.perPage)
}, },
instance_name: {
get () { return this.settings.instance_name },
set (value) { this.setSetting({ key: 'instance_name', value })}
},
enable_federation: { enable_federation: {
get () { return this.settings.enable_federation }, get () { return this.settings.enable_federation },
set (value) { this.setSetting({ key: 'enable_federation', value }) } set (value) { this.setSetting({ key: 'enable_federation', value }) }
}, },
enable_comments: { enable_resources: {
get () { return this.settings.enable_comments }, get () { return this.settings.enable_resources },
set (value) { this.setSetting({ key: 'enable_comments', value }) } set (value) { this.setSetting({ key: 'enable_resources', value }) }
}, },
disable_gamification: { hide_boosts: {
get () { return this.settings.disable_gamification }, get () { return this.settings.hide_boosts },
set (value) { this.setSetting({ key: 'disable_gamification', value }) } set (value) { this.setSetting({ key: 'hide_boosts', value }) }
} }
} }
} }

View file

@ -70,6 +70,7 @@
"nuxt": "^2.10.2", "nuxt": "^2.10.2",
"nuxt-express-module": "^0.0.11", "nuxt-express-module": "^0.0.11",
"pg": "^7.12.1", "pg": "^7.12.1",
"sanitize-html": "^1.20.1",
"sass-loader": "^8.0.0", "sass-loader": "^8.0.0",
"sequelize": "^5.21.2", "sequelize": "^5.21.2",
"sequelize-cli": "^5.5.1", "sequelize-cli": "^5.5.1",

View file

@ -1,6 +1,6 @@
<template lang='pug'> <template lang='pug'>
div div
p(v-html="$t('event.follow_me_description', { title: settings.title, account: `@${settings.fedi_admin}@${domain}`})") p(v-html="$t('event.follow_me_description', { title: settings.title, account: `@${settings.instance_name}@${domain}`})")
el-input(v-model='instance_hostname') el-input(v-model='instance_hostname')
a(slot='append' :href='link' target='_blank') a(slot='append' :href='link' target='_blank')
el-button(:disabled='(!couldGo || !proceed)' plain type="primary" icon='el-icon-document') {{$t("common.follow")}} el-button(:disabled='(!couldGo || !proceed)' plain type="primary" icon='el-icon-document') {{$t("common.follow")}}
@ -51,7 +51,7 @@ export default {
}, },
link () { link () {
// check if exists // check if exists
return `https://${this.instance_hostname}/authorize_interaction?uri=gancio@gancio.cisti.org` return `https://${this.instance_hostname}/authorize_interaction?uri=${this.settings.instance_name}@${this.domain}`
} }
} }
} }

View file

@ -8,21 +8,6 @@
el-form-item(:label="$t('settings.change_password')") el-form-item(:label="$t('settings.change_password')")
el-input(v-model='password' type='password') el-input(v-model='password' type='password')
//- allow federation
div(v-if='settings.enable_federation')
el-form-item(:label="$t('admin.enable_federation')")
el-switch(v-model='user.settings.enable_federation')
div(v-if='user.settings.enable_federation')
el-form-item(:label="$t('common.username')")
el-input(v-if='username_editable' type='text' name='username' v-model='user.username')
template(slot='suffix') @{{baseurl}}
span(v-else) {{user.username}}@{{baseurl}}
//- el-button(slot='append') {{$t('common.save')}}
el-form-item(:label="$t('common.displayname')")
el-input(type='text' v-model='user.display_name')
el-button(type='success' native-type='submit') {{$t('common.save')}} el-button(type='success' native-type='submit') {{$t('common.save')}}
el-divider {{$t('settings.danger_section')}} el-divider {{$t('settings.danger_section')}}

View file

@ -0,0 +1,12 @@
const { ap_users: APUsers } = require('../models')
const apUserController = {
async toggleBlock (req, res) {
const user_id = req.body.user_id
const user = await APUsers.findByPk(user_id)
user.update({ blocked: !user.blocked })
res.json(user)
}
}
module.exports = apUserController

View file

@ -1,27 +0,0 @@
const { fed_users: FedUsers, comment: Comment } = require('../models')
const fedUserController = {
async toggleBlock (req, res) {
const user_id = req.body.user_id
const user = await FedUsers.findByPk(user_id)
user.update({ blocked: !user.blocked })
res.json(user)
},
async hideComment (req, res) {
const comment_id = req.params.comment_id
const hidden = req.body.hidden
const comment = await Comment.findByPk(comment_id)
await comment.update({ hidden })
res.json(comment)
},
async removeComment (req, res) {
const comment_id = req.params.comment_id
const comment = await Comment.findByPk(comment_id)
await comment.destroy()
res.sendStatus(200)
}
}
module.exports = fedUserController

View file

@ -1,14 +1,14 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize')
const { fed_users: FedUsers, instances: Instances, comment: Comment } = require('../models') const { ap_user: APUser, instance: Instance, resource: Resource } = require('../models')
const instancesController = { const instancesController = {
async getAll (req, res) { async getAll (req, res) {
const instances = await Instances.findAll({ const instances = await Instance.findAll({
attributes: { attributes: {
include: [[Sequelize.fn('count', Sequelize.col('domain')), 'users']] include: [[Sequelize.fn('count', Sequelize.col('domain')), 'users']]
}, },
group: ['domain'], group: ['domain'],
include: [{ model: FedUsers, attributes: [] }] include: [{ model: APUser, attributes: [] }]
}) })
return res.json(instances) return res.json(instances)
}, },
@ -17,12 +17,12 @@ const instancesController = {
* get instance users * get instance users
*/ */
async get (req, res) { async get (req, res) {
const fedi_users = await FedUsers.findAll({ where: { instanceDomain: req.params.instance_domain }, include: [Comment] }) const fedi_users = await APUser.findAll({ where: { instanceDomain: req.params.instance_domain }, include: [Resource] })
return res.json(fedi_users) return res.json(fedi_users)
}, },
async toggleBlock (req, res) { async toggleBlock (req, res) {
const instance = await Instances.findByPk(req.body.instance) const instance = await Instance.findByPk(req.body.instance)
if (!instance) { return res.status(404).send('Not found') } if (!instance) { return res.status(404).send('Not found') }
await instance.update({ blocked: req.body.blocked }) await instance.update({ blocked: req.body.blocked })
return res.json(instance) return res.json(instance)

View file

@ -0,0 +1,18 @@
module.exports = (sequelize, DataTypes) => {
const APUser = sequelize.define('ap_user', {
ap_id: {
type: DataTypes.STRING,
primaryKey: true
},
follower: DataTypes.BOOLEAN,
blocked: DataTypes.BOOLEAN,
object: DataTypes.JSON
})
APUser.associate = function (models) {
APUser.belongsTo(models.instance)
APUser.hasMany(models.resource)
}
return APUser
}

View file

@ -1,16 +0,0 @@
module.exports = (sequelize, DataTypes) => {
const fed_users = sequelize.define('fed_users', {
ap_id: {
type: DataTypes.STRING,
primaryKey: true
},
blocked: DataTypes.BOOLEAN,
object: DataTypes.JSON
}, {})
fed_users.associate = function (models) {
fed_users.belongsTo(models.instances)
fed_users.belongsToMany(models.user, { through: 'user_followers', as: 'followers' })
fed_users.hasMany(models.comment, { foreignKey: 'fedUserApId' })
}
return fed_users
}

View file

@ -1,6 +1,6 @@
'use strict' 'use strict'
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const instances = sequelize.define('instances', { const Instance = sequelize.define('instance', {
domain: { domain: {
primaryKey: true, primaryKey: true,
allowNull: false, allowNull: false,
@ -11,9 +11,9 @@ module.exports = (sequelize, DataTypes) => {
data: DataTypes.JSON data: DataTypes.JSON
}, {}) }, {})
instances.associate = function (models) { Instance.associate = function (models) {
instances.hasMany(models.fed_users) Instance.hasMany(models.ap_user)
} }
return instances return Instance
} }

View file

@ -1,6 +1,6 @@
'use strict'
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const notification = sequelize.define('notification', { const Notification = sequelize.define('notification', {
filters: DataTypes.JSON, filters: DataTypes.JSON,
email: DataTypes.STRING, email: DataTypes.STRING,
remove_code: DataTypes.STRING, remove_code: DataTypes.STRING,
@ -19,9 +19,9 @@ module.exports = (sequelize, DataTypes) => {
}] }]
}) })
notification.associate = function (models) { Notification.associate = function (models) {
notification.belongsToMany(models.event, { through: 'event_notification' }) Notification.belongsToMany(models.event, { through: 'event_notification' })
// associations can be defined here // associations can be defined here
} }
return notification return Notification
} }

View file

@ -1,6 +1,7 @@
'use strict'
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const place = sequelize.define('place', {
const Place = sequelize.define('place', {
name: { name: {
type: DataTypes.STRING, type: DataTypes.STRING,
unique: true, unique: true,
@ -10,9 +11,9 @@ module.exports = (sequelize, DataTypes) => {
address: DataTypes.STRING address: DataTypes.STRING
}, {}) }, {})
place.associate = function (models) { Place.associate = function (models) {
place.hasMany(models.event) Place.hasMany(models.event)
} }
return place return Place
} }

View file

@ -1,6 +1,6 @@
'use strict'
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const tag = sequelize.define('tag', { const Tag = sequelize.define('tag', {
tag: { tag: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
@ -10,9 +10,9 @@ module.exports = (sequelize, DataTypes) => {
weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false } weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false }
}, {}) }, {})
tag.associate = function (models) { Tag.associate = function (models) {
tag.belongsToMany(models.event, { through: 'event_tags' }) Tag.belongsToMany(models.event, { through: 'event_tags' })
} }
return tag return Tag
} }

View file

@ -0,0 +1,18 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('fed_users', 'follower', {
type: Sequelize.BOOLEAN
})
// const users = queryInterface.sequelize.query('SELECT ')
// const followers = users.reduce((followers, user) => followers.concat(user.followers), [])
// console.error(followers)
// const followers = await Sequelize.models.user_followers.findAll()
// console.error(followers)
// get current followers
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('fed_users', 'follower')
}
}

View file

@ -0,0 +1,24 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.renameTable('fed_users', 'ap_users')
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};

View file

@ -0,0 +1,24 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.renameColumn('resources', 'fedUserApId', 'apUserApId')
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};