Skip to content

Commit eedc535

Browse files
committed
feat: follow group #21
1 parent 622992b commit eedc535

File tree

9 files changed

+101
-14
lines changed

9 files changed

+101
-14
lines changed

client/src/components/document/GroupContent.vue

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,57 @@
22
<div class="apis-doc" v-if="group">
33
<div class="title">
44
{{group.name}}接口文档
5+
<div class="control">
6+
<el-button class="follow"
7+
icon="star-on"
8+
v-if="followed"
9+
type="primary"
10+
@click="unfollowGroup(group._id)">取消订阅</el-button>
11+
<el-button class="follow"
12+
icon="star-off"
13+
v-else
14+
@click="followGroup(group._id)">订阅</el-button>
15+
</div>
516
</div>
617
<api-doc :api="api" v-for="api in apis" :key="api._id"></api-doc>
718
</div>
819
</template>
920

1021
<script>
1122
import ApiDoc from './apiDoc/Index'
23+
import { mapActions } from 'vuex'
1224
export default {
1325
components: {
1426
ApiDoc
1527
},
28+
props: ['apis'],
1629
computed: {
17-
groups () {
18-
return this.$store.state.groups
19-
},
2030
group () {
21-
return this.groups.find(g => g._id === this.$route.params.groupId)
31+
return this.$store.state.groups.find(g => g._id === this.$route.params.groupId)
32+
},
33+
user () {
34+
return this.$store.state.user
35+
},
36+
followed () {
37+
return !!this.group.follower.find(f => f === this.user._id)
2238
}
2339
},
24-
props: ['apis']
40+
methods: {
41+
...mapActions([
42+
'followGroup',
43+
'unfollowGroup'
44+
])
45+
}
2546
}
2647
</script>
27-
<style>
48+
<style lang="less">
2849
.apis-doc > .title {
2950
padding: 30px;
3051
font-size: 28px;
3152
border-bottom: 1px solid #ddd;
53+
54+
.control {
55+
float: right;
56+
}
3257
}
3358
</style>

client/src/config/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default R.map((url) => `${domain}${url}`)({
88
GROUP: '/server/group/:groupId',
99
APIS: '/server/api',
1010
GROUP_APIS: '/server/api/:groupId',
11+
GROUP_FOLLOWER: '/server/group/follower/:groupId',
1112
API: '/server/api/:groupId/:apiId',
1213
API_HISTORY: '/server/history/api/:apiId',
1314
API_AUTHORITY: '/server/authority/api/:apiId',

client/src/store/actions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ const actions = {
251251
unfollow ({ state }, apiId) {
252252
return axios.delete(API.API_FOLLOWER.replace(':apiId', apiId))
253253
},
254+
followGroup ({ state, commit }, groupId) {
255+
return axios.put(API.GROUP_FOLLOWER.replace(':groupId', groupId)).then(res => {
256+
commit('UPDATE_GROUP', res.data)
257+
return res
258+
})
259+
},
260+
unfollowGroup ({ state, commit }, groupId) {
261+
return axios.delete(API.GROUP_FOLLOWER.replace(':groupId', groupId)).then(res => {
262+
commit('UPDATE_GROUP', res.data)
263+
return res
264+
})
265+
},
254266
sendResetPassCode ({ state }, email) {
255267
return axios.post(`${API.USER}/recovery/password/code`, {email})
256268
},

client/src/store/mutations.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { apiInitData } from '@/util'
22
import Schema from '@/model/schema'
33
import R from 'ramda'
4+
import Vue from 'vue'
45

56
const mutations = {
67
FETCH_GROUPS_SUCCESS (state, groups) {
@@ -27,7 +28,7 @@ const mutations = {
2728
},
2829
UPDATE_GROUP (state, group) {
2930
const index = R.findIndex(g => g._id === group._id)(state.groups)
30-
state.groups[index] = group
31+
Vue.set(state.groups, index, group)
3132
},
3233
CREATE_GROUP_SUCCESS (state, data) {
3334
state.groups.unshift(data)

server/app/controller/api.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,25 @@ class ApiController extends AbstractController {
8989
msg: '系统错误,保存失败'
9090
})
9191
}
92-
this.service.group.updateTime(groupId)
92+
const group = await this.service.group.updateTime(groupId)
9393
// 存下历史记录,并将所有记录返回
9494
resources.history = await this.service.apiHistory.push(resources)
95-
this.notifyApiChange(resources, lastModifiedTime)
95+
this.notifyApiChange(group, resources, lastModifiedTime)
9696
this.ctx.body = { resources }
9797
}
98-
async notifyApiChange (api, lastModifiedTime) {
98+
async notifyApiChange (group, api, lastModifiedTime) {
9999
const interval = api.modifiedTime - lastModifiedTime
100100
if (interval < this.config.pushInterval.api) {
101101
return
102102
}
103-
const selfIdx = api.follower.findIndex(f => f.toString() === this.ctx.authUser._id)
103+
let follower = api.follower.concat(group.follower).map(f => f.toString())
104+
follower = Array.from(new Set(follower))
105+
const selfIdx = follower.findIndex(f => f === this.ctx.authUser._id)
104106
// 如果修改者也在关注列表中,不推送自己
105107
if (selfIdx >= 0) {
106-
api.follower.splice(selfIdx, 1)
108+
follower.splice(selfIdx, 1)
107109
}
108-
const users = await this.service.user.getByIds(api.follower)
110+
const users = await this.service.user.getByIds(follower)
109111
this.service.email.notifyApiChange(api, users)
110112
}
111113
async getApi () {

server/app/controller/group.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ class GroupController extends AbstractController {
6464
await this.service.api.deleteGroupApis(id)
6565
this.ctx.status = 204
6666
}
67+
68+
async follow () {
69+
const groupId = this.ctx.params.groupId
70+
const authId = this.ctx.authUser._id
71+
const group = (await this.service.group.getById(groupId)).toObject()
72+
group.follower = group.follower || []
73+
const isExist = group.follower.find(f => f.toString() === authId)
74+
if (isExist) {
75+
this.ctx.body = group
76+
} else {
77+
group.follower.push(authId)
78+
this.ctx.body = await this.service.group.updateFollower(groupId, group.follower)
79+
}
80+
}
81+
async unfollow () {
82+
const groupId = this.ctx.params.groupId
83+
const authId = this.ctx.authUser._id
84+
const group = (await this.service.group.getById(groupId)).toObject()
85+
const index = group.follower.findIndex(f => f.toString() === authId)
86+
if (index < 0) {
87+
this.ctx.body = group
88+
} else {
89+
group.follower.splice(index, 1)
90+
this.ctx.body = await this.service.group.updateFollower(groupId, group.follower)
91+
}
92+
}
6793
}
6894

6995
module.exports = GroupController

server/app/model/group.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = app => {
1414
type: ObjectId,
1515
required: true
1616
},
17+
follower: [{ // 订阅者
18+
type: ObjectId,
19+
ref: 'user'
20+
}],
1721
member: [ ObjectId ],
1822
operation: {
1923
type: Number,

server/app/router.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module.exports = app => {
1010
app.put('/server/group/:id/claim', 'group.claim') // 认领分组,这是历史原因导致的接口,可以不关心
1111
app.delete('/server/group/:id', 'group.delete')
1212
app.put('/server/group/:id', 'group.update')
13+
app.put('/server/group/follower/:groupId', 'group.follow')
14+
app.delete('/server/group/follower/:groupId', 'group.unfollow')
1315

1416
app.get('/server/api/', 'api.getAll')
1517
app.get('/server/api/manage', 'api.getManageApi')

server/app/service/group.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,23 @@ class Group extends Service {
4646
manager: this.ctx.authUser._id
4747
}, Object.assign(group, { modifiedTime: Date.now() }), { new: true })
4848
}
49+
updateFollower (groupId, follower) {
50+
return this.ctx.model.Group.findOneAndUpdate(
51+
{ _id: groupId },
52+
{
53+
follower,
54+
modifiedTime: Date.now()
55+
},
56+
{ new: true }
57+
)
58+
}
4959
updateTime (groupId) {
5060
// 此方法允许异步执行
51-
return this.ctx.model.Group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
61+
return this.ctx.model.Group.findOneAndUpdate(
62+
{ _id: groupId },
63+
{ modifiedTime: Date.now() },
64+
{ new: true }
65+
).exec()
5266
}
5367
create (group) {
5468
const authId = this.ctx.authUser._id

0 commit comments

Comments
 (0)