Initial commit 🍀
This commit is contained in:
53
src/api/endpoints/i/appdata/get.js
Normal file
53
src/api/endpoints/i/appdata/get.js
Normal file
@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import Appdata from '../../../models/appdata';
|
||||
|
||||
/**
|
||||
* Get app data
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user, app, isSecure) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'key' parameter
|
||||
let key = params.key;
|
||||
if (key === undefined) {
|
||||
key = null;
|
||||
}
|
||||
|
||||
if (isSecure) {
|
||||
if (!user.data) {
|
||||
return res();
|
||||
}
|
||||
if (key !== null) {
|
||||
const data = {};
|
||||
data[key] = user.data[key];
|
||||
res(data);
|
||||
} else {
|
||||
res(user.data);
|
||||
}
|
||||
} else {
|
||||
const select = {};
|
||||
if (key !== null) {
|
||||
select['data.' + key] = true;
|
||||
}
|
||||
const appdata = await Appdata.findOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, select);
|
||||
|
||||
if (appdata) {
|
||||
res(appdata.data);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
}
|
||||
});
|
55
src/api/endpoints/i/appdata/set.js
Normal file
55
src/api/endpoints/i/appdata/set.js
Normal file
@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import Appdata from '../../../models/appdata';
|
||||
import User from '../../../models/user';
|
||||
|
||||
/**
|
||||
* Set app data
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user, app, isSecure) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
const data = params.data;
|
||||
if (data == null) {
|
||||
return rej('data is required');
|
||||
}
|
||||
|
||||
if (isSecure) {
|
||||
const set = {
|
||||
$set: {
|
||||
data: Object.assign(user.data || {}, JSON.parse(data))
|
||||
}
|
||||
};
|
||||
await User.updateOne({ _id: user._id }, set);
|
||||
res(204);
|
||||
} else {
|
||||
const appdata = await Appdata.findOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
});
|
||||
const set = {
|
||||
$set: {
|
||||
data: Object.assign((appdata || {}).data || {}, JSON.parse(data))
|
||||
}
|
||||
};
|
||||
await Appdata.updateOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, Object.assign({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, set), {
|
||||
upsert: true
|
||||
});
|
||||
res(204);
|
||||
}
|
||||
});
|
60
src/api/endpoints/i/favorites.js
Normal file
60
src/api/endpoints/i/favorites.js
Normal file
@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import Favorite from '../../models/favorite';
|
||||
import serialize from '../../serializers/post';
|
||||
|
||||
/**
|
||||
* Get followers of a user
|
||||
*
|
||||
* @param {Object} params
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'limit' parameter
|
||||
let limit = params.limit;
|
||||
if (limit !== undefined && limit !== null) {
|
||||
limit = parseInt(limit, 10);
|
||||
|
||||
// From 1 to 100
|
||||
if (!(1 <= limit && limit <= 100)) {
|
||||
return rej('invalid limit range');
|
||||
}
|
||||
} else {
|
||||
limit = 10;
|
||||
}
|
||||
|
||||
// Get 'offset' parameter
|
||||
let offset = params.offset;
|
||||
if (offset !== undefined && offset !== null) {
|
||||
offset = parseInt(offset, 10);
|
||||
} else {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
// Get 'sort' parameter
|
||||
let sort = params.sort || 'desc';
|
||||
|
||||
// Get favorites
|
||||
const favorites = await Favorites
|
||||
.find({
|
||||
user_id: user._id
|
||||
}, {}, {
|
||||
limit: limit,
|
||||
skip: offset,
|
||||
sort: {
|
||||
_id: sort == 'asc' ? 1 : -1
|
||||
}
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(favorites.map(async favorite =>
|
||||
await serialize(favorite.post)
|
||||
)));
|
||||
});
|
120
src/api/endpoints/i/notifications.js
Normal file
120
src/api/endpoints/i/notifications.js
Normal file
@ -0,0 +1,120 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import Notification from '../../models/notification';
|
||||
import serialize from '../../serializers/notification';
|
||||
import getFriends from '../../common/get-friends';
|
||||
|
||||
/**
|
||||
* Get notifications
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'following' parameter
|
||||
const following = params.following === 'true';
|
||||
|
||||
// Get 'mark_as_read' parameter
|
||||
let markAsRead = params.mark_as_read;
|
||||
if (markAsRead == null) {
|
||||
markAsRead = true;
|
||||
} else {
|
||||
markAsRead = markAsRead === 'true';
|
||||
}
|
||||
|
||||
// Get 'type' parameter
|
||||
let type = params.type;
|
||||
if (type !== undefined && type !== null) {
|
||||
type = type.split(',').map(x => x.trim());
|
||||
}
|
||||
|
||||
// Get 'limit' parameter
|
||||
let limit = params.limit;
|
||||
if (limit !== undefined && limit !== null) {
|
||||
limit = parseInt(limit, 10);
|
||||
|
||||
// From 1 to 100
|
||||
if (!(1 <= limit && limit <= 100)) {
|
||||
return rej('invalid limit range');
|
||||
}
|
||||
} else {
|
||||
limit = 10;
|
||||
}
|
||||
|
||||
const since = params.since_id || null;
|
||||
const max = params.max_id || null;
|
||||
|
||||
// Check if both of since_id and max_id is specified
|
||||
if (since !== null && max !== null) {
|
||||
return rej('cannot set since_id and max_id');
|
||||
}
|
||||
|
||||
const query = {
|
||||
notifiee_id: user._id
|
||||
};
|
||||
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
|
||||
if (following) {
|
||||
// ID list of the user itself and other users who the user follows
|
||||
const followingIds = await getFriends(user._id);
|
||||
|
||||
query.notifier_id = {
|
||||
$in: followingIds
|
||||
};
|
||||
}
|
||||
|
||||
if (type) {
|
||||
query.type = {
|
||||
$in: type
|
||||
};
|
||||
}
|
||||
|
||||
if (since !== null) {
|
||||
sort._id = 1;
|
||||
query._id = {
|
||||
$gt: new mongo.ObjectID(since)
|
||||
};
|
||||
} else if (max !== null) {
|
||||
query._id = {
|
||||
$lt: new mongo.ObjectID(max)
|
||||
};
|
||||
}
|
||||
|
||||
// Issue query
|
||||
const notifications = await Notification
|
||||
.find(query, {}, {
|
||||
limit: limit,
|
||||
sort: sort
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(notifications.map(async notification =>
|
||||
await serialize(notification))));
|
||||
|
||||
// Mark as read all
|
||||
if (notifications.length > 0 && markAsRead) {
|
||||
const ids = notifications
|
||||
.filter(x => x.is_read == false)
|
||||
.map(x => x._id);
|
||||
|
||||
// Update documents
|
||||
await Notification.update({
|
||||
_id: { $in: ids }
|
||||
}, {
|
||||
$set: { is_read: true }
|
||||
}, {
|
||||
multi: true
|
||||
});
|
||||
}
|
||||
});
|
71
src/api/endpoints/i/signin_history.js
Normal file
71
src/api/endpoints/i/signin_history.js
Normal file
@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import Signin from '../../models/signin';
|
||||
import serialize from '../../serializers/signin';
|
||||
|
||||
/**
|
||||
* Get signin history of my account
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'limit' parameter
|
||||
let limit = params.limit;
|
||||
if (limit !== undefined && limit !== null) {
|
||||
limit = parseInt(limit, 10);
|
||||
|
||||
// From 1 to 100
|
||||
if (!(1 <= limit && limit <= 100)) {
|
||||
return rej('invalid limit range');
|
||||
}
|
||||
} else {
|
||||
limit = 10;
|
||||
}
|
||||
|
||||
const since = params.since_id || null;
|
||||
const max = params.max_id || null;
|
||||
|
||||
// Check if both of since_id and max_id is specified
|
||||
if (since !== null && max !== null) {
|
||||
return rej('cannot set since_id and max_id');
|
||||
}
|
||||
|
||||
const query = {
|
||||
user_id: user._id
|
||||
};
|
||||
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
|
||||
if (since !== null) {
|
||||
sort._id = 1;
|
||||
query._id = {
|
||||
$gt: new mongo.ObjectID(since)
|
||||
};
|
||||
} else if (max !== null) {
|
||||
query._id = {
|
||||
$lt: new mongo.ObjectID(max)
|
||||
};
|
||||
}
|
||||
|
||||
// Issue query
|
||||
const history = await Signin
|
||||
.find(query, {}, {
|
||||
limit: limit,
|
||||
sort: sort
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(history.map(async record =>
|
||||
await serialize(record))));
|
||||
});
|
95
src/api/endpoints/i/update.js
Normal file
95
src/api/endpoints/i/update.js
Normal file
@ -0,0 +1,95 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import User from '../../models/user';
|
||||
import serialize from '../../serializers/user';
|
||||
import event from '../../event';
|
||||
|
||||
/**
|
||||
* Update myself
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} _
|
||||
* @param {boolean} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = async (params, user, _, isSecure) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'name' parameter
|
||||
const name = params.name;
|
||||
if (name !== undefined && name !== null) {
|
||||
if (name.length > 50) {
|
||||
return rej('too long name');
|
||||
}
|
||||
|
||||
user.name = name;
|
||||
}
|
||||
|
||||
// Get 'location' parameter
|
||||
const location = params.location;
|
||||
if (location !== undefined && location !== null) {
|
||||
if (location.length > 50) {
|
||||
return rej('too long location');
|
||||
}
|
||||
|
||||
user.location = location;
|
||||
}
|
||||
|
||||
// Get 'bio' parameter
|
||||
const bio = params.bio;
|
||||
if (bio !== undefined && bio !== null) {
|
||||
if (bio.length > 500) {
|
||||
return rej('too long bio');
|
||||
}
|
||||
|
||||
user.bio = bio;
|
||||
}
|
||||
|
||||
// Get 'avatar_id' parameter
|
||||
const avatar = params.avatar_id;
|
||||
if (avatar !== undefined && avatar !== null) {
|
||||
user.avatar_id = new mongo.ObjectID(avatar);
|
||||
}
|
||||
|
||||
// Get 'banner_id' parameter
|
||||
const banner = params.banner_id;
|
||||
if (banner !== undefined && banner !== null) {
|
||||
user.banner_id = new mongo.ObjectID(banner);
|
||||
}
|
||||
|
||||
await User.updateOne({ _id: user._id }, {
|
||||
$set: user
|
||||
});
|
||||
|
||||
// Serialize
|
||||
const iObj = await serialize(user, user, {
|
||||
detail: true,
|
||||
includeSecrets: isSecure
|
||||
})
|
||||
|
||||
// Send response
|
||||
res(iObj);
|
||||
|
||||
// Publish i updated event
|
||||
event(user._id, 'i_updated', iObj);
|
||||
|
||||
// Update search index
|
||||
if (config.elasticsearch.enable) {
|
||||
const es = require('../../../db/elasticsearch');
|
||||
|
||||
es.index({
|
||||
index: 'misskey',
|
||||
type: 'user',
|
||||
id: user._id.toString(),
|
||||
body: {
|
||||
name: user.name,
|
||||
bio: user.bio
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user