server settings
This commit is contained in:
@@ -29,6 +29,7 @@ const server = table(
|
||||
id: t.u64().primaryKey().autoInc(),
|
||||
name: t.string(),
|
||||
owner: t.identity().optional(),
|
||||
avatar_id: t.u64().optional(),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -545,6 +546,76 @@ export const upload_banner = spacetimedb.reducer(
|
||||
},
|
||||
);
|
||||
|
||||
export const upload_server_avatar = spacetimedb.reducer(
|
||||
{ serverId: t.u64(), data: t.byteArray(), mimeType: t.string() },
|
||||
(ctx, { serverId, data, mimeType }) => {
|
||||
if (data.length > 1024 * 1024) {
|
||||
throw new SenderError("Avatar exceeds 1MB limit");
|
||||
}
|
||||
const s = ctx.db.server.id.find(serverId);
|
||||
if (!s) throw new SenderError("Server not found");
|
||||
/* owner check temporarily disabled */
|
||||
/* if (s.owner?.toHexString() !== ctx.sender.toHexString()) {
|
||||
throw new SenderError("Only the server owner can change the avatar");
|
||||
} */
|
||||
|
||||
const img = ctx.db.image.insert({
|
||||
id: 0n,
|
||||
data,
|
||||
mime_type: mimeType,
|
||||
name: "server_avatar",
|
||||
});
|
||||
ctx.db.server.id.update({ ...s, avatar_id: img.id });
|
||||
},
|
||||
);
|
||||
|
||||
export const update_server_name = spacetimedb.reducer(
|
||||
{ serverId: t.u64(), name: t.string() },
|
||||
(ctx, { serverId, name }) => {
|
||||
validateName(name);
|
||||
const s = ctx.db.server.id.find(serverId);
|
||||
if (!s) throw new SenderError("Server not found");
|
||||
/* owner check temporarily disabled */
|
||||
/* if (s.owner?.toHexString() !== ctx.sender.toHexString()) {
|
||||
throw new SenderError("Only the server owner can rename the server");
|
||||
} */
|
||||
ctx.db.server.id.update({ ...s, name });
|
||||
},
|
||||
);
|
||||
|
||||
export const delete_server = spacetimedb.reducer(
|
||||
{ serverId: t.u64() },
|
||||
(ctx, { serverId }) => {
|
||||
const s = ctx.db.server.id.find(serverId);
|
||||
if (!s) throw new SenderError("Server not found");
|
||||
|
||||
/* owner check temporarily disabled */
|
||||
/* if (s.owner?.toHexString() !== ctx.sender.toHexString()) {
|
||||
throw new SenderError("Only the server owner can delete the server");
|
||||
} */
|
||||
|
||||
// 1. Delete all channels in this server
|
||||
for (const c of ctx.db.channel.by_server_id.filter(serverId)) {
|
||||
// Cleanup messages in channel
|
||||
for (const m of ctx.db.message.by_channel_id.filter(c.id)) {
|
||||
ctx.db.message.id.delete(m.id);
|
||||
}
|
||||
for (const rm of ctx.db.recent_message.by_channel.filter(c.id)) {
|
||||
ctx.db.recent_message.id.delete(rm.id);
|
||||
}
|
||||
ctx.db.channel.id.delete(c.id);
|
||||
}
|
||||
|
||||
// 2. Delete all memberships
|
||||
for (const m of ctx.db.server_member.by_server_id.filter(serverId)) {
|
||||
ctx.db.server_member.id.delete(m.id);
|
||||
}
|
||||
|
||||
// 3. Delete the server itself
|
||||
ctx.db.server.id.delete(serverId);
|
||||
},
|
||||
);
|
||||
|
||||
export const toggle_reaction = spacetimedb.reducer(
|
||||
{
|
||||
messageId: t.u64(),
|
||||
@@ -651,7 +722,7 @@ export const create_server = spacetimedb.reducer(
|
||||
"You must be logged in via OIDC to create a server",
|
||||
);
|
||||
}
|
||||
const s = ctx.db.server.insert({ id: 0n, name, owner: ctx.sender });
|
||||
const s = ctx.db.server.insert({ id: 0n, name, owner: ctx.sender, avatar_id: undefined });
|
||||
ctx.db.server_member.insert({
|
||||
id: 0n,
|
||||
identity: ctx.sender,
|
||||
@@ -1227,6 +1298,7 @@ export const init = spacetimedb.init((ctx) => {
|
||||
id: 0n,
|
||||
name: "Zep",
|
||||
owner: undefined,
|
||||
avatar_id: undefined,
|
||||
});
|
||||
ctx.db.channel.insert({
|
||||
id: 0n,
|
||||
|
||||
Reference in New Issue
Block a user